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