commit bb0372fa9cd599acb5a2a6ed771ff4f994fae1fe
parent 68378e629d0ced133499efba76b570bd7a5e4068
Author: Szymon Mikulicz <szymon.mikulicz@posteo.net>
Date: Tue, 1 Sep 2026 15:00:46 +0200
Allow statements
Diffstat:
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/zlox/src/compiler.zig b/zlox/src/compiler.zig
@@ -514,7 +514,9 @@ pub fn Compiler(size: comptime_int) type {
}
fn declaration(self: *Self) void {
- if (self.match(Token.VAR)) {
+ if (self.match(Token.FUN)) {
+ self.funDeclaration();
+ } else if (self.match(Token.VAR)) {
self.varDeclaration();
} else if (self.match(Token.CON)) {
self.conDeclaration();
@@ -525,6 +527,13 @@ pub fn Compiler(size: comptime_int) type {
if (self.panicMode) self.synchronize();
}
+ fn funDeclaration(self: *Self) void {
+ const global = self.parseVariable("Expect function name.", true) catch return;
+ self.markInitialized();
+ self.function(false);
+ self.defineVariable(global, true);
+ }
+
fn function(self: *Self, _: bool) void {
var fun = self.objects.emplace(Obj.Type.Function, .Function) catch |err| {
self.errorAtPrevious("Couldn't allocate function");
@@ -538,7 +547,7 @@ pub fn Compiler(size: comptime_int) type {
return;
};
- compiler.consume(Token.LEFT_PAREN, "Expect '(' after function name");
+ compiler.consume(Token.LEFT_PAREN, "Expect '(' in function definition");
if (!compiler.check(Token.RIGHT_PAREN)) {
while (true) {
if (fun.arity == std.math.maxInt(@TypeOf(fun.arity))) {
@@ -669,7 +678,7 @@ pub fn Compiler(size: comptime_int) type {
while ((self.current.type catch Token.NIL) != Token.EOF) {
if ((self.previous.type catch Token.NIL) == Token.SEMICOLON) return;
switch (self.current.type catch Token.NIL) {
- Token.CLASS, Token.FUN, Token.VAR, Token.IF, Token.FOR, Token.WHILE, Token.PRINT, Token.RETURN => return,
+ Token.CLASS, Token.VAR, Token.CON, Token.IF, Token.FOR, Token.WHILE, Token.SWITCH, Token.PRINT, Token.LEFT_BRACE, Token.RETURN => return,
else => self.advance(),
}
}