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:
exprs []Expr
//stmts []Stmt
}

View File

@ -61,6 +61,7 @@ fn (g mut Gen) expr(node ast.Expr) {
.minus { g.write(' - ') }
.mul { g.write(' * ') }
.div { g.write(' / ') }
.plus_assign { g.write(' += ') }
else {}
}
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 {
break
}
println('expr at ' + p.tok.str())
//println('expr at ' + p.tok.str())
expr,_ := p.expr(token.lowest_prec)
exprs << expr
p.next()
}
println('nr exprs = $exprs.len')
println(exprs[0])

View File

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

View File

@ -317,8 +317,8 @@ pub fn (tok Token) precedence() int {
.mul, .div, .left_shift, .righ_shift, .amp { return 7 }
// `+` | `-` | `|` | `^`
.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 }
// `||`
@ -334,11 +334,10 @@ pub fn (tok Token) is_scalar() bool {
// is_unary returns true if the token can be in a unary expression
pub fn (tok Token) is_unary() bool {
match tok {
return tok in [
// `+` | `-` | `!` | `~` | `*` | `&`
.plus, .minus, .not, .bit_not, .mul, .amp { return true }
else { return false }
}
.plus, .minus, .not, .bit_not, .mul, .amp
]
}
// 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
pub fn (tok Token) is_left_assoc() bool {
match tok {
return tok in [
// .number,
// `*` | `/` | `%`
.mul, .div, .mod,
// `^` | `||` | `&`
.xor, .logical_or, .and,
// `,`
.comma { return true }
else { return false }
}
.comma
]
}
// is_right_assoc returns true if the token is right associative
pub fn (tok Token) is_right_assoc() bool {
match tok {
return tok in [
// `+` | `-` | `!` | `++` | `--`
.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,
// `&=` | `^=` | `|=`
.and_assign, .xor_assign, .or_assign { return true }
else { return false }
}
.and_assign, .xor_assign, .or_assign
]
}