diff --git a/src/compiler.rs b/src/compiler.rs index 317ae6a..2aa5ec3 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -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); + } } diff --git a/src/jit.rs b/src/jit.rs index e58f486..538dcce 100644 --- a/src/jit.rs +++ b/src/jit.rs @@ -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); @@ -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, diff --git a/tests/cases/ints.lyte b/tests/cases/ints.lyte index c21f289..083cd93 100644 --- a/tests/cases/ints.lyte +++ b/tests/cases/ints.lyte @@ -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)