From a2ae53f8e412983fdaa1c9c36d8c28230d026fb2 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Fri, 20 Jun 2025 14:43:14 -0400 Subject: [PATCH] wire up stub:w prelude from external file --- assets/test_prelude.ld | 12 +++++------- may_2025_thoughts.md | 8 ++++---- src/compiler.rs | 33 +++++++++++++++++---------------- src/main.rs | 16 ++-------------- 4 files changed, 28 insertions(+), 41 deletions(-) diff --git a/assets/test_prelude.ld b/assets/test_prelude.ld index 195b361..4cd7133 100644 --- a/assets/test_prelude.ld +++ b/assets/test_prelude.ld @@ -1,11 +1,7 @@ -& base :print! ("Hello from Prelude") - -& I can't figure out why I need this, but I do -& Otherwise the validator won't close over `base` -let base = base - fn add (x as :number, y as :number) -> base :add (x, y) +fn inc (n as :number) -> base :add (n, 1) + fn dec (n as :number) -> base :sub (n, 1) fn sub (x as :number, y as :number) -> base :sub (x, y) @@ -14,4 +10,6 @@ fn panics! () -> add ("foo", "bar") fn print!(x) -> base :print! (x) -#{add, dec, sub, print!, panics!} +fn eq? (x, y) -> base :eq? (x, y) + +#{add, inc, dec, sub, print!, panics!, eq?} diff --git a/may_2025_thoughts.md b/may_2025_thoughts.md index e78570a..08432e3 100644 --- a/may_2025_thoughts.md +++ b/may_2025_thoughts.md @@ -305,10 +305,10 @@ So this is my near-term TODO: * [x] update that declaration to a definition after compiling the function... * [x] but BEFORE we close over any upvalues, so the function will be an upvalue for itself - I suspect this can be done not using anything other than an index into a chunk's `constants` vec--no fancy memory swapping or anything; and also--done in the compiler rather than the VM. -* [ ] getting to prelude - - [ ] `base` should load into Prelude - - [ ] write a mock prelude with a few key functions from real prelude - - [ ] a prelude should be loaded into every context +* [x] getting to prelude + - [x] `base` should load into Prelude + - [x] write a mock prelude with a few key functions from real prelude + - [x] a prelude should be loaded into every context - [ ] the full prelude should run properly * [ ] packaging things up - [ ] add a `to_json` method for values diff --git a/src/compiler.rs b/src/compiler.rs index 3e36317..e34b695 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -328,23 +328,24 @@ impl LoopInfo { } fn get_builtin(name: &str, arity: usize) -> Option { - match (name, arity) { - ("type", 1) => Some(Op::TypeOf), - ("eq?", 2) => Some(Op::Eq), - ("add", 2) => Some(Op::Add), - ("sub", 2) => Some(Op::Sub), - ("mult", 2) => Some(Op::Mult), - ("div", 2) => Some(Op::Div), - ("unbox", 1) => Some(Op::Unbox), - ("store!", 2) => Some(Op::BoxStore), - ("assert!", 1) => Some(Op::Assert), - ("get", 2) => Some(Op::Get), - ("at", 2) => Some(Op::At), - ("not", 1) => Some(Op::Not), - ("print!", 1) => Some(Op::Print), + // match (name, arity) { + // ("type", 1) => Some(Op::TypeOf), + // ("eq?", 2) => Some(Op::Eq), + // ("add", 2) => Some(Op::Add), + // ("sub", 2) => Some(Op::Sub), + // ("mult", 2) => Some(Op::Mult), + // ("div", 2) => Some(Op::Div), + // ("unbox", 1) => Some(Op::Unbox), + // ("store!", 2) => Some(Op::BoxStore), + // ("assert!", 1) => Some(Op::Assert), + // ("get", 2) => Some(Op::Get), + // ("at", 2) => Some(Op::At), + // ("not", 1) => Some(Op::Not), + // ("print!", 1) => Some(Op::Print), - _ => None, - } + // _ => None, + // } + None } #[derive(Debug, Clone)] diff --git a/src/main.rs b/src/main.rs index 2642e5c..fd064fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,14 +27,7 @@ use value::Value; mod vm; use vm::Vm; -const PRELUDE: &str = " -fn print! (...args) -> base :print! (args) -fn inc (x) -> base :inc (x) -fn dec (x) -> base :dec (x) -fn eq? (x, y) -> base :eq? (x, y) - -#{print!, inc, dec, eq?} -"; +const PRELUDE: &str = include_str!("../assets/test_prelude.ld"); pub fn prelude() -> HashMap<&'static str, Value> { let tokens = lexer().parse(PRELUDE).into_output_errors().0.unwrap(); @@ -112,12 +105,7 @@ pub fn run(src: &'static str) { pub fn main() { env::set_var("RUST_BACKTRACE", "1"); let src = " -[ - eq? (1, 2) - eq? (:foo, :foo) - inc (3) - dec (0) -] +panics! () "; run(src); }