From 5d8d5f415a585073a9501b5c66f8b413a513ef79 Mon Sep 17 00:00:00 2001 From: Artemy Date: Tue, 9 Aug 2022 12:08:01 +0300 Subject: [PATCH] refactor: removed the delusional lines of code --- ROADMAP.md | 1 + src/interpreter.rs | 103 ++++++++++++++++++--------------------------- 2 files changed, 43 insertions(+), 61 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 7515677..9a2a2ce 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -16,5 +16,6 @@ - [ ] checking variable type - [x] checking for the existence of a variable - [x] create scopes without if, else, loop +- [ ] calculate values in arrays and something else diff --git a/src/interpreter.rs b/src/interpreter.rs index e7217c5..7e6ac50 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -242,7 +242,6 @@ impl Interpreter { if name == "continue" { return Value::String("continue".to_string()); } - return Value::Null; } else { let name = self.run_nodes(else_nodes); if name == "break" { @@ -251,7 +250,6 @@ impl Interpreter { if name == "continue" { return Value::String("continue".to_string()); } - return Value::Null; }; } _ => { @@ -263,9 +261,7 @@ impl Interpreter { if name == "continue" { return Value::String("continue".to_string()); } - return Value::Null; } - return Value::Null; } }, None => { @@ -277,18 +273,16 @@ impl Interpreter { if name == "continue" { return Value::String("continue".to_string()); } - return Value::Null; } - return Value::Null; } }, - _ => return Value::Null, + _ => {} }, None => { self.error("if must have a body"); - panic!() } } + Value::Null } fn loop_cycle(&mut self, value: &Vec) -> Value { @@ -354,7 +348,6 @@ impl Interpreter { } } else { self.error(&format!("The variable {} already exist, use assign", name)); - panic!(); } } } @@ -368,7 +361,6 @@ impl Interpreter { "The variable {} does not exist and cannot be deleted", var_name )); - panic!(); } } } @@ -376,23 +368,23 @@ impl Interpreter { fn get_var(&mut self, var_name: &String) -> Value { let var = self.vars.get(var_name); match var { - Some(var) => var.body.clone(), + Some(var) => return var.body.clone(), None => { self.error(&format!("The variable {} does not exist", var_name)); - panic!(); } } + Value::Null } fn get_var_scope(&mut self, var_name: &String) -> usize { let var = self.vars.get(var_name); match var { - Some(var) => var.scope, + Some(var) => return var.scope, None => { self.error(&format!("The variable {} does not exist", var_name)); - panic!(); } } + 0 } fn assign(&mut self, vars: &Map) { @@ -435,60 +427,56 @@ impl Interpreter { let op2 = op2.as_f64().unwrap(); match operation { Value::String(operation) => match operation.as_str() { - "+" => json!(op1 + op2), - "-" => json!(op1 - op2), - "/" => json!(op1 / op2), - "*" => json!(op1 * op2), - "%" => json!(op1 % op2), - "&" => json!(op1 as i64 & op2 as i64), - "|" => json!(op1 as i64 | op2 as i64), - "^" => json!(op1 as i64 ^ op2 as i64), - "<<" => json!((op1 as i64) << (op2 as i64)), - ">>" => json!((op1 as i64) >> (op2 as i64)), + "+" => return json!(op1 + op2), + "-" => return json!(op1 - op2), + "/" => return json!(op1 / op2), + "*" => return json!(op1 * op2), + "%" => return json!(op1 % op2), + "&" => return json!(op1 as i64 & op2 as i64), + "|" => return json!(op1 as i64 | op2 as i64), + "^" => return json!(op1 as i64 ^ op2 as i64), + "<<" => return json!((op1 as i64) << (op2 as i64)), + ">>" => return json!((op1 as i64) >> (op2 as i64)), name => { self.error(&format!( "Unsupported operation type for calculation: {}", name )); - panic!(); } }, _ => { self.error("Unexpected operator token"); - panic!(); } } } Value::Object(_) => { let op1 = Value::Number(op1.clone()); let op2 = self.eval_node(&op2.clone()); - self.calc(&vec![op1, operation.clone(), op2]) + return self.calc(&vec![op1, operation.clone(), op2]); } _ => { self.error("Unsupported operand type for calculation"); - panic!(); } }, Value::Object(_) => match op2 { Value::Number(_) => { let op1 = self.eval_node(&op1.clone()); - self.calc(&vec![op1, operation.clone(), op2.clone()]) + return self.calc(&vec![op1, operation.clone(), op2.clone()]); } Value::Object(_) => { let op1 = self.eval_node(&op1.clone()); let op2 = self.eval_node(&op2.clone()); - self.calc(&vec![op1, operation.clone(), op2]) + return self.calc(&vec![op1, operation.clone(), op2]); } _ => { self.error("Unsupported operand type for calculation"); - panic!(); } }, _ => { self.error("Unsupported operand type for calculation"); - panic!(); } } + Value::Null } fn comp(&mut self, value: &Vec) -> Value { @@ -500,106 +488,99 @@ impl Interpreter { Value::Object(_) => { let op1 = self.eval_node(&op1.clone()); let op2 = self.eval_node(&op2.clone()); - self.comp(&vec![op1, operation.clone(), op2]) + return self.comp(&vec![op1, operation.clone(), op2]); } _ => { let op1 = self.eval_node(&op1.clone()); - self.comp(&vec![op1, operation.clone(), op2.clone()]) + return self.comp(&vec![op1, operation.clone(), op2.clone()]); } }, Value::Number(op1) => match op2 { Value::Object(_) => { let op2 = self.eval_node(&op2.clone()); - self.comp(&vec![Value::Number(op1.clone()), operation.clone(), op2]) + return self.comp(&vec![Value::Number(op1.clone()), operation.clone(), op2]); } Value::Number(op2) => { let op1 = op1.as_f64().unwrap(); let op2 = op2.as_f64().unwrap(); match operation { Value::String(operation) => match operation.as_str() { - "==" => json!(op1 == op2), - "!=" => json!(op1 != op2), - ">" => json!(op1 > op2), - "<" => json!(op1 < op2), - ">=" => json!(op1 >= op2), - "<=" => json!(op1 <= op2), + "==" => return json!(op1 == op2), + "!=" => return json!(op1 != op2), + ">" => return json!(op1 > op2), + "<" => return json!(op1 < op2), + ">=" => return json!(op1 >= op2), + "<=" => return json!(op1 <= op2), name => { self.error(&format!( "Unsupported operation type for comparison: {}", name )); - panic!(); } }, _ => { self.error("Unexpected operator token"); - panic!(); } } } _ => { self.error("Unsupported operands types for comparison"); - panic!(); } }, Value::Bool(op1) => match op2 { Value::Object(_) => { let op2 = self.eval_node(&op2.clone()); - self.comp(&vec![Value::Bool(op1.clone()), operation.clone(), op2]) + return self.comp(&vec![Value::Bool(op1.clone()), operation.clone(), op2]); } Value::Bool(op2) => match operation { Value::String(operation) => match operation.as_str() { - "==" => json!(op1 == op2), - "!=" => json!(op1 != op2), - ">" => json!(op1 > op2), - "<" => json!(op1 < op2), - ">=" => json!(op1 >= op2), - "<=" => json!(op1 <= op2), - "&&" => json!(*op1 && *op2), - "||" => json!(*op1 || *op2), + "==" => return json!(op1 == op2), + "!=" => return json!(op1 != op2), + ">" => return json!(op1 > op2), + "<" => return json!(op1 < op2), + ">=" => return json!(op1 >= op2), + "<=" => return json!(op1 <= op2), + "&&" => return json!(*op1 && *op2), + "||" => return json!(*op1 || *op2), name => { self.error(&format!( "Unsupported operation type for comparison: {}", name )); - panic!(); } }, _ => { self.error("Unexpected operator token"); - panic!(); } }, _ => { self.error("Unsupported operands types for comparison"); - panic!(); } }, _ => match op2 { Value::Object(_) => { let op2 = self.eval_node(&op2.clone()); - self.comp(&vec![op1.clone(), operation.clone(), op2]) + return self.comp(&vec![op1.clone(), operation.clone(), op2]); } _ => match operation { Value::String(operation) => match operation.as_str() { - "==" => json!(op1 == op2), - "!=" => json!(op1 != op2), + "==" => return json!(op1 == op2), + "!=" => return json!(op1 != op2), name => { self.error(&format!( "Unsupported operation type for comparison: {}", name )); - panic!(); } }, _ => { self.error("Unexpected operator token"); - panic!(); } }, }, } + Value::Null } fn print(&mut self, args: &Vec, ln: bool) {