vfmt: small fixes + run on checker.v and fmt.v
parent
3c6a4dedd6
commit
8f9a8e1e7f
|
@ -384,6 +384,7 @@ pub:
|
||||||
cond Expr
|
cond Expr
|
||||||
stmts []Stmt
|
stmts []Stmt
|
||||||
pos token.Position
|
pos token.Position
|
||||||
|
comment Comment
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MatchExpr {
|
pub struct MatchExpr {
|
||||||
|
@ -392,6 +393,7 @@ pub:
|
||||||
cond Expr
|
cond Expr
|
||||||
branches []MatchBranch
|
branches []MatchBranch
|
||||||
pos token.Position
|
pos token.Position
|
||||||
|
is_mut bool // `match mut ast_node {`
|
||||||
mut:
|
mut:
|
||||||
is_expr bool // returns a value
|
is_expr bool // returns a value
|
||||||
return_type table.Type
|
return_type table.Type
|
||||||
|
@ -405,6 +407,7 @@ pub:
|
||||||
exprs []Expr
|
exprs []Expr
|
||||||
stmts []Stmt
|
stmts []Stmt
|
||||||
pos token.Position
|
pos token.Position
|
||||||
|
comment Comment // comment above `xxx {`
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CompIf {
|
pub struct CompIf {
|
||||||
|
|
|
@ -38,11 +38,18 @@ pub fn (node &FnDecl) str(t &table.Table) string {
|
||||||
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || (node.is_variadic && i ==
|
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || (node.is_variadic && i ==
|
||||||
node.args.len - 2)
|
node.args.len - 2)
|
||||||
f.write(arg.name)
|
f.write(arg.name)
|
||||||
|
mut s := t.type_to_str(arg.typ)
|
||||||
|
if arg.is_mut {
|
||||||
|
f.write(' mut')
|
||||||
|
if s.starts_with('&') {
|
||||||
|
s = s[1..]
|
||||||
|
}
|
||||||
|
}
|
||||||
if should_add_type {
|
if should_add_type {
|
||||||
if node.is_variadic && is_last_arg {
|
if node.is_variadic && is_last_arg {
|
||||||
f.write(' ...' + t.type_to_str(arg.typ))
|
f.write(' ...' + s)
|
||||||
} else {
|
} else {
|
||||||
f.write(' ' + t.type_to_str(arg.typ))
|
f.write(' ' + s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !is_last_arg {
|
if !is_last_arg {
|
||||||
|
|
|
@ -28,14 +28,12 @@ mut:
|
||||||
fn_return_type table.Type // current function's return type
|
fn_return_type table.Type // current function's return type
|
||||||
const_decl string
|
const_decl string
|
||||||
const_deps []string
|
const_deps []string
|
||||||
//assigned_var_name string
|
|
||||||
// fn_decl ast.FnDecl
|
|
||||||
pref &pref.Preferences // Preferences shared from V struct
|
pref &pref.Preferences // Preferences shared from V struct
|
||||||
in_for_count int // if checker is currently in an for loop
|
in_for_count int // if checker is currently in an for loop
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_checker(table &table.Table, pref &pref.Preferences) Checker {
|
pub fn new_checker(table &table.Table, pref &pref.Preferences) Checker {
|
||||||
return Checker{
|
return checker.Checker{
|
||||||
table: table
|
table: table
|
||||||
pref: pref
|
pref: pref
|
||||||
}
|
}
|
||||||
|
@ -119,7 +117,8 @@ pub fn (c mut Checker) struct_init(struct_init mut ast.StructInit) table.Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found_field {
|
if !found_field {
|
||||||
c.error('struct init: no such field `$field_name` for struct `$typ_sym.name`', struct_init.pos)
|
c.error('struct init: no such field `$field_name` for struct `$typ_sym.name`',
|
||||||
|
struct_init.pos)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,7 +127,8 @@ pub fn (c mut Checker) struct_init(struct_init mut ast.StructInit) table.Type {
|
||||||
expr_type_sym := c.table.get_type_symbol(expr_type)
|
expr_type_sym := c.table.get_type_symbol(expr_type)
|
||||||
field_type_sym := c.table.get_type_symbol(field.typ)
|
field_type_sym := c.table.get_type_symbol(field.typ)
|
||||||
if !c.table.check(expr_type, field.typ) {
|
if !c.table.check(expr_type, field.typ) {
|
||||||
c.error('cannot assign `$expr_type_sym.name` as `$field_type_sym.name` for field `$field.name`', struct_init.pos)
|
c.error('cannot assign `$expr_type_sym.name` as `$field_type_sym.name` for field `$field.name`',
|
||||||
|
struct_init.pos)
|
||||||
}
|
}
|
||||||
struct_init.expr_types << expr_type
|
struct_init.expr_types << expr_type
|
||||||
struct_init.expected_types << field.typ
|
struct_init.expected_types << field.typ
|
||||||
|
@ -224,7 +224,8 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
|
||||||
left_type_sym := c.table.get_type_symbol(left_type)
|
left_type_sym := c.table.get_type_symbol(left_type)
|
||||||
method_name := call_expr.name
|
method_name := call_expr.name
|
||||||
// TODO: remove this for actual methods, use only for compiler magic
|
// TODO: remove this for actual methods, use only for compiler magic
|
||||||
if left_type_sym.kind == .array && method_name in ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice'] {
|
if left_type_sym.kind == .array && method_name in ['filter', 'clone', 'repeat', 'reverse', 'map',
|
||||||
|
'slice'] {
|
||||||
if method_name in ['filter', 'map'] {
|
if method_name in ['filter', 'map'] {
|
||||||
array_info := left_type_sym.info as table.Array
|
array_info := left_type_sym.info as table.Array
|
||||||
mut scope := c.file.scope.innermost(call_expr.pos.pos)
|
mut scope := c.file.scope.innermost(call_expr.pos.pos)
|
||||||
|
@ -239,13 +240,11 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
|
||||||
// in ['clone', 'str'] {
|
// in ['clone', 'str'] {
|
||||||
call_expr.receiver_type = table.type_to_ptr(left_type)
|
call_expr.receiver_type = table.type_to_ptr(left_type)
|
||||||
// call_expr.return_type = call_expr.receiver_type
|
// call_expr.return_type = call_expr.receiver_type
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
call_expr.receiver_type = left_type
|
call_expr.receiver_type = left_type
|
||||||
}
|
}
|
||||||
return left_type
|
return left_type
|
||||||
}
|
} else if left_type_sym.kind == .array && method_name in ['first', 'last'] {
|
||||||
else if left_type_sym.kind == .array && method_name in ['first', 'last'] {
|
|
||||||
info := left_type_sym.info as table.Array
|
info := left_type_sym.info as table.Array
|
||||||
call_expr.return_type = info.elem_type
|
call_expr.return_type = info.elem_type
|
||||||
call_expr.receiver_type = left_type
|
call_expr.receiver_type = left_type
|
||||||
|
@ -255,10 +254,11 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
|
||||||
no_args := method.args.len - 1
|
no_args := method.args.len - 1
|
||||||
min_required_args := method.args.len - if method.is_variadic && method.args.len > 1 { 2 } else { 1 }
|
min_required_args := method.args.len - if method.is_variadic && method.args.len > 1 { 2 } else { 1 }
|
||||||
if call_expr.args.len < min_required_args {
|
if call_expr.args.len < min_required_args {
|
||||||
c.error('too few arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $min_required_args)', call_expr.pos)
|
c.error('too few arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $min_required_args)',
|
||||||
}
|
call_expr.pos)
|
||||||
else if !method.is_variadic && call_expr.args.len > no_args {
|
} else if !method.is_variadic && call_expr.args.len > no_args {
|
||||||
c.error('too many arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $no_args)', call_expr.pos)
|
c.error('too many arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $no_args)',
|
||||||
|
call_expr.pos)
|
||||||
return method.return_type
|
return method.return_type
|
||||||
}
|
}
|
||||||
// if method_name == 'clone' {
|
// if method_name == 'clone' {
|
||||||
|
@ -267,7 +267,11 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
|
||||||
// call_expr.args << method.args[0].typ
|
// call_expr.args << method.args[0].typ
|
||||||
// call_expr.exp_arg_types << method.args[0].typ
|
// call_expr.exp_arg_types << method.args[0].typ
|
||||||
for i, arg in call_expr.args {
|
for i, arg in call_expr.args {
|
||||||
c.expected_type = if method.is_variadic && i >= method.args.len - 1 { method.args[method.args.len - 1].typ } else { method.args[i + 1].typ }
|
c.expected_type = if method.is_variadic && i >= method.args.len - 1 {
|
||||||
|
method.args[method.args.len - 1].typ
|
||||||
|
} else {
|
||||||
|
method.args[i + 1].typ
|
||||||
|
}
|
||||||
call_expr.args[i].typ = c.expr(arg.expr)
|
call_expr.args[i].typ = c.expr(arg.expr)
|
||||||
}
|
}
|
||||||
// TODO: typ optimize.. this node can get processed more than once
|
// TODO: typ optimize.. this node can get processed more than once
|
||||||
|
@ -293,8 +297,7 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
|
||||||
}
|
}
|
||||||
c.error('unknown method: ${left_type_sym.name}.$method_name', call_expr.pos)
|
c.error('unknown method: ${left_type_sym.name}.$method_name', call_expr.pos)
|
||||||
return table.void_type
|
return table.void_type
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
fn_name := call_expr.name
|
fn_name := call_expr.name
|
||||||
// TODO: impl typeof properly (probably not going to be a fn call)
|
// TODO: impl typeof properly (probably not going to be a fn call)
|
||||||
if fn_name == 'typeof' {
|
if fn_name == 'typeof' {
|
||||||
|
@ -346,10 +349,11 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
|
||||||
}
|
}
|
||||||
min_required_args := if f.is_variadic { f.args.len - 1 } else { f.args.len }
|
min_required_args := if f.is_variadic { f.args.len - 1 } else { f.args.len }
|
||||||
if call_expr.args.len < min_required_args {
|
if call_expr.args.len < min_required_args {
|
||||||
c.error('too few arguments in call to `$fn_name` ($call_expr.args.len instead of $min_required_args)', call_expr.pos)
|
c.error('too few arguments in call to `$fn_name` ($call_expr.args.len instead of $min_required_args)',
|
||||||
}
|
call_expr.pos)
|
||||||
else if !f.is_variadic && call_expr.args.len > f.args.len {
|
} else if !f.is_variadic && call_expr.args.len > f.args.len {
|
||||||
c.error('too many arguments in call to `$fn_name` ($call_expr.args.len instead of $f.args.len)', call_expr.pos)
|
c.error('too many arguments in call to `$fn_name` ($call_expr.args.len instead of $f.args.len)',
|
||||||
|
call_expr.pos)
|
||||||
return f.return_type
|
return f.return_type
|
||||||
}
|
}
|
||||||
// println can print anything
|
// println can print anything
|
||||||
|
@ -379,9 +383,11 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
|
||||||
if typ_sym.kind == .void && arg_typ_sym.kind == .string {
|
if typ_sym.kind == .void && arg_typ_sym.kind == .string {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if typ_sym.kind == .array_fixed {}
|
if typ_sym.kind == .array_fixed {
|
||||||
|
}
|
||||||
// println('fixed')
|
// println('fixed')
|
||||||
c.error('cannot use type `$typ_sym.str()` as type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`', call_expr.pos)
|
c.error('cannot use type `$typ_sym.str()` as type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`',
|
||||||
|
call_expr.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return f.return_type
|
return f.return_type
|
||||||
|
@ -409,8 +415,7 @@ pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.T
|
||||||
}
|
}
|
||||||
if typ_sym.kind != .struct_ {
|
if typ_sym.kind != .struct_ {
|
||||||
c.error('`$typ_sym.name` is not a struct', selector_expr.pos)
|
c.error('`$typ_sym.name` is not a struct', selector_expr.pos)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
c.error('unknown field `${typ_sym.name}.$field_name`', selector_expr.pos)
|
c.error('unknown field `${typ_sym.name}.$field_name`', selector_expr.pos)
|
||||||
}
|
}
|
||||||
return table.void_type
|
return table.void_type
|
||||||
|
@ -453,7 +458,8 @@ pub fn (c mut Checker) return_stmt(return_stmt mut ast.Return) {
|
||||||
if !c.table.check(got_typ, exp_typ) {
|
if !c.table.check(got_typ, exp_typ) {
|
||||||
got_typ_sym := c.table.get_type_symbol(got_typ)
|
got_typ_sym := c.table.get_type_symbol(got_typ)
|
||||||
exp_typ_sym := c.table.get_type_symbol(exp_typ)
|
exp_typ_sym := c.table.get_type_symbol(exp_typ)
|
||||||
c.error('cannot use `$got_typ_sym.name` as type `$exp_typ_sym.name` in return argument', return_stmt.pos)
|
c.error('cannot use `$got_typ_sym.name` as type `$exp_typ_sym.name` in return argument',
|
||||||
|
return_stmt.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -494,9 +500,8 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) {
|
||||||
assign_stmt.right_types << val_type
|
assign_stmt.right_types << val_type
|
||||||
scope.update_var_type(ident.name, val_type)
|
scope.update_var_type(ident.name, val_type)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
// `a := 1` | `a,b := 1,2`
|
// `a := 1` | `a,b := 1,2`
|
||||||
else {
|
|
||||||
if assign_stmt.left.len != assign_stmt.right.len {
|
if assign_stmt.left.len != assign_stmt.right.len {
|
||||||
c.error('wrong number of vars', assign_stmt.pos)
|
c.error('wrong number of vars', assign_stmt.pos)
|
||||||
}
|
}
|
||||||
|
@ -567,9 +572,8 @@ pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type {
|
||||||
idx := c.table.find_or_register_array(elem_type, 1)
|
idx := c.table.find_or_register_array(elem_type, 1)
|
||||||
array_init.typ = table.new_type(idx)
|
array_init.typ = table.new_type(idx)
|
||||||
array_init.elem_type = elem_type
|
array_init.elem_type = elem_type
|
||||||
}
|
} else if array_init.exprs.len == 1 && array_init.elem_type != table.void_type {
|
||||||
// [50]byte
|
// [50]byte
|
||||||
else if array_init.exprs.len == 1 && array_init.elem_type != table.void_type {
|
|
||||||
mut fixed_size := 1
|
mut fixed_size := 1
|
||||||
match array_init.exprs[0] {
|
match array_init.exprs[0] {
|
||||||
ast.IntegerLiteral {
|
ast.IntegerLiteral {
|
||||||
|
@ -690,8 +694,7 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
typ := c.expr(it.cond)
|
typ := c.expr(it.cond)
|
||||||
if it.is_range {
|
if it.is_range {
|
||||||
c.expr(it.high)
|
c.expr(it.high)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
mut scope := c.file.scope.innermost(it.pos.pos)
|
mut scope := c.file.scope.innermost(it.pos.pos)
|
||||||
sym := c.table.get_type_symbol(typ)
|
sym := c.table.get_type_symbol(typ)
|
||||||
if it.key_var.len > 0 {
|
if it.key_var.len > 0 {
|
||||||
|
@ -700,7 +703,8 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
sym.map_info().key_type
|
sym.map_info().key_type
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
table.int_type}
|
table.int_type
|
||||||
|
}
|
||||||
}
|
}
|
||||||
it.key_type = key_type
|
it.key_type = key_type
|
||||||
scope.update_var_type(it.key_var, key_type)
|
scope.update_var_type(it.key_var, key_type)
|
||||||
|
@ -721,9 +725,9 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
ast.GoStmt {
|
ast.GoStmt {
|
||||||
c.expr(it.call_expr)
|
c.expr(it.call_expr)
|
||||||
}
|
}
|
||||||
// ast.GlobalDecl {}
|
|
||||||
// ast.HashStmt {}
|
// ast.HashStmt {}
|
||||||
ast.Import {}
|
ast.Import {}
|
||||||
|
// ast.GlobalDecl {}
|
||||||
ast.Return {
|
ast.Return {
|
||||||
c.return_stmt(mut it)
|
c.return_stmt(mut it)
|
||||||
}
|
}
|
||||||
|
@ -731,10 +735,10 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
ast.UnsafeStmt {
|
ast.UnsafeStmt {
|
||||||
c.stmts(it.stmts)
|
c.stmts(it.stmts)
|
||||||
}
|
}
|
||||||
else {}
|
else {
|
||||||
// println('checker.stmt(): unhandled node')
|
// println('checker.stmt(): unhandled node')
|
||||||
// println('checker.stmt(): unhandled node (${typeof(node)})')
|
// println('checker.stmt(): unhandled node (${typeof(node)})')
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -761,8 +765,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
|
||||||
c.error('cannot cast `$expr_type_sym.name` to `$type_sym.name`', it.pos)
|
c.error('cannot cast `$expr_type_sym.name` to `$type_sym.name`', it.pos)
|
||||||
// c.error('only $info.variants can be casted to `$typ`', it.pos)
|
// c.error('only $info.variants can be casted to `$typ`', it.pos)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
c.error('cannot cast non sum type `$type_sym.name` using `as`', it.pos)
|
c.error('cannot cast non sum type `$type_sym.name` using `as`', it.pos)
|
||||||
}
|
}
|
||||||
return it.typ
|
return it.typ
|
||||||
|
@ -909,17 +912,14 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
|
||||||
if ident.kind == .variable {
|
if ident.kind == .variable {
|
||||||
info := ident.info as ast.IdentVar
|
info := ident.info as ast.IdentVar
|
||||||
return info.typ
|
return info.typ
|
||||||
}
|
} else if ident.kind == .constant {
|
||||||
else if ident.kind == .constant {
|
|
||||||
info := ident.info as ast.IdentVar
|
info := ident.info as ast.IdentVar
|
||||||
return info.typ
|
return info.typ
|
||||||
}
|
} else if ident.kind == .function {
|
||||||
else if ident.kind == .function {
|
|
||||||
info := ident.info as ast.IdentFn
|
info := ident.info as ast.IdentFn
|
||||||
return info.typ
|
return info.typ
|
||||||
}
|
} else if ident.kind == .unresolved {
|
||||||
// first use
|
// first use
|
||||||
else if ident.kind == .unresolved {
|
|
||||||
start_scope := c.file.scope.innermost(ident.pos.pos)
|
start_scope := c.file.scope.innermost(ident.pos.pos)
|
||||||
if obj := start_scope.find(ident.name) {
|
if obj := start_scope.find(ident.name) {
|
||||||
match obj {
|
match obj {
|
||||||
|
@ -1009,7 +1009,8 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
|
||||||
typ := c.expr(expr)
|
typ := c.expr(expr)
|
||||||
typ_sym := c.table.get_type_symbol(typ)
|
typ_sym := c.table.get_type_symbol(typ)
|
||||||
// TODO:
|
// TODO:
|
||||||
if typ_sym.kind == .sum_type {}
|
if typ_sym.kind == .sum_type {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.stmts(branch.stmts)
|
c.stmts(branch.stmts)
|
||||||
// If the last statement is an expression, return its type
|
// If the last statement is an expression, return its type
|
||||||
|
@ -1018,13 +1019,14 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
|
||||||
ast.ExprStmt {
|
ast.ExprStmt {
|
||||||
ret_type = c.expr(it.expr)
|
ret_type = c.expr(it.expr)
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
// TODO: ask alex about this
|
// TODO: ask alex about this
|
||||||
// typ := c.expr(it.expr)
|
// typ := c.expr(it.expr)
|
||||||
// type_sym := c.table.get_type_symbol(typ)
|
// type_sym := c.table.get_type_symbol(typ)
|
||||||
// p.warn('match expr ret $type_sym.name')
|
// p.warn('match expr ret $type_sym.name')
|
||||||
// node.typ = typ
|
// node.typ = typ
|
||||||
// return typ
|
// return typ
|
||||||
else {}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1039,7 +1041,8 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
|
pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
|
||||||
if c.expected_type != table.void_type { // || c.assigned_var_name != '' {
|
if c.expected_type != table.void_type {
|
||||||
|
// | c.assigned_var_name != '' {
|
||||||
// sym := c.table.get_type_symbol(c.expected_type)
|
// sym := c.table.get_type_symbol(c.expected_type)
|
||||||
// println('$c.file.path $node.pos.line_nr IF is expr: checker exp type = ' + sym.name)
|
// println('$c.file.path $node.pos.line_nr IF is expr: checker exp type = ' + sym.name)
|
||||||
node.is_expr = true
|
node.is_expr = true
|
||||||
|
@ -1048,7 +1051,8 @@ pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
|
||||||
for i, branch in node.branches {
|
for i, branch in node.branches {
|
||||||
match branch.cond {
|
match branch.cond {
|
||||||
ast.ParExpr {
|
ast.ParExpr {
|
||||||
c.error('unnecessary `()` in an if condition. use `if expr {` instead of `if (expr) {`.', node.pos)
|
c.error('unnecessary `()` in an if condition. use `if expr {` instead of `if (expr) {`.',
|
||||||
|
node.pos)
|
||||||
}
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
|
@ -1122,18 +1126,17 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type {
|
||||||
// println('index expr left=$typ_sym.name $node.pos.line_nr')
|
// println('index expr left=$typ_sym.name $node.pos.line_nr')
|
||||||
// if typ_sym.kind == .array && (!(table.type_idx(index_type) in table.number_type_idxs) &&
|
// if typ_sym.kind == .array && (!(table.type_idx(index_type) in table.number_type_idxs) &&
|
||||||
// index_type_sym.kind != .enum_) {
|
// index_type_sym.kind != .enum_) {
|
||||||
if typ_sym.kind in [.array, .array_fixed] && !(table.is_number(index_type) || index_type_sym.kind == .enum_) {
|
if typ_sym.kind in [.array, .array_fixed] && !(table.is_number(index_type) || index_type_sym.kind ==
|
||||||
|
.enum_) {
|
||||||
c.error('non-integer index `$index_type_sym.name` (array type `$typ_sym.name`)', node.pos)
|
c.error('non-integer index `$index_type_sym.name` (array type `$typ_sym.name`)', node.pos)
|
||||||
}
|
} else if typ_sym.kind == .map && table.type_idx(index_type) != table.string_type_idx {
|
||||||
else if typ_sym.kind == .map && table.type_idx(index_type) != table.string_type_idx {
|
|
||||||
c.error('non-string map index (map type `$typ_sym.name`)', node.pos)
|
c.error('non-string map index (map type `$typ_sym.name`)', node.pos)
|
||||||
}
|
}
|
||||||
value_type := c.table.value_type(typ)
|
value_type := c.table.value_type(typ)
|
||||||
if value_type != table.void_type {
|
if value_type != table.void_type {
|
||||||
return value_type
|
return value_type
|
||||||
}
|
}
|
||||||
}
|
} else if is_range {
|
||||||
else if is_range {
|
|
||||||
// array[1..2] => array
|
// array[1..2] => array
|
||||||
// fixed_array[1..2] => array
|
// fixed_array[1..2] => array
|
||||||
if typ_sym.kind == .array_fixed {
|
if typ_sym.kind == .array_fixed {
|
||||||
|
@ -1149,8 +1152,11 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type {
|
||||||
// If a short form is used, `expected_type` needs to be an enum
|
// If a short form is used, `expected_type` needs to be an enum
|
||||||
// with this value.
|
// with this value.
|
||||||
pub fn (c mut Checker) enum_val(node mut ast.EnumVal) table.Type {
|
pub fn (c mut Checker) enum_val(node mut ast.EnumVal) table.Type {
|
||||||
typ_idx := if node.enum_name == '' { table.type_idx(c.expected_type) } else { //
|
typ_idx := if node.enum_name == '' {
|
||||||
c.table.find_type_idx(node.enum_name) }
|
table.type_idx(c.expected_type)
|
||||||
|
} else { //
|
||||||
|
c.table.find_type_idx(node.enum_name)
|
||||||
|
}
|
||||||
// println('checker: enum_val: $node.enum_name typeidx=$typ_idx')
|
// println('checker: enum_val: $node.enum_name typeidx=$typ_idx')
|
||||||
if typ_idx == 0 {
|
if typ_idx == 0 {
|
||||||
c.error('not an enum (name=$node.enum_name) (type_idx=0)', node.pos)
|
c.error('not an enum (name=$node.enum_name) (type_idx=0)', node.pos)
|
||||||
|
@ -1226,11 +1232,7 @@ fn (c mut Checker) warn_or_error(s string, pos token.Position, warn bool) {
|
||||||
print_backtrace()
|
print_backtrace()
|
||||||
}
|
}
|
||||||
typ := if warn { 'warning' } else { 'error' }
|
typ := if warn { 'warning' } else { 'error' }
|
||||||
kind := if c.pref.is_verbose {
|
kind := if c.pref.is_verbose { 'checker $typ #$c.nr_errors:' } else { '$typ:' }
|
||||||
'checker $typ #$c.nr_errors:'
|
|
||||||
} else {
|
|
||||||
'$typ:'
|
|
||||||
}
|
|
||||||
ferror := util.formated_error(kind, s, c.file.path, pos)
|
ferror := util.formated_error(kind, s, c.file.path, pos)
|
||||||
c.errors << ferror
|
c.errors << ferror
|
||||||
if !(pos.line_nr in c.error_lines) {
|
if !(pos.line_nr in c.error_lines) {
|
||||||
|
|
|
@ -10,8 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t',
|
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t']
|
||||||
'\t\t\t\t\t\t\t']
|
|
||||||
max_len = 100
|
max_len = 100
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -87,9 +86,7 @@ fn (f mut Fmt) imports(imports []ast.Import) {
|
||||||
if imports.len == 1 {
|
if imports.len == 1 {
|
||||||
imp_stmt_str := f.imp_stmt_str(imports[0])
|
imp_stmt_str := f.imp_stmt_str(imports[0])
|
||||||
f.writeln('import ${imp_stmt_str}\n')
|
f.writeln('import ${imp_stmt_str}\n')
|
||||||
}
|
} else if imports.len > 1 {
|
||||||
//
|
|
||||||
else if imports.len > 1 {
|
|
||||||
f.writeln('import (')
|
f.writeln('import (')
|
||||||
f.indent++
|
f.indent++
|
||||||
for imp in imports {
|
for imp in imports {
|
||||||
|
@ -102,11 +99,7 @@ fn (f mut Fmt) imports(imports []ast.Import) {
|
||||||
|
|
||||||
fn (f Fmt) imp_stmt_str(imp ast.Import) string {
|
fn (f Fmt) imp_stmt_str(imp ast.Import) string {
|
||||||
is_diff := imp.alias != imp.mod && !imp.mod.ends_with('.' + imp.alias)
|
is_diff := imp.alias != imp.mod && !imp.mod.ends_with('.' + imp.alias)
|
||||||
imp_alias_suffix := if is_diff {
|
imp_alias_suffix := if is_diff { ' as ${imp.alias}' } else { '' }
|
||||||
' as ${imp.alias}'
|
|
||||||
} else {
|
|
||||||
''
|
|
||||||
}
|
|
||||||
return '${imp.mod}${imp_alias_suffix}'
|
return '${imp.mod}${imp_alias_suffix}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -291,11 +284,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
ast.CompIf {
|
ast.CompIf {
|
||||||
inversion := if it.is_not {
|
inversion := if it.is_not { '!' } else { '' }
|
||||||
'!'
|
|
||||||
} else {
|
|
||||||
''
|
|
||||||
}
|
|
||||||
f.writeln('\$if ${inversion}${it.val} {')
|
f.writeln('\$if ${inversion}${it.val} {')
|
||||||
f.stmts(it.stmts)
|
f.stmts(it.stmts)
|
||||||
if it.has_else {
|
if it.has_else {
|
||||||
|
@ -504,10 +493,16 @@ fn (f mut Fmt) expr(node ast.Expr) {
|
||||||
}
|
}
|
||||||
ast.MatchExpr {
|
ast.MatchExpr {
|
||||||
f.write('match ')
|
f.write('match ')
|
||||||
|
if it.is_mut {
|
||||||
|
f.write('mut ')
|
||||||
|
}
|
||||||
f.expr(it.cond)
|
f.expr(it.cond)
|
||||||
f.writeln(' {')
|
f.writeln(' {')
|
||||||
f.indent++
|
f.indent++
|
||||||
for i, branch in it.branches {
|
for i, branch in it.branches {
|
||||||
|
if branch.comment.text != '' {
|
||||||
|
f.comment(branch.comment)
|
||||||
|
}
|
||||||
if i < it.branches.len - 1 {
|
if i < it.branches.len - 1 {
|
||||||
// normal branch
|
// normal branch
|
||||||
for j, expr in branch.exprs {
|
for j, expr in branch.exprs {
|
||||||
|
@ -683,10 +678,13 @@ fn short_module(name string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (f mut Fmt) if_expr(it ast.IfExpr) {
|
fn (f mut Fmt) if_expr(it ast.IfExpr) {
|
||||||
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len ==
|
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 && it.branches[1].stmts.len ==
|
||||||
1 && it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign)
|
1 && (it.is_expr || f.is_assign)
|
||||||
f.single_line_if = single_line
|
f.single_line_if = single_line
|
||||||
for i, branch in it.branches {
|
for i, branch in it.branches {
|
||||||
|
if branch.comment.text != '' {
|
||||||
|
f.comment(branch.comment)
|
||||||
|
}
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
f.write('if ')
|
f.write('if ')
|
||||||
f.expr(branch.cond)
|
f.expr(branch.cond)
|
||||||
|
|
|
@ -337,6 +337,7 @@ pub fn (p mut Parser) check_comment() ast.Comment {
|
||||||
if p.tok.kind == .comment {
|
if p.tok.kind == .comment {
|
||||||
return p.comment()
|
return p.comment()
|
||||||
}
|
}
|
||||||
|
return ast.Comment{}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (p mut Parser) comment() ast.Comment {
|
pub fn (p mut Parser) comment() ast.Comment {
|
||||||
|
@ -1182,10 +1183,15 @@ fn (p mut Parser) if_expr() ast.IfExpr {
|
||||||
mut has_else := false
|
mut has_else := false
|
||||||
for p.tok.kind in [.key_if, .key_else] {
|
for p.tok.kind in [.key_if, .key_else] {
|
||||||
branch_pos := p.tok.position()
|
branch_pos := p.tok.position()
|
||||||
|
mut comment := ast.Comment{}
|
||||||
if p.tok.kind == .key_if {
|
if p.tok.kind == .key_if {
|
||||||
p.check(.key_if)
|
p.check(.key_if)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
//if p.tok.kind == .comment {
|
||||||
|
//p.error('place comments inside {}')
|
||||||
|
//}
|
||||||
|
//comment = p.check_comment()
|
||||||
p.check(.key_else)
|
p.check(.key_else)
|
||||||
if p.tok.kind == .key_if {
|
if p.tok.kind == .key_if {
|
||||||
p.check(.key_if)
|
p.check(.key_if)
|
||||||
|
@ -1195,6 +1201,7 @@ fn (p mut Parser) if_expr() ast.IfExpr {
|
||||||
branches << ast.IfBranch{
|
branches << ast.IfBranch{
|
||||||
stmts: p.parse_block()
|
stmts: p.parse_block()
|
||||||
pos: branch_pos
|
pos: branch_pos
|
||||||
|
comment: comment
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -1229,6 +1236,7 @@ fn (p mut Parser) if_expr() ast.IfExpr {
|
||||||
cond: cond
|
cond: cond
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
pos: branch_pos
|
pos: branch_pos
|
||||||
|
comment: ast.Comment{}// comment
|
||||||
}
|
}
|
||||||
if p.tok.kind != .key_else {
|
if p.tok.kind != .key_else {
|
||||||
break
|
break
|
||||||
|
@ -1811,6 +1819,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
|
||||||
mut branches := []ast.MatchBranch
|
mut branches := []ast.MatchBranch
|
||||||
mut have_final_else := false
|
mut have_final_else := false
|
||||||
for {
|
for {
|
||||||
|
comment := p.check_comment() // comment before {}
|
||||||
mut exprs := []ast.Expr
|
mut exprs := []ast.Expr
|
||||||
branch_pos := p.tok.position()
|
branch_pos := p.tok.position()
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
|
@ -1862,6 +1871,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
|
||||||
exprs: exprs
|
exprs: exprs
|
||||||
stmts: stmts
|
stmts: stmts
|
||||||
pos: branch_pos
|
pos: branch_pos
|
||||||
|
comment: comment
|
||||||
}
|
}
|
||||||
p.close_scope()
|
p.close_scope()
|
||||||
if p.tok.kind == .rcbr {
|
if p.tok.kind == .rcbr {
|
||||||
|
@ -1877,6 +1887,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
|
||||||
cond: cond
|
cond: cond
|
||||||
is_sum_type: is_sum_type
|
is_sum_type: is_sum_type
|
||||||
pos: pos
|
pos: pos
|
||||||
|
is_mut: is_mut
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue