From bc81dac54dbb5d75534d8d954e876fedec920023 Mon Sep 17 00:00:00 2001 From: Don Isaac Date: Sun, 3 Nov 2024 22:56:06 -0500 Subject: [PATCH] fix(semantic): create root symbol for @This() --- .vscode/launch.json | 15 ++++++++++----- build.zig | 3 +++ src/semantic.zig | 5 +++++ src/semantic/Semantic.zig | 1 + 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 57255d6..3fd8d60 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,13 +6,11 @@ "configurations": [ { "name": "zlint", - "type": "lldb-mi", + "type": "lldb", "request": "launch", - "target": "./zig-out/bin/zlint", + "program": "./zig-out/bin/zlint", "cwd": "${workspaceRoot}", - "valuesFormatting": "parseText" }, - { "name": "zlint --print-ast", "type": "lldb", @@ -21,6 +19,13 @@ "program": "${workspaceRoot}/zig-out/bin/zlint", "cwd": "${workspaceRoot}", "preLaunchTask": "build" - } + }, + { + "name": "test-e2e", + "type": "lldb", + "request": "launch", + "program": "./zig-out/bin/test-e2e", + "cwd": "${workspaceRoot}", + }, ] } diff --git a/build.zig b/build.zig index bf93263..39ce8dd 100644 --- a/build.zig +++ b/build.zig @@ -128,15 +128,18 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); e2e_tests.root_module.addImport("zlint", zlint); + b.installArtifact(e2e_tests); // e2e_tests.linkLibrary(lib); // e2e_tests. const run_e2e_tests = b.addRunArtifact(e2e_tests); + run_e2e_tests.step.dependOn((b.getInstallStep())); if (b.args) |args| { run_e2e_tests.addArgs(args); } const e2e_step = b.step("test-e2e", "Run end-to-end tests"); + e2e_step.dependOn(&lib.step); e2e_step.dependOn(&run_e2e_tests.step); } diff --git a/src/semantic.zig b/src/semantic.zig index 43609ea..acedbf0 100644 --- a/src/semantic.zig +++ b/src/semantic.zig @@ -64,6 +64,8 @@ pub const Builder = struct { // initialize root scope try builder.enterRootScope(); builder.assertRootScope(); // sanity check + const root_symbol_id = try builder.declareSymbol(Semantic.ROOT_NODE_ID, "@This()", .public, .{ .s_const = true }); + try builder.enterContainerSymbol(root_symbol_id); // Zig guarantees that the root node ID is 0. We should be careful- they may decide to change this contract. // if (builtin.mode == .Debug) { @@ -312,6 +314,9 @@ pub const Builder = struct { /// Unconditionally get the most recent container symbol. Panics if no /// symbol has been entered. inline fn currentContainerSymbolUnwrap(self: *const Builder) Symbol.Id { + if (IS_DEBUG and self._symbol_stack.items.len == 0) { + std.debug.panic("Cannot get current container symbol: symbol stack is empty", .{}); + } return self._symbol_stack.getLast(); } diff --git a/src/semantic/Semantic.zig b/src/semantic/Semantic.zig index dff35af..256b984 100644 --- a/src/semantic/Semantic.zig +++ b/src/semantic/Semantic.zig @@ -21,6 +21,7 @@ _gpa: Allocator, _arena: ArenaAllocator, pub const ROOT_SCOPE_ID: Scope.Id = 0; +pub const ROOT_NODE_ID: Ast.Node.Index = 0; pub fn deinit(self: *Semantic) void { // NOTE: ast is arena allocated, so no need to deinit it. freeing the arena