&&, ||, &, |, &=, |=

This commit is contained in:
uan
2026-02-07 16:11:00 +01:00
parent 9d26256023
commit 005828cec2
4 changed files with 26 additions and 16 deletions

View File

@@ -18,13 +18,13 @@ enum Precedence {
fn (p Parser) get_precedence(tok_type TokenType) Precedence {
return match tok_type {
.equals, .plus_eq, .minus_eq, .star_eq, .slash_eq, .percent_eq { .assignment }
.eq_eq, .not_eq, .less_eq, .greater_eq, .less, .greater { .comparison }
.plus, .minus { .sum }
.star, .slash, .percent { .product }
.dot { .access }
.increment, .decrement { .suffix }
else { .base}
.equals, .plus_eq, .minus_eq, .star_eq, .slash_eq, .percent_eq, .and_eq, .or_eq, .and_and, .or_or {.assignment}
.eq_eq, .not_eq, .less_eq, .greater_eq, .less, .greater {.comparison}
.plus, .minus {.sum}
.star, .slash, .percent {.product}
.dot {.access}
.increment, .decrement {.suffix}
else {.base}
}
}
@@ -74,9 +74,6 @@ fn (mut s SymbolTable) define_var(name string, typ string, is_const bool) {
}
fn (mut s SymbolTable) lookup_var(name string) ?VarSymbolInfo {
$if debug {
dump(s.variable_scopes.len)
}
if s.variable_scopes.len == 0 {return none}
for variables in s.variable_scopes.reverse() {
if name in variables {
@@ -518,6 +515,7 @@ fn (mut p Parser) parse_paren() ParenExpr {
fn (mut p Parser) check_binary_expr_types(expr BinaryExpr) {
left_t := p.get_expr_type(expr.left)
right_t := p.get_expr_type(expr.right)
dump("${left_t}, ${right_t}")
if left_t != right_t {
parse_error('Type mismatch in expression: ${left_t} and ${right_t}')
}
@@ -558,7 +556,7 @@ fn (mut p Parser) get_expr_type(expr Expr) string {
BinaryExpr {
p.check_binary_expr_types(expr)
left_t := p.get_expr_type(expr.left)
if expr.op in ['<=', '==', '>=', '!=', '<', '>'] {
if expr.op in ['<=', '==', '>=', '!=', '<', '>', '||', '&&'] {
'bool'
} else {
left_t
@@ -595,7 +593,7 @@ fn (mut p Parser) is_op_valid_for_type(type string, op string) bool {
'+', '-', '*', '/', '<', '>', '<=', '>=',
'++', '--', '+=', '-=', '*=', '/=',
]}
'bool' {['=']}
'bool' {['&&', '||', '&', '|', '&=', '|=']}
else {[]}
}
legal_ops << global