diff --git a/lexer.v b/lexer.v index 8f81a84..a5f3932 100644 --- a/lexer.v +++ b/lexer.v @@ -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++ } diff --git a/one b/one index 711b1ec..6074933 100755 Binary files a/one and b/one differ diff --git a/parser.v b/parser.v index 680aa95..2a86262 100644 --- a/parser.v +++ b/parser.v @@ -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 {[]} }