compiler: add nicer error handling
- vm: fix pop opcode handler
This commit is contained in:
parent
06df2d37ee
commit
aa94396e51
2 changed files with 20 additions and 1 deletions
|
@ -337,8 +337,24 @@ pub const Compiler = struct {
|
||||||
try self.emitByte(OpCode.Pop);
|
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 {
|
fn declaration(self: *Compiler) !void {
|
||||||
try self.statement();
|
try self.statement();
|
||||||
|
if (self.parser.panicMode) try self.synchronize();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn statement(self: *Compiler) !void {
|
fn statement(self: *Compiler) !void {
|
||||||
|
|
|
@ -255,7 +255,10 @@ pub const VM = struct {
|
||||||
chunk.OpCode.True => try self.push(values.BoolVal(true)),
|
chunk.OpCode.True => try self.push(values.BoolVal(true)),
|
||||||
chunk.OpCode.False => try self.push(values.BoolVal(false)),
|
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: {
|
chunk.OpCode.Equal => blk: {
|
||||||
var a = self.pop();
|
var a = self.pop();
|
||||||
|
|
Loading…
Reference in a new issue