modulus operator

This commit is contained in:
uan
2026-02-07 15:32:52 +01:00
parent 7fddf9ab30
commit 58f0dfa969
3 changed files with 19 additions and 5 deletions

View File

@@ -25,6 +25,7 @@ enum TokenType as u8 {
minus
star
slash
percent
equals
less
greater
@@ -36,6 +37,7 @@ enum TokenType as u8 {
minus_eq
star_eq
slash_eq
percent_eq
increment
decrement
lparen
@@ -73,6 +75,7 @@ fn toktype_from_delimiter(delimiter string) TokenType {
'-' {.minus}
'*' {.star}
'/' {.slash}
'%' {.percent}
'=' {.equals}
'<' {.less}
'>' {.greater}
@@ -88,6 +91,7 @@ fn toktype_from_delimiter(delimiter string) TokenType {
'-=' {.minus_eq}
'*=' {.star_eq}
'/=' {.slash_eq}
'%=' {.percent_eq}
'++' {.increment}
'--' {.decrement}
else {.unknown}
@@ -186,7 +190,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,10 +18,10 @@ enum Precedence {
fn (p Parser) get_precedence(tok_type TokenType) Precedence {
return match tok_type {
.equals, .plus_eq, .minus_eq, .star_eq, .slash_eq { .assignment }
.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 { .product }
.star, .slash, .percent { .product }
.dot { .access }
.increment, .decrement { .suffix }
else { .base}
@@ -467,12 +467,14 @@ fn (mut p Parser) parse_unary_left(op string) UnaryExpr {
fn (mut p Parser) parse_binary(left Expr, op string, prec Precedence) BinaryExpr {
if op in ['=', '+=', '-=', '*=', '/='] {
if op in ['=', '+=', '-=', '*=', '/=', '%='] {
if p.get_expr_is_immutable(left) {
parse_error("Cannot assign to immutable expression ${left}")
}
}
dump(op)
right := p.parse_expr(prec)
binary_expr := BinaryExpr{left, op, right}
@@ -572,7 +574,15 @@ fn (mut p Parser) get_expr_type(expr Expr) string {
fn (mut p Parser) is_op_valid_for_type(type string, op string) bool {
global := ['=', '==', '!=']
mut legal_ops := match type {
'int', 'real' {['+', '-', '*', '/', '<', '>', '<=', '>=', '++', '--', '+=', '-=', '*=', '/=', '++', '--']}
'int' {[
'+', '-', '*', '/', '%', '<', '>', '<=',
'>=', '++', '--', '+=', '-=', '*=', '/=',
'%='
]}
'real' {[
'+', '-', '*', '/', '<', '>', '<=', '>=',
'++', '--', '+=', '-=', '*=', '/=',
]}
'bool' {['=']}
else {[]}
}