[rlox] mini-cleanup
This commit is contained in:
@@ -1,36 +1,5 @@
|
|||||||
mod vm;
|
mod vm;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut chunk = vm::Chunk::new("TEST".to_string());
|
|
||||||
chunk.add_constant(vm::Value::from(3.));
|
|
||||||
chunk.add_constant(vm::Value::from(7.));
|
|
||||||
chunk.add_constant(vm::Value::from(11.));
|
|
||||||
chunk.add_constant(vm::Value::from(17.));
|
|
||||||
chunk.add_constant(vm::Value::from(500.));
|
|
||||||
chunk.add_constant(vm::Value::from(1000.));
|
|
||||||
chunk.add_constant(vm::Value::from(250.));
|
|
||||||
|
|
||||||
|
|
||||||
chunk.add_op(vm::Op::Constant { offset: 0 }, 1);
|
|
||||||
chunk.add_op(vm::Op::Constant { offset: 1 }, 1);
|
|
||||||
chunk.add_op(vm::Op::Multiply, 1);
|
|
||||||
chunk.add_op(vm::Op::Constant { offset: 2 }, 1);
|
|
||||||
chunk.add_op(vm::Op::Constant { offset: 3 }, 1);
|
|
||||||
chunk.add_op(vm::Op::Multiply, 1);
|
|
||||||
chunk.add_op(vm::Op::Multiply, 1);
|
|
||||||
chunk.add_op(vm::Op::Negate, 1);
|
|
||||||
chunk.add_op(vm::Op::Constant { offset: 4 }, 2);
|
|
||||||
chunk.add_op(vm::Op::Constant { offset: 5 }, 2);
|
|
||||||
chunk.add_op(vm::Op::Add, 2);
|
|
||||||
chunk.add_op(vm::Op::Constant { offset: 6 }, 2);
|
|
||||||
chunk.add_op(vm::Op::Subtract, 2);
|
|
||||||
chunk.add_op(vm::Op::Negate, 2);
|
|
||||||
chunk.add_op(vm::Op::Divide, 2);
|
|
||||||
chunk.add_op(vm::Op::Return, 3);
|
|
||||||
|
|
||||||
println!("{:?}", chunk);
|
|
||||||
|
|
||||||
let mut interpreter = vm::VM::new();
|
|
||||||
interpreter.trace = true;
|
|
||||||
interpreter.interpret(&chunk).unwrap()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ pub enum Op {
|
|||||||
Divide,
|
Divide,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
pub struct Value {
|
pub struct Value {
|
||||||
val: f64,
|
val: f64,
|
||||||
}
|
}
|
||||||
@@ -191,3 +191,42 @@ impl VM {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{Chunk, Op, Value, VM};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simple_arithmetic() {
|
||||||
|
let mut chunk = Chunk::new("TEST".to_string());
|
||||||
|
chunk.add_constant(Value::from(3.));
|
||||||
|
chunk.add_constant(Value::from(7.));
|
||||||
|
chunk.add_constant(Value::from(11.));
|
||||||
|
chunk.add_constant(Value::from(17.));
|
||||||
|
chunk.add_constant(Value::from(500.));
|
||||||
|
chunk.add_constant(Value::from(1000.));
|
||||||
|
chunk.add_constant(Value::from(250.));
|
||||||
|
|
||||||
|
|
||||||
|
chunk.add_op(Op::Constant { offset: 0 }, 1);
|
||||||
|
chunk.add_op(Op::Constant { offset: 1 }, 1);
|
||||||
|
chunk.add_op(Op::Multiply, 1);
|
||||||
|
chunk.add_op(Op::Constant { offset: 2 }, 1);
|
||||||
|
chunk.add_op(Op::Constant { offset: 3 }, 1);
|
||||||
|
chunk.add_op(Op::Multiply, 1);
|
||||||
|
chunk.add_op(Op::Multiply, 1);
|
||||||
|
chunk.add_op(Op::Negate, 1);
|
||||||
|
chunk.add_op(Op::Constant { offset: 4 }, 2);
|
||||||
|
chunk.add_op(Op::Constant { offset: 5 }, 2);
|
||||||
|
chunk.add_op(Op::Add, 2);
|
||||||
|
chunk.add_op(Op::Constant { offset: 6 }, 2);
|
||||||
|
chunk.add_op(Op::Subtract, 2);
|
||||||
|
chunk.add_op(Op::Negate, 2);
|
||||||
|
chunk.add_op(Op::Divide, 2);
|
||||||
|
|
||||||
|
let mut interpreter = VM::new();
|
||||||
|
interpreter.interpret(&chunk).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(interpreter.stack[0], Value::from(3.1416));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user