cl-repl: Perform module inlining before submitting code to the interpreter

This commit is contained in:
John 2024-07-11 02:50:15 -05:00
parent d692f6bb80
commit 5f57924f23
2 changed files with 18 additions and 0 deletions

View File

@ -46,8 +46,22 @@ pub fn run(args: Args) -> Result<(), Box<dyn Error>> {
} }
fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue, Box<dyn Error>> { fn load_file(env: &mut Environment, path: impl AsRef<Path>) -> Result<ConValue, Box<dyn Error>> {
let inliner =
cl_parser::inliner::ModuleInliner::new(path.as_ref().parent().unwrap_or(Path::new("")));
let file = std::fs::read_to_string(path)?; let file = std::fs::read_to_string(path)?;
let code = Parser::new(Lexer::new(&file)).file()?; 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)?) Ok(env.eval(&code)?)
} }

View File

@ -38,8 +38,12 @@ pub fn main_menu(ctx: &mut ctx::Context) -> ReplResult<()> {
} }
pub fn run(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| { read_and(ansi::CYAN, "cl>", " ?>", |line| {
let code = Parser::new(Lexer::new(line)).stmt()?; let code = Parser::new(Lexer::new(line)).stmt()?;
let code = ModuleInliner::new(".").fold_stmt(code);
print!("{}", ansi::OUTPUT); print!("{}", ansi::OUTPUT);
match ctx.run(&code) { match ctx.run(&code) {