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:
parent
60eec9fd4d
commit
343ded18fd
@ -25,7 +25,9 @@ fn get_int2() int {
|
|||||||
|
|
||||||
fn myuser() {
|
fn myuser() {
|
||||||
user := User{age:10}
|
user := User{age:10}
|
||||||
age := user.age
|
age := user.age + 1
|
||||||
|
boo := 2
|
||||||
|
boo2 := boo+1
|
||||||
b := age > 0
|
b := age > 0
|
||||||
//b2 := user.age > 0
|
//b2 := user.age > 0
|
||||||
}
|
}
|
||||||
|
@ -339,29 +339,13 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
|
|||||||
// left binding power
|
// left binding power
|
||||||
for rbp < p.tok.precedence() {
|
for rbp < p.tok.precedence() {
|
||||||
prev_tok := p.tok
|
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()
|
p.next()
|
||||||
mut t2 := types.Type{}
|
mut t2 := types.Type{}
|
||||||
// left denotation (infix / postfix)
|
// 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{}
|
mut expr := ast.Expr{}
|
||||||
expr,t2 = p.expr(prev_tok.precedence() - 1)
|
expr,t2 = p.expr(prev_tok.precedence() - 1)
|
||||||
node = ast.BinaryExpr{
|
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() {
|
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 (`++` | `--`)
|
// postfix (`++` | `--`)
|
||||||
if prev_tok.kind in [.inc, .dec] {
|
else if prev_tok.kind in [.inc, .dec] {
|
||||||
node = ast.UnaryExpr{
|
node = ast.UnaryExpr{
|
||||||
left: node
|
left: node
|
||||||
op: prev_tok.kind
|
op: prev_tok.kind
|
||||||
@ -404,6 +408,11 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
|
|||||||
return node,typ
|
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 {
|
fn (p mut Parser) for_statement() ast.ForStmt {
|
||||||
p.check(.key_for)
|
p.check(.key_for)
|
||||||
// `for i in start .. end`
|
// `for i in start .. end`
|
||||||
|
@ -364,6 +364,10 @@ pub fn (tok Token) is_unary() bool {
|
|||||||
// is_left_assoc returns true if the token is left associative
|
// is_left_assoc returns true if the token is left associative
|
||||||
pub fn (tok Token) is_left_assoc() bool {
|
pub fn (tok Token) is_left_assoc() bool {
|
||||||
return tok.kind in [
|
return tok.kind in [
|
||||||
|
// `.`
|
||||||
|
.dot,
|
||||||
|
// `+` | `-`
|
||||||
|
.plus, .minus, // additive
|
||||||
// .number,
|
// .number,
|
||||||
// `++` | `--`
|
// `++` | `--`
|
||||||
.inc, .dec,
|
.inc, .dec,
|
||||||
@ -383,7 +387,7 @@ pub fn (tok Token) is_left_assoc() bool {
|
|||||||
pub fn (tok Token) is_right_assoc() bool {
|
pub fn (tok Token) is_right_assoc() bool {
|
||||||
return tok.kind in [
|
return tok.kind in [
|
||||||
// `+` | `-` | `!`
|
// `+` | `-` | `!`
|
||||||
.plus, .minus, .not,
|
.plus, .minus, .not, // unary
|
||||||
// `=` | `+=` | `-=` | `*=` | `/=`
|
// `=` | `+=` | `-=` | `*=` | `/=`
|
||||||
.assign, .plus_assign, .minus_assign, .mult_assign, .div_assign,
|
.assign, .plus_assign, .minus_assign, .mult_assign, .div_assign,
|
||||||
// `%=` | `>>=` | `<<=`
|
// `%=` | `>>=` | `<<=`
|
||||||
|
Loading…
Reference in New Issue
Block a user