2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-11-06 23:27:46 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
strings
|
|
|
|
)
|
2019-12-06 19:21:26 +03:00
|
|
|
// Returns type if used as expression
|
2019-12-19 23:36:18 +03:00
|
|
|
|
|
|
|
|
2019-11-06 23:27:46 +03:00
|
|
|
fn (p mut Parser) match_statement(is_expr bool) string {
|
|
|
|
p.check(.key_match)
|
2019-11-10 03:08:53 +03:00
|
|
|
p.fspace()
|
2020-02-03 11:25:26 +03:00
|
|
|
is_mut := p.tok == .key_mut
|
|
|
|
if is_mut {
|
|
|
|
p.next()
|
|
|
|
p.fspace()
|
|
|
|
}
|
2019-12-19 23:36:18 +03:00
|
|
|
typ,expr := p.tmp_expr()
|
2019-11-06 23:27:46 +03:00
|
|
|
if typ.starts_with('array_') {
|
|
|
|
p.error('arrays cannot be compared')
|
2019-12-06 19:21:26 +03:00
|
|
|
}
|
2019-12-22 04:34:37 +03:00
|
|
|
is_sum_type := typ in p.table.sum_types
|
|
|
|
mut sum_child_type := ''
|
2019-11-06 23:27:46 +03:00
|
|
|
// is it safe to use p.cgen.insert_before ???
|
|
|
|
tmp_var := p.get_tmp()
|
|
|
|
p.cgen.insert_before('$typ $tmp_var = $expr;')
|
2019-11-10 03:08:53 +03:00
|
|
|
p.fspace()
|
2019-11-06 23:27:46 +03:00
|
|
|
p.check(.lcbr)
|
|
|
|
mut i := 0
|
|
|
|
mut all_cases_return := true
|
|
|
|
// stores typ of resulting variable
|
|
|
|
mut res_typ := ''
|
|
|
|
defer {
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
|
|
|
for p.tok != .rcbr {
|
|
|
|
if p.tok == .key_else {
|
|
|
|
p.check(.key_else)
|
|
|
|
if p.tok == .arrow {
|
2019-11-10 03:08:53 +03:00
|
|
|
p.error(warn_match_arrow)
|
2019-12-06 19:21:26 +03:00
|
|
|
}
|
2019-11-06 23:27:46 +03:00
|
|
|
// unwrap match if there is only else
|
|
|
|
if i == 0 {
|
2019-11-18 13:10:31 +03:00
|
|
|
p.fspace()
|
2019-11-06 23:27:46 +03:00
|
|
|
if is_expr {
|
|
|
|
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
|
|
|
// allow braces is else
|
|
|
|
got_brace := p.tok == .lcbr
|
|
|
|
if got_brace {
|
2019-12-19 22:52:27 +03:00
|
|
|
p.fspace()
|
2019-11-06 23:27:46 +03:00
|
|
|
p.check(.lcbr)
|
|
|
|
}
|
|
|
|
p.gen('( ')
|
|
|
|
res_typ = p.bool_expression()
|
|
|
|
p.gen(' )')
|
|
|
|
// allow braces in else
|
|
|
|
if got_brace {
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
|
|
|
return res_typ
|
2019-12-19 23:36:18 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-11-06 23:27:46 +03:00
|
|
|
p.returns = false
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln('{ ')
|
|
|
|
p.statements()
|
|
|
|
p.returns = all_cases_return && p.returns
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_expr {
|
2019-12-19 22:52:27 +03:00
|
|
|
// statements are dissallowed (if match is expression) so
|
|
|
|
// user cant declare variables there and so on
|
2019-11-06 23:27:46 +03:00
|
|
|
p.gen(':(')
|
|
|
|
// allow braces is else
|
|
|
|
got_brace := p.tok == .lcbr
|
|
|
|
if got_brace {
|
2019-11-18 13:10:31 +03:00
|
|
|
p.fspace()
|
2019-11-06 23:27:46 +03:00
|
|
|
p.check(.lcbr)
|
|
|
|
}
|
|
|
|
p.check_types(p.bool_expression(), res_typ)
|
|
|
|
// allow braces in else
|
|
|
|
if got_brace {
|
|
|
|
p.check(.rcbr)
|
|
|
|
}
|
2019-12-19 23:36:18 +03:00
|
|
|
p.gen(strings.repeat(`)`, i + 1))
|
2019-11-06 23:27:46 +03:00
|
|
|
return res_typ
|
2019-12-19 23:36:18 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-11-06 23:27:46 +03:00
|
|
|
p.returns = false
|
|
|
|
p.genln('else // default:')
|
2019-11-18 13:10:31 +03:00
|
|
|
p.fspace()
|
2019-11-06 23:27:46 +03:00
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln('{ ')
|
|
|
|
p.statements()
|
|
|
|
p.returns = all_cases_return && p.returns
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
if is_expr {
|
|
|
|
p.gen(': (')
|
2019-12-19 23:36:18 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-11-06 23:27:46 +03:00
|
|
|
p.gen('else ')
|
|
|
|
}
|
2019-12-19 23:36:18 +03:00
|
|
|
}
|
|
|
|
else if is_expr {
|
2019-11-06 23:27:46 +03:00
|
|
|
p.gen('(')
|
|
|
|
}
|
|
|
|
if is_expr {
|
|
|
|
p.gen('(')
|
2019-12-19 23:36:18 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-11-06 23:27:46 +03:00
|
|
|
p.gen('if (')
|
|
|
|
}
|
|
|
|
ph := p.cgen.add_placeholder()
|
|
|
|
// Multiple checks separated by comma
|
2019-12-22 04:34:37 +03:00
|
|
|
p.open_scope()
|
2019-11-06 23:27:46 +03:00
|
|
|
mut got_comma := false
|
|
|
|
for {
|
|
|
|
if got_comma {
|
|
|
|
p.gen(') || (')
|
|
|
|
}
|
|
|
|
mut got_string := false
|
|
|
|
if typ == 'string' {
|
|
|
|
got_string = true
|
|
|
|
p.gen('string_eq($tmp_var, ')
|
|
|
|
}
|
2019-12-22 04:34:37 +03:00
|
|
|
else if is_sum_type {
|
|
|
|
p.gen('${tmp_var}.typ == ')
|
|
|
|
}
|
2019-11-06 23:27:46 +03:00
|
|
|
else {
|
|
|
|
p.gen('$tmp_var == ')
|
|
|
|
}
|
|
|
|
p.expected_type = typ
|
2019-12-22 04:34:37 +03:00
|
|
|
// `match node { ast.BoolExpr { it := node as BoolExpr ... } }`
|
|
|
|
if is_sum_type {
|
|
|
|
sum_child_type = p.get_type2().name
|
|
|
|
tt := sum_child_type.all_after('_')
|
2020-02-26 07:15:38 +03:00
|
|
|
p.gen('SumType_${typ}_$tt')
|
2020-01-11 15:21:35 +03:00
|
|
|
// println('got child $sum_child_type')
|
2019-12-22 04:34:37 +03:00
|
|
|
p.register_var(Var{
|
|
|
|
name: 'it'
|
2020-02-03 11:11:10 +03:00
|
|
|
typ: sum_child_type+'*'
|
2020-02-03 11:25:26 +03:00
|
|
|
is_mut: is_mut
|
2020-02-03 11:11:10 +03:00
|
|
|
ptr: true
|
2019-12-22 04:34:37 +03:00
|
|
|
})
|
2020-01-11 15:21:35 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-22 04:34:37 +03:00
|
|
|
p.check_types(p.bool_expression(), typ)
|
|
|
|
}
|
2019-11-06 23:27:46 +03:00
|
|
|
p.expected_type = ''
|
|
|
|
if got_string {
|
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
if p.tok != .comma {
|
|
|
|
if got_comma {
|
|
|
|
p.gen(') ')
|
|
|
|
p.cgen.set_placeholder(ph, '(')
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.check(.comma)
|
2019-12-20 00:29:37 +03:00
|
|
|
p.fspace()
|
2019-11-06 23:27:46 +03:00
|
|
|
got_comma = true
|
|
|
|
}
|
|
|
|
p.gen(')')
|
|
|
|
if p.tok == .arrow {
|
2019-11-27 09:01:25 +03:00
|
|
|
p.error(warn_match_arrow)
|
2019-11-06 23:27:46 +03:00
|
|
|
p.check(.arrow)
|
2019-12-06 19:21:26 +03:00
|
|
|
}
|
2019-11-06 23:27:46 +03:00
|
|
|
// statements are dissallowed (if match is expression) so user cant declare variables there and so on
|
|
|
|
if is_expr {
|
|
|
|
p.gen('? (')
|
|
|
|
// braces are required for now
|
|
|
|
p.check(.lcbr)
|
|
|
|
if i == 0 {
|
|
|
|
// on the first iteration we set value of res_typ
|
|
|
|
res_typ = p.bool_expression()
|
2019-12-19 23:36:18 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-11-06 23:27:46 +03:00
|
|
|
// later on we check that the value is of res_typ type
|
|
|
|
p.check_types(p.bool_expression(), res_typ)
|
|
|
|
}
|
|
|
|
// braces are required for now
|
2019-12-19 22:52:27 +03:00
|
|
|
p.fgen_nl()
|
2019-11-06 23:27:46 +03:00
|
|
|
p.check(.rcbr)
|
|
|
|
p.gen(')')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.returns = false
|
2019-11-18 13:10:31 +03:00
|
|
|
p.fspace()
|
2019-11-06 23:27:46 +03:00
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln('{ ')
|
2019-12-22 04:34:37 +03:00
|
|
|
if is_sum_type {
|
2020-02-03 11:11:10 +03:00
|
|
|
//p.genln(' $sum_child_type it = *($sum_child_type*)$tmp_var .obj ;')
|
|
|
|
p.genln(' $sum_child_type* it = ($sum_child_type*)${tmp_var}.obj ;')
|
2019-12-22 04:34:37 +03:00
|
|
|
}
|
2019-11-06 23:27:46 +03:00
|
|
|
p.statements()
|
|
|
|
all_cases_return = all_cases_return && p.returns
|
|
|
|
// p.gen(')')
|
|
|
|
}
|
|
|
|
i++
|
2019-11-18 13:10:31 +03:00
|
|
|
p.fgen_nl()
|
2019-12-22 04:34:37 +03:00
|
|
|
p.close_scope()
|
2019-11-06 23:27:46 +03:00
|
|
|
}
|
2019-12-08 14:34:51 +03:00
|
|
|
p.error('match must be exhaustive')
|
2019-12-19 23:36:18 +03:00
|
|
|
// p.returns = false // only get here when no default, so return is not guaranteed
|
2019-11-06 23:27:46 +03:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) switch_statement() {
|
2019-12-19 23:36:18 +03:00
|
|
|
p.error('`switch` statement has been removed, use `match` instead:\n' + 'https://vlang.io/docs#match')
|
2019-11-06 23:27:46 +03:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:21:26 +03:00
|
|
|
fn (p mut Parser) if_statement(is_expr bool, elif_depth int) string {
|
|
|
|
if is_expr {
|
2019-12-19 23:36:18 +03:00
|
|
|
// if p.fileis('if_expr') {
|
|
|
|
// println('IF EXPR')
|
|
|
|
// }
|
2019-12-06 19:21:26 +03:00
|
|
|
p.inside_if_expr = true
|
|
|
|
p.gen('((')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen('if (')
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
p.fspace()
|
2020-01-11 15:21:35 +03:00
|
|
|
if p.tok == .name && p.peek() == .assign {
|
|
|
|
p.error('cannot assign on if-else statement')
|
|
|
|
}
|
|
|
|
if p.tok == .name && (p.peek() == .inc || p.peek() == .dec) {
|
|
|
|
p.error('`${p.peek().str()}` is a statement')
|
|
|
|
}
|
2019-12-06 19:21:26 +03:00
|
|
|
// `if a := opt() { }` syntax
|
|
|
|
if p.tok == .name && p.peek() == .decl_assign {
|
|
|
|
p.check_not_reserved()
|
|
|
|
option_tmp := p.get_tmp()
|
|
|
|
var_name := p.lit
|
2019-12-07 23:09:48 +03:00
|
|
|
if p.known_var(var_name) {
|
|
|
|
p.error('redefinition of `$var_name`')
|
|
|
|
}
|
2019-12-08 00:30:06 +03:00
|
|
|
p.open_scope()
|
2019-12-06 19:21:26 +03:00
|
|
|
p.next()
|
2019-12-25 16:08:52 +03:00
|
|
|
p.fspace()
|
2019-12-06 19:21:26 +03:00
|
|
|
p.check(.decl_assign)
|
2019-12-25 16:08:52 +03:00
|
|
|
p.fspace()
|
2019-12-06 19:21:26 +03:00
|
|
|
p.is_var_decl = true
|
2019-12-19 23:36:18 +03:00
|
|
|
option_type,expr := p.tmp_expr() // := p.bool_expression()
|
2019-12-07 23:05:54 +03:00
|
|
|
if !option_type.starts_with('Option_') {
|
|
|
|
p.error('`if x := opt() {` syntax requires a function that returns an optional value')
|
|
|
|
}
|
2019-12-06 19:21:26 +03:00
|
|
|
p.is_var_decl = false
|
2019-12-22 01:44:16 +03:00
|
|
|
typ := parse_pointer(option_type[7..])
|
2019-12-06 19:21:26 +03:00
|
|
|
// Option_User tmp = get_user(1);
|
|
|
|
// if (tmp.ok) {
|
2019-12-19 23:36:18 +03:00
|
|
|
// User user = *(User*)tmp.data;
|
|
|
|
// [statements]
|
2019-12-06 19:21:26 +03:00
|
|
|
// }
|
|
|
|
p.cgen.insert_before('$option_type $option_tmp = $expr; ')
|
2019-12-25 16:08:52 +03:00
|
|
|
p.fspace()
|
2019-12-06 19:21:26 +03:00
|
|
|
p.check(.lcbr)
|
|
|
|
p.genln(option_tmp + '.ok) {')
|
|
|
|
p.genln('$typ $var_name = *($typ*) $option_tmp . data;')
|
2019-12-19 23:36:18 +03:00
|
|
|
p.register_var(Var{
|
2019-12-06 19:21:26 +03:00
|
|
|
name: var_name
|
|
|
|
typ: typ
|
|
|
|
is_mut: false // TODO
|
2020-02-03 11:11:10 +03:00
|
|
|
|
2019-12-06 19:23:24 +03:00
|
|
|
is_used: true // TODO
|
2019-12-19 23:36:18 +03:00
|
|
|
// is_alloc: p.is_alloc || typ.starts_with('array_')
|
|
|
|
// line_nr: p.tokens[ var_token_idx ].line_nr
|
|
|
|
// token_idx: var_token_idx
|
2020-02-03 11:11:10 +03:00
|
|
|
|
2019-12-06 19:21:26 +03:00
|
|
|
})
|
|
|
|
p.statements()
|
2019-12-08 00:30:06 +03:00
|
|
|
p.close_scope()
|
2019-12-06 19:21:26 +03:00
|
|
|
p.returns = false
|
2020-01-01 12:31:50 +03:00
|
|
|
if p.tok == .key_else {
|
|
|
|
p.next()
|
|
|
|
p.genln('else {')
|
|
|
|
p.check(.lcbr)
|
|
|
|
p.statements()
|
|
|
|
}
|
2019-12-06 19:21:26 +03:00
|
|
|
return 'void'
|
2019-12-19 23:36:18 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-06 19:21:26 +03:00
|
|
|
p.check_types(p.bool_expression(), 'bool')
|
|
|
|
}
|
|
|
|
if is_expr {
|
|
|
|
p.gen(') ? (')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln(') {')
|
|
|
|
}
|
|
|
|
p.fspace()
|
|
|
|
p.check(.lcbr)
|
2019-12-18 21:56:30 +03:00
|
|
|
if p.inside_if_expr {
|
|
|
|
p.fspace()
|
|
|
|
}
|
2019-12-06 19:21:26 +03:00
|
|
|
mut typ := ''
|
|
|
|
// if { if hack
|
|
|
|
if p.tok == .key_if && p.inside_if_expr {
|
|
|
|
typ = p.factor()
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
typ = p.statements()
|
|
|
|
}
|
|
|
|
if_returns := p.returns
|
|
|
|
p.returns = false
|
|
|
|
if p.tok == .key_else {
|
2019-12-18 21:56:30 +03:00
|
|
|
if p.inside_if_expr {
|
|
|
|
p.fspace()
|
2019-12-19 23:36:18 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-06 19:21:26 +03:00
|
|
|
p.fgen_nl()
|
|
|
|
}
|
|
|
|
p.check(.key_else)
|
|
|
|
p.fspace()
|
|
|
|
if p.tok == .key_if {
|
|
|
|
if is_expr {
|
|
|
|
p.gen(') : (')
|
|
|
|
nested := p.if_statement(is_expr, elif_depth + 1)
|
|
|
|
nested_returns := p.returns
|
|
|
|
p.returns = if_returns && nested_returns
|
|
|
|
return nested
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.gen(' else ')
|
|
|
|
nested := p.if_statement(is_expr, 0)
|
|
|
|
nested_returns := p.returns
|
|
|
|
p.returns = if_returns && nested_returns
|
|
|
|
return nested
|
|
|
|
}
|
|
|
|
// return ''
|
|
|
|
}
|
|
|
|
if is_expr {
|
|
|
|
p.gen(') : (')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln(' else { ')
|
|
|
|
}
|
|
|
|
p.check(.lcbr)
|
2019-12-18 21:56:30 +03:00
|
|
|
if is_expr {
|
|
|
|
p.fspace()
|
|
|
|
}
|
2019-12-06 19:21:26 +03:00
|
|
|
// statements() returns the type of the last statement
|
|
|
|
first_typ := typ
|
|
|
|
typ = p.statements()
|
|
|
|
p.inside_if_expr = false
|
|
|
|
if is_expr {
|
|
|
|
p.check_types(first_typ, typ)
|
|
|
|
p.gen(strings.repeat(`)`, 2 * (elif_depth + 1)))
|
|
|
|
}
|
|
|
|
else_returns := p.returns
|
|
|
|
p.returns = if_returns && else_returns
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
p.inside_if_expr = false
|
|
|
|
if p.fileis('test_test') {
|
|
|
|
println('if ret typ="$typ" line=$p.scanner.line_nr')
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|