Skip to content

Commit

Permalink
Work on unary operators
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Sep 22, 2023
1 parent 2f6941b commit cdd520f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,17 @@ mod tests {

jit(code);
}

#[test]
fn test_neg() {
let code = r#"
assert(cond: bool) → void
main {
assert(1-2 == -1)
}
"#;

jit(code);
}
}
25 changes: 21 additions & 4 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,8 @@ impl<'a> FunctionTranslator<'a> {
Expr::Binop(op, lhs_id, rhs_id) => {
self.translate_binop(*op, *lhs_id, *rhs_id, decl, decls)
}
Expr::Unop(_op, expr_id) => {
let v = self.translate_expr(*expr_id, decl, decls);
let bnot = self.builder.ins().bnot(v);
self.builder.ins().band_imm(bnot, 1)
Expr::Unop(op, arg_id) => {
self.translate_unop(*op, *arg_id, decl, decls)
}
Expr::Call(fn_id, arg_ids) => {
let f = self.translate_expr(*fn_id, decl, decls);
Expand Down Expand Up @@ -426,6 +424,25 @@ impl<'a> FunctionTranslator<'a> {
}
}

fn translate_unop(
&mut self,
unop: Unop,
arg_id: ExprID,
decl: &FuncDecl,
decls: &crate::DeclTable,
) -> Value {
let v = self.translate_expr(arg_id, decl, decls);
match unop {
Unop::Neg => {
self.builder.ins().imul_imm(v, -1)
}
Unop::Not => {
let bnot = self.builder.ins().bnot(v);
self.builder.ins().band_imm(bnot, 1)
}
}
}

fn translate_binop(
&mut self,
binop: Binop,
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/ints.lyte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ main {
assert(2+2 == 2+2)
assert(1-1 == 0)
assert(2-2 == 0)
//assert(1-2 == -1)
assert(1-2 == -1)
assert(1+1+1+1+1+1+1+1 == 8)
assert(1+1+1+1+1+1+1*2 == 8)
assert(2⋅2⋅2⋅2⋅2⋅2 == 64)
Expand Down

0 comments on commit cdd520f

Please sign in to comment.