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