From 7a8075bd221c17ae24fabc0462659b3bedbb7be5 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Wed, 7 Jan 2026 23:15:53 -0500 Subject: [PATCH] make imports actually work --- hylo-lang/examples/fib.hylo | 3 +++ hylo-lang/examples/put2.hylo | 1 + hylo-lang/hyloc/src/ir.rs | 20 ++++++++++++++------ hylo-lang/hyloc/src/parser.rs | 14 ++++++++------ hylo-lang/hyloc/src/tokenizer.rs | 2 +- 5 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 hylo-lang/examples/put2.hylo diff --git a/hylo-lang/examples/fib.hylo b/hylo-lang/examples/fib.hylo index e59b34e..9057c6d 100644 --- a/hylo-lang/examples/fib.hylo +++ b/hylo-lang/examples/fib.hylo @@ -1,3 +1,4 @@ +import "examples/put2.hylo" : fib dup 1 > if @@ -18,3 +19,5 @@ 8 fib putn 9 fib putn 10 fib putn + +5 fib 6 fib put2 diff --git a/hylo-lang/examples/put2.hylo b/hylo-lang/examples/put2.hylo new file mode 100644 index 0000000..440f50f --- /dev/null +++ b/hylo-lang/examples/put2.hylo @@ -0,0 +1 @@ +: put2 putn putn ; diff --git a/hylo-lang/hyloc/src/ir.rs b/hylo-lang/hyloc/src/ir.rs index e3d23e7..db95898 100644 --- a/hylo-lang/hyloc/src/ir.rs +++ b/hylo-lang/hyloc/src/ir.rs @@ -9,7 +9,7 @@ macro_rules! push_num { ($num:ident) => { IR::StackPush(*$num as u64) } } -fn import(specifier: &str, imported: &mut HashSet) -> Option { +fn import(specifier: &str, imported: &mut HashSet, is_entrypoint: bool) -> Option { // TODO paths relative to the files, not just the invocation let path = PathBuf::from(specifier).canonicalize().unwrap(); if imported.contains(&path) { @@ -18,7 +18,7 @@ fn import(specifier: &str, imported: &mut HashSet) -> Option IRModule { @@ -44,7 +44,7 @@ fn collapse_module(mut module_w: ModuleWithImports) -> IRModule { prev_data_len += mod_data_len; }); - + IRModule { data, text, @@ -53,10 +53,11 @@ fn collapse_module(mut module_w: ModuleWithImports) -> IRModule { pub fn compile(path: &str) -> IRModule { let mut imported = HashSet::new(); - let module = import(path, &mut imported).unwrap(); + let module = import(path, &mut imported, true).unwrap(); collapse_module(module) // TODO remove unused words } +#[derive(Debug)] struct ModuleWithImports { module: Option, imports: Option> @@ -99,7 +100,7 @@ fn generate_internal(module: &Module, imported: &mut HashSet) -> Module }, Token::String(text) => { if last_was_import { - if let Some(module) = import(text, imported) { + if let Some(module) = import(text, imported, false) { imports.push(module); } IR::ImportString // This will be elided later @@ -124,6 +125,13 @@ fn generate_internal(module: &Module, imported: &mut HashSet) -> Module _ => false, }; mapped_ir + }).filter(|ir| { + // Elide IRs that shouldn't actually be in the output IR. + match ir { + IR::Import => false, + IR::ImportString => false, + _ => true, + } }).collect::>(); let mut result = vec![IR::Label(def.name.to_string())]; @@ -138,6 +146,6 @@ fn generate_internal(module: &Module, imported: &mut HashSet) -> Module text: text.into_iter().flatten().collect::>(), data, }), - imports: Some(Vec::new()) // TODO + imports: Some(imports) } } diff --git a/hylo-lang/hyloc/src/parser.rs b/hylo-lang/hyloc/src/parser.rs index e678e14..040a03f 100644 --- a/hylo-lang/hyloc/src/parser.rs +++ b/hylo-lang/hyloc/src/parser.rs @@ -12,7 +12,7 @@ pub struct Module<'a> { } impl<'a> Module<'a> { - pub fn parse(input: Vec>) -> Self { + pub fn parse(input: Vec>, is_entrypoint: bool) -> Self { let mut result = vec![]; let mut main = vec![]; let mut current_word: Option = None; @@ -55,10 +55,12 @@ impl<'a> Module<'a> { panic!("unfinished word definition!"); } - result.push(WordDefinition { - name: "main", - instructions: main, - }); + if is_entrypoint { + result.push(WordDefinition { + name: "main", + instructions: main, + }); + } Module { words: result } } @@ -87,7 +89,7 @@ mod tests { : soup chicken 4.5 hello ; hello soup -")); +"), true); result.debug_print(); } } diff --git a/hylo-lang/hyloc/src/tokenizer.rs b/hylo-lang/hyloc/src/tokenizer.rs index b2e79b0..dbc764a 100644 --- a/hylo-lang/hyloc/src/tokenizer.rs +++ b/hylo-lang/hyloc/src/tokenizer.rs @@ -61,7 +61,7 @@ pub fn tokenize<'a>(input: &'a str) -> Vec> { let mut string_start: Option = None; let mut word_or_num_start: Option = None; let mut last_is_escape = false; - let mut last_is_whitespace = false; + let mut last_is_whitespace = true; let mut in_comment = false; let mut index = 0; let mut first_char = true; -- 2.43.0