diff --git a/build.zig b/build.zig index 4030430..d569b95 100644 --- a/build.zig +++ b/build.zig @@ -37,6 +37,17 @@ pub fn build(b: *std.Build) void { error_module.addImport("config", options_module); exe.root_module.addImport("errors", error_module); + // + // Context module + // + const context_module = b.addModule("context", .{ + .root_source_file = b.path("src/context/root.zig"), + .target = target, + .optimize = optimize, + }); + context_module.addImport("config", options_module); + exe.root_module.addImport("context", context_module); + // // Lexic module // @@ -47,6 +58,7 @@ pub fn build(b: *std.Build) void { }); lexic_module.addImport("config", options_module); lexic_module.addImport("errors", error_module); + lexic_module.addImport("context", context_module); exe.root_module.addImport("lexic", lexic_module); // @@ -60,6 +72,7 @@ pub fn build(b: *std.Build) void { syntax_module.addImport("config", options_module); syntax_module.addImport("lexic", lexic_module); syntax_module.addImport("errors", error_module); + syntax_module.addImport("context", context_module); exe.root_module.addImport("syntax", syntax_module); // Install step @@ -88,6 +101,7 @@ pub fn build(b: *std.Build) void { exe_unit_tests.root_module.addImport("lexic", lexic_module); exe_unit_tests.root_module.addImport("syntax", syntax_module); exe_unit_tests.root_module.addImport("errors", error_module); + exe_unit_tests.root_module.addImport("context", context_module); const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); @@ -110,6 +124,7 @@ pub fn build(b: *std.Build) void { file_unit_test.root_module.addImport("lexic", lexic_module); file_unit_test.root_module.addImport("syntax", syntax_module); file_unit_test.root_module.addImport("errors", error_module); + file_unit_test.root_module.addImport("context", context_module); var test_artifact = b.addRunArtifact(file_unit_test); test_step.dependOn(&test_artifact.step); diff --git a/src/context/root.zig b/src/context/root.zig new file mode 100644 index 0000000..2c7db59 --- /dev/null +++ b/src/context/root.zig @@ -0,0 +1,41 @@ +const std = @import("std"); + +/// Compiler wide state about. +/// For now only stores errors generated +pub const CompilerContext = struct { + allocator: std.mem.Allocator, + errors: std.ArrayList(ErrorData), + + pub fn init(allocator: std.mem.Allocator) CompilerContext { + return .{ + .allocator = allocator, + .errors = std.ArrayList(ErrorData).init(allocator), + }; + } + + pub fn deinit(self: *CompilerContext) void { + self.errors.deinit(); + } +}; + +pub const ErrorData = struct { + /// A high level reason why the error occured + reason: []const u8, + /// A message with direct instructions to solve the error + help: ?[]const u8, + /// The absolute position from where the faulty code starts + start_position: usize, + /// The absolute position where the faulty code ends + end_position: usize, + /// A list of detailed messages about the error + labels: std.ArrayList(ErrorLabel), +}; + +pub const ErrorLabel = struct { + message: union(enum) { + static: []const u8, + dynamic: []u8, + }, + start: usize, + end: usize, +}; diff --git a/src/main.zig b/src/main.zig index e80c7bf..1ca2def 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); const lexic = @import("lexic"); const syntax = @import("syntax"); const errors = @import("errors"); +const context = @import("context"); const cli = @import("cli.zig"); @@ -69,6 +70,10 @@ fn repl() !void { defer std.heap.page_allocator.free(bare_line); const line = std.mem.trim(u8, bare_line, "\r"); + // Setup compiler context + var ctx = context.CompilerContext.init(alloc); + defer ctx.deinit(); + // // Tokenize //