feat(tazjin/rlox): Support trivial literals in bytecode compiler

Adds support for true, false & nil. These each come with a new
separate opcode and are pushed directly on the stack.

Change-Id: I405b5b09496dcf99d514d3411c083e0834377167
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2571
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2021-02-28 15:15:46 +02:00 committed by tazjin
parent 127ef98486
commit 47ffa80711
5 changed files with 52 additions and 6 deletions

View file

@ -1,12 +1,17 @@
use super::value::Value;
use super::*;
use crate::Lox;
fn expect_num(code: &str, value: f64) {
fn expect(code: &str, value: Value) {
let result = Interpreter::create()
.interpret(code.into())
.expect("evaluation failed");
assert_eq!(result, value::Value::Number(value));
assert_eq!(result, value);
}
fn expect_num(code: &str, value: f64) {
expect(code, Value::Number(value))
}
#[test]
@ -46,3 +51,10 @@ fn arithmetic() {
expect_num("-4 * -4 + (14 - 5)", 25.0);
expect_num("(702 + 408) - ((239 - 734) / -5) + -4", 1007.0);
}
#[test]
fn trivial_literals() {
expect("true", Value::Bool(true));
expect("false", Value::Bool(false));
expect("nil", Value::Nil);
}