From 5f57924f23fe28111882783440bd653002209a20 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 11 Jul 2024 02:50:15 -0500 Subject: [PATCH] cl-repl: Perform module inlining before submitting code to the interpreter --- compiler/cl-repl/src/cli.rs | 14 ++++++++++++++ compiler/cl-repl/src/menu.rs | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/compiler/cl-repl/src/cli.rs b/compiler/cl-repl/src/cli.rs index 809547e..20233d4 100644 --- a/compiler/cl-repl/src/cli.rs +++ b/compiler/cl-repl/src/cli.rs @@ -46,8 +46,22 @@ pub fn run(args: Args) -> Result<(), Box> { } fn load_file(env: &mut Environment, path: impl AsRef) -> Result> { + let inliner = + cl_parser::inliner::ModuleInliner::new(path.as_ref().parent().unwrap_or(Path::new(""))); let file = std::fs::read_to_string(path)?; let code = Parser::new(Lexer::new(&file)).file()?; + let code = match inliner.inline(code) { + Ok(a) => a, + Err((code, io_errs, parse_errs)) => { + for (file, err) in io_errs { + eprintln!("{}:{err}", file.display()); + } + for (file, err) in parse_errs { + eprintln!("{}:{err}", file.display()); + } + code + } + }; Ok(env.eval(&code)?) } diff --git a/compiler/cl-repl/src/menu.rs b/compiler/cl-repl/src/menu.rs index ba5f993..bb47079 100644 --- a/compiler/cl-repl/src/menu.rs +++ b/compiler/cl-repl/src/menu.rs @@ -38,8 +38,12 @@ pub fn main_menu(ctx: &mut ctx::Context) -> ReplResult<()> { } pub fn run(ctx: &mut ctx::Context) -> ReplResult<()> { + use cl_ast::ast_visitor::Fold; + use cl_parser::inliner::ModuleInliner; + read_and(ansi::CYAN, "cl>", " ?>", |line| { let code = Parser::new(Lexer::new(line)).stmt()?; + let code = ModuleInliner::new(".").fold_stmt(code); print!("{}", ansi::OUTPUT); match ctx.run(&code) {