feat: comparisons

This commit is contained in:
Artemy 2022-08-03 19:03:29 +03:00
parent 757d668f46
commit 4cf0471d66
2 changed files with 247 additions and 21 deletions

View file

@ -23,10 +23,9 @@ impl Interpreter {
for command in arr { for command in arr {
self.eval_node(command); self.eval_node(command);
}
self.pos += 1; self.pos += 1;
} }
}
fn eval_node(&self, command: &mut Value) -> Value { fn eval_node(&self, command: &mut Value) -> Value {
match command { match command {
@ -76,6 +75,21 @@ impl Interpreter {
} }
return Value::Null; return Value::Null;
} }
"comp" => {
match value {
Value::Array(value) => {
if value.len() == 3 {
return self.comp(value);
} else {
return Value::Null;
}
}
_ => {
self.error("Unsupported data type for the println argument");
}
}
return Value::Null;
}
name => { name => {
self.unk_token(&name); self.unk_token(&name);
return Value::Null; return Value::Null;
@ -138,17 +152,14 @@ impl Interpreter {
panic!(); panic!();
} }
}, },
name => { _ => {
self.error(&format!( self.error("Unexpected operator token");
"Unsupported operation type for calculation: {}",
name
));
panic!(); panic!();
} }
} }
} }
Value::Object(_) => self.calc(&mut vec![ Value::Object(_) => self.calc(&mut vec![
serde_json::Value::Number(op1.clone()), Value::Number(op1.clone()),
operation.clone(), operation.clone(),
self.eval_node(&mut op2.clone()), self.eval_node(&mut op2.clone()),
]), ]),
@ -165,7 +176,7 @@ impl Interpreter {
]), ]),
Value::Object(_) => self.calc(&mut vec![ Value::Object(_) => self.calc(&mut vec![
self.eval_node(&mut op1.clone()), self.eval_node(&mut op1.clone()),
Value::String(operation.to_string()), operation.clone(),
self.eval_node(&mut op2.clone()), self.eval_node(&mut op2.clone()),
]), ]),
_ => { _ => {
@ -180,6 +191,121 @@ impl Interpreter {
} }
} }
fn comp(&self, value: &mut Vec<Value>) -> Value {
let op1 = &value[0];
let operation = &value[1];
let op2 = &value[2];
match op1 {
Value::Object(_) => match op2 {
Value::Object(_) => self.comp(&mut vec![
self.eval_node(&mut op1.clone()),
operation.clone(),
self.eval_node(&mut op2.clone()),
]),
_ => self.comp(&mut vec![
self.eval_node(&mut op1.clone()),
operation.clone(),
op2.clone(),
]),
},
Value::Number(op1) => match op2 {
Value::Object(_) => self.comp(&mut vec![
Value::Number(op1.clone()),
operation.clone(),
self.eval_node(&mut op2.clone()),
]),
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),
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(_) => self.comp(&mut vec![
Value::Bool(op1.clone()),
operation.clone(),
self.eval_node(&mut op2.clone()),
]),
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),
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(_) => self.comp(&mut vec![
op1.clone(),
operation.clone(),
self.eval_node(&mut op2.clone()),
]),
_ => match operation {
Value::String(operation) => match operation.as_str() {
"==" => json!(op1 == op2),
"!=" => json!(op1 != op2),
name => {
self.error(&format!(
"Unsupported operation type for comparison: {}",
name
));
panic!();
}
},
_ => {
self.error("Unexpected operator token");
panic!();
}
},
},
}
}
fn print(&self, args: &mut Vec<Value>, ln: bool) { fn print(&self, args: &mut Vec<Value>, ln: bool) {
for arg in args { for arg in args {
self.print_one(arg); self.print_one(arg);

View file

@ -14,40 +14,140 @@
{ {
print: [ print: [
"-----All operators-----\n\n\tNORMAL", "-----All operators-----\n\n\tNORMAL",
"\n\t2 * 3 = ", "\n 2 * 3 = ",
{ calc: [2, "*", 3] }, { calc: [2, "*", 3] },
"\n\t6 / 2 = ", "\n 6 / 2 = ",
{ calc: [6, "/", 2.5] }, { calc: [6, "/", 2.5] },
"\n\t2 + 3 = ", "\n 2 + 3 = ",
{ calc: [2, "+", 3] }, { calc: [2, "+", 3] },
"\n\t2 - 3 = ", "\n 2 - 3 = ",
{ calc: [2, "-", 3] }, { calc: [2, "-", 3] },
"\n\t2 % 2 = ", "\n 2 % 2 = ",
{ calc: [2, "%", 2] }, { calc: [2, "%", 2] },
"\n\t3 % 2 = ", "\n 3 % 2 = ",
{ calc: [3, "%", 2] }, { calc: [3, "%", 2] },
"\n\n\tBit operators", "\n\n\tBit operators",
"\n\t4 & 4 = ", "\n 4 & 4 = ",
{ calc: [4, "&", 4] }, { calc: [4, "&", 4] },
"\n\t4 & 3 = ", "\n 4 & 3 = ",
{ calc: [4, "&", 3] }, { calc: [4, "&", 3] },
"\n\t4 | 4 = ", "\n 4 | 4 = ",
{ calc: [4, "|", 4] }, { calc: [4, "|", 4] },
"\n\t4 | 3 = ", "\n 4 | 3 = ",
{ calc: [4, "|", 3] }, { calc: [4, "|", 3] },
"\n\t-8 >> 3 = ", "\n -8 >> 3 = ",
{ calc: [-8, ">>", 3] }, { calc: [-8, ">>", 3] },
"\n\t5 << 3 = ", "\n 5 << 3 = ",
{ calc: [5, "<<", 3] }, { calc: [5, "<<", 3] },
"\n\n--End of All operators--", "\n\n--End of All operators--",
], ],
}, },
{
print: [
"-----All comparisons-----\n\n\tBool",
"\n true != false: ",
{ comp: [true, "!=", false] },
"\n true == false: ",
{ comp: [true, "==", false] },
"\n true && false: ",
{ comp: [true, "&&", false] },
"\n true || false: ",
{ comp: [true, "||", false] },
"\n true > false: ",
{ comp: [true, ">", false] },
"\n true >= false: ",
{ comp: [true, ">=", false] },
"\n true <= false: ",
{ comp: [true, "<=", false] },
"\n\n\tNumbers",
"\n 1 != 2: ",
{ comp: [1, "!=", 2] },
"\n 4 == 2+2: ",
{ comp: [4, "==", { calc: [2, "+", 2] }] },
"\n 4 > 2+2: ",
{ comp: [4, ">", { calc: [2, "+", 2] }] },
"\n 4 < 2+2: ",
{ comp: [4, "<", { calc: [2, "+", 2] }] },
"\n 4 >= 2+2: ",
{ comp: [4, ">=", { calc: [2, "+", 2] }] },
"\n 8 <= 2+2: ",
{ comp: [8, "<=", { calc: [2, "+", 2] }] },
"\n 8 <= 4+4: ",
{ comp: [8, "<=", { calc: [4, "+", 4] }] },
"\n\n\tOther",
"\n [0,1] != [1,0]: ",
{ comp: [[0, 1], "!=", [1, 0]] },
"\n [0,1] == [0,1]: ",
{ comp: [[0, 1], "==", [0, 1]] },
'\n "What" != "Not What": ',
{ comp: ["What", "!=", "Not What"] },
'\n "What" == "Not What": ',
{ comp: ["What", "==", "Not What"] },
"\n\n\tMore complex",
"\n (1 + 1 > 3) == false: ",
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", false] },
"\n (( 1 + 1 > 3 ) == false) && (( 1 + 1 > 3 ) == true): ",
{
comp: [
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", false] },
"&&",
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", true] },
],
},
"\n (( 1 + 1 > 3 ) == false) || (( 1 + 1 > 3 ) == true): ",
{
comp: [
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", false] },
"||",
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", true] },
],
},
"\n\n-End of All comparisons-",
],
},
// {
// print: [
// {
// print: [
// { ░██╗░░░░░░░██╗░█████╗░████████╗
// print: [ ░██║░░██╗░░██║██╔══██╗╚══██╔══╝
// { ░╚██╗████╗██╔╝███████║░░░██║░░░
// print: [ ░░████╔═████║░██╔══██║░░░██║░░░
// { ░░╚██╔╝░╚██╔╝░██║░░██║░░░██║░░░
// print: [ ░░░╚═╝░░░╚═╝░░╚═╝░░╚═╝░░░╚═╝░░░
// { print: [{ print: [{ print: [{ print: "wat" }] }] }] },
// ],
// },
// ],
// },
// ],
// },
// ],
// },
// ],
// },
// //print functions // //print functions
// { // {
// print: ["Hello world", "! ", 1, "\n", ["wow"], "\n", { calc: [1, "+", 2] }], // print: ["Hello world", "! ", 1, "\n", ["wow"], "\n", { calc: [1, "+", 2] }],