Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/codegen/zig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,11 @@ impl ZigBackend {
}

ExprKind::BinOp { op, lhs, rhs } => {
let lhs_s = self.gen_expr(lhs);
let rhs_s = self.gen_expr(rhs);
let op_str = match op {
// Zig requires @rem for signed integer remainder (% only works on unsigned)
BinOp::Mod => return format!("@rem({}, {})", lhs_s, rhs_s),
BinOp::Add => "+",
BinOp::Sub => "-",
BinOp::Mul => "*",
Expand All @@ -534,8 +538,6 @@ impl ZigBackend {
BinOp::And => "and",
BinOp::Or => "or",
};
let lhs_s = self.gen_expr(lhs);
let rhs_s = self.gen_expr(rhs);
format!("({} {} {})", lhs_s, op_str, rhs_s)
}

Expand Down
5 changes: 5 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum Token {
Minus, // -
Star, // *
Slash, // /
Percent, // %
And, // &&
Or, // ||
Bang, // !
Expand Down Expand Up @@ -204,6 +205,10 @@ fn tokenize_raw(source: &str) -> Vec<(Token, Span)> {
chars.next();
tokens.push((Token::Star, (pos, end_pos!())));
}
'%' => {
chars.next();
tokens.push((Token::Percent, (pos, end_pos!())));
}
'.' => {
chars.next();
tokens.push((Token::Dot, (pos, end_pos!())));
Expand Down
2 changes: 2 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum BinOp {
Sub,
Mul,
Div,
Mod,
Eq,
NotEq,
Lt,
Expand Down Expand Up @@ -706,6 +707,7 @@ impl Parser {
let op = match self.peek() {
Token::Star => BinOp::Mul,
Token::Slash => BinOp::Div,
Token::Percent => BinOp::Mod,
_ => break,
};
self.advance();
Expand Down
12 changes: 12 additions & 0 deletions src/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,15 @@ fn test_codegen_export_const_hoisted() {
let main_start = out.find("pub fn main").unwrap_or(out.len());
assert!(!out[main_start..].contains("const c ="), "got:\n{}", out);
}

#[test]
fn test_codegen_modulo() {
let out = compile(
r#"
export fn rem(a: i32, b: i32): i32 {
return a % b;
}
"#,
);
assert!(out.contains("@rem(a, b)"), "got:\n{}", out);
}
3 changes: 3 additions & 0 deletions src/tests/fixtures/modulo.zy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export fn rem(a: i32, b: i32): i32 {
return a % b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: src/tests/snapshots.rs
assertion_line: 10
expression: output
input_file: src/tests/fixtures/modulo.zy
---
pub fn rem(a: i32, b: i32) i32 {
return @rem(a, b);
}
21 changes: 21 additions & 0 deletions src/tests/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,24 @@ fn test_if_expr_branch_type_mismatch() {
"if branches have different types",
);
}

#[test]
fn test_modulo_ok() {
ok(r#"
export fn rem(a: i32, b: i32): i32 { return a % b; }
"#);
}

#[test]
fn test_modulo_type_mismatch() {
err(
r#"
fn main(): void {
const x: i32 = 10;
const y: f64 = 3.0;
const _z = x % y;
}
"#,
"Binary op type mismatch",
);
}
2 changes: 1 addition & 1 deletion src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ impl TypeChecker {
let lhs_ty = self.check_expr(lhs, scope);
let rhs_ty = self.check_expr(rhs, scope);
match op {
BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div => {
BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod => {
if lhs_ty != rhs_ty {
self.error(format!(
"Binary op type mismatch: '{}' vs '{}'",
Expand Down