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::Mul => Instr::NumMul,
parser::BinaryOp::Div => Instr::NumDiv, parser::BinaryOp::Div => Instr::NumDiv,
parser::BinaryOp::Eq => Instr::NumEq, parser::BinaryOp::Eq => Instr::NumEq,
parser::BinaryOp::Ne => todo!(), parser::BinaryOp::Ne => Instr::NumNe,
parser::BinaryOp::Lt => todo!(), parser::BinaryOp::Lt => Instr::NumLt,
parser::BinaryOp::Gt => todo!(), parser::BinaryOp::Gt => Instr::NumGt,
parser::BinaryOp::Le => todo!(), parser::BinaryOp::Le => Instr::NumLe,
parser::BinaryOp::Ge => todo!(), parser::BinaryOp::Ge => Instr::NumGe,
parser::BinaryOp::And => Instr::BoolAnd, parser::BinaryOp::And => Instr::BoolAnd,
parser::BinaryOp::Or => Instr::BoolOr, parser::BinaryOp::Or => Instr::BoolOr,
parser::BinaryOp::Pipe => todo!(), 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::NumDiv => impl_binop!(/, Num, Num),
Instr::NumMod => impl_binop!(%, Num, Num), Instr::NumMod => impl_binop!(%, Num, Num),
Instr::NumEq => impl_binop!(==, Num, Bool), 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) => { Instr::BoolPush(x) => {
self.push(Value::Bool(*x))?; self.push(Value::Bool(*x))?;

View file

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