1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: dot fix

This commit is contained in:
joe-conigliaro 2020-01-03 21:36:17 +11:00 committed by Alexander Medvednikov
parent 60eec9fd4d
commit 343ded18fd
3 changed files with 38 additions and 23 deletions

View File

@ -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
}

View File

@ -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`

View File

@ -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,
// `%=` | `>>=` | `<<=`