[fine] Simplify function compilation
This is simpler because we don't "discover" functions to compile as we go, we just compile all the ones we can find, and functions have pre-defined exports. This is good and useful to us as we can now refer to functions in different modules by known indices, but it *does* make me wonder what we're going to do for compiling generic specializations. The previous technique was better for that sort of thing. This is all predicated on the idea that I want to have partially-compiled modules, which I can't really say why I want it. If I'm happy to just compile things cross module in the same kind of space then it's much easier to go back to the function key way of working.
This commit is contained in:
parent
4c061fbd28
commit
ab477cd783
14 changed files with 190 additions and 214 deletions
|
|
@ -142,20 +142,11 @@ impl std::fmt::Debug for Function {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||
struct FunctionKey {
|
||||
tree: TreeRef,
|
||||
}
|
||||
|
||||
struct Compiler<'a> {
|
||||
source: &'a str,
|
||||
semantics: &'a Semantics,
|
||||
syntax: &'a SyntaxTree,
|
||||
|
||||
function_bindings: HashMap<FunctionKey, usize>,
|
||||
pending_functions: Vec<(FunctionKey, usize, Function)>,
|
||||
temp_functions: Vec<Option<Rc<Function>>>,
|
||||
|
||||
module: Module,
|
||||
function: Function,
|
||||
}
|
||||
|
|
@ -249,16 +240,33 @@ macro_rules! ice {
|
|||
}}
|
||||
}
|
||||
|
||||
// macro_rules! inst_panic {
|
||||
// ($($t:tt)+) => {{
|
||||
// // eprintln!($($t)*);
|
||||
// Instruction::Panic
|
||||
// }};
|
||||
// }
|
||||
type CR = Result<(), &'static str>;
|
||||
const OK: CR = CR::Ok(());
|
||||
|
||||
// macro_rules! ice {
|
||||
// ($compiler:expr, $tr:expr, $($t:tt)*) => {{}};
|
||||
// }
|
||||
fn function_from_function_decl(
|
||||
source: &str,
|
||||
syntax: &SyntaxTree,
|
||||
tree: &Tree,
|
||||
) -> Result<Function, &'static str> {
|
||||
// TODO: If this is a method the name should be different.
|
||||
let name = tree.nth_token(1).ok_or("no id")?.as_str(source);
|
||||
|
||||
let param_list = tree
|
||||
.child_tree_of_kind(syntax, TreeKind::ParamList)
|
||||
.ok_or("no paramlist")?;
|
||||
let param_count = param_list.children.len() - 2;
|
||||
|
||||
Ok(Function::new(name, param_count))
|
||||
}
|
||||
|
||||
fn function_from_class_decl(source: &str, tree: &Tree) -> Result<Function, &'static str> {
|
||||
let name = tree.nth_token(1).ok_or("no name")?.as_str(source);
|
||||
|
||||
// TODO: I think this is incorrect!
|
||||
let field_count = tree.children.len() - 2;
|
||||
|
||||
Ok(Function::new(name, field_count))
|
||||
}
|
||||
|
||||
pub fn compile(semantics: &Semantics) -> Rc<Module> {
|
||||
let source = semantics.source();
|
||||
|
|
@ -269,32 +277,44 @@ pub fn compile(semantics: &Semantics) -> Rc<Module> {
|
|||
semantics: &semantics,
|
||||
syntax: &syntax_tree,
|
||||
|
||||
function_bindings: HashMap::new(),
|
||||
pending_functions: Vec::new(),
|
||||
temp_functions: Vec::new(),
|
||||
|
||||
module: Module::new(),
|
||||
function: Function::new("<< module >>", 0),
|
||||
};
|
||||
|
||||
let mut functions = vec![None; semantics.function_count() + 1];
|
||||
if let Some(t) = semantics.tree().root() {
|
||||
compiler.temp_functions.push(None);
|
||||
file(&mut compiler, t);
|
||||
compiler.temp_functions[0] = Some(Rc::new(compiler.function));
|
||||
compiler.module.init = 0;
|
||||
let index = functions.len() - 1;
|
||||
functions[index] = Some(Rc::new(compiler.function));
|
||||
compiler.module.init = index;
|
||||
}
|
||||
|
||||
while let Some((fk, idx, func)) = compiler.pending_functions.pop() {
|
||||
if idx >= compiler.temp_functions.len() {
|
||||
compiler.temp_functions.resize(idx + 1, None);
|
||||
for t in semantics.tree().trees() {
|
||||
if let Some(function_index) = semantics.get_function_index(t) {
|
||||
let tree = &semantics.tree()[t];
|
||||
let function = match tree.kind {
|
||||
TreeKind::FunctionDecl => function_from_function_decl(&source, &syntax_tree, tree),
|
||||
TreeKind::ClassDecl => function_from_class_decl(&source, tree),
|
||||
_ => Err("don't know how to make a function of this"),
|
||||
};
|
||||
|
||||
if let Ok(function) = function {
|
||||
compiler.function = function;
|
||||
|
||||
let _ = compile_function(&mut compiler, t);
|
||||
|
||||
let function = Rc::new(compiler.function);
|
||||
compiler
|
||||
.module
|
||||
.exports
|
||||
.insert(function.name.clone(), Export::Function(function_index));
|
||||
functions[function_index] = Some(function);
|
||||
}
|
||||
}
|
||||
compiler.function = func;
|
||||
let _ = compile_function(&mut compiler, fk.tree);
|
||||
compiler.temp_functions[idx] = Some(Rc::new(compiler.function));
|
||||
}
|
||||
|
||||
let mut module = compiler.module;
|
||||
for f in compiler.temp_functions {
|
||||
for f in functions {
|
||||
module.functions.push(f.unwrap());
|
||||
}
|
||||
|
||||
|
|
@ -317,9 +337,6 @@ fn file(c: &mut Compiler, t: TreeRef) {
|
|||
c.push(Instruction::Return);
|
||||
}
|
||||
|
||||
type CR = Result<(), &'static str>;
|
||||
const OK: CR = CR::Ok(());
|
||||
|
||||
fn compile_expression(c: &mut Compiler, t: TreeRef) {
|
||||
let tree = &c.syntax[t];
|
||||
let cr = match tree.kind {
|
||||
|
|
@ -656,43 +673,15 @@ fn compile_load_declaration(c: &mut Compiler, t: TreeRef, declaration: &Declarat
|
|||
Instruction::LoadSlot(index)
|
||||
}
|
||||
|
||||
Location::Function => {
|
||||
// TODO: Assert declaration is local
|
||||
let Origin::Source(ft) = declaration.origin else {
|
||||
ice!(c, t, "Function location but external origin?");
|
||||
};
|
||||
let key = FunctionKey { tree: ft };
|
||||
let index = match c.function_bindings.get(&key) {
|
||||
Some(index) => *index,
|
||||
None => {
|
||||
let tree = &c.syntax[ft];
|
||||
compiler_assert_eq!(c, t, tree.kind, TreeKind::FunctionDecl);
|
||||
|
||||
compile_function_declaration(c, ft, tree, false)?;
|
||||
|
||||
match c.function_bindings.get(&key) {
|
||||
Some(index) => *index,
|
||||
None => {
|
||||
ice!(
|
||||
c,
|
||||
t,
|
||||
"did not compile the function with key {:?}!",
|
||||
declaration
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Instruction::LoadFunction(index)
|
||||
}
|
||||
Location::Function => Instruction::LoadFunction(index),
|
||||
|
||||
Location::ExternalFunction => Instruction::LoadExternFunction(index),
|
||||
|
||||
// Must be a static don't worry about it.
|
||||
Location::Class => return OK,
|
||||
|
||||
// fix later
|
||||
Location::Import => ice!(c, t, "import compile not supported"),
|
||||
// Imports are handled with an instruction prefix.
|
||||
Location::Import => return OK,
|
||||
};
|
||||
|
||||
c.push(instruction);
|
||||
|
|
@ -988,24 +977,7 @@ fn compile_new_object_expression(c: &mut Compiler, t: TreeRef, tree: &Tree) -> C
|
|||
let declaration = environment.bind(identifier).ok_or("cannot bind type")?;
|
||||
match declaration.location {
|
||||
Location::Class => {
|
||||
let Origin::Source(classdecl) = declaration.origin else {
|
||||
ice!(c, t, "this class declaration has no source?");
|
||||
};
|
||||
let key = FunctionKey { tree: classdecl };
|
||||
let index = match c.function_bindings.get(&key) {
|
||||
Some(index) => *index,
|
||||
None => {
|
||||
let tree = &c.syntax[classdecl];
|
||||
compiler_assert_eq!(c, t, tree.kind, TreeKind::ClassDecl);
|
||||
|
||||
compile_class_declaration(c, t, tree, false)?;
|
||||
|
||||
*c.function_bindings
|
||||
.get(&key)
|
||||
.expect("did not compile the class constructor!")
|
||||
}
|
||||
};
|
||||
c.push(Instruction::LoadFunction(index));
|
||||
c.push(Instruction::LoadFunction(declaration.index));
|
||||
}
|
||||
_ => return Err("unsupported type for construction"),
|
||||
}
|
||||
|
|
@ -1039,20 +1011,7 @@ fn compile_member_access(c: &mut Compiler, t: TreeRef, tree: &Tree) -> CR {
|
|||
let typ = c.semantics.type_of(lhs);
|
||||
let ident = tree.nth_token(2).ok_or("no ident")?.as_str(&c.source);
|
||||
|
||||
let environment = match &typ {
|
||||
Type::Object(mid, ct, _) => {
|
||||
let class = c.semantics.class_of(*mid, *ct);
|
||||
class.env.clone()
|
||||
}
|
||||
Type::Class(mid, ct, _) => {
|
||||
let class = c.semantics.class_of(*mid, *ct);
|
||||
class.static_env.clone()
|
||||
}
|
||||
_ => {
|
||||
c.push_panic("cannot get environment of {typ}");
|
||||
return Err("cannot get environment");
|
||||
}
|
||||
};
|
||||
let environment = c.semantics.member_environment(t, &typ);
|
||||
let declaration = environment.bind(ident).ok_or("cannot bind")?;
|
||||
|
||||
// NOTE: If this is a method call we still don't have to do anything
|
||||
|
|
@ -1092,10 +1051,10 @@ fn compile_statement(c: &mut Compiler, t: TreeRef, gen_value: bool) {
|
|||
|
||||
TreeKind::Import => compile_import_statement(c, gen_value),
|
||||
TreeKind::Block => compile_block_statement(c, t, gen_value),
|
||||
TreeKind::ClassDecl => compile_class_declaration(c, t, tree, gen_value),
|
||||
TreeKind::ClassDecl => compile_class_declaration(c, gen_value),
|
||||
TreeKind::ExpressionStatement => compile_expression_statement(c, tree, gen_value),
|
||||
TreeKind::ForStatement => compile_for_statement(c, tree, gen_value),
|
||||
TreeKind::FunctionDecl => compile_function_declaration(c, t, tree, gen_value),
|
||||
TreeKind::FunctionDecl => compile_function_declaration(c, gen_value),
|
||||
TreeKind::IfStatement => compile_if_statement(c, tree, gen_value),
|
||||
TreeKind::LetStatement => compile_let_statement(c, t, tree, gen_value),
|
||||
TreeKind::ReturnStatement => compile_return_statement(c, tree),
|
||||
|
|
@ -1176,32 +1135,7 @@ fn compile_let_statement(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_value: b
|
|||
OK
|
||||
}
|
||||
|
||||
fn compile_function_declaration(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_value: bool) -> CR {
|
||||
// Only compile a given function once.
|
||||
//
|
||||
// TODO: When it's time for generics, this should only actually compile
|
||||
// if we have no unbound type variables.
|
||||
let fk = FunctionKey { tree: t };
|
||||
if !c.function_bindings.contains_key(&fk) {
|
||||
// TODO: If this is a method the name should be different.
|
||||
let name = tree.nth_token(1).ok_or("no id")?.as_str(&c.source);
|
||||
|
||||
let param_list = tree
|
||||
.child_tree_of_kind(&c.syntax, TreeKind::ParamList)
|
||||
.ok_or("no paramlist")?;
|
||||
let param_count = param_list.children.len() - 2;
|
||||
|
||||
let function_index = c.temp_functions.len();
|
||||
c.temp_functions.push(None);
|
||||
|
||||
c.pending_functions
|
||||
.push((fk.clone(), function_index, Function::new(name, param_count)));
|
||||
c.function_bindings.insert(fk, function_index);
|
||||
c.module
|
||||
.exports
|
||||
.insert(name.to_string(), Export::Function(function_index));
|
||||
}
|
||||
|
||||
fn compile_function_declaration(c: &mut Compiler, gen_value: bool) -> CR {
|
||||
if gen_value {
|
||||
c.push(Instruction::PushNothing);
|
||||
}
|
||||
|
|
@ -1209,26 +1143,7 @@ fn compile_function_declaration(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_v
|
|||
OK
|
||||
}
|
||||
|
||||
fn compile_class_declaration(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_value: bool) -> CR {
|
||||
// Only compile a given function once.
|
||||
// Classes get compiled as constructor functions which get called.
|
||||
let fk = FunctionKey { tree: t };
|
||||
if !c.function_bindings.contains_key(&fk) {
|
||||
let name = tree.nth_token(1).ok_or("no name")?.as_str(&c.source);
|
||||
|
||||
let field_count = tree.children.len() - 2;
|
||||
|
||||
let function_index = c.temp_functions.len();
|
||||
c.temp_functions.push(None);
|
||||
|
||||
c.pending_functions
|
||||
.push((fk.clone(), function_index, Function::new(name, field_count)));
|
||||
c.function_bindings.insert(fk, function_index);
|
||||
c.module
|
||||
.exports
|
||||
.insert(name.to_string(), Export::Function(function_index));
|
||||
}
|
||||
|
||||
fn compile_class_declaration(c: &mut Compiler, gen_value: bool) -> CR {
|
||||
if gen_value {
|
||||
c.push(Instruction::PushNothing);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue