From a9459d564baeb0b84693acac09435883a46dd87f Mon Sep 17 00:00:00 2001 From: chloe caruso Date: Mon, 27 May 2024 20:49:10 -0700 Subject: [PATCH] move compiler->core, add lsp binary --- build.zig | 164 ++++++++++++++--------- src/{compiler => core}/Air.Dump.zig | 0 src/{compiler => core}/Air.zig | 0 src/{compiler => core}/Ast.Dump.zig | 0 src/{compiler => core}/Ast.zig | 0 src/{compiler => core}/Cache.zig | 0 src/{compiler => core}/Comp.zig | 0 src/{compiler => core}/InternPool.zig | 0 src/{compiler => core}/JsLink.zig | 0 src/{compiler => core}/JsNodeGen.zig | 0 src/{compiler => core}/JsTree.zig | 0 src/{compiler => core}/Parse.zig | 0 src/{compiler => core}/Report.zig | 0 src/{compiler => core}/Sema.zig | 0 src/{compiler => core}/Teg.Dump.zig | 0 src/{compiler => core}/Teg.zig | 0 src/{compiler => core}/Tokenizer.zig | 0 src/{compiler => core}/Transform.zig | 0 src/{compiler => core}/array.zig | 0 src/{compiler => core}/main.zig | 23 ++-- src/core/mod.zig | 14 ++ src/{compiler => core}/unicode_props.zig | 4 +- src/{compiler => core}/util.zig | 0 src/lsp/lsp.zig | 41 ++++++ 24 files changed, 171 insertions(+), 75 deletions(-) rename src/{compiler => core}/Air.Dump.zig (100%) rename src/{compiler => core}/Air.zig (100%) rename src/{compiler => core}/Ast.Dump.zig (100%) rename src/{compiler => core}/Ast.zig (100%) rename src/{compiler => core}/Cache.zig (100%) rename src/{compiler => core}/Comp.zig (100%) rename src/{compiler => core}/InternPool.zig (100%) rename src/{compiler => core}/JsLink.zig (100%) rename src/{compiler => core}/JsNodeGen.zig (100%) rename src/{compiler => core}/JsTree.zig (100%) rename src/{compiler => core}/Parse.zig (100%) rename src/{compiler => core}/Report.zig (100%) rename src/{compiler => core}/Sema.zig (100%) rename src/{compiler => core}/Teg.Dump.zig (100%) rename src/{compiler => core}/Teg.zig (100%) rename src/{compiler => core}/Tokenizer.zig (100%) rename src/{compiler => core}/Transform.zig (100%) rename src/{compiler => core}/array.zig (100%) rename src/{compiler => core}/main.zig (89%) create mode 100644 src/core/mod.zig rename src/{compiler => core}/unicode_props.zig (97%) rename src/{compiler => core}/util.zig (100%) create mode 100644 src/lsp/lsp.zig diff --git a/build.zig b/build.zig index a66998bce30b70508240713d6052815013e3e2c3..33a9d274adf43ac2ae78886bc9817abd140bdbfb 100644 --- a/build.zig +++ b/build.zig @@ -19,47 +19,29 @@ pub fn build(b: *std.Build) void { }, }; - const tracing = b.option(enum { - none, - spall, - chrome, - log, - }, "trace", "Enable tracing") orelse .none; + const tracing = b.option( + TracingMode, + "trace", + "Enable tracing", + ) orelse .none; - const dep_opts = .{ .target = target, .optimize = optimize }; + const main = buildWithTarget(b, target, optimize, tracing); - const zg = b.dependency("zg", dep_opts); - const tracer = b.dependency("tracer", dep_opts); - // const ada = b.dependency("ada", dep_opts); + inline for (.{ + .{ main.compiler, "compiler" }, + .{ main.lsp, "lsp" }, + }) |entry| { + const artifact, const name = entry; - const opts = b.addOptions(); - opts.addOption([]const u8, "tracing", @tagName(tracing)); - const opts_mod = opts.createModule(); - - const exe = b.addExecutable(.{ - .name = "compiler", - .root_source_file = b.path("src/compiler/main.zig"), - .target = target, - .optimize = optimize, - .link_libc = true, - }); - b.installArtifact(exe); - - exe.root_module.addImport("build_options", opts_mod); - exe.root_module.addImport("zg:PropsData", zg.module("PropsData")); - exe.root_module.addImport("tracer", tracer.module("tracer")); - // if (!target.result.isWasm()) { - // exe.root_module.addImport("ada", ada.module("ada")); - // } - - { - const run_cmd = b.addRunArtifact(exe); - run_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| { - run_cmd.addArgs(args); + { + const run_cmd = b.addRunArtifact(artifact); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + const run_step = b.step(name, "Run " ++ name ++ " binary"); + run_step.dependOn(&run_cmd.step); } - const run_step = b.step("compiler", "Run compiler CLI"); - run_step.dependOn(&run_cmd.step); } // const wasm_api = b.addExecutable(.{ @@ -102,30 +84,90 @@ pub fn build(b: *std.Build) void { }, ) catch @panic("OOM"); - // std.log.info("{s}\n", .{cross_target.result.zigTriple(b.allocator) catch @panic("OOM")}); + const artifacts = buildWithTarget(b, cross_target, .ReleaseSafe, .none); - const cross_exe = b.addExecutable(.{ - .name = "compiler", - .root_source_file = b.path("src/compiler/main.zig"), - .target = cross_target, - .optimize = .ReleaseSafe, - .strip = true, - .link_libc = true, - }); - cross_exe.root_module.addImport("build_options", opts_mod); - cross_exe.root_module.addImport("zg:PropsData", zg.module("PropsData")); - cross_exe.root_module.addImport("tracer", tracer.module("tracer")); - - release.dependOn(&b.addInstallFile( - cross_exe.getEmittedBin(), - b.pathJoin(&.{ - "release", - name, - std.mem.concat(b.allocator, u8, &.{ - "compiler", - cross_target.result.exeFileExt(), - }) catch @panic("OOM"), - }), - ).step); + inline for (.{ + .{ artifacts.compiler, "compiler" }, + .{ artifacts.lsp, "lsp" }, + }) |entry| { + const artifact, const bin_name = entry; + release.dependOn(&b.addInstallFile( + artifact.getEmittedBin(), + b.pathJoin(&.{ + "release", + name, + std.mem.concat(b.allocator, u8, &.{ + bin_name, + cross_target.result.exeFileExt(), + }) catch @panic("OOM"), + }), + ).step); + } } } + +const TracingMode = enum { none, spall, chrome, log }; + +const Artifacts = struct { + core: *Build.Module, + compiler: *Build.Step.Compile, + lsp: *Build.Step.Compile, +}; + +fn buildWithTarget( + b: *Build, + target: Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + tracing: TracingMode, +) Artifacts { + const dep_opts = .{ .target = target, .optimize = optimize }; + + // Dependencies + const zg = b.dependency("zg", dep_opts); + const tracer = b.dependency("tracer", dep_opts); + + // Options + const opts = b.addOptions(); + opts.addOption([]const u8, "tracing", @tagName(tracing)); + const opts_mod = opts.createModule(); + + // Core + const core = b.addModule("compiler", .{ + .root_source_file = b.path("src/core/mod.zig"), + .optimize = optimize, + .target = target, + }); + core.addImport("build_options", opts_mod); + core.addImport("tracer", tracer.module("tracer")); + core.addImport("zg:PropsData", zg.module("PropsData")); + + // Compiler Testing Exe + const compiler_exe = b.addExecutable(.{ + .name = "compiler", + .root_source_file = b.path("src/core/main.zig"), + .target = target, + .optimize = optimize, + .link_libc = true, + }); + compiler_exe.root_module.addImport("build_options", opts_mod); + compiler_exe.root_module.addImport("core", core); + compiler_exe.root_module.addImport("tracer", tracer.module("tracer")); + + // Language Server + const lsp_exe = b.addExecutable(.{ + .name = "lsp", + .root_source_file = b.path("src/lsp/lsp.zig"), + .target = target, + .optimize = optimize, + .link_libc = true, + }); + lsp_exe.root_module.addImport("build_options", opts_mod); + lsp_exe.root_module.addImport("core", core); + lsp_exe.root_module.addImport("tracer", tracer.module("tracer")); + + return .{ + .core = core, + .compiler = compiler_exe, + .lsp = lsp_exe, + }; +} diff --git a/src/compiler/Air.Dump.zig b/src/core/Air.Dump.zig similarity index 100% rename from src/compiler/Air.Dump.zig rename to src/core/Air.Dump.zig diff --git a/src/compiler/Air.zig b/src/core/Air.zig similarity index 100% rename from src/compiler/Air.zig rename to src/core/Air.zig diff --git a/src/compiler/Ast.Dump.zig b/src/core/Ast.Dump.zig similarity index 100% rename from src/compiler/Ast.Dump.zig rename to src/core/Ast.Dump.zig diff --git a/src/compiler/Ast.zig b/src/core/Ast.zig similarity index 100% rename from src/compiler/Ast.zig rename to src/core/Ast.zig diff --git a/src/compiler/Cache.zig b/src/core/Cache.zig similarity index 100% rename from src/compiler/Cache.zig rename to src/core/Cache.zig diff --git a/src/compiler/Comp.zig b/src/core/Comp.zig similarity index 100% rename from src/compiler/Comp.zig rename to src/core/Comp.zig diff --git a/src/compiler/InternPool.zig b/src/core/InternPool.zig similarity index 100% rename from src/compiler/InternPool.zig rename to src/core/InternPool.zig diff --git a/src/compiler/JsLink.zig b/src/core/JsLink.zig similarity index 100% rename from src/compiler/JsLink.zig rename to src/core/JsLink.zig diff --git a/src/compiler/JsNodeGen.zig b/src/core/JsNodeGen.zig similarity index 100% rename from src/compiler/JsNodeGen.zig rename to src/core/JsNodeGen.zig diff --git a/src/compiler/JsTree.zig b/src/core/JsTree.zig similarity index 100% rename from src/compiler/JsTree.zig rename to src/core/JsTree.zig diff --git a/src/compiler/Parse.zig b/src/core/Parse.zig similarity index 100% rename from src/compiler/Parse.zig rename to src/core/Parse.zig diff --git a/src/compiler/Report.zig b/src/core/Report.zig similarity index 100% rename from src/compiler/Report.zig rename to src/core/Report.zig diff --git a/src/compiler/Sema.zig b/src/core/Sema.zig similarity index 100% rename from src/compiler/Sema.zig rename to src/core/Sema.zig diff --git a/src/compiler/Teg.Dump.zig b/src/core/Teg.Dump.zig similarity index 100% rename from src/compiler/Teg.Dump.zig rename to src/core/Teg.Dump.zig diff --git a/src/compiler/Teg.zig b/src/core/Teg.zig similarity index 100% rename from src/compiler/Teg.zig rename to src/core/Teg.zig diff --git a/src/compiler/Tokenizer.zig b/src/core/Tokenizer.zig similarity index 100% rename from src/compiler/Tokenizer.zig rename to src/core/Tokenizer.zig diff --git a/src/compiler/Transform.zig b/src/core/Transform.zig similarity index 100% rename from src/compiler/Transform.zig rename to src/core/Transform.zig diff --git a/src/compiler/array.zig b/src/core/array.zig similarity index 100% rename from src/compiler/array.zig rename to src/core/array.zig diff --git a/src/compiler/main.zig b/src/core/main.zig similarity index 89% rename from src/compiler/main.zig rename to src/core/main.zig index ef8964f9cb66b4fdcee80059967ce6fcbb9b0a1d..b7f63ed496648a3de79e35cd668ca74eabcf1155 100644 --- a/src/compiler/main.zig +++ b/src/core/main.zig @@ -1,18 +1,19 @@ +//! This file is used to test the compiler implementation without +//! needing to build the language server and other tooling. const std = @import("std"); const builtin = @import("builtin"); +const core = @import("core"); -const Tokenizer = @import("Tokenizer.zig"); -const Parse = @import("Parse.zig"); -const Transform = @import("Transform.zig"); -const Comp = @import("Comp.zig"); -const unicode_props = @import("unicode_props.zig"); +const Tokenizer = core.Tokenizer; +const Parse = core.Parse; +const Transform = core.Transform; +const Comp = core.Comp; +const unicode_props = core.unicode_props; +pub const Report = core.Report; const tracer = @import("tracer"); - pub const tracer_impl = @field(tracer, @import("build_options").tracing); -pub const Report = @import("Report.zig"); - pub fn main() !void { try tracer.init(); defer tracer.deinit(); @@ -22,17 +23,15 @@ pub fn main() !void { try unicode_props.init(); if (builtin.mode == .Debug) { + // zig build system doesnt print a newline :/ std.debug.print("\r\x1b[K", .{}); } const use_gpa = builtin.mode == .Debug; - var gpa: if (use_gpa) std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = 100, }) else struct {} = .{}; - defer if (use_gpa) { - _ = gpa.deinit(); - }; + defer _ = if (use_gpa) gpa.deinit(); const alloc = if (use_gpa) gpa.allocator() else diff --git a/src/core/mod.zig b/src/core/mod.zig new file mode 100644 index 0000000000000000000000000000000000000000..77c4714c40beba307c4ee71b5d30cfa0b69867a9 --- /dev/null +++ b/src/core/mod.zig @@ -0,0 +1,14 @@ +pub const Air = @import("Air.zig"); +pub const array = @import("array.zig"); +pub const Cache = @import("Cache.zig"); +pub const Comp = @import("Comp.zig"); +pub const InternPool = @import("InternPool.zig"); +pub const JsLink = @import("JsLink.zig"); +pub const Parse = @import("Parse.zig"); +pub const Report = @import("Report.zig"); +pub const Sema = @import("Sema.zig"); +pub const Teg = @import("Teg.zig"); +pub const Tokenizer = @import("Tokenizer.zig"); +pub const Transform = @import("Transform.zig"); +pub const unicode_props = @import("unicode_props.zig"); +pub const util = @import("util.zig"); diff --git a/src/compiler/unicode_props.zig b/src/core/unicode_props.zig similarity index 97% rename from src/compiler/unicode_props.zig rename to src/core/unicode_props.zig index f7e2f98f9686de6527a169bb37435880a7a890e9..2a71ff3f46182203a77145f6f7df67868db3d271 100644 --- a/src/compiler/unicode_props.zig +++ b/src/core/unicode_props.zig @@ -1,7 +1,7 @@ //! Wrapper around the 'zg' PropsData library. const std = @import("std"); const PropsData = @import("zg:PropsData"); -const T = @import("tracer"); +const tracer = @import("tracer"); var props: ?PropsData = undefined; var props_memory: []u8 = undefined; @@ -14,7 +14,7 @@ else pub fn init() !void { if (props != null) return; - const trace = T.trace(@src()); + const trace = tracer.trace(@src()); defer trace.end(); // We know exactly how much this uses, so let's preallocate these pages and use a FBA. diff --git a/src/compiler/util.zig b/src/core/util.zig similarity index 100% rename from src/compiler/util.zig rename to src/core/util.zig diff --git a/src/lsp/lsp.zig b/src/lsp/lsp.zig new file mode 100644 index 0000000000000000000000000000000000000000..340c01d5d3878669148836d67c7515697cf04ccb --- /dev/null +++ b/src/lsp/lsp.zig @@ -0,0 +1,41 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const core = @import("core"); +const tracer = @import("tracer"); + +pub const tracer_impl = @field(tracer, @import("build_options").tracing); + +pub fn main() !void { + if (builtin.mode == .Debug) { + // zig build system doesnt print a newline :/ + std.debug.print("\r\x1b[K", .{}); + } + + std.debug.print("All your codebase are belong to us\n", .{}); + + const use_gpa = builtin.mode == .Debug; + var gpa_state: if (use_gpa) std.heap.GeneralPurposeAllocator(.{ + .stack_trace_frames = 100, + }) else struct {} = .{}; + defer _ = if (use_gpa) gpa_state.deinit(); + const gpa = if (use_gpa) + gpa_state.allocator() + else + std.heap.c_allocator; + + // if `parse` returns `error.ParseFail`, this `err` is set. + // + // at some point later this will change to be a `Report` + // so that multiple parse errors may be reported at once. + var err: core.Parse.AstError.WithTrace = undefined; + var ast = try core.Parse.parse(gpa, + \\let x = 1; + \\ + \\pub fn hello() -> float { + \\ 42.2 + \\} + , &err); + defer ast.deinit(gpa); + + try ast.dumpToWriter(std.io.getStdErr().writer()); +} -- 2.54.0