From d821944c602330bccb0487fb9a9af6cb17e3d093 Mon Sep 17 00:00:00 2001 From: Peterino2 Date: Thu, 26 Nov 2026 01:49:35 -0700 Subject: [PATCH] reucrsive descent parser getting there --- engine/src/core/api.tad | 8 +- engine/tools/tad/tad.zig | 246 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 238 insertions(+), 16 deletions(-) diff --git a/engine/src/core/api.tad b/engine/src/core/api.tad index 0566619..35a7587 100644 --- a/engine/src/core/api.tad +++ b/engine/src/core/api.tad @@ -24,22 +24,18 @@ # module core - @import platform # - @output "src/core.zig" - + @import platform # platform isn't real it can't hurt you # Verbosity for the logger enum Verbosity @backing u8 # this is a docstring - verbose = 0 # this is another docstring info - warn err struct SliceMutable - ptr: c_ptr u8 + ptr: c_ptr u8 len: usize struct Slice diff --git a/engine/tools/tad/tad.zig b/engine/tools/tad/tad.zig index 8938f8a..c52c1d8 100644 --- a/engine/tools/tad/tad.zig +++ b/engine/tools/tad/tad.zig @@ -37,13 +37,13 @@ const StructDefinition = struct { const FunctionDefinition = struct { name: []const u8, - args: std.ArrayList(Field), + args: std.ArrayList(Field) = .empty, returnType: TypeRef, }; -const TypeRef = union { - @"struct": u32, - @"enum": u32, +const TypeRef = struct { + lineRef: u32, + tokens: []Token, }; const Field = struct { @@ -74,10 +74,23 @@ fn parseInt(x: []const u8) ?i64 { return std.fmt.parseInt(i64, x, 0) catch null; } +const Keyword = enum { + @"fn", + @"struct", + @"enum", + module, +}; + const Token = struct { start: u32, end: u32, - tokenKind: union { term: TerminalKind, label: void }, + tokenKind: union(enum(u8)) { + term: TerminalKind, + label: void, + comment: []const u8, + directive: Directive, + keyword: Keyword, + }, fn slice(self: @This(), contents: []const u8) []const u8 { return contents[self.start..self.end]; @@ -96,6 +109,12 @@ pub const Line = struct { const terminals: []const []const u8 = &.{ "->", ";", ":", ")", "(", ">", "<", "[", "]" }; const TerminalKind = enum { arrow, semicolon, colon, rparen, lparen, rangle, langle, lsqbrack, rsqbrack }; +const Directive = enum { + import, + backing, +}; +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, @@ -131,19 +150,66 @@ pub const Module = struct { } } - pub fn next(self: *@This()) ?Token { + fn checkKeyword(segment: []const u8) ?Keyword { + const keywords = @typeInfo(Keyword).@"enum".field_names; + inline for (keywords, 0..) |k, i| { + if (mem.eql(u8, segment, k)) { + const kw: Keyword = @fromBackingInt(@intCast(i)); + return kw; + } + } + return null; + } + + pub fn next(self: *@This()) !?Token { while (self.cursor < self.line.len and self.line[self.cursor] == ' ') : (self.cursor += 1) {} if (self.cursor >= self.line.len) return null; switch (self.line[self.cursor]) { + '@' => { + // directive? + const start = self.cursor; + const c = self.cursor + 1; + + const directive = blk: { + for (directives, 0..) |d, i| { + if (mem.startsWith(u8, self.line[c..], d)) { + const directiveKind: Directive = @fromBackingInt(@intCast(i)); + break :blk directiveKind; + } + } + return error.UnrecognizedDirective; + }; + + self.cursor = c; + self.cursor += @intCast(@tagName(directive).len); + return Token{ + .start = start, + .end = self.cursor, + .tokenKind = .{ .directive = directive }, + }; + }, 'a'...'z', 'A'...'Z', '_' => { var end: u32 = self.cursor; while (end < self.line.len and isLabelChar(self.line[end])) : (end += 1) {} - const tok = Token{ .start = self.cursor, .end = end, .tokenKind = .{ .label = {} } }; + + var tok = Token{ .start = self.cursor, .end = end, .tokenKind = .{ .label = {} } }; self.cursor = end; + + if (checkKeyword(self.line[tok.start..tok.end])) |kw| { + tok.tokenKind = .{ .keyword = kw }; + } return tok; }, + '#' => { + // gobble the comments + const start = self.cursor; + self.cursor = @intCast(self.line.len); + return Token{ .start = start, .end = self.cursor, .tokenKind = .{ + .comment = self.line[start..self.cursor], + } }; + }, else => { for (terminals, 0..) |t, i| { if (mem.startsWith(u8, self.line[self.cursor..], t)) { @@ -166,7 +232,7 @@ pub const Module = struct { fn tokenize(gpa: std.mem.Allocator, line: []const u8) ![]Token { var rvl: ArrayList(Token) = .empty; var tokenizer = Tokenizer.init(line); - while (tokenizer.next()) |n| { + while (try tokenizer.next()) |n| { try rvl.append(gpa, n); } @@ -197,15 +263,175 @@ pub const Module = struct { .num = linenum, }; for (nl.tokens) |tok| { - log.info(" (token: {s})", .{tok.slice(nl.contents)}); + log.info( + " (token [{s}]: {s})", + .{ + @tagName(tok.tokenKind), + tok.slice(nl.contents), + }, + ); } + try lines.append(gpa, nl); } self.lines = try lines.toOwnedSlice(gpa); } + const ModuleDefinition = struct { + name: []const u8, + imports: std.ArrayList([]const u8) = .empty, + }; + + const Definition = union { + comment: void, // skip these for now + module: ModuleDefinition, // + }; + + fn expectTerminal(token: Token, kind: TerminalKind) !void { + if (token.tokenKind != .term) return error.ExpectedTerminal; + if (token.tokenKind.term != kind) return error.ExpectedTerminal; + } + + fn expectKeyword(token: Token, kind: Keyword) !void { + if (token.tokenKind != .keyword or token.tokenKind.keyword != kind) return error.ExpectedKeyword; + } + + fn parseStruct(self: @This(), cursor: *u32) !StructDefinition { + const line, const body = try self.setupBodyState(cursor, .@"struct"); + _ = line; + _ = body; + return undefined; + } + + fn parseEnum(self: @This(), cursor: *u32) !EnumDefnition { + const line, const body = try self.setupBodyState(cursor, .@"enum"); + _ = line; + _ = body; + return undefined; + } + + fn expectLabel(token: Token) !void { + if (!token.tokenKind == .label) return error.ExpectedLabel; + } + + fn parseFunction(self: @This(), cursor: *u32) !FunctionDefinition { + const lineRef = cursor.*; + const line, const body = try self.setupBodyState(cursor, .@"fn"); + if (line.tokens.len < 4) return error.MalformedFunctionDefinition; + if (line.tokens[1].tokenKind != .label) return error.ExpectedLabel; + try expectTerminal(line.tokens[2], .arrow); + + var func: FunctionDefinition = .{ + .name = line.tokens[1].slice(line.contents), + .returnType = .{ .lineRef = lineRef, .tokens = line.tokens[3..] }, + }; + + 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 func.args.append(self.gpa, .{ + .docstring = null, + .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, + } + } + + return func; + } + + fn setupBodyState(self: @This(), cursor: *u32, keyword: Keyword) !struct { Line, []Line } { + const lines = self.lines[cursor.*..]; + try expectKeyword(lines[0].tokens[0], keyword); + const indent = lines[0].indent + 1; + const body = readBody(lines[1..], indent); + cursor.* += @as(u32, @intCast(body.len)); + return .{ lines[0], body }; + } + + fn readBody(lines: []Line, indent: u8) []Line { + for (lines, 0..) |line, i| { + if (line.indent < indent) + return lines[0..i]; + } + return lines; + } + + fn parseModule(self: @This(), cursor: *u32) !ModuleDefinition { + const line, const body = try self.setupBodyState(cursor, .module); + var mod: ModuleDefinition = .{ .name = line.tokens[0].slice(line.contents) }; + for (body) |l| { + switch (l.tokens[0].tokenKind) { + .directive => |d| { + switch (d) { + .import => { + try mod.imports.append(self.gpa, l.tokens[1].slice(l.contents)); + }, + else => return error.UnexpectedDirective, + } + }, + .comment => {}, + else => return error.UnexpectedStatement, + } + } + for (mod.imports.items) |i| { + log.info(" (module import:{s})", .{i}); + } + return mod; + } + + pub fn parseLine(self: *@This(), cursor: *u32) !Definition { + 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, + }); + switch (line.tokens[0].tokenKind) { + .comment => { + return .{ .comment = {} }; + }, + .keyword => |kw| { + switch (kw) { + .@"enum" => { + const newdef = try self.parseEnum(cursor); + _ = newdef; + }, + .@"fn" => { + const newdef = try self.parseFunction(cursor); + _ = newdef; + }, + .@"struct" => { + const newdef = try self.parseStruct(cursor); + _ = newdef; + }, + .module => { + const newdef = try self.parseModule(cursor); + _ = newdef; + }, + } + }, + else => return error.UnexpectedToken, + } + return .{ .comment = {} }; + } + pub fn parse(self: *@This()) !void { - _ = self; + var cursor: u32 = 0; + while (cursor < @as(u32, @intCast(self.lines.len))) { + const line = try self.parseLine(&cursor); + _ = line; + cursor += 1; + } } };