From 7a9efb28f31bb8062fc38e707692bf3206703356 Mon Sep 17 00:00:00 2001 From: Peterino2 Date: Fri, 27 Nov 2026 22:21:12 -0700 Subject: [PATCH] initial implementation complete --- engine/tools/generate-api.zig | 8 ++ engine/tools/tad/tad.zig | 228 +++++++++++++++++++++++++++------- 2 files changed, 190 insertions(+), 46 deletions(-) diff --git a/engine/tools/generate-api.zig b/engine/tools/generate-api.zig index fa186a1..3b24391 100644 --- a/engine/tools/generate-api.zig +++ b/engine/tools/generate-api.zig @@ -1,5 +1,13 @@ // Reads in a .tad file and generates an api +pub const std_options: std.Options = .{ + .log_level = .info, // Options: .err, .warn, .info, .debug + .log_scope_levels = &.{ + .{ .scope = .generateApi, .level = .info }, + .{ .scope = .tad, .level = .debug }, + }, +}; + const helpText = \\generate-api \\ diff --git a/engine/tools/tad/tad.zig b/engine/tools/tad/tad.zig index c52c1d8..dd65bbc 100644 --- a/engine/tools/tad/tad.zig +++ b/engine/tools/tad/tad.zig @@ -18,17 +18,7 @@ fn normalizeContents(buffer: []u8) []u8 { return buffer[0..wcursor]; } -const BuiltinType = enum { - _usize, - _int, - _opaque, - _i8, - _u8, - _i32, - _u32, - _i64, - _u64, -}; +const BuiltinType = enum(u8) { usize, int, @"opaque", i8, u8, i32, u32, i64, u64 }; const StructDefinition = struct { name: []const u8, @@ -44,6 +34,14 @@ const FunctionDefinition = struct { const TypeRef = struct { lineRef: u32, tokens: []Token, + + pub fn format(self: @This(), writer: *std.Io.Writer) !void { + try writer.print("(type line:{d}", .{self.lineRef}); + for (self.tokens) |tok| { + try writer.print(" {s}", .{@tagName(tok.tokenKind)}); + } + try writer.writeByte(')'); + } }; const Field = struct { @@ -56,7 +54,7 @@ const EnumDefnition = struct { name: []const u8, values: std.ArrayList(EnumValue), docstring: ?[]const u8, - backingType: BuiltinType = ._int, + backingType: BuiltinType = .int, allowAnyInt: bool = false, }; @@ -64,6 +62,11 @@ const EnumValue = struct { name: []const u8, value: i64, docstring: ?[]const u8 = null, + + pub fn format(self: @This(), writer: *std.Io.Writer) !void { + try writer.print("(value name:{s} value:{d}", .{ self.name, self.value }); + try writer.writeByte(')'); + } }; fn eql(a: []const u8, b: []const u8) bool { @@ -74,12 +77,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 }; const Token = struct { start: u32, @@ -90,6 +88,8 @@ const Token = struct { comment: []const u8, directive: Directive, keyword: Keyword, + integerValue: i64, + floatValue: f64, }, fn slice(self: @This(), contents: []const u8) []const u8 { @@ -106,8 +106,8 @@ pub const Line = struct { indent: u8, }; -const terminals: []const []const u8 = &.{ "->", ";", ":", ")", "(", ">", "<", "[", "]" }; -const TerminalKind = enum { arrow, semicolon, colon, rparen, lparen, rangle, langle, lsqbrack, rsqbrack }; +const terminals: []const []const u8 = &.{ "->", ";", ":", ")", "(", ">", "<", "[", "]", "=" }; +const TerminalKind = enum { arrow, semicolon, colon, rparen, lparen, rangle, langle, lsqbrack, rsqbrack, equals }; const Directive = enum { import, @@ -115,11 +115,12 @@ const Directive = enum { }; const directives = @typeInfo(Directive).@"enum".field_names; -// new technique we define all structures and enums and command types then just a single recursive view based descender pub const Module = struct { enums: ArrayList(EnumDefnition) = .empty, structs: ArrayList(StructDefinition) = .empty, functions: ArrayList(FunctionDefinition) = .empty, + module: ?ModuleDefinition = null, + gpa: std.mem.Allocator, lines: []Line, file: []const u8, @@ -167,6 +168,23 @@ pub const Module = struct { if (self.cursor >= self.line.len) return null; switch (self.line[self.cursor]) { + '0'...'9' => { + const start = self.cursor; + + var e = self.cursor; + while (e < self.line.len and !(std.ascii.isWhitespace(self.line[e]) or (self.line[e] == '\n'))) { + e += 1; + } + const value = try std.fmt.parseInt(i64, self.line[start..e], 0); + + self.cursor = e; + + return Token{ + .start = start, + .end = e, + .tokenKind = .{ .integerValue = value }, + }; + }, '@' => { // directive? const start = self.cursor; @@ -244,6 +262,7 @@ pub const Module = struct { .file = file, .contents = contents, .gpa = gpa, + .module = null, .lines = undefined, }; @@ -253,7 +272,7 @@ pub const Module = struct { var linenum: u32 = 0; while (toks.next()) |line| { linenum += 1; - log.info("(line: {s})", .{line}); + log.debug("(line: {s})", .{line}); const nl: Line = .{ .offset = @intCast(toks.index - line.len), .contents = line, @@ -263,7 +282,7 @@ pub const Module = struct { .num = linenum, }; for (nl.tokens) |tok| { - log.info( + log.debug( " (token [{s}]: {s})", .{ @tagName(tok.tokenKind), @@ -282,11 +301,58 @@ pub const Module = struct { imports: std.ArrayList([]const u8) = .empty, }; - const Definition = union { + const Definition = union(enum(u8)) { comment: void, // skip these for now module: ModuleDefinition, // + @"fn": FunctionDefinition, + @"struct": StructDefinition, + @"enum": EnumDefnition, + + pub fn format(self: @This(), writer: *std.Io.Writer) !void { + switch (self) { + .module => |mod| { + try writer.print("(module", .{}); + for (mod.imports.items) |i| { + try writer.print(" import:{s}", .{i}); + } + try writer.writeByte(')'); + }, + .@"struct" => |s| { + try writer.print("(struct {s}", .{s.name}); + for (s.fields.items) |i| { + try writer.print("\n field {s}:{f}", .{ i.name, i.typeReference }); + } + try writer.writeByte(')'); + }, + .@"fn" => |f| { + try writer.print("(fn {s} -> {f}", .{ f.name, f.returnType }); + for (f.args.items) |i| { + try writer.print("\n arg {s}:{f}", .{ i.name, i.typeReference }); + } + try writer.writeByte(')'); + }, + .comment => { + try writer.print("(comment)", .{}); + }, + .@"enum" => |x| { + try writer.print("(enum name:{s} values:{d} backing:{d} any:{})", .{ + x.name, + x.values.items.len, + x.backingType, + x.allowAnyInt, + }); + }, + } + } }; + fn isTerminal(token: Token, kind: TerminalKind) bool { + if (token.tokenKind != .term) return false; + if (token.tokenKind.term != kind) return false; + + return true; + } + fn expectTerminal(token: Token, kind: TerminalKind) !void { if (token.tokenKind != .term) return error.ExpectedTerminal; if (token.tokenKind.term != kind) return error.ExpectedTerminal; @@ -297,21 +363,87 @@ pub const Module = struct { } fn parseStruct(self: @This(), cursor: *u32) !StructDefinition { + const lineRef = cursor.*; const line, const body = try self.setupBodyState(cursor, .@"struct"); - _ = line; - _ = body; - return undefined; + if (line.tokens.len < 2) return error.MalformedEnum; + if (line.tokens[1].tokenKind != .label) return error.ExpectedLabel; + + var s: StructDefinition = .{ + .name = line.tokens[1].slice(line.contents), + .fields = .empty, + }; + + for (body) |l| { + switch (l.tokens[0].tokenKind) { + .directive => return error.UnexpectedDirective, // can easily add directives support here + .label => { + try expectTerminal(l.tokens[1], .colon); + + try s.fields.append(self.gpa, .{ + .docstring = null, + .name = l.tokens[0].slice(l.contents), + .typeReference = .{ .lineRef = lineRef, .tokens = l.tokens[2..] }, + }); + }, + .comment => {}, + else => return error.UnexpectedStatement, + } + } + + return s; } fn parseEnum(self: @This(), cursor: *u32) !EnumDefnition { const line, const body = try self.setupBodyState(cursor, .@"enum"); - _ = line; - _ = body; - return undefined; + + if (line.tokens.len < 2) return error.MalformedStructDefinition; + if (line.tokens[1].tokenKind != .label) return error.ExpectedLabel; + + var s: EnumDefnition = .{ + .name = line.tokens[1].slice(line.contents), + .values = .empty, + .docstring = null, + }; + + var nextValue: i64 = 0; + + for (body) |l| { + switch (l.tokens[0].tokenKind) { + .directive => |dir| { + switch (dir) { + .backing => { + try expectLabel(l.tokens[1]); + const backing = std.meta.stringToEnum(BuiltinType, l.tokens[1].slice(l.contents)) orelse return error.InvalidBuiltinValue; + s.backingType = backing; + }, + else => { + return error.UnexpectedDirective; + }, + } + }, + .label => { + if (l.tokens.len > 1) { + if (isTerminal(l.tokens[1], .equals)) { + if (l.tokens[2].tokenKind != .integerValue) return error.ExpectedInteger; + nextValue = l.tokens[2].tokenKind.integerValue; + } else { + return error.UnexpectedToken; + } + } + + try s.values.append(self.gpa, .{ .name = l.tokens[0].slice(l.contents), .value = nextValue }); + nextValue += 1; + }, + .comment => {}, + else => return error.UnexpectedStatement, + } + } + + return s; } fn expectLabel(token: Token) !void { - if (!token.tokenKind == .label) return error.ExpectedLabel; + if (token.tokenKind != .label) return error.ExpectedLabel; } fn parseFunction(self: @This(), cursor: *u32) !FunctionDefinition { @@ -337,7 +469,6 @@ pub const Module = struct { .name = l.tokens[0].slice(l.contents), .typeReference = .{ .lineRef = lineRef, .tokens = l.tokens[2..] }, }); - log.info(" (function argument:{s})", .{func.args.last().?.name}); }, .comment => {}, else => return error.UnexpectedStatement, @@ -391,11 +522,13 @@ pub const Module = struct { const lines = self.lines[cursor.*..]; const line = lines[0]; - log.debug("(kind: {s} {s} {s})", .{ - @tagName(line.tokens[0].tokenKind), - if (line.tokens[0].tokenKind == .keyword) @tagName(line.tokens[0].tokenKind.keyword) else "", - line.contents, - }); + // log.debug("(kind: {s} {s} {s})", .{ + // @tagName(line.tokens[0].tokenKind), + // if (line.tokens[0].tokenKind == .keyword) @tagName(line.tokens[0].tokenKind.keyword) else "", + // line.contents, + // }); + + var newdef: Definition = undefined; switch (line.tokens[0].tokenKind) { .comment => { return .{ .comment = {} }; @@ -403,26 +536,27 @@ pub const Module = struct { .keyword => |kw| { switch (kw) { .@"enum" => { - const newdef = try self.parseEnum(cursor); - _ = newdef; + newdef = .{ .@"enum" = try self.parseEnum(cursor) }; + try self.enums.append(self.gpa, newdef.@"enum"); }, .@"fn" => { - const newdef = try self.parseFunction(cursor); - _ = newdef; + newdef = .{ .@"fn" = try self.parseFunction(cursor) }; + try self.functions.append(self.gpa, newdef.@"fn"); }, .@"struct" => { - const newdef = try self.parseStruct(cursor); - _ = newdef; + newdef = .{ .@"struct" = try self.parseStruct(cursor) }; + try self.structs.append(self.gpa, newdef.@"struct"); }, .module => { - const newdef = try self.parseModule(cursor); - _ = newdef; + newdef = .{ .module = try self.parseModule(cursor) }; + self.module = newdef.module; }, } }, else => return error.UnexpectedToken, } - return .{ .comment = {} }; + log.info("{f}", .{newdef}); + return newdef; } pub fn parse(self: *@This()) !void { @@ -435,6 +569,8 @@ pub const Module = struct { } }; +pub const TadModuleOptions = struct {}; + pub fn compile(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !void { const rawf = try std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .unlimited); const normalized = normalizeContents(rawf);