From 0de15e008bff7e61b1902a1ceb92b8dcef20be05 Mon Sep 17 00:00:00 2001 From: Peterino Date: Tue, 1 Dec 2026 21:21:38 -0700 Subject: [PATCH] Saving before push --- engine/build.zig | 2 +- engine/src/core/api.tad | 27 +++++++- engine/tools/generate-api.zig | 2 +- engine/tools/tad/tad.zig | 105 ++++++++++++++++++++++++++--- engine/tools/tad/zig-generator.zig | 48 +++++++------ roadmap.md | 4 ++ 6 files changed, 157 insertions(+), 31 deletions(-) diff --git a/engine/build.zig b/engine/build.zig index 14e0f7b..1bc11e8 100644 --- a/engine/build.zig +++ b/engine/build.zig @@ -29,7 +29,7 @@ pub fn build(b: *std.Build) void { }), .name = "parser", }); - + parser.root_module.addImport("p2", p2.module("p2")); const parse_interface_core = b.addRunArtifact(parser); parse_interface_core.addFileArg2(b.path("src/core/api.tad"), .{ .prefix = "--input-file=" }); diff --git a/engine/src/core/api.tad b/engine/src/core/api.tad index cefbe5e..03475f2 100644 --- a/engine/src/core/api.tad +++ b/engine/src/core/api.tad @@ -20,7 +20,10 @@ # moduleoptions := label string | integer | label # # functiondef := "fn" label "->" [([pointertype] builtin) | "void"] [field+] -# +# +# new addition december 1st; +# `alias` keyword +# `fnptr` keyword module core @import platform # platform isn't real it can't hurt you @@ -37,10 +40,22 @@ struct SliceMutable ptr: c_ptr u8 len: usize + struct Slice ptr: const_c_ptr u8 len: usize +struct Vec2 + x: u32 + y: u32 + +alias Size = Vec2 +alias Point = Vec2 + +struct Rect + pos: Point + size: Size + fn get_engine -> c_ptr opaque fn get_tag_registry -> c_ptr opaque fn start -> void @@ -55,6 +70,16 @@ fn log_puts -> void verbosity: Verbosity string: Slice + +# define a function type +fnptr on_log -> void + verbosity: Verbosity + slice: Slice + +struct callback + func: fnptr onlog # gets called every time a log is called + + # todo: constants ... # # This will need a 3rd pass of the parser... preferrably maybe with a bit of a parser generator diff --git a/engine/tools/generate-api.zig b/engine/tools/generate-api.zig index 48fd449..90a8906 100644 --- a/engine/tools/generate-api.zig +++ b/engine/tools/generate-api.zig @@ -28,7 +28,7 @@ pub fn main(init: std.process.Init) !void { if (AP.cliArgParse(a, "input-file", "input .tad file to parse")) |arg| { log.info("parsing input file: {s}", .{arg}); - try tad.compile(.{ + try tad.compileAll(.{ .io = init.io, .allocator = init.arena.allocator(), .path = arg, diff --git a/engine/tools/tad/tad.zig b/engine/tools/tad/tad.zig index 3e60260..e8c72bd 100644 --- a/engine/tools/tad/tad.zig +++ b/engine/tools/tad/tad.zig @@ -29,9 +29,10 @@ const FunctionDefinition = struct { name: []const u8, args: std.ArrayList(Field) = .empty, returnType: TypeRef, + isInstance: bool, // if true this function definition is a function pointer type }; -const TypeRef = struct { +pub const TypeRef = struct { lineRef: u32, tokens: []Token, @@ -80,7 +81,7 @@ fn parseInt(x: []const u8) ?i64 { return std.fmt.parseInt(i64, x, 0) catch null; } -const Keyword = enum { @"fn", @"struct", @"enum", module }; +const Keyword = enum { @"fn", @"struct", @"enum", module, fnptr, alias }; const TokenKind = union(enum(u8)) { term: TerminalKind, @@ -203,6 +204,7 @@ pub const Module = struct { structs: ArrayList(StructDefinition) = .empty, functions: ArrayList(FunctionDefinition) = .empty, module: ?ModuleDefinition = null, + typeRefs: ArrayList(TypeRef), gpa: std.mem.Allocator, lines: []Line, @@ -466,6 +468,7 @@ pub const Module = struct { }; for (body) |l| { + if (l.tokens.len == 0) continue; switch (l.tokens[0].tokenKind) { .directive => return error.UnexpectedDirective, // can easily add directives support here .label => { @@ -500,6 +503,7 @@ pub const Module = struct { var nextValue: i64 = 0; for (body) |l| { + if (l.tokens.len == 0) continue; switch (l.tokens[0].tokenKind) { .directive => |dir| { switch (dir) { @@ -543,7 +547,7 @@ pub const Module = struct { fn parseFunction(self: @This(), cursor: *u32) !FunctionDefinition { const lineRef = cursor.*; - const line, const body = try self.setupBodyState(cursor, .@"fn"); + const line, const body, const isInstance = try self.setupFunctionBodyState(cursor); if (line.tokens.len < 4) return error.MalformedFunctionDefinition; if (line.tokens[1].tokenKind != .label) return error.ExpectedLabel; try expectTerminal(line.tokens[2], .arrow); @@ -551,9 +555,11 @@ pub const Module = struct { var func: FunctionDefinition = .{ .name = line.tokens[1].slice(line.contents), .returnType = .{ .lineRef = lineRef, .tokens = line.tokens[3..] }, + .isInstance = isInstance, }; for (body) |l| { + if (l.tokens.len == 0) continue; switch (l.tokens[0].tokenKind) { .directive => return error.UnexpectedDirective, // can easily add directives support here .label => { @@ -573,6 +579,27 @@ pub const Module = struct { return func; } + fn setupFunctionBodyState(self: @This(), cursor: *u32) !struct { Line, []Line, bool } { + const lines = self.lines[cursor.*..]; + if (lines[0].tokens[0].tokenKind != .keyword) { + return error.ExpectedKeyword; + } + + var isInstance: bool = false; + + // zig fmt: off + if(lines[0].tokens[0].tokenKind.keyword == .@"fn") + { + if(lines[0].tokens[0].tokenKind.keyword == .fnptr) + isInstance = true; + } + // zig fmt: on + const indent = lines[0].indent + 1; + const body = readBody(lines[1..], indent); + cursor.* += @as(u32, @intCast(body.len)); + return .{ lines[0], body, isInstance }; + } + fn setupBodyState(self: @This(), cursor: *u32, keyword: Keyword) !struct { Line, []Line } { const lines = self.lines[cursor.*..]; try expectKeyword(lines[0].tokens[0], keyword); @@ -595,6 +622,7 @@ pub const Module = struct { const line, const body = try self.setupBodyState(cursor, .module); var mod: ModuleDefinition = .{ .name = line.tokens[0].slice(line.contents) }; for (body) |l| { + if (l.tokens.len == 0) continue; switch (l.tokens[0].tokenKind) { .directive => |d| { switch (d) { @@ -626,10 +654,17 @@ pub const Module = struct { }, .keyword => |kw| { switch (kw) { + .alias => { + // dont do anything right now + }, .@"enum" => { newdef = .{ .@"enum" = try self.parseEnum(cursor) }; try self.enums.append(self.gpa, newdef.@"enum"); }, + .fnptr => { + newdef = .{ .@"fn" = try self.parseFunction(cursor) }; + try self.functions.append(self.gpa, newdef.@"fn"); + }, .@"fn" => { newdef = .{ .@"fn" = try self.parseFunction(cursor) }; try self.functions.append(self.gpa, newdef.@"fn"); @@ -684,23 +719,75 @@ pub const Module = struct { } }; +const ModuleId = u32; +pub const PointerId = u32; +pub const EnumId = u32; +pub const StructId = u32; + +pub const TokensLink = struct { + module: u32, + id: u32, +}; + +pub const StructNode = struct { + name: []const u8, + fields: std.ArrayList(Field), +}; + +pub const EnumNode = struct { + name: []const u8, + values: std.ArrayList(EnumValue), +}; + +pub const AliasNode = struct {}; + +pub const FunctionNode = struct {}; + +pub const StoreNode = struct { + id: u32, // index in the nodes paged vector + link: TokensLink, + node: union(enum(u8)) { + @"struct": StructNode, + @"enum": EnumNode, + alias: AliasNode, + function: FunctionNode, + }, +}; + +// new store operator +pub const Store = struct { + gpa: std.mem.Allocator, + nodes: p2.PagedVectorUnmanaged(StoreNode) = .empty, + + pub fn init(self: *@This(), gpa: std.mem.Allocator) !void { + self.* = .{ + .gpa = gpa, + }; + } +}; + pub const TadModuleOptions = struct { io: std.Io, allocator: std.mem.Allocator, path: []const u8, }; -pub fn compile(opts: TadModuleOptions) !void { +pub fn compileAll(opts: TadModuleOptions) !void { const rawf = try std.Io.Dir.cwd().readFileAlloc(opts.io, opts.path, opts.allocator, .unlimited); const normalized = normalizeContents(rawf); var arena = std.heap.ArenaAllocator.init(opts.allocator); defer arena.deinit(); + var store: Store = undefined; + try store.init(arena.allocator(), opts.path, normalized); + var mod: Module = undefined; try mod.lex(arena.allocator(), opts.path, normalized); try mod.parse(); + // parse submodules here... + // generate try mod.generateZigApi(.{ .io = opts.io, .output = "./zig-out/api/core.zig" }); @@ -709,10 +796,10 @@ pub fn compile(opts: TadModuleOptions) !void { const log_tokenizer = std.log.scoped(.tad_tok); const log_main = std.log.scoped(.tad); -pub const logScopeLevels: []const std.log.ScopeLevel = &.{ - .{ .scope = .tad, .level = .warn }, - .{ .scope = .tad_tok, .level = .warn }, -}; +pub const logScopeLevels: []const std.log.ScopeLevel = &[_]std.log.ScopeLevel{ + .{ .scope = .tad, .level = .debug }, + .{ .scope = .tad_tok, .level = .debug }, +} ++ zig_generator.logScopeLevels; const std = @import("std"); const ArrayList = std.ArrayList; @@ -720,4 +807,6 @@ const mem = std.mem; const WriteBuffer = @import("../common/WriteBuffer.zig"); const fmt = std.fmt; +const p2 = @import("p2"); + const zig_generator = @import("zig-generator.zig"); diff --git a/engine/tools/tad/zig-generator.zig b/engine/tools/tad/zig-generator.zig index 5616e5d..b45739d 100644 --- a/engine/tools/tad/zig-generator.zig +++ b/engine/tools/tad/zig-generator.zig @@ -1,39 +1,39 @@ pub fn generateApi(self: *Module, w: *std.Io.Writer) !void { try w.print("// hello world \n", .{}); - for (0..self.enums.items) |i| { + for (0..self.enums.items.len) |i| { try generateEnum(self, w, i); } - for (0..self.structs.items) |i| - { + for (0..self.structs.items.len) |i| { try generateStruct(self, w, i); } } -fn pointerTypeLookup(x: []const u8) []const u8 -{ - if() - { - +// writes into the buffer, allocates +fn pointerTypeLookup(typedef: []tad.TypeRef, module: *const Module, gpa: mem.Allocator) ![]const u8 { + _ = module; + for (typedef) |tok| { + switch (tok.tokenKind) { + .label => {}, + else => return error.InvalidTokenType, + } } + + var writer: std.Io.Writer.Allocating = .init(gpa); + const w = &writer.writer; + _ = w; + + return try writer.toOwnedSlice(); } -fn generateStruct(self: *Module, w: *std.Io.Writer, structIndex: usize) !void -{ +fn generateStruct(self: *Module, w: *std.Io.Writer, structIndex: usize) !void { const st = self.structs.items[structIndex]; - try w.print("pub const {s} = extern struct {{\n", .{}); - switch() + try w.print("pub const {s} = extern struct {{\n", .{st.name}); - { - - } - - - for(st.fields.items) |field| - { - try w.print(" {s}: {s}\n", .{}); + for (st.fields.items) |field| { + try w.print(" {s}: {s}\n", .{ field.name, "void, // unimplemented" }); } } @@ -108,4 +108,12 @@ fn generateEnum(self: *Module, w: *std.Io.Writer, index: usize) !void { const std = @import("std"); const tad = @import("tad.zig"); +const Token = tad.Token; +const mem = std.mem; const Module = tad.Module; + +pub const logScopeLevels: []const std.log.ScopeLevel = &.{ + .{ .scope = .tad_zig_generator, .level = .debug }, +}; + +pub const log = std.log.scoped(.tad_zig_generator); diff --git a/roadmap.md b/roadmap.md index c5f9592..0a71433 100644 --- a/roadmap.md +++ b/roadmap.md @@ -9,6 +9,10 @@ Chapter 1: core tech - simple repl - done - Hot Reloading - module loading via cli infrastructure - done +- Tad compiler + - Compiler store migration + - move node store from tad files into the global store + - API generation - generating the handbomb from the parser - done - API generation