compiler: add scope support

This commit is contained in:
Luna 2019-06-03 15:17:07 -03:00
parent 25ee586acb
commit 5138410be4
1 changed files with 21 additions and 1 deletions

View File

@ -244,6 +244,14 @@ pub const Compiler = struct {
}
}
fn beginScope(self: *Compiler) void {
self.scopeDepth += 1;
}
fn endScope(self: *Compiler) void {
self.scopeDepth -= 1;
}
fn grouping(self: *Compiler, canAssign: bool) !void {
try self.expression();
try self.consume(.RIGHT_PAREN, "Expect ')' after expression.");
@ -456,7 +464,7 @@ pub const Compiler = struct {
try self.defineVariable(global);
}
fn declaration(self: *Compiler) !void {
fn declaration(self: *Compiler) anyerror!void {
if (try self.match(.VAR)) {
try self.varDecl();
} else {
@ -465,9 +473,21 @@ pub const Compiler = struct {
if (self.parser.panicMode) try self.synchronize();
}
fn block(self: *Compiler) anyerror!void {
while (!self.check(.RIGHT_BRACE) and !self.check(.EOF)) {
try self.declaration();
}
try self.consume(.RIGHT_BRACE, "Expect '}' after block.");
}
fn statement(self: *Compiler) !void {
if (try self.match(.PRINT)) {
try self.printStmt();
} else if (try self.match(.LEFT_BRACE)) {
self.beginScope();
try self.block();
self.endScope();
} else {
try self.exprStmt();
}