From f43e83cffe0dbf0368c660ce342ac452ebe7ca22 Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Tue, 21 Mar 2023 14:29:38 +1100 Subject: [PATCH] Compile throw expressions --- valuescript_compiler/src/capture_finder.rs | 4 +++- valuescript_compiler/src/function_compiler.rs | 9 ++++++++- valuescript_compiler/src/scope_analysis.rs | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/valuescript_compiler/src/capture_finder.rs b/valuescript_compiler/src/capture_finder.rs index 7f7e6ad..f8b27f7 100644 --- a/valuescript_compiler/src/capture_finder.rs +++ b/valuescript_compiler/src/capture_finder.rs @@ -293,7 +293,9 @@ impl CaptureFinder { } } Switch(_) => std::panic!("Not implemented: Switch statement"), - Throw(_) => std::panic!("Not implemented: Throw statement"), + Throw(throw) => { + self.expr(scope, &throw.arg); + } Try(_) => std::panic!("Not implemented: Try statement"), While(while_) => { self.expr(scope, &while_.test); diff --git a/valuescript_compiler/src/function_compiler.rs b/valuescript_compiler/src/function_compiler.rs index ade6593..ec7a61b 100644 --- a/valuescript_compiler/src/function_compiler.rs +++ b/valuescript_compiler/src/function_compiler.rs @@ -796,7 +796,14 @@ impl FunctionCompiler { self.if_(if_, scope); } Switch(switch) => self.todo(switch.span, "Switch statement"), - Throw(throw) => self.todo(throw.span, "Throw statement"), + Throw(throw) => { + let mut expression_compiler = ExpressionCompiler { fnc: self, scope }; + + let arg = expression_compiler.compile(&throw.arg, None); + let instr = Instruction::Throw(self.use_(arg)); + + self.push(instr); + } Try(try_) => self.todo(try_.span, "Try statement"), While(while_) => { self.while_(while_, scope); diff --git a/valuescript_compiler/src/scope_analysis.rs b/valuescript_compiler/src/scope_analysis.rs index bb9f9e3..d66e3f6 100644 --- a/valuescript_compiler/src/scope_analysis.rs +++ b/valuescript_compiler/src/scope_analysis.rs @@ -1398,8 +1398,8 @@ impl ScopeAnalysis { } } } - Stmt::Throw(throw_) => { - self.expr(&scope, &throw_.arg); + Stmt::Throw(throw) => { + self.expr(&scope, &throw.arg); } Stmt::Try(try_) => { self.block_stmt(&scope, &try_.block);