forked from luna/jorts
compiler: add scope support
This commit is contained in:
parent
25ee586acb
commit
5138410be4
1 changed files with 21 additions and 1 deletions
|
@ -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 {
|
fn grouping(self: *Compiler, canAssign: bool) !void {
|
||||||
try self.expression();
|
try self.expression();
|
||||||
try self.consume(.RIGHT_PAREN, "Expect ')' after expression.");
|
try self.consume(.RIGHT_PAREN, "Expect ')' after expression.");
|
||||||
|
@ -456,7 +464,7 @@ pub const Compiler = struct {
|
||||||
try self.defineVariable(global);
|
try self.defineVariable(global);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn declaration(self: *Compiler) !void {
|
fn declaration(self: *Compiler) anyerror!void {
|
||||||
if (try self.match(.VAR)) {
|
if (try self.match(.VAR)) {
|
||||||
try self.varDecl();
|
try self.varDecl();
|
||||||
} else {
|
} else {
|
||||||
|
@ -465,9 +473,21 @@ pub const Compiler = struct {
|
||||||
if (self.parser.panicMode) try self.synchronize();
|
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 {
|
fn statement(self: *Compiler) !void {
|
||||||
if (try self.match(.PRINT)) {
|
if (try self.match(.PRINT)) {
|
||||||
try self.printStmt();
|
try self.printStmt();
|
||||||
|
} else if (try self.match(.LEFT_BRACE)) {
|
||||||
|
self.beginScope();
|
||||||
|
try self.block();
|
||||||
|
self.endScope();
|
||||||
} else {
|
} else {
|
||||||
try self.exprStmt();
|
try self.exprStmt();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue