From 262ac430f40c60947bfdf8bab79aca4026c8a20c Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Thu, 6 Jul 2023 14:17:52 +1000 Subject: [PATCH] Simplify get_pattern_register --- .../src/expression_compiler.rs | 25 +-------- valuescript_compiler/src/function_compiler.rs | 55 +++++++++---------- 2 files changed, 28 insertions(+), 52 deletions(-) diff --git a/valuescript_compiler/src/expression_compiler.rs b/valuescript_compiler/src/expression_compiler.rs index 19ab10ce..2d7552d1 100644 --- a/valuescript_compiler/src/expression_compiler.rs +++ b/valuescript_compiler/src/expression_compiler.rs @@ -1385,13 +1385,7 @@ impl<'a> ExpressionCompiler<'a> { match pat { Pat::Ident(ident) => { - let ident_reg = match self.fnc.get_pattern_register(pat) { - Some(r) => r, - None => { - self.fnc.error(pat.span(), &"Invalid pattern".to_string()); - self.fnc.allocate_reg(&"_invalid_pattern".to_string()) - } - }; + let ident_reg = self.fnc.get_pattern_register(pat); if register != &ident_reg { self.fnc.diagnostics.push(Diagnostic { @@ -1431,13 +1425,7 @@ impl<'a> ExpressionCompiler<'a> { None => continue, }; - let elem_reg = match self.fnc.get_pattern_register(elem) { - Some(r) => r, - None => { - self.fnc.error(elem.span(), "Invalid pattern"); - self.fnc.allocate_reg(&"_invalid_pattern".to_string()) - } - }; + let elem_reg = self.fnc.get_pattern_register(elem); self.fnc.push(Instruction::Sub( Value::Register(register.clone()), @@ -1458,14 +1446,7 @@ impl<'a> ExpressionCompiler<'a> { match prop { ObjectPatProp::KeyValue(kv) => { - let param_reg = match self.fnc.get_pattern_register(&kv.value) { - Some(r) => r, - None => { - self.fnc.error(kv.value.span(), "Invalid pattern"); - self.fnc.allocate_reg(&"_invalid_pattern".to_string()) - } - }; - + let param_reg = self.fnc.get_pattern_register(&kv.value); let compiled_key = self.prop_name(&kv.key); self.fnc.push(Instruction::Sub( diff --git a/valuescript_compiler/src/function_compiler.rs b/valuescript_compiler/src/function_compiler.rs index f5350222..b234df87 100644 --- a/valuescript_compiler/src/function_compiler.rs +++ b/valuescript_compiler/src/function_compiler.rs @@ -388,12 +388,12 @@ impl FunctionCompiler { match functionish { Functionish::Fn(_, fn_) => { for p in &fn_.params { - param_registers.push(self.get_pattern_register(&p.pat)); + param_registers.push(self.get_pattern_register_opt(&p.pat)); } } Functionish::Arrow(arrow) => { for p in &arrow.params { - param_registers.push(self.get_pattern_register(p)); + param_registers.push(self.get_pattern_register_opt(p)); } } Functionish::Constructor(_, _class_span, constructor) => { @@ -406,7 +406,7 @@ impl FunctionCompiler { )); } swc_ecma_ast::ParamOrTsParamProp::Param(p) => { - param_registers.push(self.get_pattern_register(&p.pat)) + param_registers.push(self.get_pattern_register_opt(&p.pat)) } } } @@ -416,7 +416,7 @@ impl FunctionCompiler { return param_registers; } - pub fn get_pattern_register(&mut self, param_pat: &swc_ecma_ast::Pat) -> Option { + pub fn get_pattern_register_opt(&mut self, param_pat: &swc_ecma_ast::Pat) -> Option { use swc_ecma_ast::Pat; Some(match param_pat { @@ -424,7 +424,7 @@ impl FunctionCompiler { "this" => return None, _ => self.get_variable_register(&ident.id), }, - Pat::Assign(assign) => return self.get_pattern_register(&assign.left), + Pat::Assign(assign) => return self.get_pattern_register_opt(&assign.left), Pat::Array(_) => self.allocate_numbered_reg(&"_array_pat".to_string()), Pat::Object(_) => self.allocate_numbered_reg(&"_object_pat".to_string()), Pat::Invalid(_) => self.allocate_numbered_reg(&"_invalid_pat".to_string()), @@ -433,6 +433,16 @@ impl FunctionCompiler { }) } + pub fn get_pattern_register(&mut self, param_pat: &swc_ecma_ast::Pat) -> Register { + match self.get_pattern_register_opt(param_pat) { + Some(r) => r, + None => { + self.error(param_pat.span(), "Invalid pattern"); + self.allocate_reg(&"_invalid_pattern".to_string()) + } + } + } + pub fn get_variable_register(&mut self, ident: &swc_ecma_ast::Ident) -> Register { match self.scope_analysis.lookup_value(&self.owner_id, ident) { Some(Value::Register(reg)) => reg, @@ -849,17 +859,15 @@ impl FunctionCompiler { if let Some(param) = &catch_clause.param { let mut ec = ExpressionCompiler { fnc: self }; - if let Some(pattern_reg) = ec.fnc.get_pattern_register(¶m) { - // TODO: Set up this register through set_catch instead of copying into it - ec.fnc.push(Instruction::Mov( - Value::Register(catch_error_reg.unwrap()), - pattern_reg.clone(), - )); + let pattern_reg = ec.fnc.get_pattern_register(¶m); - ec.pat(¶m, &pattern_reg, false); - } else { - ec.fnc.error(param.span(), "Invalid catch pattern"); - } + // TODO: Set up this register through set_catch instead of copying into it + ec.fnc.push(Instruction::Mov( + Value::Register(catch_error_reg.unwrap()), + pattern_reg.clone(), + )); + + ec.pat(¶m, &pattern_reg, false); } self.block_statement(&catch_clause.body); @@ -1132,14 +1140,7 @@ impl FunctionCompiler { swc_ecma_ast::VarDeclOrPat::Pat(pat) => pat, }; - let value_reg = match ec.fnc.get_pattern_register(pat) { - Some(value_reg) => value_reg, - None => { - ec.fnc - .error(pat.span(), "Loop variable has invalid pattern"); - ec.fnc.allocate_reg(&"_invalid_pattern".to_string()) - } - }; + let value_reg = ec.fnc.get_pattern_register(pat); let iter_reg = ec.fnc.allocate_numbered_reg(&"_iter".to_string()); let iter_res_reg = ec.fnc.allocate_numbered_reg(&"_iter_res".to_string()); @@ -1255,13 +1256,7 @@ impl FunctionCompiler { for decl in &var_decl.decls { match &decl.init { Some(expr) => { - let target_register = match self.get_pattern_register(&decl.name) { - Some(tr) => tr, - None => { - self.error(decl.name.span(), "Invalid pattern"); - self.allocate_reg(&"_invalid_pattern".to_string()) - } - }; + let target_register = self.get_pattern_register(&decl.name); let mut ec = ExpressionCompiler { fnc: self }; ec.compile_into(expr, target_register.clone());