aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/compiler.rs15
-rw-r--r--src/vm.rs14
-rw-r--r--tests/simple.tdy12
3 files changed, 21 insertions, 20 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index ca4fcd3..d3d4c2c 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -6,9 +6,11 @@ struct Compiler {
tokens: TokenStream,
}
+//TODO rustify
const PREC_NO: u64 = 0;
-const PREC_TERM: u64 = 1;
-const PREC_FACTOR: u64 = 2;
+const PREC_COMP: u64 = 1;
+const PREC_TERM: u64 = 2;
+const PREC_FACTOR: u64 = 3;
impl Compiler {
pub fn new(tokens: TokenStream) -> Self {
@@ -45,6 +47,8 @@ impl Compiler {
Token::Star => PREC_FACTOR,
Token::Slash => PREC_FACTOR,
+ Token::EqualEqual => PREC_COMP,
+
_ => PREC_NO,
}
}
@@ -71,6 +75,8 @@ impl Compiler {
Token::Slash => self.binary(block),
Token::Star => self.binary(block),
+ Token::EqualEqual => self.binary(block),
+
_ => { return false; },
}
return true;
@@ -87,13 +93,13 @@ impl Compiler {
fn grouping(&mut self, block: &mut Block) {
if Token::LeftParen != self.eat() {
- self.error("Expected left parentasis around expression.");
+ self.error("Expected left parenthesis around expression.");
}
self.expression(block);
if Token::RightParen != self.eat() {
- self.error("Expected closing parentasis after expression.");
+ self.error("Expected closing parenthesis after expression.");
}
}
@@ -115,6 +121,7 @@ impl Compiler {
Token::Minus => Op::Sub,
Token::Star => Op::Mul,
Token::Slash => Op::Div,
+ Token::EqualEqual => Op::CompEq,
_ => { self.error("Illegal operator"); }
};
block.add(op);
diff --git a/src/vm.rs b/src/vm.rs
index 7ab2235..7ba7f19 100644
--- a/src/vm.rs
+++ b/src/vm.rs
@@ -1,5 +1,5 @@
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Value {
Float(f64),
Int(i64),
@@ -17,6 +17,8 @@ pub enum Op {
Div,
Neg,
+ CompEq,
+
Print,
Return,
}
@@ -143,12 +145,10 @@ impl VM {
}
}
- Op::Mul => {
- todo!();
- }
-
- Op::Div => {
- todo!();
+ Op::CompEq => {
+ let b = self.stack.pop().unwrap();
+ let a = self.stack.pop().unwrap();
+ self.stack.push(Value::Bool(a == b));
}
Op::Print => {
diff --git a/tests/simple.tdy b/tests/simple.tdy
index a2d36b3..a4f6c45 100644
--- a/tests/simple.tdy
+++ b/tests/simple.tdy
@@ -1,9 +1,3 @@
-// Comments!
-// 3
-1 + 1 * 2
-// -1
--1 * 3 + 2
-// 8
-2 * 2 * 2
-// 34
-1 + (3 * 2 + 3 + 2) * 3
+1 == 1
+1 + 1 == 2
+1 + 1 == 3