Skip to content

Commit

Permalink
fix a crash in repl
Browse files Browse the repository at this point in the history
closes #334
  • Loading branch information
kaikalii committed Dec 27, 2023
1 parent 6c9cd6e commit 3a21ed1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion site/src/editor/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ fn run_code_single(code: &str) -> Vec<OutputItem> {
.load_str(code)
.and_then(|comp| rt.run_asm(comp.finish()))
{
Ok(()) => rt.take_stack(),
Ok(_) => rt.take_stack(),
Err(e) => {
error = Some(e);
rt.take_stack()
Expand Down
10 changes: 10 additions & 0 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ impl Compiler {
..Self::default()
}
}
/// Get a reference to the assembly
pub fn assembly(&self) -> &Assembly {
&self.asm
}
/// Get a mutable reference to the assembly
pub fn assembly_mut(&mut self) -> &mut Assembly {
&mut self.asm
}
/// Take a completed assembly from the compiler
pub fn finish(&mut self) -> Assembly {
take(&mut self.asm)
Expand Down Expand Up @@ -183,7 +191,9 @@ impl Compiler {
if let InputSrc::File(path) = &src {
self.current_imports.push(path.to_path_buf());
}

let res = self.catching_crash(input, |env| env.items(items, false));

if let InputSrc::File(_) = &src {
self.current_imports.pop();
}
Expand Down
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,11 +845,21 @@ fn repl(mut rt: Uiua, mut compiler: Compiler, color: bool, config: FormatConfig)
print!("↪ ");
println!("{}", color_code(&code));

let backup = compiler.clone();
let res = compiler
.load_str(&code)
.and_then(|comp| rt.run_asm(comp.finish()));
print_stack(&rt.take_stack(), color);
res.map(|()| true)
match res {
Ok(asm) => {
compiler.assembly_mut().bindings = asm.bindings;
Ok(true)
}
Err(e) => {
compiler = backup;
Err(e)
}
}
};

println!("Uiua {} (end with ctrl+C)\n", env!("CARGO_PKG_VERSION"));
Expand Down
7 changes: 4 additions & 3 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ impl Uiua {
self.compile_run(|comp| comp.load_file(path))
}
/// Run a Uiua assembly
pub fn run_asm(&mut self, asm: impl Into<Assembly>) -> UiuaResult {
fn run_asm(env: &mut Uiua, asm: Assembly) -> UiuaResult {
pub fn run_asm(&mut self, asm: impl Into<Assembly>) -> UiuaResult<Assembly> {
fn run_asm(env: &mut Uiua, asm: Assembly) -> UiuaResult<Assembly> {
env.asm = asm;
env.rt.execution_start = instant::now();
let top_slices = take(&mut env.asm.top_slices);
Expand All @@ -337,7 +337,8 @@ impl Uiua {
..Runtime::default()
};
}
res
let asm = take(&mut env.asm);
res.map(|_| asm)
}
run_asm(self, asm.into())
}
Expand Down

0 comments on commit 3a21ed1

Please sign in to comment.