clean up parser_test.v

pull/3222/head
Alexander Medvednikov 2019-12-26 12:02:38 +03:00
parent d9835c1ecf
commit 9b37fc7310
5 changed files with 27 additions and 51 deletions

View File

@ -158,7 +158,7 @@ fn (p mut Parser) fnext() {
//println('eof ret') //println('eof ret')
//return //return
//} //}
if p.tok == .rcbr && !p.inside_if_expr { //&& p.prev_tok != .lcbr { if p.tok == .rcbr && !p.inside_if_expr && p.prev_tok != .lcbr {
p.fmt_dec() p.fmt_dec()
} }
s := p.strtok() s := p.strtok()
@ -167,7 +167,7 @@ fn (p mut Parser) fnext() {
} }
// vfmt: increase indentation on `{` unless it's `{}` // vfmt: increase indentation on `{` unless it's `{}`
inc_indent := false inc_indent := false
if p.tok == .lcbr && !p.inside_if_expr {// && p.peek() != .rcbr { if p.tok == .lcbr && !p.inside_if_expr && p.peek() != .rcbr {
p.fgen_nl() p.fgen_nl()
p.fmt_inc() p.fmt_inc()
} }

View File

@ -48,6 +48,15 @@ fn (g mut Gen) expr(node ast.Expr) Type {
g.write(it.val.str()) g.write(it.val.str())
return int_type return int_type
} }
ast.ScalarExpr {
g.expr(it.left)
g.write(' $it.val ')
}
ast.UnaryExpr {
g.expr(it.left)
g.write(' $it.op ')
}
ast.StringLiteral { ast.StringLiteral {
g.write('"$it.val"') g.write('"$it.val"')
return string_type return string_type

View File

@ -52,8 +52,7 @@ pub fn (p mut Parser) expr(rbp int) ast.Expr {
tok := p.tok tok := p.tok
lit := p.lit lit := p.lit
p.next() p.next()
mut node := ast.Expr{ mut node := ast.Expr{}
}
match tok { match tok {
.lpar { .lpar {
node = p.expr(0) node = p.expr(0)
@ -122,6 +121,5 @@ fn (p mut Parser) stmt() ast.Stmt {
} }
*/ */
return ast.VarDecl{ return ast.VarDecl{}
}
} }

View File

@ -6,12 +6,12 @@ import (
) )
fn test_parser() { fn test_parser() {
//if true { return }
//expr := ast.IntegerExpr {val:10} //expr := ast.IntegerExpr {val:10}
//expr := ast.BinaryExpr{} //expr := ast.BinaryExpr{}
// print using walk // print using walk
expr := parse_expr('3 + 7') expr := parse_expr('3 + 7')
walk(expr)
println('\n') println('\n')
text_expr := [ text_expr := [
@ -23,8 +23,10 @@ fn test_parser() {
'3 + (7 * 6)', '3 + (7 * 6)',
'2 ^ 8 * (7 * 6)', '2 ^ 8 * (7 * 6)',
'20 + (10 * 15) / 5', // 50 '20 + (10 * 15) / 5', // 50
'(2) + (17*2-30) * (5)+2 - (8/2)*4' // 8 '(2) + (17*2-30) * (5)+2 - (8/2)*4', // 8
'2 + "hi"'
] ]
for s in text_expr { for s in text_expr {
// print using str method // print using str method
x := parse_expr(s) x := parse_expr(s)
@ -35,47 +37,17 @@ fn test_parser() {
} }
fn walk(node ast.Expr) { fn test_cgen() {
//println('walk()')
match node {
ast.BinaryExpr {
print(' (')
walk(it.left)
// print('$it.op.str()')
match it.op {
.plus {
print(' + ')
}
.minus {
print(' - ')
}
else {}
}
walk(it.right)
print(') ')
}
ast.ScalarExpr {
walk(it.left)
print(' $it.val ')
}
ast.UnaryExpr {
walk(it.left)
print(' $it.op ')
}
else { }
}
/*
//expr := parse_expr('3 + 7 * 2') //expr := parse_expr('3 + 7 * 2')
expr := parse_stmt('a := 3 + "f"') //expr2 := parse_stmt('a := 3 + "f"')
expr2 := parse_expr('2 +3 ')//"helo"')
program := ast.Program{ program := ast.Program{
exprs: [ exprs: [
expr, expr2,
//parse_expr('2 * 2'), //parse_expr('2 * 2'),
] ]
} }
cgen.gen(program) cgen.gen(program)
//cgen.save() //cgen.save()
*/
} }

View File

@ -329,10 +329,7 @@ pub fn (tok Token) precedence() int {
// is_scalar returns true if the token is a scalar // is_scalar returns true if the token is a scalar
pub fn (tok Token) is_scalar() bool { pub fn (tok Token) is_scalar() bool {
match tok { return tok in [.number, .str]
.number { return true }
else { return false }
}
} }
// 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