Add Stmt and incorporate meta statements

This helps split up expressions, meta calls (like includes) that can be
expanded into more expressions.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-01-16 14:14:50 -08:00
parent c3d667ec54
commit effa99e65d
6 changed files with 114 additions and 27 deletions

View File

@@ -17,14 +17,39 @@ impl<'s> Compile<'s> {
}
}
pub fn compile(&mut self, expr_list: &Vec<SpExpr>) -> Vec<Inst> {
pub fn compile(&mut self, stmt_list: Vec<SpStmt>) -> Vec<Inst> {
// Compile meta-statements
let expr_list = self.compile_meta(stmt_list);
self.compile_expr_list(&expr_list)
}
/// Compile meta-expressions, like includes.
fn compile_meta(&mut self, stmt_list: Vec<SpStmt>) -> Vec<SpExpr> {
let mut expr_list = Vec::new();
for stmt in stmt_list {
match stmt.into_inner() {
Stmt::Include(path) => {
let stmt_list = self.parse_include(&path);
expr_list.extend(self.compile_meta(stmt_list));
}
Stmt::Expr(expr) => expr_list.push(expr),
}
}
expr_list
}
fn parse_include(&mut self, _path: &str) -> Vec<SpStmt> {
todo!("includes")
}
fn compile_expr_list(&mut self, expr_list: &Vec<SpExpr>) -> Vec<Inst> {
// Scoping is done here.
// Local scopes are implicit. If a variable is assigned to at
// all in the current scope, it's considered to be a local.
let mut code = Vec::new();
for expr in expr_list {
self.discover_locals(expr);
let thunk = self.compile_expr(expr);
self.discover_locals(&expr);
let thunk = self.compile_expr(&expr);
code.extend(thunk.flatten());
}
code
@@ -34,13 +59,14 @@ impl<'s> Compile<'s> {
match expr.inner() {
Expr::Atom(atom) => self.compile_atom(atom),
// this gets compiled whenever it gets evaluated
Expr::Quote(exprs) => {
Expr::Quote(stmts) => {
self.scope_stack.push_scope();
let compiled = Rc::new(self.compile(exprs));
// TODO - is self.compile the right thing to do here?
let compiled = Rc::new(self.compile(stmts.clone()));
let locals = self.scope_stack.pop_scope().unwrap();
let quote = self
.quote_table
.insert(expr.span(), locals, exprs.clone(), compiled);
.insert(expr.span(), locals, stmts.clone(), compiled);
Inst::PushValue(Value::Quote(quote)).into()
}
}