fix function with lambda body

pull/4/head
azur 2022-12-14 12:50:02 +07:00
parent 07e3af2664
commit a53d7b31f0
5 changed files with 51 additions and 24 deletions

View File

@ -43,15 +43,15 @@ impl Compiler {
parser::BinaryOp::Sub => Instr::NumSub,
parser::BinaryOp::Mul => Instr::NumMul,
parser::BinaryOp::Div => Instr::NumDiv,
// parser::BinaryOp::Eq => Instr::Eq,
// parser::BinaryOp::Ne => Instr::Neq,
// parser::BinaryOp::Lt => Instr::Lt,
// parser::BinaryOp::Gt => Instr::Gt,
// parser::BinaryOp::Le => Instr::Lte,
// parser::BinaryOp::Ge => Instr::Gte,
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::And => Instr::BoolAnd,
parser::BinaryOp::Or => Instr::BoolOr,
_ => todo!(),
parser::BinaryOp::Pipe => todo!(),
});
instrs
}
@ -128,10 +128,13 @@ impl Compiler {
match stmt {
Stmt::Fun(name, args, body) => {
let is_main = name == "main";
let mut instrs = vec![
Instr::FuncMake(args, self.compile_expr(body.0)),
Instr::Set(name),
];
let mut instrs = match body.0 {
// If the body is a lambda then we don't have to compile
// it into a function
Expr::Lambda(_, _) => self.compile_expr(body.0),
_ => vec![Instr::FuncMake(args, self.compile_expr(body.0))],
};
instrs.push(Instr::Set(name));
if is_main {
instrs.pop();
instrs.push(Instr::FuncApply);

View File

@ -13,9 +13,12 @@ fn main() {
if let Some(ast) = ast {
let mut compiler = Compiler::new();
let instrs = compiler.compile_program(ast);
instrs.iter().for_each(|i| println!("{:?}", i));
// instrs.iter().for_each(|i| println!("{:?}", i));
let mut executor = Executor::new(instrs);
match executor.run() {
match executor.run_with(|exec| {
// println!("{:?}", exec.stack);
Ok(())
}) {
Ok(_) => {}
Err(e) => println!("Runtime error: {:?}", e),
}

View File

@ -1,4 +1,8 @@
fun foo x = x
fun fac n = if n == 0 then 1 else n * fac(n - 1)
fun main = do
let foo = if true then 10 else 5 in
print(foo)
print(foo(1))
print(fac(5))
end

View File

@ -38,6 +38,15 @@ impl Executor {
Ok(())
}
pub fn run_with<F: Fn(&mut Self) -> Result<(), Error>>(&mut self, f: F) -> Result<(), Error> {
while self.ip < self.instrs.len() {
self.step()?;
self.ip += 1;
f(self)?;
}
Ok(())
}
fn err(&self, msg: &str) -> Error {
Error::make(msg, self.ip)
}
@ -93,10 +102,10 @@ impl Executor {
.ok_or_else(|| self.err("invalid instruction pointer"))?;
macro_rules! impl_num_binop {
($op:tt) => {
($op:tt, $ret:ident) => {
match (self.pop()?, self.pop()?) {
(Value::Num(a), Value::Num(b)) => {
self.stack.push(Value::Num(a $op b));
self.stack.push(Value::$ret(a $op b));
}
_ => return Err(Error::make("can't apply operator to non-numbers", self.ip)),
}
@ -117,11 +126,12 @@ impl Executor {
Instr::NumPush(x) => {
self.push(Value::Num(*x))?;
}
Instr::NumAdd => impl_num_binop!(+),
Instr::NumSub => impl_num_binop!(-),
Instr::NumMul => impl_num_binop!(*),
Instr::NumDiv => impl_num_binop!(/),
Instr::NumMod => impl_num_binop!(%),
Instr::NumAdd => impl_num_binop!(+, Num),
Instr::NumSub => impl_num_binop!(-, Num),
Instr::NumMul => impl_num_binop!(*, Num),
Instr::NumDiv => impl_num_binop!(/, Num),
Instr::NumMod => impl_num_binop!(%, Num),
Instr::NumEq => impl_num_binop!(==, Bool),
Instr::BoolPush(x) => {
self.push(Value::Bool(*x))?;

View File

@ -103,7 +103,8 @@ pub enum Instr {
NumSub, // │ 1 byte
NumMul, // │
NumDiv, // │
NumMod, // ┘
NumMod, // │
NumEq, // ┘
BoolPush(bool), // 2 bytes: 1 byte for the enum, 1 byte for the bool
BoolAnd, // ┐ 1 byte
@ -171,7 +172,12 @@ impl Instr {
pub fn size(&self) -> usize {
match self {
Instr::NumPush(_) => 1 + std::mem::size_of::<i64>(),
Instr::NumAdd | Instr::NumSub | Instr::NumMul | Instr::NumDiv | Instr::NumMod => 1,
Instr::NumAdd
| Instr::NumSub
| Instr::NumMul
| Instr::NumDiv
| Instr::NumMod
| Instr::NumEq => 1,
Instr::BoolPush(_) => 1 + std::mem::size_of::<bool>(),
Instr::BoolAnd | Instr::BoolOr | Instr::BoolNot => 1,
@ -227,6 +233,7 @@ impl Instr {
Instr::NumMul => bytes.push(index!()),
Instr::NumDiv => bytes.push(index!()),
Instr::NumMod => bytes.push(index!()),
Instr::NumEq => bytes.push(index!()),
Instr::BoolPush(b) => {
bytes.push(index!());