Add function compilation

Functions are compiled in the most naiive way right now. I want to fix
up how scope lookups are done before it becomes too much to update.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-09-26 18:31:23 -07:00
parent 3976b2135a
commit 4848a342f0
7 changed files with 53 additions and 21 deletions

View File

@@ -345,9 +345,28 @@ impl Visit for CompileBody<'_> {
Ok(thunk)
}
fn visit_fun_expr(&mut self, _expr: &FunExpr) -> Self::Out {
// TODO : fun exprs should be compiled as constants and put into the constant pool
todo!()
fn visit_fun_expr(&mut self, expr: &FunExpr) -> Self::Out {
// - push const
// (functions are unique const values so a new function will be created for every literal
// function defined in code)
// This is pretty much the only place where a new scope layer gets pushed beyond the start
// of the program
self.compile.push_scope_layer();
for param in expr.params.iter() {
let sym = global_sym(param.to_string());
self.compile.create_local(sym);
}
let locals = self.compile.pop_scope_layer().unwrap();
let code = self.visit_body(&expr.body)?
.flatten()
.to_vec();
let (hdl, _fun) = self.compile.push_const(UserFun::new_obj(code, locals));
// TODO(compile) : determine return value at the end of the body (preferably at parse-time)
// oh yeah, we were compiling a function body weren't we
Ok(Inst::PushConst(hdl).into())
}
fn visit_atom(&mut self, atom: &Atom) -> Self::Out {