[fine] Oh no a runtime and module loading and stuff
Lots of test work to use the new mechanism. I'm not sure I like it.
This commit is contained in:
parent
2093502031
commit
994268abb6
6 changed files with 224 additions and 90 deletions
|
|
@ -237,7 +237,7 @@ macro_rules! inst_panic {
|
|||
// ($compiler:expr, $tr:expr, $($t:tt)*) => {{}};
|
||||
// }
|
||||
|
||||
pub fn compile(semantics: Rc<Semantics>) -> Rc<Module> {
|
||||
pub fn compile(semantics: &Semantics) -> Rc<Module> {
|
||||
let source = semantics.source();
|
||||
let syntax_tree = semantics.tree();
|
||||
|
||||
|
|
@ -998,6 +998,8 @@ fn compile_list_constructor_element(c: &mut Compiler, tree: &Tree) -> CR {
|
|||
fn compile_statement(c: &mut Compiler, t: TreeRef, gen_value: bool) {
|
||||
let tree = &c.semantics.tree()[t];
|
||||
let cr = match tree.kind {
|
||||
TreeKind::Error => None,
|
||||
|
||||
TreeKind::Block => compile_block_statement(c, t, gen_value),
|
||||
TreeKind::ClassDecl => compile_class_declaration(c, t, tree, gen_value),
|
||||
TreeKind::ExpressionStatement => compile_expression_statement(c, tree, gen_value),
|
||||
|
|
|
|||
154
fine/src/lib.rs
154
fine/src/lib.rs
|
|
@ -1,8 +1,8 @@
|
|||
use std::{fs, rc::Rc};
|
||||
use std::{collections::HashMap, fs, rc::Rc};
|
||||
|
||||
use compiler::compile;
|
||||
use parser::parse;
|
||||
use semantics::{check, Semantics};
|
||||
use semantics::{check, Error, Semantics};
|
||||
use vm::{eval, Context};
|
||||
|
||||
pub mod compiler;
|
||||
|
|
@ -11,38 +11,139 @@ pub mod semantics;
|
|||
pub mod tokens;
|
||||
pub mod vm;
|
||||
|
||||
// struct SourceModule {
|
||||
// semantics: Rc<Semantics>,
|
||||
// }
|
||||
pub enum ModuleSource {
|
||||
SourceText(String),
|
||||
}
|
||||
|
||||
// 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 }
|
||||
// }
|
||||
// }
|
||||
#[derive(Debug)]
|
||||
pub enum ModuleLoadError {
|
||||
IO(std::io::Error),
|
||||
}
|
||||
|
||||
// struct Environment {}
|
||||
pub trait ModuleLoader {
|
||||
fn normalize_module_name(&self, name: String) -> String;
|
||||
fn load_module(&self, name: &String) -> Result<ModuleSource, ModuleLoadError>;
|
||||
}
|
||||
|
||||
pub struct StandardModuleLoader {}
|
||||
|
||||
impl ModuleLoader for StandardModuleLoader {
|
||||
fn normalize_module_name(&self, name: String) -> String {
|
||||
match std::fs::canonicalize(&name) {
|
||||
Ok(p) => match p.into_os_string().into_string() {
|
||||
Ok(s) => s,
|
||||
Err(_) => name,
|
||||
},
|
||||
Err(_) => name,
|
||||
}
|
||||
}
|
||||
|
||||
fn load_module(&self, name: &String) -> Result<ModuleSource, ModuleLoadError> {
|
||||
match fs::read_to_string(name) {
|
||||
Ok(c) => Ok(ModuleSource::SourceText(c)),
|
||||
Err(e) => Err(ModuleLoadError::IO(e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Module {
|
||||
module: Rc<compiler::Module>,
|
||||
semantics: Rc<Semantics>,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
pub fn semantics(&self) -> Rc<Semantics> {
|
||||
self.semantics.clone()
|
||||
}
|
||||
|
||||
pub fn compiled(&self) -> Rc<compiler::Module> {
|
||||
self.module.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Runtime {
|
||||
modules: HashMap<String, Rc<Module>>,
|
||||
loader: Box<dyn ModuleLoader>,
|
||||
}
|
||||
|
||||
impl Runtime {
|
||||
pub fn new(loader: Box<dyn ModuleLoader>) -> Self {
|
||||
Runtime {
|
||||
modules: HashMap::new(),
|
||||
loader,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_module(&mut self, name: &str) -> Result<(Vec<Error>, Rc<Module>), ModuleLoadError> {
|
||||
let mut init_pending = HashMap::new();
|
||||
let mut names = Vec::new();
|
||||
let name = self.loader.normalize_module_name(name.to_string());
|
||||
names.push(name.clone());
|
||||
|
||||
while let Some(name) = names.pop() {
|
||||
if self.modules.contains_key(&name) {
|
||||
continue;
|
||||
}
|
||||
if !init_pending.contains_key(&name) {
|
||||
let loaded = self.loader.load_module(&name)?;
|
||||
match loaded {
|
||||
ModuleSource::SourceText(source) => {
|
||||
let source: Rc<str> = source.into();
|
||||
let (tree, lines) = parse(&source);
|
||||
let semantics = Rc::new(Semantics::new(source, tree, lines));
|
||||
|
||||
let mut normalized = Vec::new();
|
||||
for import in semantics.imports() {
|
||||
let import = self.loader.normalize_module_name(import);
|
||||
names.push(import.clone());
|
||||
normalized.push(import);
|
||||
}
|
||||
|
||||
init_pending.insert(name, (normalized, semantics));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (_, (imports, semantics)) in init_pending.iter() {
|
||||
let mut import_table = HashMap::new();
|
||||
for import in imports.iter() {
|
||||
let target = if let Some(module) = self.modules.get(&*import) {
|
||||
Rc::downgrade(&module.semantics)
|
||||
} else {
|
||||
Rc::downgrade(&init_pending.get(&*import).unwrap().1)
|
||||
};
|
||||
import_table.insert(import.clone(), target);
|
||||
}
|
||||
semantics.set_imports(import_table);
|
||||
}
|
||||
|
||||
let mut errors = Vec::new();
|
||||
for (name, (_, semantics)) in init_pending.into_iter() {
|
||||
check(&semantics);
|
||||
errors.append(&mut semantics.snapshot_errors());
|
||||
let module = compile(&semantics);
|
||||
self.modules
|
||||
.insert(name, Rc::new(Module { semantics, module }));
|
||||
}
|
||||
|
||||
let result = self.modules.get(&name).unwrap().clone();
|
||||
Ok((errors, result))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_file(file: &str) {
|
||||
let source = match fs::read_to_string(file) {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
eprintln!("Unable to read file {file}: {e}");
|
||||
let mut runtime = Runtime::new(Box::new(StandardModuleLoader {}));
|
||||
|
||||
let (errors, module) = match runtime.load_module(file) {
|
||||
Ok(r) => r,
|
||||
Err(_) => {
|
||||
eprintln!("Error loading module");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// What am I doing here?
|
||||
let source: Rc<str> = source.into();
|
||||
let (tree, lines) = parse(&source);
|
||||
let semantics = Rc::new(Semantics::new(source, tree, lines));
|
||||
check(&semantics);
|
||||
|
||||
// OK now there might be errors.
|
||||
let errors = semantics.snapshot_errors();
|
||||
if errors.len() > 0 {
|
||||
for e in errors {
|
||||
eprintln!("{file}: {}:{}: {}", e.start.0, e.start.1, e.message);
|
||||
|
|
@ -50,7 +151,8 @@ pub fn process_file(file: &str) {
|
|||
return;
|
||||
}
|
||||
|
||||
let module = compile(semantics);
|
||||
// shrug
|
||||
let module = module.module.clone();
|
||||
let main_function = module.functions[module.init].clone();
|
||||
|
||||
let mut context = Context::new(module.clone());
|
||||
|
|
|
|||
|
|
@ -3,7 +3,12 @@ use crate::{
|
|||
tokens::{Lines, Token, TokenKind},
|
||||
vm::StackValue,
|
||||
};
|
||||
use std::{cell::RefCell, collections::HashMap, fmt, rc::Rc};
|
||||
use std::{
|
||||
cell::{OnceCell, RefCell},
|
||||
collections::HashMap,
|
||||
fmt,
|
||||
rc::{Rc, Weak},
|
||||
};
|
||||
|
||||
// TODO: Unused variables?
|
||||
// TODO: Underscore for discard?
|
||||
|
|
@ -610,6 +615,8 @@ pub struct Semantics {
|
|||
syntax_tree: Rc<SyntaxTree>,
|
||||
lines: Rc<Lines>,
|
||||
|
||||
import_map: OnceCell<HashMap<String, Weak<Semantics>>>,
|
||||
|
||||
// Instead of physical parents, this is the set of *logical* parents.
|
||||
// This is what is used for binding.
|
||||
logical_parents: Vec<Option<TreeRef>>,
|
||||
|
|
@ -635,6 +642,7 @@ impl Semantics {
|
|||
source,
|
||||
syntax_tree: tree.clone(),
|
||||
lines,
|
||||
import_map: OnceCell::new(),
|
||||
logical_parents,
|
||||
errors: RefCell::new(vec![]),
|
||||
types: RefCell::new(vec![Incremental::None; tree.len()]),
|
||||
|
|
@ -653,6 +661,10 @@ impl Semantics {
|
|||
semantics
|
||||
}
|
||||
|
||||
pub fn set_imports(&self, imports: HashMap<String, Weak<Semantics>>) {
|
||||
self.import_map.set(imports).expect("imports already set");
|
||||
}
|
||||
|
||||
pub fn source(&self) -> Rc<str> {
|
||||
self.source.clone()
|
||||
}
|
||||
|
|
@ -665,6 +677,10 @@ impl Semantics {
|
|||
self.lines.clone()
|
||||
}
|
||||
|
||||
pub fn imports(&self) -> Vec<String> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
pub fn snapshot_errors(&self) -> Vec<Error> {
|
||||
let mut result = (*self.errors.borrow()).clone();
|
||||
result.sort_by(|a, b| match a.start.0.cmp(&b.start.0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue