Compare commits
2 commits
1d774c6011
...
39e28f01ac
Author | SHA1 | Date | |
---|---|---|---|
39e28f01ac | |||
c5d704a34f |
4 changed files with 28 additions and 3 deletions
17
README.md
17
README.md
|
@ -1,5 +1,18 @@
|
|||
# jorts
|
||||
|
||||
an interpreter for the lox language from https://craftinginterpreters.com
|
||||
a compiler for the lox language from https://craftinginterpreters.com
|
||||
|
||||
this is a learning project.
|
||||
this is a learning project. the implemtation is based heavily off the C part
|
||||
of the book, but also the Java part for the scanner.
|
||||
|
||||
## notes
|
||||
|
||||
- jorts' lox bytecode is not compatible with any implementation.
|
||||
|
||||
## how do?
|
||||
|
||||
```
|
||||
zig build run
|
||||
```
|
||||
|
||||
and play around with it
|
||||
|
|
|
@ -18,6 +18,8 @@ const AllOpcodes = struct {
|
|||
pub Nil: u8 = 8,
|
||||
pub True: u8 = 9,
|
||||
pub False: u8 = 10,
|
||||
|
||||
pub Not: u8 = 11,
|
||||
};
|
||||
|
||||
pub const OpCode = AllOpcodes{};
|
||||
|
@ -174,6 +176,8 @@ pub const Chunk = struct {
|
|||
return try simpleInstruction(stdout, "OP_TRUE", index);
|
||||
} else if (instruction == OpCode.False) {
|
||||
return try simpleInstruction(stdout, "OP_FALSE", index);
|
||||
} else if (instruction == OpCode.Not) {
|
||||
return try simpleInstruction(stdout, "OP_NOT", index);
|
||||
} else {
|
||||
try stdout.print("Unknown opcode: {}\n", instruction);
|
||||
return index + 1;
|
||||
|
|
|
@ -75,7 +75,7 @@ var rules = []ParseRule{
|
|||
ParseRule{ .infix = Compiler.binary, .precedence = .Factor },
|
||||
|
||||
// as the token enum says, those are 1/2 char tokens.
|
||||
ParseRule{},
|
||||
ParseRule{ .prefix = Compiler.unary },
|
||||
// this is specifically for the != operator
|
||||
ParseRule{ .precedence = .Equality },
|
||||
ParseRule{},
|
||||
|
@ -236,6 +236,7 @@ pub const Compiler = struct {
|
|||
|
||||
switch (ttype) {
|
||||
.MINUS => try self.emitByte(OpCode.Negate),
|
||||
.BANG => try self.emitByte(OpCode.Not),
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,10 @@ pub const InterpretResult = error{
|
|||
RuntimeError,
|
||||
};
|
||||
|
||||
fn isFalsey(val: value.Value) bool {
|
||||
return val.vtype == .Nil or (val.vtype == .Bool and !val.as.Bool);
|
||||
}
|
||||
|
||||
pub const VM = struct {
|
||||
chk: *Chunk = undefined,
|
||||
src: []const u8,
|
||||
|
@ -170,6 +174,9 @@ pub const VM = struct {
|
|||
chunk.OpCode.Subtract => try self.doSub(),
|
||||
chunk.OpCode.Multiply => try self.doMul(),
|
||||
chunk.OpCode.Divide => try self.doDiv(),
|
||||
chunk.OpCode.Not => blk: {
|
||||
try self.push(values.BoolVal(isFalsey(self.pop())));
|
||||
},
|
||||
chunk.OpCode.Negate => blk: {
|
||||
var val = self.peek(0);
|
||||
if (val.vtype != .Bool) {
|
||||
|
|
Loading…
Reference in a new issue