refactor: function
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::bytecode::*;
|
||||
use crate::ir;
|
||||
use crate::ty::internal::Const;
|
||||
use crate::ty::internal::Func;
|
||||
|
||||
pub struct Compiler {
|
||||
opcodes: Vec<OpCode>,
|
||||
@@ -16,6 +17,15 @@ pub fn compile(downgraded: ir::Downgraded) -> Program {
|
||||
opcodes: Compiler::new().compile(thunk),
|
||||
})
|
||||
.collect(),
|
||||
funcs: downgraded
|
||||
.funcs
|
||||
.into_iter()
|
||||
.map(|func| Func {
|
||||
env: None,
|
||||
param: func.param.into(),
|
||||
opcodes: Compiler::new().compile(*func.body)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,10 +53,6 @@ impl Compiler {
|
||||
self.opcodes[idx] = code;
|
||||
}
|
||||
|
||||
fn last(&self) -> Option<OpCode> {
|
||||
self.opcodes.last().cloned()
|
||||
}
|
||||
|
||||
fn pop(&mut self) -> Option<OpCode> {
|
||||
self.opcodes.pop()
|
||||
}
|
||||
@@ -393,31 +399,9 @@ impl Compile for ir::Assert {
|
||||
}
|
||||
}
|
||||
|
||||
impl Compile for ir::Func {
|
||||
impl Compile for ir::LoadFunc {
|
||||
fn compile(self, comp: &mut Compiler) {
|
||||
comp.push(OpCode::Func { idx: self.body.idx });
|
||||
use ir::Param::*;
|
||||
match self.param {
|
||||
Ident(sym) => comp.push(OpCode::PushIdentParam { sym }),
|
||||
Formals {
|
||||
formals,
|
||||
ellipsis,
|
||||
alias,
|
||||
} => {
|
||||
for (sym, default) in formals {
|
||||
comp.push(OpCode::PushFormalParam { sym });
|
||||
if let Some(ir::Thunk { idx }) = default {
|
||||
comp.push(OpCode::PushDefaultParam { idx });
|
||||
}
|
||||
}
|
||||
if ellipsis {
|
||||
comp.push(OpCode::SetEllipsis);
|
||||
}
|
||||
if let Some(sym) = alias {
|
||||
comp.push(OpCode::SetAlias { sym });
|
||||
}
|
||||
}
|
||||
}
|
||||
comp.push(OpCode::Func { idx: self.idx });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user