enum generation complete
This commit is contained in:
parent
9b716d038f
commit
9c7967676f
|
|
@ -1,5 +1,4 @@
|
||||||
# tiny api definition - tadpole
|
# tiny api definition - tadpole reads linearly from top to down 4 spaces denotes an indent
|
||||||
# reads linearly from top to down 4 spaces denotes an indent
|
|
||||||
#
|
#
|
||||||
# comments are skipped/stripped,
|
# comments are skipped/stripped,
|
||||||
#
|
#
|
||||||
|
|
@ -28,11 +27,13 @@ module core
|
||||||
|
|
||||||
# Verbosity for the logger
|
# Verbosity for the logger
|
||||||
enum Verbosity
|
enum Verbosity
|
||||||
@backing u8 # this is a docstring
|
@backing int # docstring
|
||||||
verbose = 0 # this is another docstring
|
verbose = 0 # this is another docstring
|
||||||
info
|
info
|
||||||
warn
|
warn
|
||||||
err
|
err
|
||||||
|
wutangClan = 0x42061
|
||||||
|
wutangclank2 = 0x42061
|
||||||
|
|
||||||
struct SliceMutable
|
struct SliceMutable
|
||||||
ptr: c_ptr u8
|
ptr: c_ptr u8
|
||||||
|
|
@ -55,3 +56,10 @@ fn log_puts -> void
|
||||||
category: u32
|
category: u32
|
||||||
verbosity: Verbosity
|
verbosity: Verbosity
|
||||||
string: Slice
|
string: Slice
|
||||||
|
|
||||||
|
# todo: constants ...
|
||||||
|
#
|
||||||
|
# This will need a 3rd pass of the parser... preferrably maybe with a bit of a parser generator
|
||||||
|
#
|
||||||
|
# const my_constant: i32 = 123456
|
||||||
|
# const my_string: c_ptr = "what the hell did you just say about me";
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
buf: [4096]u8,
|
||||||
|
w: std.Io.File.Writer,
|
||||||
|
file: std.Io.File,
|
||||||
|
dir: std.Io.Dir,
|
||||||
|
io: std.Io,
|
||||||
|
|
||||||
|
pub fn opencwd(self: *@This(), io: std.Io, path: []const u8) !void {
|
||||||
|
try self.open(io, std.Io.Dir.cwd(), path);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open(self: *@This(), io: std.Io, dir: std.Io.Dir, path: []const u8) !void {
|
||||||
|
self.dir = dir;
|
||||||
|
self.file = try dir.openFile(io, path, .{});
|
||||||
|
self.io = io;
|
||||||
|
self.w = self.file.writer(self.io, &self.buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn createcwd(self: *@This(), io: std.Io, path: []const u8) !void {
|
||||||
|
try self.create(io, std.Io.Dir.cwd(), path);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(self: *@This(), io: std.Io, dir: std.Io.Dir, path: []const u8) !void {
|
||||||
|
self.dir = dir;
|
||||||
|
self.io = io;
|
||||||
|
self.file = try dir.createFile(io, path, .{});
|
||||||
|
self.w = self.file.writer(self.io, &self.buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn close(self: *@This()) void {
|
||||||
|
self.w.flush() catch {};
|
||||||
|
self.file.close(self.io);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn writer(self: *@This()) *std.Io.Writer {
|
||||||
|
return &self.w.interface;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
pub const std_options: std.Options = .{
|
pub const std_options: std.Options = .{
|
||||||
.log_level = .info, // Options: .err, .warn, .info, .debug
|
.log_level = .info, // Options: .err, .warn, .info, .debug
|
||||||
.log_scope_levels = &.{
|
.log_scope_levels = &.{
|
||||||
.{ .scope = .generateApi, .level = .info },
|
.{ .scope = .generateApi, .level = .debug },
|
||||||
.{ .scope = .tad, .level = .debug },
|
.{ .scope = .tad, .level = .debug },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
@ -28,7 +28,12 @@ pub fn main(init: std.process.Init) !void {
|
||||||
}
|
}
|
||||||
if (AP.cliArgParse(a, "input-file", "input .tad file to parse")) |arg| {
|
if (AP.cliArgParse(a, "input-file", "input .tad file to parse")) |arg| {
|
||||||
log.info("parsing input file: {s}", .{arg});
|
log.info("parsing input file: {s}", .{arg});
|
||||||
try tad.compile(init.io, init.arena.allocator(), arg);
|
|
||||||
|
try tad.compile(.{
|
||||||
|
.io = init.io,
|
||||||
|
.allocator = init.arena.allocator(),
|
||||||
|
.path = arg,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
pub fn generateApi(self: *Module, w: *std.Io.Writer) !void {
|
||||||
|
try w.print("// hello world \n", .{});
|
||||||
|
|
||||||
|
for (self.enums.items, 0..) |en, i| {
|
||||||
|
_ = en;
|
||||||
|
try generateEnum(self, w, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generateEnum(self: *Module, w: *std.Io.Writer, index: usize) !void {
|
||||||
|
const en = &self.enums.items[index];
|
||||||
|
|
||||||
|
const gpa = self.gpa;
|
||||||
|
|
||||||
|
try w.print("pub const {s} = enum", .{en.name});
|
||||||
|
|
||||||
|
// todo... this is kind of perfect for a checkpoint allocator
|
||||||
|
var seenValues: std.AutoHashMapUnmanaged(i64, usize) = .empty;
|
||||||
|
defer seenValues.deinit(gpa);
|
||||||
|
|
||||||
|
// todo... need to swap i64 with a struct IntegerValue { backingType: union(BackingType){...}};
|
||||||
|
var dupes: std.ArrayList(usize) = .empty;
|
||||||
|
defer dupes.deinit(gpa);
|
||||||
|
|
||||||
|
switch (en.backingType) {
|
||||||
|
else => {
|
||||||
|
return error.InvalidBackingType;
|
||||||
|
},
|
||||||
|
.int, .uint => {
|
||||||
|
try w.print("(c_{s}) ", .{@tagName(en.backingType)});
|
||||||
|
},
|
||||||
|
.i32, .i64, .u32, .u64, .u8, .i8 => {
|
||||||
|
const tag = @tagName(en.backingType);
|
||||||
|
try w.print("({s})", .{tag});
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
try w.writeAll("{\n");
|
||||||
|
|
||||||
|
var value: i64 = 0;
|
||||||
|
for (en.values.items, 0..) |v, i| {
|
||||||
|
const isDefine = (v.value != value);
|
||||||
|
value = v.value;
|
||||||
|
|
||||||
|
if (!seenValues.contains(v.value)) {
|
||||||
|
try w.print(" {s}", .{v.name});
|
||||||
|
if (isDefine) {
|
||||||
|
try w.print(" = {s}{d}", .{ if (v.presentation == .hex) "0x" else "", v.value });
|
||||||
|
}
|
||||||
|
try seenValues.put(gpa, value, i);
|
||||||
|
try w.print(",\n", .{});
|
||||||
|
} else {
|
||||||
|
try dupes.append(gpa, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
value += 1;
|
||||||
|
}
|
||||||
|
const anyint = en.allowAnyInt or dupes.items.len > 0;
|
||||||
|
if (anyint) {
|
||||||
|
try w.print(" _,\n", .{});
|
||||||
|
}
|
||||||
|
try w.print("\n", .{});
|
||||||
|
|
||||||
|
// duplicates aliases as const in zig.
|
||||||
|
|
||||||
|
var fbuf: [64]u8 = undefined;
|
||||||
|
for (dupes.items) |d| {
|
||||||
|
const dupe = en.values.items[d];
|
||||||
|
const radix: u8 = if (dupe.presentation == .hex) 16 else 10;
|
||||||
|
|
||||||
|
try w.print(" pub const {s}: @This() = {s};\n", .{
|
||||||
|
dupe.name,
|
||||||
|
fbuf[0..std.fmt.printInt(&fbuf, dupe.value, radix, .lower, .{})],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
try w.print("}};", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const tad = @import("tad.zig");
|
||||||
|
const Module = tad.Module;
|
||||||
|
|
@ -18,7 +18,7 @@ fn normalizeContents(buffer: []u8) []u8 {
|
||||||
return buffer[0..wcursor];
|
return buffer[0..wcursor];
|
||||||
}
|
}
|
||||||
|
|
||||||
const BuiltinType = enum(u8) { usize, int, @"opaque", i8, u8, i32, u32, i64, u64 };
|
const BuiltinType = enum(u8) { usize, int, uint, @"opaque", i8, u8, i32, u32, i64, u64 };
|
||||||
|
|
||||||
const StructDefinition = struct {
|
const StructDefinition = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
|
|
@ -58,13 +58,16 @@ const EnumDefnition = struct {
|
||||||
allowAnyInt: bool = false,
|
allowAnyInt: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const IntegerPresentation = enum { decimal, hex };
|
||||||
|
|
||||||
const EnumValue = struct {
|
const EnumValue = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
value: i64,
|
value: i64,
|
||||||
docstring: ?[]const u8 = null,
|
docstring: ?[]const u8 = null,
|
||||||
|
presentation: IntegerPresentation,
|
||||||
|
|
||||||
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
|
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
|
||||||
try writer.print("(value name:{s} value:{d}", .{ self.name, self.value });
|
try writer.print("(value name:{s} value:{d} presentation:{}", .{ self.name, self.value, @tagName(self.presentation) });
|
||||||
try writer.writeByte(')');
|
try writer.writeByte(')');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -88,7 +91,7 @@ const Token = struct {
|
||||||
comment: []const u8,
|
comment: []const u8,
|
||||||
directive: Directive,
|
directive: Directive,
|
||||||
keyword: Keyword,
|
keyword: Keyword,
|
||||||
integerValue: i64,
|
integerValue: struct { i64, IntegerPresentation },
|
||||||
floatValue: f64,
|
floatValue: f64,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -175,14 +178,22 @@ pub const Module = struct {
|
||||||
while (e < self.line.len and !(std.ascii.isWhitespace(self.line[e]) or (self.line[e] == '\n'))) {
|
while (e < self.line.len and !(std.ascii.isWhitespace(self.line[e]) or (self.line[e] == '\n'))) {
|
||||||
e += 1;
|
e += 1;
|
||||||
}
|
}
|
||||||
const value = try std.fmt.parseInt(i64, self.line[start..e], 0);
|
|
||||||
|
const span = self.line[start..e];
|
||||||
|
const value = try std.fmt.parseInt(i64, span, 0);
|
||||||
|
|
||||||
|
var radix: IntegerPresentation = .decimal;
|
||||||
|
|
||||||
|
if (span.len > 2 and std.mem.eql(u8, span[0..2], "0x")) {
|
||||||
|
radix = .hex;
|
||||||
|
}
|
||||||
|
|
||||||
self.cursor = e;
|
self.cursor = e;
|
||||||
|
|
||||||
return Token{
|
return Token{
|
||||||
.start = start,
|
.start = start,
|
||||||
.end = e,
|
.end = e,
|
||||||
.tokenKind = .{ .integerValue = value },
|
.tokenKind = .{ .integerValue = .{ value, radix } },
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
'@' => {
|
'@' => {
|
||||||
|
|
@ -422,16 +433,19 @@ pub const Module = struct {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
.label => {
|
.label => {
|
||||||
|
var radix: IntegerPresentation = .decimal;
|
||||||
|
|
||||||
if (l.tokens.len > 1) {
|
if (l.tokens.len > 1) {
|
||||||
if (isTerminal(l.tokens[1], .equals)) {
|
if (isTerminal(l.tokens[1], .equals)) {
|
||||||
if (l.tokens[2].tokenKind != .integerValue) return error.ExpectedInteger;
|
if (l.tokens[2].tokenKind != .integerValue) return error.ExpectedInteger;
|
||||||
nextValue = l.tokens[2].tokenKind.integerValue;
|
nextValue = l.tokens[2].tokenKind.integerValue[0];
|
||||||
|
radix = l.tokens[2].tokenKind.integerValue[1];
|
||||||
} else {
|
} else {
|
||||||
return error.UnexpectedToken;
|
return error.UnexpectedToken;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try s.values.append(self.gpa, .{ .name = l.tokens[0].slice(l.contents), .value = nextValue });
|
try s.values.append(self.gpa, .{ .name = l.tokens[0].slice(l.contents), .value = nextValue, .presentation = radix });
|
||||||
nextValue += 1;
|
nextValue += 1;
|
||||||
},
|
},
|
||||||
.comment => {},
|
.comment => {},
|
||||||
|
|
@ -522,12 +536,6 @@ 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})", .{
|
|
||||||
// @tagName(line.tokens[0].tokenKind),
|
|
||||||
// if (line.tokens[0].tokenKind == .keyword) @tagName(line.tokens[0].tokenKind.keyword) else "",
|
|
||||||
// line.contents,
|
|
||||||
// });
|
|
||||||
|
|
||||||
var newdef: Definition = undefined;
|
var newdef: Definition = undefined;
|
||||||
switch (line.tokens[0].tokenKind) {
|
switch (line.tokens[0].tokenKind) {
|
||||||
.comment => {
|
.comment => {
|
||||||
|
|
@ -567,20 +575,52 @@ pub const Module = struct {
|
||||||
cursor += 1;
|
cursor += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const GenerateZigApiOptions = struct {
|
||||||
|
io: std.Io,
|
||||||
|
output: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn fmt(self: *@This(), comptime f: []const u8, a: anytype) ?[]const u8 {
|
||||||
|
return self.gpa.print(f, a) catch return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generateZigApi(self: *@This(), opts: GenerateZigApiOptions) !void {
|
||||||
|
const basename = std.fs.path.dirname(opts.output) orelse "./";
|
||||||
|
const dir = try std.Io.Dir.cwd().createDirPathOpen(opts.io, basename, .{});
|
||||||
|
defer dir.close(opts.io);
|
||||||
|
|
||||||
|
const x = self.fmt("{s}{s}", .{ std.fs.path.stem(opts.output), ".zig" }) orelse return error.BadFormat;
|
||||||
|
|
||||||
|
var fwb: WriteBuffer = undefined;
|
||||||
|
try fwb.create(opts.io, dir, x);
|
||||||
|
defer fwb.close();
|
||||||
|
const w = fwb.writer();
|
||||||
|
|
||||||
|
try zig_generator.generateApi(self, w);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const TadModuleOptions = struct {};
|
pub const TadModuleOptions = struct {
|
||||||
|
io: std.Io,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
path: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn compile(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !void {
|
pub fn compile(opts: TadModuleOptions) !void {
|
||||||
const rawf = try std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .unlimited);
|
const rawf = try std.Io.Dir.cwd().readFileAlloc(opts.io, opts.path, opts.allocator, .unlimited);
|
||||||
const normalized = normalizeContents(rawf);
|
const normalized = normalizeContents(rawf);
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
var arena = std.heap.ArenaAllocator.init(opts.allocator);
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
|
|
||||||
var mod: Module = undefined;
|
var mod: Module = undefined;
|
||||||
try mod.lex(arena.allocator(), path, normalized);
|
try mod.lex(arena.allocator(), opts.path, normalized);
|
||||||
try mod.parse();
|
try mod.parse();
|
||||||
|
|
||||||
|
// generate
|
||||||
|
|
||||||
|
try mod.generateZigApi(.{ .io = opts.io, .output = "./zig-out/api/core.zig" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const log = std.log.scoped(.tad);
|
const log = std.log.scoped(.tad);
|
||||||
|
|
@ -588,3 +628,7 @@ const log = std.log.scoped(.tad);
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const ArrayList = std.ArrayList;
|
const ArrayList = std.ArrayList;
|
||||||
const mem = std.mem;
|
const mem = std.mem;
|
||||||
|
const WriteBuffer = @import("../common/WriteBuffer.zig");
|
||||||
|
const fmt = std.fmt;
|
||||||
|
|
||||||
|
const zig_generator = @import("generate-zig.zig");
|
||||||
|
|
|
||||||
21
roadmap.md
21
roadmap.md
|
|
@ -10,19 +10,24 @@ Chapter 1: core tech
|
||||||
- Hot Reloading
|
- Hot Reloading
|
||||||
- module loading via cli infrastructure - done
|
- module loading via cli infrastructure - done
|
||||||
- API generation
|
- API generation
|
||||||
- generating the handbomb from the parser
|
- generating the handbomb from the parser - done
|
||||||
- API generation
|
- API generation
|
||||||
- logging cli and tools directory - done
|
- logging cli and tools directory - done
|
||||||
- logging task (game thread)
|
- logging task (game thread)
|
||||||
|
|
||||||
|
Chapter 2: platform
|
||||||
|
|
||||||
|
- platform:
|
||||||
|
- linux, windows, mac - sdl + zopengl
|
||||||
|
- p2 (my own)
|
||||||
|
- gfx renderer api
|
||||||
|
- GL Backend
|
||||||
|
|
||||||
|
Chapter 3: Game 1 - Cognesia 2
|
||||||
|
|
||||||
|
Chapter 4:
|
||||||
- IO and threadpool
|
- IO and threadpool
|
||||||
- logging task
|
|
||||||
- Allocations and memory pooling
|
- Allocations and memory pooling
|
||||||
- performance profiling
|
- performance profiling
|
||||||
|
|
||||||
Chapter 2: graphics
|
|
||||||
|
|
||||||
- GL Backend
|
|
||||||
- runtime reflection
|
|
||||||
- gpu-one renderer api
|
|
||||||
|
|
||||||
Chapter 3: Game 1 - Cognesia
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue