refactor: removed the delusional lines of code

This commit is contained in:
Artemy 2022-08-09 12:08:01 +03:00
parent 69676bb525
commit 5d8d5f415a
2 changed files with 43 additions and 61 deletions

View file

@ -16,5 +16,6 @@
- [ ] checking variable type - [ ] checking variable type
- [x] checking for the existence of a variable - [x] checking for the existence of a variable
- [x] create scopes without if, else, loop - [x] create scopes without if, else, loop
- [ ] calculate values in arrays
and something else and something else

View file

@ -242,7 +242,6 @@ impl Interpreter {
if name == "continue" { if name == "continue" {
return Value::String("continue".to_string()); return Value::String("continue".to_string());
} }
return Value::Null;
} else { } else {
let name = self.run_nodes(else_nodes); let name = self.run_nodes(else_nodes);
if name == "break" { if name == "break" {
@ -251,7 +250,6 @@ impl Interpreter {
if name == "continue" { if name == "continue" {
return Value::String("continue".to_string()); return Value::String("continue".to_string());
} }
return Value::Null;
}; };
} }
_ => { _ => {
@ -263,9 +261,7 @@ impl Interpreter {
if name == "continue" { if name == "continue" {
return Value::String("continue".to_string()); return Value::String("continue".to_string());
} }
return Value::Null;
} }
return Value::Null;
} }
}, },
None => { None => {
@ -277,18 +273,16 @@ impl Interpreter {
if name == "continue" { if name == "continue" {
return Value::String("continue".to_string()); return Value::String("continue".to_string());
} }
return Value::Null;
} }
return Value::Null;
} }
}, },
_ => return Value::Null, _ => {}
}, },
None => { None => {
self.error("if must have a body"); self.error("if must have a body");
panic!()
} }
} }
Value::Null
} }
fn loop_cycle(&mut self, value: &Vec<Value>) -> Value { fn loop_cycle(&mut self, value: &Vec<Value>) -> Value {
@ -354,7 +348,6 @@ impl Interpreter {
} }
} else { } else {
self.error(&format!("The variable {} already exist, use assign", name)); 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", "The variable {} does not exist and cannot be deleted",
var_name var_name
)); ));
panic!();
} }
} }
} }
@ -376,23 +368,23 @@ impl Interpreter {
fn get_var(&mut self, var_name: &String) -> Value { fn get_var(&mut self, var_name: &String) -> Value {
let var = self.vars.get(var_name); let var = self.vars.get(var_name);
match var { match var {
Some(var) => var.body.clone(), Some(var) => return var.body.clone(),
None => { None => {
self.error(&format!("The variable {} does not exist", var_name)); self.error(&format!("The variable {} does not exist", var_name));
panic!();
} }
} }
Value::Null
} }
fn get_var_scope(&mut self, var_name: &String) -> usize { fn get_var_scope(&mut self, var_name: &String) -> usize {
let var = self.vars.get(var_name); let var = self.vars.get(var_name);
match var { match var {
Some(var) => var.scope, Some(var) => return var.scope,
None => { None => {
self.error(&format!("The variable {} does not exist", var_name)); self.error(&format!("The variable {} does not exist", var_name));
panic!();
} }
} }
0
} }
fn assign(&mut self, vars: &Map<String, Value>) { fn assign(&mut self, vars: &Map<String, Value>) {
@ -435,60 +427,56 @@ impl Interpreter {
let op2 = op2.as_f64().unwrap(); let op2 = op2.as_f64().unwrap();
match operation { match operation {
Value::String(operation) => match operation.as_str() { Value::String(operation) => match operation.as_str() {
"+" => json!(op1 + op2), "+" => return json!(op1 + op2),
"-" => json!(op1 - op2), "-" => return json!(op1 - op2),
"/" => json!(op1 / op2), "/" => return json!(op1 / op2),
"*" => json!(op1 * op2), "*" => return json!(op1 * op2),
"%" => json!(op1 % op2), "%" => return json!(op1 % op2),
"&" => json!(op1 as i64 & op2 as i64), "&" => return json!(op1 as i64 & op2 as i64),
"|" => json!(op1 as i64 | op2 as i64), "|" => return json!(op1 as i64 | op2 as i64),
"^" => json!(op1 as i64 ^ op2 as i64), "^" => return json!(op1 as i64 ^ op2 as i64),
"<<" => json!((op1 as i64) << (op2 as i64)), "<<" => return json!((op1 as i64) << (op2 as i64)),
">>" => json!((op1 as i64) >> (op2 as i64)), ">>" => return json!((op1 as i64) >> (op2 as i64)),
name => { name => {
self.error(&format!( self.error(&format!(
"Unsupported operation type for calculation: {}", "Unsupported operation type for calculation: {}",
name name
)); ));
panic!();
} }
}, },
_ => { _ => {
self.error("Unexpected operator token"); self.error("Unexpected operator token");
panic!();
} }
} }
} }
Value::Object(_) => { Value::Object(_) => {
let op1 = Value::Number(op1.clone()); let op1 = Value::Number(op1.clone());
let op2 = self.eval_node(&op2.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"); self.error("Unsupported operand type for calculation");
panic!();
} }
}, },
Value::Object(_) => match op2 { Value::Object(_) => match op2 {
Value::Number(_) => { Value::Number(_) => {
let op1 = self.eval_node(&op1.clone()); 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(_) => { Value::Object(_) => {
let op1 = self.eval_node(&op1.clone()); let op1 = self.eval_node(&op1.clone());
let op2 = self.eval_node(&op2.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"); self.error("Unsupported operand type for calculation");
panic!();
} }
}, },
_ => { _ => {
self.error("Unsupported operand type for calculation"); self.error("Unsupported operand type for calculation");
panic!();
} }
} }
Value::Null
} }
fn comp(&mut self, value: &Vec<Value>) -> Value { fn comp(&mut self, value: &Vec<Value>) -> Value {
@ -500,106 +488,99 @@ impl Interpreter {
Value::Object(_) => { Value::Object(_) => {
let op1 = self.eval_node(&op1.clone()); let op1 = self.eval_node(&op1.clone());
let op2 = self.eval_node(&op2.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()); 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::Number(op1) => match op2 {
Value::Object(_) => { Value::Object(_) => {
let op2 = self.eval_node(&op2.clone()); 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) => { Value::Number(op2) => {
let op1 = op1.as_f64().unwrap(); let op1 = op1.as_f64().unwrap();
let op2 = op2.as_f64().unwrap(); let op2 = op2.as_f64().unwrap();
match operation { match operation {
Value::String(operation) => match operation.as_str() { Value::String(operation) => match operation.as_str() {
"==" => json!(op1 == op2), "==" => return json!(op1 == op2),
"!=" => json!(op1 != op2), "!=" => return json!(op1 != op2),
">" => json!(op1 > op2), ">" => return json!(op1 > op2),
"<" => json!(op1 < op2), "<" => return json!(op1 < op2),
">=" => json!(op1 >= op2), ">=" => return json!(op1 >= op2),
"<=" => json!(op1 <= op2), "<=" => return json!(op1 <= op2),
name => { name => {
self.error(&format!( self.error(&format!(
"Unsupported operation type for comparison: {}", "Unsupported operation type for comparison: {}",
name name
)); ));
panic!();
} }
}, },
_ => { _ => {
self.error("Unexpected operator token"); self.error("Unexpected operator token");
panic!();
} }
} }
} }
_ => { _ => {
self.error("Unsupported operands types for comparison"); self.error("Unsupported operands types for comparison");
panic!();
} }
}, },
Value::Bool(op1) => match op2 { Value::Bool(op1) => match op2 {
Value::Object(_) => { Value::Object(_) => {
let op2 = self.eval_node(&op2.clone()); 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::Bool(op2) => match operation {
Value::String(operation) => match operation.as_str() { Value::String(operation) => match operation.as_str() {
"==" => json!(op1 == op2), "==" => return json!(op1 == op2),
"!=" => json!(op1 != op2), "!=" => return json!(op1 != op2),
">" => json!(op1 > op2), ">" => return json!(op1 > op2),
"<" => json!(op1 < op2), "<" => return json!(op1 < op2),
">=" => json!(op1 >= op2), ">=" => return json!(op1 >= op2),
"<=" => json!(op1 <= op2), "<=" => return json!(op1 <= op2),
"&&" => json!(*op1 && *op2), "&&" => return json!(*op1 && *op2),
"||" => json!(*op1 || *op2), "||" => return json!(*op1 || *op2),
name => { name => {
self.error(&format!( self.error(&format!(
"Unsupported operation type for comparison: {}", "Unsupported operation type for comparison: {}",
name name
)); ));
panic!();
} }
}, },
_ => { _ => {
self.error("Unexpected operator token"); self.error("Unexpected operator token");
panic!();
} }
}, },
_ => { _ => {
self.error("Unsupported operands types for comparison"); self.error("Unsupported operands types for comparison");
panic!();
} }
}, },
_ => match op2 { _ => match op2 {
Value::Object(_) => { Value::Object(_) => {
let op2 = self.eval_node(&op2.clone()); 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 { _ => match operation {
Value::String(operation) => match operation.as_str() { Value::String(operation) => match operation.as_str() {
"==" => json!(op1 == op2), "==" => return json!(op1 == op2),
"!=" => json!(op1 != op2), "!=" => return json!(op1 != op2),
name => { name => {
self.error(&format!( self.error(&format!(
"Unsupported operation type for comparison: {}", "Unsupported operation type for comparison: {}",
name name
)); ));
panic!();
} }
}, },
_ => { _ => {
self.error("Unexpected operator token"); self.error("Unexpected operator token");
panic!();
} }
}, },
}, },
} }
Value::Null
} }
fn print(&mut self, args: &Vec<Value>, ln: bool) { fn print(&mut self, args: &Vec<Value>, ln: bool) {