[fine] Many more import-related shuffles

This commit is contained in:
John Doty 2024-02-15 06:12:18 -08:00
parent 994268abb6
commit a3ae4339cf
8 changed files with 159 additions and 67 deletions

View file

@ -1,10 +1,11 @@
use fine::compiler::{Function, Module};
use fine::compiler::{compile, 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::path::PathBuf;
use std::rc::Rc;
fn rebase_section(source_path: &str, section: &str, value: &str) {
@ -135,20 +136,26 @@ struct TestLoader {
}
impl TestLoader {
fn new(source: Rc<str>) -> Box<Self> {
fn new(base_path: PathBuf, source: Rc<str>) -> Box<Self> {
let base_path = base_path
.parent()
.map(|p| p.to_owned())
.unwrap_or(base_path);
Box::new(TestLoader {
source,
base: StandardModuleLoader {},
base: StandardModuleLoader::new(base_path),
})
}
}
impl ModuleLoader for TestLoader {
fn normalize_module_name(&self, name: String) -> String {
fn normalize_module_name(&self, base: &str, name: String) -> String {
if name == "__test__" {
name
} else {
self.base.normalize_module_name(name)
let base = if base == "__test__" { "" } else { base };
self.base.normalize_module_name(base, name)
}
}
@ -161,8 +168,8 @@ impl ModuleLoader for TestLoader {
}
}
fn test_runtime(source: Rc<str>) -> Runtime {
Runtime::new(TestLoader::new(source))
fn test_runtime(_source_path: &str, source: Rc<str>) -> Runtime {
Runtime::new(TestLoader::new(_source_path.into(), source))
}
fn assert_type_at(module: Rc<fine::Module>, pos: usize, expected: &str, _source_path: &str) {
@ -237,7 +244,7 @@ fn dump_function(out: &mut String, function: &Function) -> std::fmt::Result {
let strings = function.strings();
writeln!(out, " strings ({}):", strings.len())?;
for (i, s) in strings.iter().enumerate() {
writeln!(out, " {}: {}", i, s)?; // TODO: ESCAPE
writeln!(out, " {}: \"{}\"", i, s)?; // TODO: ESCAPE
}
let code = function.instructions();
@ -259,7 +266,7 @@ fn dump_module(out: &mut String, module: &Module) -> std::fmt::Result {
fn assert_compiles_to(module: Rc<fine::Module>, expected: &str, source_path: &str) {
let semantics = module.semantics();
let module = module.compiled();
let module = compile(&semantics);
let mut actual = String::new();
dump_module(&mut actual, &module).expect("no dumping?");
@ -294,7 +301,7 @@ fn assert_no_errors(module: Rc<fine::Module>, errors: &[Error]) {
fn assert_eval_ok(module: Rc<fine::Module>, expected: &str) {
let semantics = module.semantics();
let module = module.compiled();
let module = compile(&semantics);
let mut context = Context::new(module.clone());
context.init().expect("Unable to initialize module");

View file

@ -37,7 +37,7 @@ fun test() -> f64 {
// | 1: Return
// | function Point (6 args, 0 locals):
// | strings (1):
// | 0: Point
// | 0: "Point"
// | code (6):
// | 0: LoadArgument(1)
// | 1: LoadArgument(0)
@ -47,7 +47,7 @@ fun test() -> f64 {
// | 5: Return
// | function Line (4 args, 0 locals):
// | strings (1):
// | 0: Line
// | 0: "Line"
// | code (6):
// | 0: LoadArgument(1)
// | 1: LoadArgument(0)

View file

@ -1,3 +1,3 @@
export fun hello() -> string {
"hello"
}
}

View file

@ -1,4 +1,4 @@
import "./foo" as foo;
import "./foo.fine" as foo;
fun test() -> string {
foo.hello() + " world"