diff --git a/vlib/v/gen/tests/1.vv b/vlib/v/gen/tests/1.vv index 860d0366cd..6bca86123d 100644 --- a/vlib/v/gen/tests/1.vv +++ b/vlib/v/gen/tests/1.vv @@ -25,7 +25,9 @@ fn get_int2() int { fn myuser() { user := User{age:10} - age := user.age + age := user.age + 1 + boo := 2 + boo2 := boo+1 b := age > 0 //b2 := user.age > 0 } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index b2e69d72e3..6580bf0273 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -339,29 +339,13 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) { // left binding power for rbp < p.tok.precedence() { prev_tok := p.tok - if prev_tok.kind == .dot { - p.warn('dot prev_tok = $prev_tok.str() typ=$typ.name') - p.next() - field := p.check_name() - mut ok := false - for f in typ.fields { - if f.name == field { - ok = true - } - } - if !ok { - p.error('unknown field `${typ.name}.$field`') - } - node = ast.SelectorExpr{ - expr: node - field: field - } - return node,typ - } p.next() mut t2 := types.Type{} // left denotation (infix / postfix) - if prev_tok.is_right_assoc() { + if prev_tok.is_right_assoc() && + !p.tok.kind in [.plus, .minus] && // think of better way to handle this + !p.peek_tok.kind in [.number, .name] { // supposed to be only unary, additive handled in left asssoc + mut expr := ast.Expr{} expr,t2 = p.expr(prev_tok.precedence() - 1) node = ast.BinaryExpr{ @@ -376,8 +360,28 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) { } } else if prev_tok.is_left_assoc() { + // postfix `.` + if prev_tok.kind == .dot { + p.warn('dot prev_tok = $prev_tok.str() typ=$typ.name') + // p.next() + field := p.check_name() + mut ok := false + for f in typ.fields { + if f.name == field { + ok = true + } + } + if !ok { + p.error('unknown field `${typ.name}.$field`') + } + node = ast.SelectorExpr{ + expr: node + field: field + } + // return node,typ + } // postfix (`++` | `--`) - if prev_tok.kind in [.inc, .dec] { + else if prev_tok.kind in [.inc, .dec] { node = ast.UnaryExpr{ left: node op: prev_tok.kind @@ -404,6 +408,11 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) { return node,typ } +[inline] +fn (p &Parser) is_addative() bool { + return p.tok.kind in [.plus, .minus] && p.peek_tok.kind in [.number, .name] +} + fn (p mut Parser) for_statement() ast.ForStmt { p.check(.key_for) // `for i in start .. end` diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index 468418a439..3b7deb3d1f 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -364,6 +364,10 @@ 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 { return tok.kind in [ + // `.` + .dot, + // `+` | `-` + .plus, .minus, // additive // .number, // `++` | `--` .inc, .dec, @@ -383,7 +387,7 @@ pub fn (tok Token) is_left_assoc() bool { pub fn (tok Token) is_right_assoc() bool { return tok.kind in [ // `+` | `-` | `!` - .plus, .minus, .not, + .plus, .minus, .not, // unary // `=` | `+=` | `-=` | `*=` | `/=` .assign, .plus_assign, .minus_assign, .mult_assign, .div_assign, // `%=` | `>>=` | `<<=`