[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:
John Doty 2024-02-12 22:49:34 -08:00
parent 2093502031
commit 994268abb6
6 changed files with 224 additions and 90 deletions

View file

@ -1,8 +1,8 @@
use fine::compiler::{compile, Function, Module};
use fine::parser::SyntaxTree;
use fine::semantics::{check, Error, Semantics, Type};
use fine::tokens::Lines;
use fine::compiler::{Function, Module};
use fine::semantics::{Error, Type};
use fine::vm::{eval_export_fn, Context};
use fine::{ModuleLoadError, ModuleLoader, ModuleSource, Runtime, StandardModuleLoader};
use pretty_assertions::assert_eq;
use std::fmt::Write as _;
use std::rc::Rc;
@ -84,7 +84,8 @@ fn should_rebase() -> bool {
}
}
fn assert_concrete(source: Rc<str>, tree: Rc<SyntaxTree>, expected: &str, source_path: &str) {
fn assert_concrete(source: Rc<str>, expected: &str, source_path: &str) {
let (tree, _) = fine::parser::parse(&source);
let dump = tree.dump(&source, false);
if dump != expected {
if should_rebase() {
@ -128,15 +129,46 @@ macro_rules! semantic_assert_eq {
}};
}
fn assert_type_at(
struct TestLoader {
source: Rc<str>,
tree: Rc<SyntaxTree>,
lines: Rc<Lines>,
pos: usize,
expected: &str,
_source_path: &str,
) {
let semantics = Semantics::new(source, tree.clone(), lines);
base: StandardModuleLoader,
}
impl TestLoader {
fn new(source: Rc<str>) -> Box<Self> {
Box::new(TestLoader {
source,
base: StandardModuleLoader {},
})
}
}
impl ModuleLoader for TestLoader {
fn normalize_module_name(&self, name: String) -> String {
if name == "__test__" {
name
} else {
self.base.normalize_module_name(name)
}
}
fn load_module(&self, name: &String) -> Result<ModuleSource, ModuleLoadError> {
if name == "__test__" {
Ok(ModuleSource::SourceText(self.source.to_string()))
} else {
self.base.load_module(name)
}
}
}
fn test_runtime(source: Rc<str>) -> Runtime {
Runtime::new(TestLoader::new(source))
}
fn assert_type_at(module: Rc<fine::Module>, pos: usize, expected: &str, _source_path: &str) {
let semantics = module.semantics();
let tree = semantics.tree();
let tree_ref = match tree.find_tree_at(pos) {
Some(t) => t,
None => semantic_panic!(
@ -158,14 +190,15 @@ fn assert_type_at(
}
fn assert_type_error_at(
source: Rc<str>,
tree: Rc<SyntaxTree>,
lines: Rc<Lines>,
module: Rc<fine::Module>,
errors: &[Error],
pos: usize,
expected: &str,
_source_path: &str,
) {
let semantics = Semantics::new(source, tree.clone(), lines);
let semantics = module.semantics();
let tree = semantics.tree();
let tree_ref = match tree.find_tree_at(pos) {
Some(t) => t,
None => semantic_panic!(
@ -184,7 +217,6 @@ fn assert_type_error_at(
tree[tree_ref].kind
);
let errors = semantics.snapshot_errors();
semantic_assert!(
&semantics,
Some(tree_ref),
@ -225,15 +257,9 @@ fn dump_module(out: &mut String, module: &Module) -> std::fmt::Result {
Ok(())
}
fn assert_compiles_to(
source: Rc<str>,
tree: Rc<SyntaxTree>,
lines: Rc<Lines>,
expected: &str,
source_path: &str,
) {
let semantics = Rc::new(Semantics::new(source, tree, lines));
let module = compile(semantics.clone());
fn assert_compiles_to(module: Rc<fine::Module>, expected: &str, source_path: &str) {
let semantics = module.semantics();
let module = module.compiled();
let mut actual = String::new();
dump_module(&mut actual, &module).expect("no dumping?");
@ -253,12 +279,10 @@ fn assert_compiles_to(
}
}
fn assert_no_errors(source: Rc<str>, tree: Rc<SyntaxTree>, lines: Rc<Lines>) {
let semantics = Semantics::new(source, tree, lines);
check(&semantics);
fn assert_no_errors(module: Rc<fine::Module>, errors: &[Error]) {
let semantics = module.semantics();
let expected_errors: Vec<Error> = Vec::new();
let errors = semantics.snapshot_errors();
let expected_errors: &[Error] = &[];
semantic_assert_eq!(
&semantics,
None,
@ -268,10 +292,10 @@ fn assert_no_errors(source: Rc<str>, tree: Rc<SyntaxTree>, lines: Rc<Lines>) {
);
}
fn assert_eval_ok(source: Rc<str>, tree: Rc<SyntaxTree>, lines: Rc<Lines>, expected: &str) {
let semantics = Rc::new(Semantics::new(source, tree, lines));
fn assert_eval_ok(module: Rc<fine::Module>, expected: &str) {
let semantics = module.semantics();
let module = module.compiled();
let module = compile(semantics.clone());
let mut context = Context::new(module.clone());
context.init().expect("Unable to initialize module");
@ -310,20 +334,10 @@ fn assert_eval_ok(source: Rc<str>, tree: Rc<SyntaxTree>, lines: Rc<Lines>, expec
}
}
fn assert_errors(
source: Rc<str>,
tree: Rc<SyntaxTree>,
lines: Rc<Lines>,
expected_errors: Vec<&str>,
) {
let semantics = Semantics::new(source, tree, lines);
check(&semantics);
fn assert_errors(module: Rc<fine::Module>, errors: &[Error], expected_errors: Vec<&str>) {
let semantics = module.semantics();
let errors: Vec<String> = semantics
.snapshot_errors()
.iter()
.map(|e| format!("{}", e))
.collect();
let errors: Vec<String> = errors.iter().map(|e| format!("{}", e)).collect();
semantic_assert_eq!(
&semantics,
@ -334,11 +348,9 @@ fn assert_errors(
);
}
fn assert_check_error(source: Rc<str>, tree: Rc<SyntaxTree>, lines: Rc<Lines>, expected: &str) {
let semantics = Semantics::new(source, tree, lines);
check(&semantics);
fn assert_check_error(module: Rc<fine::Module>, errors: &[Error], expected: &str) {
let semantics = module.semantics();
let errors = semantics.snapshot_errors();
semantic_assert!(
&semantics,
None,

View file

@ -141,5 +141,5 @@ fun test() -> f64 {
// like the above.
}
/// @ignore WIP
// @ignore never finished compiling `match`
// @no-errors