[fine] Lifetime garbage, big refactor

So it turns out that I can't hold `&str` in token because it makes it
impossible to encapsulate a source file in the larger context- self
referential structure problems again. Everything gets rebuilt so that
the source can be passed through. While we're at it, more things
become Rc<> because, man..... life it too short.

Semantics in particular has become a giant hub of the module state: we
can basically just hold an Rc<Semantics> and have everything we could
possibly want to know about a source file, computed lazily if
necessary.
This commit is contained in:
John Doty 2024-02-11 09:31:51 -08:00
parent d5059dd450
commit 2dbdbb3957
7 changed files with 502 additions and 329 deletions

View file

@ -1,4 +1,4 @@
use std::fs;
use std::{fs, rc::Rc};
use compiler::compile;
use parser::parse;
@ -11,6 +11,21 @@ pub mod semantics;
pub mod tokens;
pub mod vm;
// struct SourceModule {
// semantics: Rc<Semantics>,
// }
// impl SourceModule {
// pub fn new(source: &str) -> Self {
// let source: Rc<str> = source.into();
// let (syntax, lines) = parse(&source);
// let semantics = Rc::new(Semantics::new(source, syntax, lines));
// SourceModule { semantics }
// }
// }
// struct Environment {}
pub fn process_file(file: &str) {
let source = match fs::read_to_string(file) {
Ok(c) => c,
@ -21,21 +36,21 @@ pub fn process_file(file: &str) {
};
// What am I doing here?
let source: Rc<str> = source.into();
let (tree, lines) = parse(&source);
let semantics = Semantics::new(&tree, &lines);
let semantics = Rc::new(Semantics::new(source, tree, lines));
check(&semantics);
// OK now there might be errors.
let mut errors = semantics.snapshot_errors();
let errors = semantics.snapshot_errors();
if errors.len() > 0 {
errors.reverse();
for e in errors {
eprintln!("{file}: {}:{}: {}", e.start.0, e.start.1, e.message);
}
return;
}
let module = compile(&semantics);
let module = compile(semantics);
let main_function = module.functions[module.init].clone();
let mut context = Context::new(module.clone());