From 1d774c60117e1e5f189fe10c5fbe2761d932ac02 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 1 Jun 2019 23:44:59 -0300 Subject: [PATCH] add support for nil, true and false literal values --- src/chunk.zig | 11 +++++++++++ src/compiler.zig | 17 ++++++++++++++--- src/value.zig | 2 ++ src/vm.zig | 4 ++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/chunk.zig b/src/chunk.zig index a3008c7..c32345e 100644 --- a/src/chunk.zig +++ b/src/chunk.zig @@ -13,6 +13,11 @@ const AllOpcodes = struct { pub Multiply: u8 = 5, pub Divide: u8 = 6, pub Negate: u8 = 7, + + // basic type op codes + pub Nil: u8 = 8, + pub True: u8 = 9, + pub False: u8 = 10, }; pub const OpCode = AllOpcodes{}; @@ -163,6 +168,12 @@ pub const Chunk = struct { return try simpleInstruction(stdout, "OP_MULTIPLY", index); } else if (instruction == OpCode.Divide) { return try simpleInstruction(stdout, "OP_DIVIDE", index); + } else if (instruction == OpCode.Nil) { + return try simpleInstruction(stdout, "OP_NIL", index); + } else if (instruction == OpCode.True) { + return try simpleInstruction(stdout, "OP_TRUE", index); + } else if (instruction == OpCode.False) { + return try simpleInstruction(stdout, "OP_FALSE", index); } else { try stdout.print("Unknown opcode: {}\n", instruction); return index + 1; diff --git a/src/compiler.zig b/src/compiler.zig index ae21e84..350e6ad 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -94,17 +94,19 @@ var rules = []ParseRule{ ParseRule{ .precedence = .And }, ParseRule{}, ParseRule{}, + + // false + ParseRule{ .prefix = Compiler.literal }, ParseRule{}, ParseRule{}, ParseRule{}, - ParseRule{}, - ParseRule{}, + ParseRule{ .prefix = Compiler.literal }, ParseRule{ .precedence = .Or }, ParseRule{}, ParseRule{}, ParseRule{}, ParseRule{}, - ParseRule{}, + ParseRule{ .prefix = Compiler.literal }, ParseRule{}, ParseRule{}, ParseRule{}, @@ -252,6 +254,15 @@ pub const Compiler = struct { } } + fn literal(self: *Compiler) !void { + switch (self.parser.previous.ttype) { + .FALSE => try self.emitByte(OpCode.False), + .NIL => try self.emitByte(OpCode.Nil), + .TRUE => try self.emitByte(OpCode.True), + else => unreachable, + } + } + fn parsePrecedence(self: *Compiler, precedence: Precedence) !void { try self.advance(); var as_int = @enumToInt(precedence); diff --git a/src/value.zig b/src/value.zig index aa33ecc..713c4b1 100644 --- a/src/value.zig +++ b/src/value.zig @@ -36,6 +36,8 @@ pub fn NumberVal(val: f64) Value { pub fn printValue(stdout: var, value: Value) !void { switch (value.as) { + .Nil => try stdout.print("nil"), + .Bool => try stdout.print("{}", value.as.Bool), .Number => try stdout.print("{}", value.as.Number), else => unreachable, } diff --git a/src/vm.zig b/src/vm.zig index 398f9ab..f1ce443 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -162,6 +162,10 @@ pub const VM = struct { return InterpretResult.Ok; }, + chunk.OpCode.Nil => try self.push(values.NilVal()), + chunk.OpCode.True => try self.push(values.BoolVal(true)), + chunk.OpCode.False => try self.push(values.BoolVal(false)), + chunk.OpCode.Add => try self.doAdd(), chunk.OpCode.Subtract => try self.doSub(), chunk.OpCode.Multiply => try self.doMul(),