test_parser + minor fixes

pull/3230/head
Alexander Medvednikov 2019-12-27 09:35:30 +01:00
parent 3f0f8bac49
commit 1af274a714
5 changed files with 47 additions and 37 deletions

View File

@ -52,6 +52,7 @@ pub:
pub struct Program { pub struct Program {
pub: pub:
exprs []Expr exprs []Expr
//stmts []Stmt
} }

View File

@ -61,6 +61,7 @@ fn (g mut Gen) expr(node ast.Expr) {
.minus { g.write(' - ') } .minus { g.write(' - ') }
.mul { g.write(' * ') } .mul { g.write(' * ') }
.div { g.write(' / ') } .div { g.write(' / ') }
.plus_assign { g.write(' += ') }
else {} else {}
} }
g.expr(it.right) g.expr(it.right)

View File

@ -48,10 +48,9 @@ pub fn parse_file(text string, table &table.Table) ast.Program {
if p.tok == .eof { if p.tok == .eof {
break break
} }
println('expr at ' + p.tok.str()) //println('expr at ' + p.tok.str())
expr,_ := p.expr(token.lowest_prec) expr,_ := p.expr(token.lowest_prec)
exprs << expr exprs << expr
p.next()
} }
println('nr exprs = $exprs.len') println('nr exprs = $exprs.len')
println(exprs[0]) println(exprs[0])

View File

@ -6,15 +6,9 @@ import (
v.table v.table
) )
/*
fn test_parser() { fn test_parser() {
if true { return } if true { return }
//expr := ast.IntegerExpr {val:10}
//expr := ast.BinaryExpr{}
// print using walk
//expr := parse_expr('3 + 7')
//println('\n')
text_expr := [ text_expr := [
'1 += 2', '1 += 2',
'1.2 + 3.4', '1.2 + 3.4',
@ -38,22 +32,23 @@ fn test_parser() {
println('===================') println('===================')
} }
} }
/*
fn test_cgen2() { */
s := '2 + 3 fn test_parse_file() {
s := '12 + 3
x := 10
5+7 5+7
//x := 100 8+4
' '
table := &table.Table{} table := &table.Table{}
prog := parse_file(s, table) prog := parse_file(s, table)
cgen.gen(prog) res := cgen.gen(prog)
println(res)
println('done') println('done')
} }
*/
fn test_cgen() { fn test_parse_expr() {
//if true { return }
input := [ input := [
'2 + 3', '2 + 3',
'2+2*4', '2+2*4',
@ -61,9 +56,23 @@ fn test_cgen() {
'x := 10', 'x := 10',
'a := 12', 'a := 12',
'ab := 10 + 3 * 9', 'ab := 10 + 3 * 9',
's := "hi"' 's := "hi"',
'1 += 2',
//'1.2 + 3.4',
/*
'4 + 4',
'1 + 2 * 5',
'(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"',
'x := 10',
*/
] ]
output := [ expecting := [
'2 + 3', '2 + 3',
'2 + 2 * 4', '2 + 2 * 4',
//'(2 + 2) * 4', //'(2 + 2) * 4',
@ -71,6 +80,8 @@ fn test_cgen() {
'int a = 12;', 'int a = 12;',
'int ab = 10 + 3 * 9;', 'int ab = 10 + 3 * 9;',
'string s = tos3("hi");', 'string s = tos3("hi");',
'1 += 2',
//'1.2 + 3.4',
] ]
//expr := parse_expr('3 + 7 * 2') //expr := parse_expr('3 + 7 * 2')
//expr2 := parse_stmt('a := 3 + "f"') //expr2 := parse_stmt('a := 3 + "f"')
@ -84,16 +95,16 @@ fn test_cgen() {
println('========') println('========')
println(res) println(res)
println('========') println('========')
lines := res.split_into_lines() lines := res.trim_space().split_into_lines()
mut i := 0 mut i := 0
for line in lines { for line in lines {
if line == '' { if line == '' {
continue continue
} }
println('"$line" "${output[i]}"') println('V:"$line" expecting:"${expecting[i]}"')
assert line == output[i] assert line == expecting[i]
i++ i++
if i >= output.len { if i >= expecting.len {
break break
} }
} }

View File

@ -317,8 +317,8 @@ pub fn (tok Token) precedence() int {
.mul, .div, .left_shift, .righ_shift, .amp { return 7 } .mul, .div, .left_shift, .righ_shift, .amp { return 7 }
// `+` | `-` | `|` | `^` // `+` | `-` | `|` | `^`
.plus, .minus, .pipe, .xor { return 6 } .plus, .minus, .pipe, .xor { return 6 }
// `==` | `!=` | `<` | `<=` | `>` | `>=` // `==` | `!=` | `<` | `<=` | `>` | `>=` | +=
.eq, .ne, .lt, .le, .gt, .ge { return 5 } .eq, .ne, .lt, .le, .gt, .ge, .plus_assign { return 5 }
// `&&` // `&&`
.and { return 4 } .and { return 4 }
// `||` // `||`
@ -334,11 +334,10 @@ pub fn (tok Token) is_scalar() bool {
// is_unary returns true if the token can be in a unary expression // is_unary returns true if the token can be in a unary expression
pub fn (tok Token) is_unary() bool { pub fn (tok Token) is_unary() bool {
match tok { return tok in [
// `+` | `-` | `!` | `~` | `*` | `&` // `+` | `-` | `!` | `~` | `*` | `&`
.plus, .minus, .not, .bit_not, .mul, .amp { return true } .plus, .minus, .not, .bit_not, .mul, .amp
else { return false } ]
}
} }
// NOTE: do we need this for all tokens (is_left_assoc / is_right_assoc), // NOTE: do we need this for all tokens (is_left_assoc / is_right_assoc),
@ -346,21 +345,21 @@ pub fn (tok Token) is_unary() bool {
// is_left_assoc returns true if the token is left associative // is_left_assoc returns true if the token is left associative
pub fn (tok Token) is_left_assoc() bool { pub fn (tok Token) is_left_assoc() bool {
match tok { return tok in [
// .number, // .number,
// `*` | `/` | `%` // `*` | `/` | `%`
.mul, .div, .mod, .mul, .div, .mod,
// `^` | `||` | `&` // `^` | `||` | `&`
.xor, .logical_or, .and, .xor, .logical_or, .and,
// `,` // `,`
.comma { return true } .comma
else { return false } ]
}
} }
// is_right_assoc returns true if the token is right associative // is_right_assoc returns true if the token is right associative
pub fn (tok Token) is_right_assoc() bool { pub fn (tok Token) is_right_assoc() bool {
match tok { return tok in [
// `+` | `-` | `!` | `++` | `--` // `+` | `-` | `!` | `++` | `--`
.plus, .minus, .not, .inc, .dec, .plus, .minus, .not, .inc, .dec,
// `=` | `+=` | `-=` | `*=` | `/=` // `=` | `+=` | `-=` | `*=` | `/=`
@ -368,7 +367,6 @@ pub fn (tok Token) is_right_assoc() bool {
// `%=` | `>>=` | `<<=` // `%=` | `>>=` | `<<=`
.mod_assign, .righ_shift_assign, .left_shift_assign, .mod_assign, .righ_shift_assign, .left_shift_assign,
// `&=` | `^=` | `|=` // `&=` | `^=` | `|=`
.and_assign, .xor_assign, .or_assign { return true } .and_assign, .xor_assign, .or_assign
else { return false } ]
}
} }