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

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 # Binaries for programs and plugins
main main
onev one
exec exec
*.exe *.exe
*.exe~ *.exe~

18
lexer.v
View File

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

BIN
one

Binary file not shown.

View File

@@ -18,7 +18,7 @@ enum Precedence {
fn (p Parser) get_precedence(tok_type TokenType) Precedence { fn (p Parser) get_precedence(tok_type TokenType) Precedence {
return match tok_type { return match tok_type {
.equals, .plus_eq, .minus_eq, .star_eq, .slash_eq, .percent_eq { .assignment } .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} .eq_eq, .not_eq, .less_eq, .greater_eq, .less, .greater {.comparison}
.plus, .minus {.sum} .plus, .minus {.sum}
.star, .slash, .percent {.product} .star, .slash, .percent {.product}
@@ -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 { fn (mut s SymbolTable) lookup_var(name string) ?VarSymbolInfo {
$if debug {
dump(s.variable_scopes.len)
}
if s.variable_scopes.len == 0 {return none} if s.variable_scopes.len == 0 {return none}
for variables in s.variable_scopes.reverse() { for variables in s.variable_scopes.reverse() {
if name in variables { 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) { fn (mut p Parser) check_binary_expr_types(expr BinaryExpr) {
left_t := p.get_expr_type(expr.left) left_t := p.get_expr_type(expr.left)
right_t := p.get_expr_type(expr.right) right_t := p.get_expr_type(expr.right)
dump("${left_t}, ${right_t}")
if left_t != right_t { if left_t != right_t {
parse_error('Type mismatch in expression: ${left_t} and ${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 { BinaryExpr {
p.check_binary_expr_types(expr) p.check_binary_expr_types(expr)
left_t := p.get_expr_type(expr.left) left_t := p.get_expr_type(expr.left)
if expr.op in ['<=', '==', '>=', '!=', '<', '>'] { if expr.op in ['<=', '==', '>=', '!=', '<', '>', '||', '&&'] {
'bool' 'bool'
} else { } else {
left_t left_t
@@ -595,7 +593,7 @@ fn (mut p Parser) is_op_valid_for_type(type string, op string) bool {
'+', '-', '*', '/', '<', '>', '<=', '>=', '+', '-', '*', '/', '<', '>', '<=', '>=',
'++', '--', '+=', '-=', '*=', '/=', '++', '--', '+=', '-=', '*=', '/=',
]} ]}
'bool' {['=']} 'bool' {['&&', '||', '&', '|', '&=', '|=']}
else {[]} else {[]}
} }
legal_ops << global legal_ops << global