comparisons
This commit is contained in:
parent
2fc6c449b3
commit
215ffb298e
5 changed files with 26 additions and 10 deletions
|
|
@ -2,7 +2,8 @@ use tracing::debug;
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
use crate::vm::{
|
use crate::vm::{
|
||||||
OP_ADD, OP_BITAND, OP_BITOR, OP_BITXOR, OP_CONSTANT, OP_DIVIDE, OP_FALSE, OP_MULTIPLY,
|
OP_ADD, OP_BITAND, OP_BITOR, OP_BITXOR, OP_CONSTANT, OP_DIVIDE, OP_FALSE, OP_MULTIPLY,
|
||||||
OP_NEGATE, OP_RETURN, OP_SUBTRACT, OP_TRUE, OP_NOT, OP_SHL, OP_SHR
|
OP_NEGATE, OP_RETURN, OP_SUBTRACT, OP_TRUE, OP_NOT, OP_SHL, OP_SHR, OP_LESS, OP_LESS_EQUAL,
|
||||||
|
OP_GREATER, OP_GREATER_EQUAL, OP_EQUAL
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Chunk {
|
pub struct Chunk {
|
||||||
|
|
@ -65,6 +66,11 @@ impl Chunk {
|
||||||
OP_RETURN => self.simple_inst("RET", offset),
|
OP_RETURN => self.simple_inst("RET", offset),
|
||||||
OP_SHL => self.simple_inst("SHL", offset),
|
OP_SHL => self.simple_inst("SHL", offset),
|
||||||
OP_SHR => self.simple_inst("SHR", offset),
|
OP_SHR => self.simple_inst("SHR", offset),
|
||||||
|
OP_LESS => self.simple_inst("LT", offset),
|
||||||
|
OP_LESS_EQUAL => self.simple_inst("LTE", offset),
|
||||||
|
OP_GREATER => self.simple_inst("GT", offset),
|
||||||
|
OP_GREATER_EQUAL => self.simple_inst("GTE", offset),
|
||||||
|
OP_EQUAL => self.simple_inst("EQ", offset),
|
||||||
_ => {
|
_ => {
|
||||||
println!("Unknown instruction");
|
println!("Unknown instruction");
|
||||||
offset + 1
|
offset + 1
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use crate::chunk::Chunk;
|
||||||
use crate::scanner::scan;
|
use crate::scanner::scan;
|
||||||
use crate::tokens::{Token, TokenType};
|
use crate::tokens::{Token, TokenType};
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
use crate::vm::{OP_ADD, OP_BITAND, OP_BITOR, OP_BITXOR, OP_CONSTANT, OP_DIVIDE, OP_FALSE, OP_MULTIPLY, OP_NEGATE, OP_NOT, OP_RETURN, OP_SHR, OP_SHL, OP_SUBTRACT, OP_TRUE};
|
use crate::vm::{OP_ADD, OP_BITAND, OP_BITOR, OP_BITXOR, OP_CONSTANT, OP_DIVIDE, OP_FALSE, OP_MULTIPLY, OP_NEGATE, OP_NOT, OP_RETURN, OP_SHR, OP_SHL, OP_SUBTRACT, OP_TRUE, OP_EQUAL, OP_GREATER, OP_GREATER_EQUAL, OP_LESS, OP_LESS_EQUAL};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
|
|
@ -184,6 +184,11 @@ fn binary(s: &mut Compiler) -> anyhow::Result<()> {
|
||||||
TokenType::BitXor => s.emit_byte(OP_BITXOR),
|
TokenType::BitXor => s.emit_byte(OP_BITXOR),
|
||||||
TokenType::GreaterGreater => s.emit_byte(OP_SHR),
|
TokenType::GreaterGreater => s.emit_byte(OP_SHR),
|
||||||
TokenType::LessLess => s.emit_byte(OP_SHL),
|
TokenType::LessLess => s.emit_byte(OP_SHL),
|
||||||
|
TokenType::EqualEqual => s.emit_byte(OP_EQUAL),
|
||||||
|
TokenType::Greater => s.emit_byte(OP_GREATER),
|
||||||
|
TokenType::GreaterEqual => s.emit_byte(OP_GREATER_EQUAL),
|
||||||
|
TokenType::Less => s.emit_byte(OP_LESS),
|
||||||
|
TokenType::LessEqual => s.emit_byte(OP_LESS_EQUAL),
|
||||||
_ => unimplemented!("binary other than plus, minus, star, slash"),
|
_ => unimplemented!("binary other than plus, minus, star, slash"),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -218,12 +223,12 @@ static RULES: LazyLock<HashMap<TokenType, Rule>> = LazyLock::new(|| {
|
||||||
rules.insert(TokenType::Bang, Rule::new(Some(unary), None, PREC_UNARY));
|
rules.insert(TokenType::Bang, Rule::new(Some(unary), None, PREC_UNARY));
|
||||||
rules.insert(TokenType::BangEqual, Rule::new(None, None, PREC_NONE));
|
rules.insert(TokenType::BangEqual, Rule::new(None, None, PREC_NONE));
|
||||||
rules.insert(TokenType::Equal, Rule::new(None, None, PREC_NONE));
|
rules.insert(TokenType::Equal, Rule::new(None, None, PREC_NONE));
|
||||||
rules.insert(TokenType::EqualEqual, Rule::new(None, None, PREC_NONE));
|
rules.insert(TokenType::EqualEqual, Rule::new(None, Some(binary), PREC_COMPARISON));
|
||||||
rules.insert(TokenType::Greater, Rule::new(None, None, PREC_NONE));
|
rules.insert(TokenType::Greater, Rule::new(None, Some(binary), PREC_COMPARISON));
|
||||||
rules.insert(TokenType::GreaterEqual, Rule::new(None, None, PREC_NONE));
|
rules.insert(TokenType::GreaterEqual, Rule::new(None, Some(binary), PREC_COMPARISON));
|
||||||
rules.insert(TokenType::GreaterGreater, Rule::new(None, Some(binary), PREC_BITSHIFT));
|
rules.insert(TokenType::GreaterGreater, Rule::new(None, Some(binary), PREC_BITSHIFT));
|
||||||
rules.insert(TokenType::Less, Rule::new(None, None, PREC_NONE));
|
rules.insert(TokenType::Less, Rule::new(None, Some(binary), PREC_COMPARISON));
|
||||||
rules.insert(TokenType::LessEqual, Rule::new(None, None, PREC_NONE));
|
rules.insert(TokenType::LessEqual, Rule::new(None, Some(binary), PREC_COMPARISON));
|
||||||
rules.insert(TokenType::LessLess, Rule::new(None, Some(binary), PREC_BITSHIFT));
|
rules.insert(TokenType::LessLess, Rule::new(None, Some(binary), PREC_BITSHIFT));
|
||||||
rules.insert(TokenType::Identifier, Rule::new(None, None, PREC_NONE));
|
rules.insert(TokenType::Identifier, Rule::new(None, None, PREC_NONE));
|
||||||
rules.insert(TokenType::String, Rule::new(Some(literal), None, PREC_NONE));
|
rules.insert(TokenType::String, Rule::new(Some(literal), None, PREC_NONE));
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
let chunk = crudlang::compiler::compile("\"a\"+\"b\"")?;
|
let chunk = crudlang::compiler::compile("\"1\"+\"2\"")?;
|
||||||
chunk.disassemble();
|
chunk.disassemble();
|
||||||
|
|
||||||
let result = crudlang::vm::interpret(chunk);
|
let result = crudlang::vm::interpret(chunk);
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ impl Add<&Value> for &Value {
|
||||||
(Value::String(s), Value::F64(f)) => Ok(Value::String(format!("{}{}", s, f))),
|
(Value::String(s), Value::F64(f)) => Ok(Value::String(format!("{}{}", s, f))),
|
||||||
(Value::String(s), Value::Bool(b)) => Ok(Value::String(format!("{}{}", s, b))),
|
(Value::String(s), Value::Bool(b)) => Ok(Value::String(format!("{}{}", s, b))),
|
||||||
(Value::String(s), Value::Char(c)) => Ok(Value::String(format!("{}{}", s, c))),
|
(Value::String(s), Value::Char(c)) => Ok(Value::String(format!("{}{}", s, c))),
|
||||||
(Value::String(s1), Value::String(s2)) => Ok(Value::String(format!("{}{}", s2, s1))),
|
(Value::String(s1), Value::String(s2)) => Ok(Value::String(format!("{}{}", s1, s2))),
|
||||||
(Value::String(s1), Value::List(l)) => Ok(Value::String(format!("{}{:?}", s1, l))),
|
(Value::String(s1), Value::List(l)) => Ok(Value::String(format!("{}{:?}", s1, l))),
|
||||||
(Value::String(s1), Value::Map(m)) => Ok(Value::String(format!("{}{:?}", s1, m))),
|
(Value::String(s1), Value::Map(m)) => Ok(Value::String(format!("{}{:?}", s1, m))),
|
||||||
//enum?
|
//enum?
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,11 @@ impl Vm {
|
||||||
}
|
}
|
||||||
OP_SHL => binary_op(self, |a, b| a << b),
|
OP_SHL => binary_op(self, |a, b| a << b),
|
||||||
OP_SHR => binary_op(self, |a, b| a >> b),
|
OP_SHR => binary_op(self, |a, b| a >> b),
|
||||||
|
OP_EQUAL => binary_op(self, |a, b| Ok(Value::Bool(a == b))),
|
||||||
|
OP_GREATER => binary_op(self, |a, b| Ok(Value::Bool(a > b))),
|
||||||
|
OP_GREATER_EQUAL => binary_op(self, |a, b| Ok(Value::Bool(a >= b))),
|
||||||
|
OP_LESS => binary_op(self, |a, b| Ok(Value::Bool(a < b))),
|
||||||
|
OP_LESS_EQUAL => binary_op(self, |a, b| Ok(Value::Bool(a <= b))),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -86,8 +91,8 @@ impl Vm {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn binary_op(vm: &mut Vm, op: impl Fn(&Value, &Value) -> anyhow::Result<Value> + Copy) {
|
fn binary_op(vm: &mut Vm, op: impl Fn(&Value, &Value) -> anyhow::Result<Value> + Copy) {
|
||||||
let a = vm.pop();
|
|
||||||
let b = vm.pop();
|
let b = vm.pop();
|
||||||
|
let a = vm.pop();
|
||||||
|
|
||||||
let result = op(&a, &b);
|
let result = op(&a, &b);
|
||||||
match result {
|
match result {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue