From 1af274a71496dce3ab88e37a9eec0f9b5a050698 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 27 Dec 2019 09:35:30 +0100 Subject: [PATCH] test_parser + minor fixes --- vlib/v/ast/ast.v | 1 + vlib/v/cgen/cgen.v | 1 + vlib/v/parser/parser.v | 3 +-- vlib/v/parser/parser_test.v | 53 ++++++++++++++++++++++--------------- vlib/v/token/token.v | 26 +++++++++--------- 5 files changed, 47 insertions(+), 37 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 0fa347267d..a3646f1b3e 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -52,6 +52,7 @@ pub: pub struct Program { pub: exprs []Expr + //stmts []Stmt } diff --git a/vlib/v/cgen/cgen.v b/vlib/v/cgen/cgen.v index 5179bb8be7..c93f3c75db 100644 --- a/vlib/v/cgen/cgen.v +++ b/vlib/v/cgen/cgen.v @@ -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) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 820f526444..94e0045e31 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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]) diff --git a/vlib/v/parser/parser_test.v b/vlib/v/parser/parser_test.v index 842bc4bf49..635abca15f 100644 --- a/vlib/v/parser/parser_test.v +++ b/vlib/v/parser/parser_test.v @@ -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 } } diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index d48e70b25f..ee65720593 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -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 + ] }