compiler: add nicer error handling

- vm: fix pop opcode handler
This commit is contained in:
Luna 2019-06-02 18:11:23 -03:00
parent 06df2d37ee
commit aa94396e51
2 changed files with 20 additions and 1 deletions

View file

@ -337,8 +337,24 @@ pub const Compiler = struct {
try self.emitByte(OpCode.Pop);
}
fn synchronize(self: *Compiler) !void {
self.parser.panicMode = false;
while (self.parser.current.ttype != .EOF) {
if (self.parser.previous.ttype == .SEMICOLON) return;
switch (self.parser.current.ttype) {
.CLASS, .FUN, .VAR, .FOR, .IF, .WHILE, .PRINT, .RETURN => return,
else => {},
}
try self.advance();
}
}
fn declaration(self: *Compiler) !void {
try self.statement();
if (self.parser.panicMode) try self.synchronize();
}
fn statement(self: *Compiler) !void {

View file

@ -255,7 +255,10 @@ pub const VM = struct {
chunk.OpCode.True => try self.push(values.BoolVal(true)),
chunk.OpCode.False => try self.push(values.BoolVal(false)),
chunk.OpCode.Pop => self.pop(),
chunk.OpCode.Pop => blk: {
_ = self.pop();
break :blk;
},
chunk.OpCode.Equal => blk: {
var a = self.pop();