1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

more number operators

This commit is contained in:
azur 2022-12-16 20:09:46 +07:00
parent 7248580e24
commit b5856a35e7
4 changed files with 44 additions and 15 deletions

View file

@ -44,11 +44,11 @@ impl Compiler {
parser::BinaryOp::Mul => Instr::NumMul,
parser::BinaryOp::Div => Instr::NumDiv,
parser::BinaryOp::Eq => Instr::NumEq,
parser::BinaryOp::Ne => todo!(),
parser::BinaryOp::Lt => todo!(),
parser::BinaryOp::Gt => todo!(),
parser::BinaryOp::Le => todo!(),
parser::BinaryOp::Ge => todo!(),
parser::BinaryOp::Ne => Instr::NumNe,
parser::BinaryOp::Lt => Instr::NumLt,
parser::BinaryOp::Gt => Instr::NumGt,
parser::BinaryOp::Le => Instr::NumLe,
parser::BinaryOp::Ge => Instr::NumGe,
parser::BinaryOp::And => Instr::BoolAnd,
parser::BinaryOp::Or => Instr::BoolOr,
parser::BinaryOp::Pipe => todo!(),

9
examples/factorial.sial Normal file
View file

@ -0,0 +1,9 @@
fun factorial n =
let helper = \n acc ->
if n > 0
then helper(n - 1, acc * n)
else acc
in
helper(n, 1)
fun main = println(factorial(20))

View file

@ -122,6 +122,11 @@ impl Executor {
Instr::NumDiv => impl_binop!(/, Num, Num),
Instr::NumMod => impl_binop!(%, Num, Num),
Instr::NumEq => impl_binop!(==, Num, Bool),
Instr::NumNe => impl_binop!(!=, Num, Bool),
Instr::NumLt => impl_binop!(<, Num, Bool),
Instr::NumGt => impl_binop!(>, Num, Bool),
Instr::NumLe => impl_binop!(<=, Num, Bool),
Instr::NumGe => impl_binop!(>=, Num, Bool),
Instr::BoolPush(x) => {
self.push(Value::Bool(*x))?;

View file

@ -99,12 +99,17 @@ pub enum Instr {
// 00 67 00 00 00 00 00 00 00
// 01
NumPush(i64), // 9 bytes: 1 byte for the enum, 8 bytes for the i64
NumAdd, // ┐
NumSub, // │ 1 byte
NumAdd, // ┐ 1 byte
NumSub, // │
NumMul, // │
NumDiv, // │
NumMod, // │
NumEq, // ┘
NumEq, // │
NumNe, // │
NumLt, // │
NumGt, // │
NumLe, // │
NumGe, // ┘
BoolPush(bool), // 2 bytes: 1 byte for the enum, 1 byte for the bool
BoolAnd, // ┐ 1 byte
@ -179,7 +184,12 @@ impl Instr {
| Instr::NumMul
| Instr::NumDiv
| Instr::NumMod
| Instr::NumEq => 1,
| Instr::NumEq
| Instr::NumNe
| Instr::NumLt
| Instr::NumGt
| Instr::NumLe
| Instr::NumGe => 1,
Instr::BoolPush(_) => 1 + std::mem::size_of::<bool>(),
Instr::BoolAnd | Instr::BoolOr | Instr::BoolNot => 1,
@ -231,12 +241,17 @@ impl Instr {
bytes.push(index!());
bytes.extend(n.to_le_bytes());
}
Instr::NumAdd => bytes.push(index!()),
Instr::NumSub => bytes.push(index!()),
Instr::NumMul => bytes.push(index!()),
Instr::NumDiv => bytes.push(index!()),
Instr::NumMod => bytes.push(index!()),
Instr::NumEq => bytes.push(index!()),
Instr::NumAdd
| Instr::NumSub
| Instr::NumMul
| Instr::NumDiv
| Instr::NumMod
| Instr::NumEq
| Instr::NumNe
| Instr::NumLt
| Instr::NumGt
| Instr::NumLe
| Instr::NumGe => bytes.push(index!()),
Instr::BoolPush(b) => {
bytes.push(index!());