fmt: run on parser/assign.v

pull/5532/head
Alexander Medvednikov 2020-06-27 15:00:28 +02:00
parent 2bfe8e5153
commit 6a335c4416
3 changed files with 50 additions and 110 deletions

View File

@ -5,7 +5,6 @@ module parser
import v.ast
fn (mut p Parser) assign_stmt() ast.Stmt {
return p.partial_assign_stmt(p.expr_list())
}
@ -16,27 +15,27 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) {
for expr in exprs {
if expr is ast.Ident {
ident := expr as ast.Ident
if ident.name == it.name {
p.error_with_pos('undefined variable: `$it.name`', it.pos)
if ident.name == val.name {
p.error_with_pos('undefined variable: `$val.name`', val.pos)
}
}
}
}
ast.InfixExpr {
p.check_undefined_variables(exprs, it.left)
p.check_undefined_variables(exprs, it.right)
p.check_undefined_variables(exprs, val.left)
p.check_undefined_variables(exprs, val.right)
}
ast.ParExpr {
p.check_undefined_variables(exprs, it.expr)
p.check_undefined_variables(exprs, val.expr)
}
ast.PostfixExpr {
p.check_undefined_variables(exprs, it.expr)
p.check_undefined_variables(exprs, val.expr)
}
ast.PrefixExpr {
p.check_undefined_variables(exprs, it.right)
p.check_undefined_variables(exprs, val.right)
}
ast.StringInterLiteral {
for expr_ in it.exprs {
for expr_ in val.exprs {
p.check_undefined_variables(exprs, expr_)
}
}
@ -46,23 +45,17 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) {
fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool {
match val {
ast.Ident {
for expr in exprs {
ast.Ident { for expr in exprs {
if expr is ast.Ident {
ident := expr as ast.Ident
if ident.name == it.name { return true }
if ident.name == val.name {
return true
}
}
}
}
ast.InfixExpr {
return p.check_cross_variables(exprs, it.left) || p.check_cross_variables(exprs, it.right)
}
ast.PrefixExpr {
return p.check_cross_variables(exprs, it.right)
}
ast.PostfixExpr {
return p.check_cross_variables(exprs, it.expr)
}
} }
ast.InfixExpr { return p.check_cross_variables(exprs, val.left) || p.check_cross_variables(exprs, val.right) }
ast.PrefixExpr { return p.check_cross_variables(exprs, val.right) }
ast.PostfixExpr { return p.check_cross_variables(exprs, val.expr) }
else {}
}
return false
@ -80,8 +73,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt {
for r in right {
p.check_undefined_variables(left, r)
}
}
else if left.len > 1 {
} else if left.len > 1 {
// a, b = b, a
for r in right {
has_cross_var = p.check_cross_variables(left, r)
@ -112,7 +104,8 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt {
}
ast.IndexExpr {
if op == .decl_assign {
p.error_with_pos('non-name `$lx.left[$lx.index]` on left side of `:=`', lx.pos)
p.error_with_pos('non-name `$lx.left[$lx.index]` on left side of `:=`',
lx.pos)
}
lx.is_setter = true
}
@ -120,13 +113,22 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt {
ast.PrefixExpr {}
ast.SelectorExpr {
if op == .decl_assign {
p.error_with_pos('struct fields can only be declared during the initialization', lx.pos)
p.error_with_pos('struct fields can only be declared during the initialization',
lx.pos)
}
}
else {}
// TODO: parexpr ( check vars)
// else { p.error_with_pos('unexpected `${typeof(lx)}`', lx.position()) }
else {
// TODO: parexpr ( check vars)
// else { p.error_with_pos('unexpected `${typeof(lx)}`', lx.position()) }
}
}
}
return ast.AssignStmt{op: op, left: left, right: right, pos: pos, has_cross_var: has_cross_var, is_simple: p.inside_for && p.tok.kind == .lcbr}
return ast.AssignStmt{
op: op
left: left
right: right
pos: pos
has_cross_var: has_cross_var
is_simple: p.inside_for && p.tok.kind == .lcbr
}
}

View File

@ -1,10 +1,10 @@
module parser
// import v.eval
import v.ast
import v.gen
import v.table
import v.checker
//import v.eval
import v.pref
import term
@ -77,7 +77,9 @@ x := 10
'
table := &table.Table{}
vpref := &pref.Preferences{}
gscope := &ast.Scope{ parent: 0 }
gscope := &ast.Scope{
parent: 0
}
prog := parse_file(s, table, .skip_comments, vpref, gscope)
mut checker := checker.new_checker(table, vpref)
checker.check(prog)
@ -90,12 +92,7 @@ fn test_one() {
return
}
println('\n\ntest_one()')
input := ['a := 10',
// 'a = 20',
'b := -a',
'c := 20',
//
]
input := ['a := 10', 'b := -a', 'c := 20']
expected := 'int a = 10;int b = -a;int c = 20;'
table := table.new_table()
vpref := &pref.Preferences{}
@ -119,7 +116,8 @@ fn test_one() {
ok := expected == res
println(res)
assert ok
if !ok {}
if !ok {
}
// exit(0)
}
@ -127,73 +125,14 @@ fn test_parse_expr() {
if true {
return
}
input := ['1 == 1',
'234234',
'2 * 8 + 3',
'a := 3',
'a++',
'b := 4 + 2',
'neg := -a',
'a + a',
'bo := 2 + 3 == 5',
'2 + 1',
'q := 1',
'q + 777',
'2 + 3',
'2+2*4',
// '(2+2)*4',
'x := 10',
'mut aa := 12',
'ab := 10 + 3 * 9',
's := "hi"',
// '1 += 2',
'x = 11',
'a += 10',
'1.2 + 3.4',
'4 + 4',
'1 + 2 * 5',
'-a+1',
'2+2',
/*
/*
'(2 * 3) / 2',
'3 + (7 * 6)',
'2 ^ 8 * (7 * 6)',
'20 + (10 * 15) / 5', // 50
'(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8
//'2 + "hi"',
*/
*/
]
expecting := ['1 == 1;',
'234234;',
'2 * 8 + 3;',
'int a = 3;',
'a++;',
'int b = 4 + 2;',
'int neg = -a;',
'a + a;',
'bool bo = 2 + 3 == 5;',
'2 + 1;',
'int q = 1;',
'q + 777;',
'2 + 3;',
'2 + 2 * 4;',
// '(2 + 2) * 4',
'int x = 10;',
'int aa = 12;',
'int ab = 10 + 3 * 9;',
'string s = tos3("hi");',
// '1 += 2;',
'x = 11;',
'a += 10;',
'1.2 + 3.4;',
'4 + 4;',
'1 + 2 * 5;',
'-a + 1;',
'2 + 2;',
]
input := ['1 == 1', '234234', '2 * 8 + 3', 'a := 3', 'a++', 'b := 4 + 2', 'neg := -a',
'a + a', 'bo := 2 + 3 == 5', '2 + 1', 'q := 1', 'q + 777', '2 + 3', '2+2*4', 'x := 10',
'mut aa := 12', 'ab := 10 + 3 * 9', 's := "hi"', 'x = 11', 'a += 10', '1.2 + 3.4', '4 + 4',
'1 + 2 * 5', '-a+1', '2+2']
expecting := ['1 == 1;', '234234;', '2 * 8 + 3;', 'int a = 3;', 'a++;', 'int b = 4 + 2;',
'int neg = -a;', 'a + a;', 'bool bo = 2 + 3 == 5;', '2 + 1;', 'int q = 1;', 'q + 777;',
'2 + 3;', '2 + 2 * 4;', 'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;', 'string s = tos3("hi");',
'x = 11;', 'a += 10;', '1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;', '-a + 1;', '2 + 2;']
mut e := []ast.Stmt{}
table := table.new_table()
vpref := &pref.Preferences{}
@ -237,7 +176,7 @@ fn test_parse_expr() {
}
/*
table := &table.Table{}
table := &table.Table{}
for s in text_expr {
// print using str method
x := parse_expr(s, table)

View File

@ -121,8 +121,7 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt {
update_exprs << p.expr(0)
if p.tok.kind == .comma {
p.check(.comma)
}
else {
} else {
break
}
}