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

compiler2: update operator precedence and associativity (#3218)

This commit is contained in:
joe-conigliaro
2019-12-26 13:40:18 +11:00
committed by Alexander Medvednikov
parent c1f4fc3f0f
commit fe8a6abb41
3 changed files with 64 additions and 34 deletions

View File

@@ -313,10 +313,16 @@ const (
// Precedence returns a tokens precedence if defined, otherwise lowest_prec
pub fn (tok Token) precedence() int {
match tok {
.plus, .minus { return 4 }
.mul, .div { return 4 }
.xor { return 6 }
.mod {return 7 }
// `*` | `/` | `%` | `<<` | `>>` | `&`
.mul, .div, .left_shift, .righ_shift, .amp { return 7 }
// `+` | `-` | `|` | `^`
.plus, .minus, .pipe, .xor { return 6 }
// `==` | `!=` | `<` | `<=` | `>` | `>=`
.eq, .ne, .lt, .le, .gt, .ge { return 5 }
// `&&`
.and { return 4 }
// `||`
.logical_or { return 3 }
else { return lowest_prec }
}
}
@@ -332,15 +338,25 @@ 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 {
.plus, .minus { return true }
// `+` | `-` | `!` | `~` | `*` | `&`
.plus, .minus, .not, .bit_not, .mul, .amp { return true }
else { return false }
}
}
// NOTE: do we need this for all tokens (is_left_assoc / is_right_assoc),
// or only ones with the same precedence?
// is_left_assoc returns true if the token is left associative
pub fn (tok Token) is_left_assoc() bool {
match tok {
.number, .plus, .minus, .mul, .div, .mod { return true }
// .number,
// `*` | `/` | `%`
.mul, .div, .mod,
// `^` | `||` | `&`
.xor, .logical_or, .and,
// `,`
.comma { return true }
else { return false }
}
}
@@ -348,7 +364,14 @@ pub fn (tok Token) is_left_assoc() bool {
// is_right_assoc returns true if the token is right associative
pub fn (tok Token) is_right_assoc() bool {
match tok {
.xor { return true }
// `+` | `-` | `!` | `++` | `--`
.plus, .minus, .not, .inc, .dec,
// `=` | `+=` | `-=` | `*=` | `/=`
.assign, .plus_assign, .minus_assign, .mult_assign, .div_assign,
// `%=` | `>>=` | `<<=`
.mod_assign, .righ_shift_assign, .left_shift_assign,
// `&=` | `^=` | `|=`
.and_assign, .xor_assign, .or_assign { return true }
else { return false }
}
}