refactor: introduce compiler context
This commit is contained in:
parent
3a97e59886
commit
e695777f07
15
build.zig
15
build.zig
@ -37,6 +37,17 @@ pub fn build(b: *std.Build) void {
|
|||||||
error_module.addImport("config", options_module);
|
error_module.addImport("config", options_module);
|
||||||
exe.root_module.addImport("errors", error_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
|
// Lexic module
|
||||||
//
|
//
|
||||||
@ -47,6 +58,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
});
|
});
|
||||||
lexic_module.addImport("config", options_module);
|
lexic_module.addImport("config", options_module);
|
||||||
lexic_module.addImport("errors", error_module);
|
lexic_module.addImport("errors", error_module);
|
||||||
|
lexic_module.addImport("context", context_module);
|
||||||
exe.root_module.addImport("lexic", lexic_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("config", options_module);
|
||||||
syntax_module.addImport("lexic", lexic_module);
|
syntax_module.addImport("lexic", lexic_module);
|
||||||
syntax_module.addImport("errors", error_module);
|
syntax_module.addImport("errors", error_module);
|
||||||
|
syntax_module.addImport("context", context_module);
|
||||||
exe.root_module.addImport("syntax", syntax_module);
|
exe.root_module.addImport("syntax", syntax_module);
|
||||||
|
|
||||||
// Install step
|
// 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("lexic", lexic_module);
|
||||||
exe_unit_tests.root_module.addImport("syntax", syntax_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("errors", error_module);
|
||||||
|
exe_unit_tests.root_module.addImport("context", context_module);
|
||||||
|
|
||||||
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
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("lexic", lexic_module);
|
||||||
file_unit_test.root_module.addImport("syntax", syntax_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("errors", error_module);
|
||||||
|
file_unit_test.root_module.addImport("context", context_module);
|
||||||
|
|
||||||
var test_artifact = b.addRunArtifact(file_unit_test);
|
var test_artifact = b.addRunArtifact(file_unit_test);
|
||||||
test_step.dependOn(&test_artifact.step);
|
test_step.dependOn(&test_artifact.step);
|
||||||
|
41
src/context/root.zig
Normal file
41
src/context/root.zig
Normal file
@ -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,
|
||||||
|
};
|
@ -2,6 +2,7 @@ const std = @import("std");
|
|||||||
const lexic = @import("lexic");
|
const lexic = @import("lexic");
|
||||||
const syntax = @import("syntax");
|
const syntax = @import("syntax");
|
||||||
const errors = @import("errors");
|
const errors = @import("errors");
|
||||||
|
const context = @import("context");
|
||||||
|
|
||||||
const cli = @import("cli.zig");
|
const cli = @import("cli.zig");
|
||||||
|
|
||||||
@ -69,6 +70,10 @@ fn repl() !void {
|
|||||||
defer std.heap.page_allocator.free(bare_line);
|
defer std.heap.page_allocator.free(bare_line);
|
||||||
const line = std.mem.trim(u8, bare_line, "\r");
|
const line = std.mem.trim(u8, bare_line, "\r");
|
||||||
|
|
||||||
|
// Setup compiler context
|
||||||
|
var ctx = context.CompilerContext.init(alloc);
|
||||||
|
defer ctx.deinit();
|
||||||
|
|
||||||
//
|
//
|
||||||
// Tokenize
|
// Tokenize
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user