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

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

2
.gitignore vendored
View File

@@ -1,6 +1,6 @@
# Binaries for programs and plugins
main
onev
one
exec
*.exe
*.exe~

18
lexer.v
View File

@@ -26,6 +26,10 @@ enum TokenType as u8 {
star
slash
percent
and
or
and_and
or_or
equals
less
greater
@@ -38,6 +42,8 @@ enum TokenType as u8 {
star_eq
slash_eq
percent_eq
and_eq
or_eq
increment
decrement
lparen
@@ -94,6 +100,12 @@ fn toktype_from_delimiter(delimiter string) TokenType {
'%=' {.percent_eq}
'++' {.increment}
'--' {.decrement}
'&' {.and}
'&&' {.and_and}
'|' {.or}
'||' {.or_or}
'&=' {.and_eq}
'|=' {.or_eq}
else {.unknown}
}
}
@@ -120,8 +132,8 @@ fn toktype_from_kw(kw string) TokenType {
fn is_delimiter(c u8, is_inside_number bool) bool {
valid_chars := match is_inside_number {
true {" #+-*/,;:%<>()[]{}=\n\""}
false {". #+-*/,;:%<>()[]{}=\n\""}
true {" #+-*/,;:%<>()[]{}=|&!\n\""}
false {". #+-*/,;:%<>()[]{}=|&!\n\""}
}
return valid_chars.contains(c.ascii_str())
}
@@ -190,7 +202,7 @@ fn lex(input string) ?[]Token {
mut tok_str := input[right].ascii_str()
if right + 1 < input.len {
combined := input.substr(right, right + 2)
if combined in ['==', '>=', '<=', '!=', '+=', '-=', '*=', '/=', '%=', '++', '--'] {
if combined in ['==', '>=', '<=', '!=', '+=', '-=', '*=', '/=', '%=', '++', '--', '||', '&&', '&=', '|='] {
tok_str = combined
right++
}

BIN
one

Binary file not shown.

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