fmt: run on parser/assign.v
parent
2bfe8e5153
commit
6a335c4416
|
@ -5,7 +5,6 @@ module parser
|
||||||
|
|
||||||
import v.ast
|
import v.ast
|
||||||
|
|
||||||
|
|
||||||
fn (mut p Parser) assign_stmt() ast.Stmt {
|
fn (mut p Parser) assign_stmt() ast.Stmt {
|
||||||
return p.partial_assign_stmt(p.expr_list())
|
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 {
|
for expr in exprs {
|
||||||
if expr is ast.Ident {
|
if expr is ast.Ident {
|
||||||
ident := expr as ast.Ident
|
ident := expr as ast.Ident
|
||||||
if ident.name == it.name {
|
if ident.name == val.name {
|
||||||
p.error_with_pos('undefined variable: `$it.name`', it.pos)
|
p.error_with_pos('undefined variable: `$val.name`', val.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast.InfixExpr {
|
ast.InfixExpr {
|
||||||
p.check_undefined_variables(exprs, it.left)
|
p.check_undefined_variables(exprs, val.left)
|
||||||
p.check_undefined_variables(exprs, it.right)
|
p.check_undefined_variables(exprs, val.right)
|
||||||
}
|
}
|
||||||
ast.ParExpr {
|
ast.ParExpr {
|
||||||
p.check_undefined_variables(exprs, it.expr)
|
p.check_undefined_variables(exprs, val.expr)
|
||||||
}
|
}
|
||||||
ast.PostfixExpr {
|
ast.PostfixExpr {
|
||||||
p.check_undefined_variables(exprs, it.expr)
|
p.check_undefined_variables(exprs, val.expr)
|
||||||
}
|
}
|
||||||
ast.PrefixExpr {
|
ast.PrefixExpr {
|
||||||
p.check_undefined_variables(exprs, it.right)
|
p.check_undefined_variables(exprs, val.right)
|
||||||
}
|
}
|
||||||
ast.StringInterLiteral {
|
ast.StringInterLiteral {
|
||||||
for expr_ in it.exprs {
|
for expr_ in val.exprs {
|
||||||
p.check_undefined_variables(exprs, expr_)
|
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 {
|
fn (mut p Parser) check_cross_variables(exprs []ast.Expr, val ast.Expr) bool {
|
||||||
match val {
|
match val {
|
||||||
ast.Ident {
|
ast.Ident { for expr in exprs {
|
||||||
for expr in exprs {
|
|
||||||
if expr is ast.Ident {
|
if expr is ast.Ident {
|
||||||
ident := expr as 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, val.left) || p.check_cross_variables(exprs, val.right) }
|
||||||
ast.InfixExpr {
|
ast.PrefixExpr { return p.check_cross_variables(exprs, val.right) }
|
||||||
return p.check_cross_variables(exprs, it.left) || p.check_cross_variables(exprs, it.right)
|
ast.PostfixExpr { return p.check_cross_variables(exprs, val.expr) }
|
||||||
}
|
|
||||||
ast.PrefixExpr {
|
|
||||||
return p.check_cross_variables(exprs, it.right)
|
|
||||||
}
|
|
||||||
ast.PostfixExpr {
|
|
||||||
return p.check_cross_variables(exprs, it.expr)
|
|
||||||
}
|
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -80,8 +73,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt {
|
||||||
for r in right {
|
for r in right {
|
||||||
p.check_undefined_variables(left, r)
|
p.check_undefined_variables(left, r)
|
||||||
}
|
}
|
||||||
}
|
} else if left.len > 1 {
|
||||||
else if left.len > 1 {
|
|
||||||
// a, b = b, a
|
// a, b = b, a
|
||||||
for r in right {
|
for r in right {
|
||||||
has_cross_var = p.check_cross_variables(left, r)
|
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 {
|
ast.IndexExpr {
|
||||||
if op == .decl_assign {
|
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
|
lx.is_setter = true
|
||||||
}
|
}
|
||||||
|
@ -120,13 +113,22 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr) ast.Stmt {
|
||||||
ast.PrefixExpr {}
|
ast.PrefixExpr {}
|
||||||
ast.SelectorExpr {
|
ast.SelectorExpr {
|
||||||
if op == .decl_assign {
|
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 {}
|
else {
|
||||||
// TODO: parexpr ( check vars)
|
// TODO: parexpr ( check vars)
|
||||||
// else { p.error_with_pos('unexpected `${typeof(lx)}`', lx.position()) }
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
module parser
|
module parser
|
||||||
|
|
||||||
|
// import v.eval
|
||||||
import v.ast
|
import v.ast
|
||||||
import v.gen
|
import v.gen
|
||||||
import v.table
|
import v.table
|
||||||
import v.checker
|
import v.checker
|
||||||
//import v.eval
|
|
||||||
import v.pref
|
import v.pref
|
||||||
import term
|
import term
|
||||||
|
|
||||||
|
@ -77,7 +77,9 @@ x := 10
|
||||||
'
|
'
|
||||||
table := &table.Table{}
|
table := &table.Table{}
|
||||||
vpref := &pref.Preferences{}
|
vpref := &pref.Preferences{}
|
||||||
gscope := &ast.Scope{ parent: 0 }
|
gscope := &ast.Scope{
|
||||||
|
parent: 0
|
||||||
|
}
|
||||||
prog := parse_file(s, table, .skip_comments, vpref, gscope)
|
prog := parse_file(s, table, .skip_comments, vpref, gscope)
|
||||||
mut checker := checker.new_checker(table, vpref)
|
mut checker := checker.new_checker(table, vpref)
|
||||||
checker.check(prog)
|
checker.check(prog)
|
||||||
|
@ -90,12 +92,7 @@ fn test_one() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
println('\n\ntest_one()')
|
println('\n\ntest_one()')
|
||||||
input := ['a := 10',
|
input := ['a := 10', 'b := -a', 'c := 20']
|
||||||
// 'a = 20',
|
|
||||||
'b := -a',
|
|
||||||
'c := 20',
|
|
||||||
//
|
|
||||||
]
|
|
||||||
expected := 'int a = 10;int b = -a;int c = 20;'
|
expected := 'int a = 10;int b = -a;int c = 20;'
|
||||||
table := table.new_table()
|
table := table.new_table()
|
||||||
vpref := &pref.Preferences{}
|
vpref := &pref.Preferences{}
|
||||||
|
@ -119,7 +116,8 @@ fn test_one() {
|
||||||
ok := expected == res
|
ok := expected == res
|
||||||
println(res)
|
println(res)
|
||||||
assert ok
|
assert ok
|
||||||
if !ok {}
|
if !ok {
|
||||||
|
}
|
||||||
// exit(0)
|
// exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,73 +125,14 @@ fn test_parse_expr() {
|
||||||
if true {
|
if true {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
input := ['1 == 1',
|
input := ['1 == 1', '234234', '2 * 8 + 3', 'a := 3', 'a++', 'b := 4 + 2', 'neg := -a',
|
||||||
'234234',
|
'a + a', 'bo := 2 + 3 == 5', '2 + 1', 'q := 1', 'q + 777', '2 + 3', '2+2*4', 'x := 10',
|
||||||
'2 * 8 + 3',
|
'mut aa := 12', 'ab := 10 + 3 * 9', 's := "hi"', 'x = 11', 'a += 10', '1.2 + 3.4', '4 + 4',
|
||||||
'a := 3',
|
'1 + 2 * 5', '-a+1', '2+2']
|
||||||
'a++',
|
expecting := ['1 == 1;', '234234;', '2 * 8 + 3;', 'int a = 3;', 'a++;', 'int b = 4 + 2;',
|
||||||
'b := 4 + 2',
|
'int neg = -a;', 'a + a;', 'bool bo = 2 + 3 == 5;', '2 + 1;', 'int q = 1;', 'q + 777;',
|
||||||
'neg := -a',
|
'2 + 3;', '2 + 2 * 4;', 'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;', 'string s = tos3("hi");',
|
||||||
'a + a',
|
'x = 11;', 'a += 10;', '1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;', '-a + 1;', '2 + 2;']
|
||||||
'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;',
|
|
||||||
]
|
|
||||||
mut e := []ast.Stmt{}
|
mut e := []ast.Stmt{}
|
||||||
table := table.new_table()
|
table := table.new_table()
|
||||||
vpref := &pref.Preferences{}
|
vpref := &pref.Preferences{}
|
||||||
|
@ -237,7 +176,7 @@ fn test_parse_expr() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
table := &table.Table{}
|
table := &table.Table{}
|
||||||
for s in text_expr {
|
for s in text_expr {
|
||||||
// print using str method
|
// print using str method
|
||||||
x := parse_expr(s, table)
|
x := parse_expr(s, table)
|
||||||
|
|
|
@ -121,8 +121,7 @@ fn (mut p Parser) sql_stmt() ast.SqlStmt {
|
||||||
update_exprs << p.expr(0)
|
update_exprs << p.expr(0)
|
||||||
if p.tok.kind == .comma {
|
if p.tok.kind == .comma {
|
||||||
p.check(.comma)
|
p.check(.comma)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue