initial implementation complete
This commit is contained in:
parent
d821944c60
commit
7a9efb28f3
|
|
@ -1,5 +1,13 @@
|
||||||
// Reads in a .tad file and generates an api
|
// 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 =
|
const helpText =
|
||||||
\\generate-api
|
\\generate-api
|
||||||
\\
|
\\
|
||||||
|
|
|
||||||
|
|
@ -18,17 +18,7 @@ fn normalizeContents(buffer: []u8) []u8 {
|
||||||
return buffer[0..wcursor];
|
return buffer[0..wcursor];
|
||||||
}
|
}
|
||||||
|
|
||||||
const BuiltinType = enum {
|
const BuiltinType = enum(u8) { usize, int, @"opaque", i8, u8, i32, u32, i64, u64 };
|
||||||
_usize,
|
|
||||||
_int,
|
|
||||||
_opaque,
|
|
||||||
_i8,
|
|
||||||
_u8,
|
|
||||||
_i32,
|
|
||||||
_u32,
|
|
||||||
_i64,
|
|
||||||
_u64,
|
|
||||||
};
|
|
||||||
|
|
||||||
const StructDefinition = struct {
|
const StructDefinition = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
|
|
@ -44,6 +34,14 @@ const FunctionDefinition = struct {
|
||||||
const TypeRef = struct {
|
const TypeRef = struct {
|
||||||
lineRef: u32,
|
lineRef: u32,
|
||||||
tokens: []Token,
|
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 {
|
const Field = struct {
|
||||||
|
|
@ -56,7 +54,7 @@ const EnumDefnition = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
values: std.ArrayList(EnumValue),
|
values: std.ArrayList(EnumValue),
|
||||||
docstring: ?[]const u8,
|
docstring: ?[]const u8,
|
||||||
backingType: BuiltinType = ._int,
|
backingType: BuiltinType = .int,
|
||||||
allowAnyInt: bool = false,
|
allowAnyInt: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -64,6 +62,11 @@ const EnumValue = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
value: i64,
|
value: i64,
|
||||||
docstring: ?[]const u8 = null,
|
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 {
|
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;
|
return std.fmt.parseInt(i64, x, 0) catch null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Keyword = enum {
|
const Keyword = enum { @"fn", @"struct", @"enum", module };
|
||||||
@"fn",
|
|
||||||
@"struct",
|
|
||||||
@"enum",
|
|
||||||
module,
|
|
||||||
};
|
|
||||||
|
|
||||||
const Token = struct {
|
const Token = struct {
|
||||||
start: u32,
|
start: u32,
|
||||||
|
|
@ -90,6 +88,8 @@ const Token = struct {
|
||||||
comment: []const u8,
|
comment: []const u8,
|
||||||
directive: Directive,
|
directive: Directive,
|
||||||
keyword: Keyword,
|
keyword: Keyword,
|
||||||
|
integerValue: i64,
|
||||||
|
floatValue: f64,
|
||||||
},
|
},
|
||||||
|
|
||||||
fn slice(self: @This(), contents: []const u8) []const u8 {
|
fn slice(self: @This(), contents: []const u8) []const u8 {
|
||||||
|
|
@ -106,8 +106,8 @@ pub const Line = struct {
|
||||||
indent: u8,
|
indent: u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
const terminals: []const []const u8 = &.{ "->", ";", ":", ")", "(", ">", "<", "[", "]" };
|
const terminals: []const []const u8 = &.{ "->", ";", ":", ")", "(", ">", "<", "[", "]", "=" };
|
||||||
const TerminalKind = enum { arrow, semicolon, colon, rparen, lparen, rangle, langle, lsqbrack, rsqbrack };
|
const TerminalKind = enum { arrow, semicolon, colon, rparen, lparen, rangle, langle, lsqbrack, rsqbrack, equals };
|
||||||
|
|
||||||
const Directive = enum {
|
const Directive = enum {
|
||||||
import,
|
import,
|
||||||
|
|
@ -115,11 +115,12 @@ const Directive = enum {
|
||||||
};
|
};
|
||||||
const directives = @typeInfo(Directive).@"enum".field_names;
|
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 {
|
pub const Module = struct {
|
||||||
enums: ArrayList(EnumDefnition) = .empty,
|
enums: ArrayList(EnumDefnition) = .empty,
|
||||||
structs: ArrayList(StructDefinition) = .empty,
|
structs: ArrayList(StructDefinition) = .empty,
|
||||||
functions: ArrayList(FunctionDefinition) = .empty,
|
functions: ArrayList(FunctionDefinition) = .empty,
|
||||||
|
module: ?ModuleDefinition = null,
|
||||||
|
|
||||||
gpa: std.mem.Allocator,
|
gpa: std.mem.Allocator,
|
||||||
lines: []Line,
|
lines: []Line,
|
||||||
file: []const u8,
|
file: []const u8,
|
||||||
|
|
@ -167,6 +168,23 @@ pub const Module = struct {
|
||||||
if (self.cursor >= self.line.len) return null;
|
if (self.cursor >= self.line.len) return null;
|
||||||
|
|
||||||
switch (self.line[self.cursor]) {
|
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?
|
// directive?
|
||||||
const start = self.cursor;
|
const start = self.cursor;
|
||||||
|
|
@ -244,6 +262,7 @@ pub const Module = struct {
|
||||||
.file = file,
|
.file = file,
|
||||||
.contents = contents,
|
.contents = contents,
|
||||||
.gpa = gpa,
|
.gpa = gpa,
|
||||||
|
.module = null,
|
||||||
.lines = undefined,
|
.lines = undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -253,7 +272,7 @@ pub const Module = struct {
|
||||||
var linenum: u32 = 0;
|
var linenum: u32 = 0;
|
||||||
while (toks.next()) |line| {
|
while (toks.next()) |line| {
|
||||||
linenum += 1;
|
linenum += 1;
|
||||||
log.info("(line: {s})", .{line});
|
log.debug("(line: {s})", .{line});
|
||||||
const nl: Line = .{
|
const nl: Line = .{
|
||||||
.offset = @intCast(toks.index - line.len),
|
.offset = @intCast(toks.index - line.len),
|
||||||
.contents = line,
|
.contents = line,
|
||||||
|
|
@ -263,7 +282,7 @@ pub const Module = struct {
|
||||||
.num = linenum,
|
.num = linenum,
|
||||||
};
|
};
|
||||||
for (nl.tokens) |tok| {
|
for (nl.tokens) |tok| {
|
||||||
log.info(
|
log.debug(
|
||||||
" (token [{s}]: {s})",
|
" (token [{s}]: {s})",
|
||||||
.{
|
.{
|
||||||
@tagName(tok.tokenKind),
|
@tagName(tok.tokenKind),
|
||||||
|
|
@ -282,11 +301,58 @@ pub const Module = struct {
|
||||||
imports: std.ArrayList([]const u8) = .empty,
|
imports: std.ArrayList([]const u8) = .empty,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Definition = union {
|
const Definition = union(enum(u8)) {
|
||||||
comment: void, // skip these for now
|
comment: void, // skip these for now
|
||||||
module: ModuleDefinition, //
|
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 {
|
fn expectTerminal(token: Token, kind: TerminalKind) !void {
|
||||||
if (token.tokenKind != .term) return error.ExpectedTerminal;
|
if (token.tokenKind != .term) return error.ExpectedTerminal;
|
||||||
if (token.tokenKind.term != kind) 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 {
|
fn parseStruct(self: @This(), cursor: *u32) !StructDefinition {
|
||||||
|
const lineRef = cursor.*;
|
||||||
const line, const body = try self.setupBodyState(cursor, .@"struct");
|
const line, const body = try self.setupBodyState(cursor, .@"struct");
|
||||||
_ = line;
|
if (line.tokens.len < 2) return error.MalformedEnum;
|
||||||
_ = body;
|
if (line.tokens[1].tokenKind != .label) return error.ExpectedLabel;
|
||||||
return undefined;
|
|
||||||
|
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 {
|
fn parseEnum(self: @This(), cursor: *u32) !EnumDefnition {
|
||||||
const line, const body = try self.setupBodyState(cursor, .@"enum");
|
const line, const body = try self.setupBodyState(cursor, .@"enum");
|
||||||
_ = line;
|
|
||||||
_ = body;
|
if (line.tokens.len < 2) return error.MalformedStructDefinition;
|
||||||
return undefined;
|
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 {
|
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 {
|
fn parseFunction(self: @This(), cursor: *u32) !FunctionDefinition {
|
||||||
|
|
@ -337,7 +469,6 @@ pub const Module = struct {
|
||||||
.name = l.tokens[0].slice(l.contents),
|
.name = l.tokens[0].slice(l.contents),
|
||||||
.typeReference = .{ .lineRef = lineRef, .tokens = l.tokens[2..] },
|
.typeReference = .{ .lineRef = lineRef, .tokens = l.tokens[2..] },
|
||||||
});
|
});
|
||||||
log.info(" (function argument:{s})", .{func.args.last().?.name});
|
|
||||||
},
|
},
|
||||||
.comment => {},
|
.comment => {},
|
||||||
else => return error.UnexpectedStatement,
|
else => return error.UnexpectedStatement,
|
||||||
|
|
@ -391,11 +522,13 @@ pub const Module = struct {
|
||||||
const lines = self.lines[cursor.*..];
|
const lines = self.lines[cursor.*..];
|
||||||
const line = lines[0];
|
const line = lines[0];
|
||||||
|
|
||||||
log.debug("(kind: {s} {s} {s})", .{
|
// log.debug("(kind: {s} {s} {s})", .{
|
||||||
@tagName(line.tokens[0].tokenKind),
|
// @tagName(line.tokens[0].tokenKind),
|
||||||
if (line.tokens[0].tokenKind == .keyword) @tagName(line.tokens[0].tokenKind.keyword) else "",
|
// if (line.tokens[0].tokenKind == .keyword) @tagName(line.tokens[0].tokenKind.keyword) else "",
|
||||||
line.contents,
|
// line.contents,
|
||||||
});
|
// });
|
||||||
|
|
||||||
|
var newdef: Definition = undefined;
|
||||||
switch (line.tokens[0].tokenKind) {
|
switch (line.tokens[0].tokenKind) {
|
||||||
.comment => {
|
.comment => {
|
||||||
return .{ .comment = {} };
|
return .{ .comment = {} };
|
||||||
|
|
@ -403,26 +536,27 @@ pub const Module = struct {
|
||||||
.keyword => |kw| {
|
.keyword => |kw| {
|
||||||
switch (kw) {
|
switch (kw) {
|
||||||
.@"enum" => {
|
.@"enum" => {
|
||||||
const newdef = try self.parseEnum(cursor);
|
newdef = .{ .@"enum" = try self.parseEnum(cursor) };
|
||||||
_ = newdef;
|
try self.enums.append(self.gpa, newdef.@"enum");
|
||||||
},
|
},
|
||||||
.@"fn" => {
|
.@"fn" => {
|
||||||
const newdef = try self.parseFunction(cursor);
|
newdef = .{ .@"fn" = try self.parseFunction(cursor) };
|
||||||
_ = newdef;
|
try self.functions.append(self.gpa, newdef.@"fn");
|
||||||
},
|
},
|
||||||
.@"struct" => {
|
.@"struct" => {
|
||||||
const newdef = try self.parseStruct(cursor);
|
newdef = .{ .@"struct" = try self.parseStruct(cursor) };
|
||||||
_ = newdef;
|
try self.structs.append(self.gpa, newdef.@"struct");
|
||||||
},
|
},
|
||||||
.module => {
|
.module => {
|
||||||
const newdef = try self.parseModule(cursor);
|
newdef = .{ .module = try self.parseModule(cursor) };
|
||||||
_ = newdef;
|
self.module = newdef.module;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => return error.UnexpectedToken,
|
else => return error.UnexpectedToken,
|
||||||
}
|
}
|
||||||
return .{ .comment = {} };
|
log.info("{f}", .{newdef});
|
||||||
|
return newdef;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(self: *@This()) !void {
|
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 {
|
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 rawf = try std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .unlimited);
|
||||||
const normalized = normalizeContents(rawf);
|
const normalized = normalizeContents(rawf);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue