Add prelude and while macro

This commit introduces a new prelude file that defines the `while` macro
and registers it during environment bootstrapping. The `again` macro has
been updated to accept multiple arguments for its recursive call, and
several examples have been adjusted to reflect this change.
Additionally, the `MacroRegistry` in the compiler is now cloneable and
can be moved out of the `MacroExpander`.
This commit is contained in:
Michael Schimmel
2026-02-28 12:41:19 +01:00
parent 83324a1892
commit 7126668934
8 changed files with 56 additions and 26 deletions
+12 -1
View File
@@ -32,6 +32,7 @@ pub struct Environment {
pub monomorph_cache: Rc<RefCell<MonoCache>>,
pub debug_mode: bool,
pub optimization: bool,
pub macro_registry: Rc<RefCell<MacroRegistry>>,
}
struct EnvFunctionRegistry {
@@ -104,6 +105,7 @@ impl Environment {
monomorph_cache: Rc::new(RefCell::new(HashMap::new())),
debug_mode: false,
optimization: true,
macro_registry: Rc::new(RefCell::new(MacroRegistry::new())),
};
env.register_stdlib();
env
@@ -119,7 +121,15 @@ impl Environment {
global_types: self.global_types.clone(),
global_values: self.global_values.clone(),
};
MacroExpander::new(MacroRegistry::new(), evaluator)
MacroExpander::new(self.macro_registry.borrow().clone(), evaluator)
}
fn eval_prelude(&self, source: &str) {
let mut parser = crate::ast::parser::Parser::new(source).expect("Failed to parse prelude");
let untyped_ast = parser.parse_expression().expect("Failed to parse prelude expression");
let mut expander = self.get_expander();
let _ = expander.expand(untyped_ast).expect("Failed to expand prelude");
*self.macro_registry.borrow_mut() = expander.into_registry();
}
pub fn register_native(
@@ -174,6 +184,7 @@ impl Environment {
fn register_stdlib(&self) {
rtl::register(self);
self.eval_prelude(include_str!("rtl/prelude.myc"));
}
pub fn dump_ast(&self, source: &str) -> Result<String, String> {