Skip to content

Commit

Permalink
supports comparisions
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 19, 2023
1 parent ec82223 commit 789e136
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 1 deletion.
4 changes: 4 additions & 0 deletions components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ fn compile_expr(db: &dyn crate::Db, expr: &syntax::Expr, chunk: &mut Chunk) {
syntax::Op::Star => chunk.emit_byte(Code::Multiply),
syntax::Op::EqualEqual => chunk.emit_byte(Code::Equal),
syntax::Op::NotEqual => chunk.emit_byte(Code::NotEqual),
syntax::Op::Greater => chunk.emit_byte(Code::Greater),
syntax::Op::GreaterEqual => chunk.emit_byte(Code::GreaterEqual),
syntax::Op::Less => chunk.emit_byte(Code::Less),
syntax::Op::LessEqual => chunk.emit_byte(Code::LessEqual),
_ => todo!(),
}
}
Expand Down
29 changes: 29 additions & 0 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ impl std::cmp::PartialEq for Value {
}
}

impl std::cmp::PartialOrd for Value {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(Value::Number(a), Value::Number(b)) => a.partial_cmp(b),
_ => None,
}
}
}

pub struct VM {
chunk: bytecode::Chunk,
ip: usize,
Expand Down Expand Up @@ -172,6 +181,26 @@ impl VM {
let a = self.pop();
self.push(a != b);
},
bytecode::Code::Greater => {
let b = self.pop();
let a = self.pop();
self.push(a > b);
},
bytecode::Code::GreaterEqual => {
let b = self.pop();
let a = self.pop();
self.push(a >= b);
},
bytecode::Code::Less => {
let b = self.pop();
let a = self.pop();
self.push(a < b);
},
bytecode::Code::LessEqual => {
let b = self.pop();
let a = self.pop();
self.push(a <= b);
},
}
if let Some(step_inspect) = &mut step_inspect {
step_inspect(instruction, self);
Expand Down
6 changes: 5 additions & 1 deletion components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ pub enum Code {
Negate,
Not,
Equal,
NotEqual
NotEqual,
Greater,
GreaterEqual,
Less,
LessEqual,
}

#[derive(PartialEq, Eq, Debug, Clone, Default)]
Expand Down
48 changes: 48 additions & 0 deletions lox_tests/comparison.bytecode
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Chunk {
code: [
Constant(
F64(
1.0,
),
),
Constant(
F64(
2.0,
),
),
Less,
Constant(
F64(
1.0,
),
),
Constant(
F64(
1.0,
),
),
LessEqual,
Constant(
F64(
1.0,
),
),
Constant(
F64(
2.0,
),
),
Greater,
Constant(
F64(
1.0,
),
),
Constant(
F64(
1.0,
),
),
GreaterEqual,
],
}
182 changes: 182 additions & 0 deletions lox_tests/comparison.execute
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
execute: Constant(
F64(
1.0,
),
)
stack: [
Number(
1.0,
),
]

execute: Constant(
F64(
2.0,
),
)
stack: [
Number(
1.0,
),
Number(
2.0,
),
]

execute: Less
stack: [
Boolean(
true,
),
]

execute: Constant(
F64(
1.0,
),
)
stack: [
Boolean(
true,
),
Number(
1.0,
),
]

execute: Constant(
F64(
1.0,
),
)
stack: [
Boolean(
true,
),
Number(
1.0,
),
Number(
1.0,
),
]

execute: LessEqual
stack: [
Boolean(
true,
),
Boolean(
true,
),
]

execute: Constant(
F64(
1.0,
),
)
stack: [
Boolean(
true,
),
Boolean(
true,
),
Number(
1.0,
),
]

execute: Constant(
F64(
2.0,
),
)
stack: [
Boolean(
true,
),
Boolean(
true,
),
Number(
1.0,
),
Number(
2.0,
),
]

execute: Greater
stack: [
Boolean(
true,
),
Boolean(
true,
),
Boolean(
false,
),
]

execute: Constant(
F64(
1.0,
),
)
stack: [
Boolean(
true,
),
Boolean(
true,
),
Boolean(
false,
),
Number(
1.0,
),
]

execute: Constant(
F64(
1.0,
),
)
stack: [
Boolean(
true,
),
Boolean(
true,
),
Boolean(
false,
),
Number(
1.0,
),
Number(
1.0,
),
]

execute: GreaterEqual
stack: [
Boolean(
true,
),
Boolean(
true,
),
Boolean(
false,
),
Boolean(
true,
),
]

4 changes: 4 additions & 0 deletions lox_tests/comparison.lox
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 < 2
1 <= 1
1 > 2
1 >= 1
20 changes: 20 additions & 0 deletions lox_tests/comparison.syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
BinaryOp {
left: NumberLiteral(1),
op: Less,
right: NumberLiteral(2),
}
BinaryOp {
left: NumberLiteral(1),
op: LessEqual,
right: NumberLiteral(1),
}
BinaryOp {
left: NumberLiteral(1),
op: Greater,
right: NumberLiteral(2),
}
BinaryOp {
left: NumberLiteral(1),
op: GreaterEqual,
right: NumberLiteral(1),
}

0 comments on commit 789e136

Please sign in to comment.