cl-repl: Perform module inlining before submitting code to the interpreter
This commit is contained in:
parent
d692f6bb80
commit
5f57924f23
@ -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)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user