parser: fix `a & b == c` precedence

pull/2927/head
Alexander Medvednikov 2019-11-29 22:42:32 +03:00
parent e957fd6f30
commit f724a956b3
2 changed files with 30 additions and 13 deletions

View File

@ -66,6 +66,15 @@ fn test_str_methods() {
assert u64(-1).str() == '18446744073709551615' assert u64(-1).str() == '18446744073709551615'
} }
fn test_and() {
c:=[1,2,3,4,5]
assert c[0] & 1 != 0
assert c[1] & 1 == 0
assert c[2] & 1 != 0
assert c[3] & 1 == 0
assert c[4] & 1 != 0
}
/* /*
fn test_cmp() { fn test_cmp() {
assert 1 2 assert 1 2

View File

@ -473,14 +473,22 @@ fn (p mut Parser) expression() string {
p.error('strings only support `+` operator') p.error('strings only support `+` operator')
} }
expr_type := p.term() expr_type := p.term()
if (tok_op in [.pipe, .amp, .xor]) && !(is_integer_type(expr_type) && mut open := false
is_integer_type(typ)) { if tok_op in [.pipe, .amp, .xor] {
p.error('operator ${tok_op.str()} is defined only on integer types') if !(is_integer_type(expr_type) && is_integer_type(typ)) {
p.error('operator ${tok_op.str()} is defined only on integer types')
}
p.cgen.set_placeholder(ph, '(')
open = true
} }
p.check_types(expr_type, typ) p.check_types(expr_type, typ)
if (is_str || is_ustr) && tok_op == .plus && !p.is_js { if (is_str || is_ustr) && tok_op == .plus && !p.is_js {
p.gen(')') p.gen(')')
} }
if open {
p.gen(')')
}
// Make sure operators are used with correct types // Make sure operators are used with correct types
if !p.pref.translated && !is_str && !is_ustr && !is_num { if !p.pref.translated && !is_str && !is_ustr && !is_num {
T := p.table.find_type(typ) T := p.table.find_type(typ)