diff --git a/src/compile.zig b/src/compile.zig index 48eaeb0..5f8a26c 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -46,10 +46,9 @@ const Parser = struct { scanner: *Scanner, had_error: bool, panic_mode: bool, - chunk: *Chunk, vm: *VM, - fn new(vm: *VM, compiler: *Compiler, scanner: *Scanner, chunk: *Chunk) Parser { + fn new(vm: *VM, compiler: *Compiler, scanner: *Scanner) Parser { return Parser{ .compiler = compiler, .current = null, @@ -57,13 +56,12 @@ const Parser = struct { .scanner = scanner, .had_error = false, .panic_mode = false, - .chunk = chunk, .vm = vm, }; } inline fn current_chunk(self: *Parser) *Chunk { - return self.chunk; + return &self.compiler.function.chunk; } fn advance(self: *Parser) void { @@ -137,11 +135,14 @@ const Parser = struct { try self.emit_byte(@intFromEnum(OpCode.OP_RETURN)); } - fn end_parser(self: *Parser) !void { - if (!self.had_error and self.vm.has_tracing()) { + fn end_parser(self: *Parser) !*Obj.Function { + if (!self.had_error and constants.DEBUG_PRINT_CODE) { self.current_chunk().dissassemble("code"); } + try self.emit_return(); + + return self.compiler.function; } fn number(self: *Parser, can_assign: bool) ParsingError!void { @@ -702,17 +703,41 @@ const Parser = struct { } }; +const FunctionType = enum { + Function, + Script, +}; + const Compiler = struct { + function: *Obj.Function, + function_type: FunctionType, + locals: [constants.UINT8_COUNT]Local, local_count: usize, scope_depth: usize, - fn new() Compiler { - return Compiler{ + fn new(allocator: std.mem.Allocator, function_type: FunctionType) Compiler { + const obj_function = Obj.Function.new(allocator); + + var compiler = Compiler{ .locals = undefined, .local_count = 0, .scope_depth = 0, + .function = obj_function, + .function_type = function_type, }; + + compiler.locals[0].depth = 0; + compiler.locals[0].name = Token{ + .token_type = TokenType.EOF, + .start = "", + .length = 0, + .line = 0, + }; + + compiler.local_count += 1; + + return compiler; } }; @@ -721,13 +746,11 @@ const Local = struct { depth: ?usize, }; -pub fn compile(allocator: Allocator, vm: *VM, contents: []const u8, chunk: *Chunk) !bool { - _ = allocator; - - var compiler = Compiler.new(); +pub fn compile(allocator: Allocator, vm: *VM, contents: []const u8) !?*Obj.Function { + var compiler = Compiler.new(allocator, FunctionType.Script); var scanner = Scanner.init(contents); - var parser = Parser.new(vm, &compiler, &scanner, chunk); + var parser = Parser.new(vm, &compiler, &scanner); parser.advance(); @@ -735,7 +758,11 @@ pub fn compile(allocator: Allocator, vm: *VM, contents: []const u8, chunk: *Chun try parser.declaration(); } - try parser.end_parser(); + const function = try parser.end_parser(); - return !parser.had_error; + if (!parser.had_error) { + return function; + } else { + return null; + } } diff --git a/src/constant.zig b/src/constant.zig index d0279d8..68a7740 100644 --- a/src/constant.zig +++ b/src/constant.zig @@ -7,4 +7,10 @@ pub const UINT16_MAX = std.math.maxInt(u16); pub const UINT8_COUNT = UINT8_MAX + 1; -pub const STACK_MAX = 256; +pub const FRAMES_MAX = 64; +pub const STACK_MAX = (FRAMES_MAX * UINT8_MAX); + +pub const DEBUG_PRINT_CODE = true; +pub const DEBUG_TRACE_EXECUTION = true; +pub const DEBUG_PRINT_INTERNAL_STRINGS = false; +pub const DEBUG_PRINT_GLOBALS = false; diff --git a/src/main.zig b/src/main.zig index 945ec7d..e919faa 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,8 @@ const std = @import("std"); const debug = std.debug; const Allocator = std.mem.Allocator; +const constants = @import("./constant.zig"); + const Chunk = @import("./chunk.zig").Chunk; const OpCode = @import("./opcode.zig").OpCode; const VM = @import("./vm.zig").VM; @@ -17,7 +19,7 @@ pub fn repl(allocator: Allocator, vm: *VM) !void { const stdout = std.io.getStdOut().writer(); while (true) { - if (vm.has_tracing()) { + if (constants.DEBUG_PRINT_GLOBALS) { vm.globals.dump(); } @@ -63,12 +65,6 @@ pub fn main() !void { var vm = VM.new(allocator); defer vm.free(); - var env = try std.process.getEnvMap(allocator); - defer env.deinit(); - if (env.get("TRACE") != null) { - vm.set_trace(true); - } - if (args.len == 1) { try repl(allocator, &vm); } else if (args.len == 2) { diff --git a/src/object.zig b/src/object.zig index 0192d2d..38e8297 100644 --- a/src/object.zig +++ b/src/object.zig @@ -45,8 +45,8 @@ pub const Obj = struct { pub const Function = struct { obj: Obj, arity: usize, - chunk: *Chunk, - name: *Obj.String, + chunk: Chunk, + name: ?*Obj.String, pub fn new(allocator: std.mem.Allocator) *Function { const obj = Obj{ @@ -58,6 +58,7 @@ pub const Obj = struct { function_obj.obj = obj; function_obj.arity = 0; function_obj.chunk = Chunk.new(allocator); + function_obj.name = null; return function_obj; } @@ -89,7 +90,11 @@ pub const Obj = struct { }, ObjType.Function => { const obj = self.as_function(); - debug.print("", .{obj.name.chars}); + if (obj.name == null) { + debug.print("