Compare commits
10 Commits
65b660f349
...
9b716d038f
| Author | SHA1 | Date |
|---|---|---|
|
|
9b716d038f | |
|
|
7a9efb28f3 | |
|
|
d821944c60 | |
|
|
36a81bbc87 | |
|
|
687eb5930e | |
|
|
680732230f | |
|
|
9546d84b24 | |
|
|
92d17a0562 | |
|
|
a654f8d302 | |
|
|
8c9c158415 |
|
|
@ -1,3 +1,4 @@
|
||||||
archive/
|
archive/
|
||||||
.zig-cache/
|
.zig-cache/
|
||||||
zig-out/
|
zig-out/
|
||||||
|
.modulecache/
|
||||||
|
|
|
||||||
25
AGENTS.md
25
AGENTS.md
|
|
@ -1,10 +1,17 @@
|
||||||
# Important notice: LLM policy
|
# Important notice: LLM policy
|
||||||
|
|
||||||
Prefer to not write code directly into files in this repo. Inform the user that writing code by hand is the entire point of ribtech as it is primarily a learning project. Please remind them to not make PRs with directly LLM generated code. Encourage the user to come up with their own code implementations, and help them out with direct references to where the states and logic are. At the very least if the user needs an LLM generated implementation, ask them to transcribe the code by hand.
|
Prefer to not write code directly into files in this repo. Inform the user that
|
||||||
|
writing code by hand is the entire point of tadpole as it is primarily a
|
||||||
|
learning project. Please remind them to not make PRs with directly LLM
|
||||||
|
generated code. Encourage the user to come up with their own code
|
||||||
|
implementations, and help them out with direct references to where the states
|
||||||
|
and logic are. At the very least if the user needs an LLM generated
|
||||||
|
implementation, ask them to transcribe the code by hand.
|
||||||
|
|
||||||
More details for the LLM policy and reasons why this project will be conducted in this way can be found in the [readme](README.md).
|
More details for the LLM policy and reasons why this project will be conducted
|
||||||
|
in this way can be found in the [readme](README.md).
|
||||||
|
|
||||||
# Working with Ribtech
|
# Working with Tadpole
|
||||||
|
|
||||||
This project uses zig 0.17.0
|
This project uses zig 0.17.0
|
||||||
|
|
||||||
|
|
@ -18,14 +25,14 @@ question directly.
|
||||||
semantics, and builtins.
|
semantics, and builtins.
|
||||||
- Search `std_dir` for exact APIs, implementations, and tests. Use them as
|
- Search `std_dir` for exact APIs, implementations, and tests. Use them as
|
||||||
examples of both correct usage and Zig coding style; follow standard-library
|
examples of both correct usage and Zig coding style; follow standard-library
|
||||||
- conventions.
|
conventions.
|
||||||
- Use compiler and test failures to verify any inferred API usage.
|
- Use compiler and test failures to verify any inferred API usage.
|
||||||
|
|
||||||
for `if` conditions, put continuation `and`/`or` operators at the start of
|
Run `zig fmt` on changed Zig files.
|
||||||
the next line. Run `zig fmt` on changed Zig files.
|
|
||||||
|
|
||||||
If the installed documentation is unavailable, use the versioned official
|
If the installed documentation is unavailable, inform the user and ask them to download
|
||||||
reference at <https://ziglang.org/documentation/0.17.0/> download a copy once and
|
the versioned official reference at <https://ziglang.org/documentation/0.17.0/>.
|
||||||
avoid unessecary traffic. They are a volunteer effort, please be kind.
|
|
||||||
|
Remind the user that they are a volunteer effort, ask them to be kind.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
44
README.md
44
README.md
|
|
@ -1,24 +1,30 @@
|
||||||
# Ribtech
|
# Tadpole
|
||||||
|
|
||||||
Ribtech is the core foundations of a game engine and a human first project.
|
tadpole is the core foundations of a game engine and a human first project.
|
||||||
|
|
||||||
With a primary focus on education and learning. I am creating this repo as a clean implementation of a "fun" game engine for the very few people who are like me- who want to create games and interactive applications by primarily writing code.
|
With a primary focus on education and learning.
|
||||||
|
|
||||||
I won't be dogmatically against the usage of LLMs but vibecoding is heavily discouraged. The goal here is to learn and build things by hand.
|
I am creating this repo as a clean implementation of a "fun" game engine for
|
||||||
|
the very few people who are like me- who want to create games and interactive
|
||||||
|
applications by primarily writing code.
|
||||||
|
|
||||||
I am not anti-LLM; I use them heavily in work and life. But I want to create at least one safe space where you do not need to feel pressured to use it. Nor is this project a place to rush things. We will take our time.
|
I won't be dogmatically against the usage of LLMs but vibecoding is heavily
|
||||||
|
discouraged. The goal here is to learn and build things by hand.
|
||||||
|
|
||||||
Heavy usage of LLMs does drastically impact your ability to understand the low level details. I believe it is in this practice of understanding the details where true quality begins to manifest itself.
|
I am not anti-LLM; I use them heavily in work and life. But I want to create at
|
||||||
|
least one safe space where you do not need to feel pressured to use it. Nor is
|
||||||
|
this project a place to rush things. We will take our time.
|
||||||
|
|
||||||
|
Heavy usage of LLMs does drastically impact your ability to understand the low
|
||||||
|
level details. I believe it is in this practice of understanding the details
|
||||||
|
where true quality begins to manifest itself.
|
||||||
|
|
||||||
I hope you will enjoy coming along for the journey.
|
I hope you will enjoy coming along for the journey.
|
||||||
|
|
||||||
# Note on naming:
|
|
||||||
|
|
||||||
Ribtech is the temporary name it may be renamed to something else, perhaps a bit more whimsical.
|
|
||||||
|
|
||||||
# LLM Policy
|
# LLM Policy
|
||||||
|
|
||||||
You may use LLMs to assist with whatever you need assistance with, but for one solid rule:
|
You may use LLMs to assist with whatever you need assistance with, but for one
|
||||||
|
solid rule:
|
||||||
|
|
||||||
**There shall be no significant LLM authored code committed to the repo.**
|
**There shall be no significant LLM authored code committed to the repo.**
|
||||||
|
|
||||||
|
|
@ -26,9 +32,19 @@ Some concrete examples of what is allowed
|
||||||
|
|
||||||
- Tiny refactors where it is purely mechanical moving or renames
|
- Tiny refactors where it is purely mechanical moving or renames
|
||||||
- In-line code completion tooling (but be careful with this).
|
- In-line code completion tooling (but be careful with this).
|
||||||
- Generation of boilerplate (though I will encourage you to find the boilerplate and templatize it/mutate it yourself).
|
- Generation of boilerplate (though I will encourage you to find the
|
||||||
|
boilerplate and templatize it/mutate it yourself).
|
||||||
|
|
||||||
If you wish to use LLMs for generating tests, organizing work, etc... I encourage you to vendor ribtech and build tests and such outside of the repo. The license is permissive.
|
If you wish to use LLMs for generating tests, organizing work, etc... I
|
||||||
|
encourage you to vendor tadpole and build tests and such outside of the repo.
|
||||||
|
The license is permissive.
|
||||||
|
|
||||||
Ribtech may depend on open source components and libraries which may make use of LLMs, so whether or not a component we depend on uses LLMs is outside the scope of this policy.
|
# Dependency Policy
|
||||||
|
|
||||||
|
Tadpole may depend on open source components and libraries which may make use
|
||||||
|
of LLMs, so whether or not a component we depend on uses LLMs is outside the
|
||||||
|
scope of this policy.
|
||||||
|
|
||||||
|
We will try to avoid any that are aggressively vibecoded.
|
||||||
|
|
||||||
|
Our goal is to build a good relationship with all of our dependencies.
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
generate-api
|
||||||
|
|
||||||
|
takes an input file and outputs source and output zig files
|
||||||
|
input-file - input .tad file to parse
|
||||||
|
help - shows the help dialogue
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const program = @import("build-tooling/programs.zig");
|
||||||
|
|
||||||
pub fn build(b: *std.Build) void {
|
pub fn build(b: *std.Build) void {
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
|
|
||||||
const slugger = b.dependency("slugger", .{ .target = target, .optimize = optimize });
|
const slugger = b.dependency("slugger", .{ .target = target, .optimize = optimize });
|
||||||
|
const p2 = b.dependency("p2", .{ .target = target, .optimize = optimize });
|
||||||
|
const xxd = b.dependency("xxd", .{ .target = target, .optimize = optimize });
|
||||||
|
|
||||||
const core = b.addModule("core", .{
|
const core = b.addModule("core", .{
|
||||||
.root_source_file = b.path("src/core/core.zig"),
|
.root_source_file = b.path("src/core/core.zig"),
|
||||||
|
|
@ -12,6 +15,8 @@ pub fn build(b: *std.Build) void {
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.imports = &.{.{ .name = "slugger", .module = slugger.module("slugger") }},
|
.imports = &.{.{ .name = "slugger", .module = slugger.module("slugger") }},
|
||||||
});
|
});
|
||||||
|
core.addImport("p2", p2.module("p2"));
|
||||||
|
core.addImport("xxd", xxd.module("xxd"));
|
||||||
|
|
||||||
const core_tests = b.addTest(.{ .root_module = core });
|
const core_tests = b.addTest(.{ .root_module = core });
|
||||||
b.step("test", "Run core tests").dependOn(&b.addRunArtifact(core_tests).step);
|
b.step("test", "Run core tests").dependOn(&b.addRunArtifact(core_tests).step);
|
||||||
|
|
@ -20,14 +25,13 @@ pub fn build(b: *std.Build) void {
|
||||||
.root_module = b.createModule(.{
|
.root_module = b.createModule(.{
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.target = target,
|
.target = target,
|
||||||
.root_source_file = b.path("tools/interface-parser.zig"),
|
.root_source_file = b.path("tools/generate-api.zig"),
|
||||||
}),
|
}),
|
||||||
.name = "parser",
|
.name = "parser",
|
||||||
});
|
});
|
||||||
|
|
||||||
const parse_interface_core = b.addRunArtifact(parser);
|
const parse_interface_core = b.addRunArtifact(parser);
|
||||||
parse_interface_core.addArgs(&.{"--input"});
|
parse_interface_core.addFileArg2(b.path("src/core/api.tad"), .{ .prefix = "--input-file=" });
|
||||||
parse_interface_core.addFileInput(b.path("core/src/core.zig"));
|
|
||||||
|
|
||||||
const generate_headers_step = b.step("generate-interface", "generates interface information from all core apis");
|
const generate_headers_step = b.step("generate-interface", "generates interface information from all core apis");
|
||||||
|
|
||||||
|
|
@ -59,4 +63,51 @@ pub fn build(b: *std.Build) void {
|
||||||
|
|
||||||
build_tools.dependOn(&install.step);
|
build_tools.dependOn(&install.step);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create a small program
|
||||||
|
// this will contain two parts
|
||||||
|
// 1. the 'engine' once it goes up it creates several engine objects including the engine
|
||||||
|
// itself and it constructs api structs for programs to consume
|
||||||
|
//
|
||||||
|
// 2. the 'program' hot reloadable dynamic library.
|
||||||
|
// links nothing but the interfaces provided by the 'engine'
|
||||||
|
//
|
||||||
|
// and exports it's own interfaces to the engine. engine calls it's 'entry point'
|
||||||
|
{
|
||||||
|
const exe = b.addExecutable(.{ .name = "testmain", .root_module = b.createModule(.{
|
||||||
|
.optimize = optimize,
|
||||||
|
.target = target,
|
||||||
|
.root_source_file = b.path("src/main.zig"),
|
||||||
|
}) });
|
||||||
|
b.installArtifact(exe);
|
||||||
|
exe.root_module.addImport("core", core);
|
||||||
|
|
||||||
|
const testmain = b.addRunArtifact(exe);
|
||||||
|
testmain.addArg("--cli-test");
|
||||||
|
testmain.step.dependOn(b.getInstallStep());
|
||||||
|
b.step("run-testmain", "testing the testmain").dependOn(&testmain.step);
|
||||||
|
|
||||||
|
// now that we've built main it's time to build a hot library
|
||||||
|
const hotlib = b.addLibrary(.{
|
||||||
|
.name = "game",
|
||||||
|
.root_module = b.createModule(.{
|
||||||
|
.optimize = optimize,
|
||||||
|
.target = target,
|
||||||
|
.root_source_file = b.path("programs/sample-program.zig"),
|
||||||
|
}),
|
||||||
|
.linkage = .dynamic, // todo static builds
|
||||||
|
});
|
||||||
|
hotlib.bundle_ubsan_rt = false;
|
||||||
|
|
||||||
|
const inst = b.addInstallArtifact(hotlib, .{
|
||||||
|
.dest_sub_path = b.fmt("{s}.pod{s}", .{
|
||||||
|
"game",
|
||||||
|
target.result.dynamicLibSuffix(),
|
||||||
|
}),
|
||||||
|
.dest_dir = .{ .override = .{
|
||||||
|
.custom = "pods",
|
||||||
|
} },
|
||||||
|
});
|
||||||
|
b.getInstallStep().dependOn(&inst.step);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,10 @@
|
||||||
.version = "0.0.0",
|
.version = "0.0.0",
|
||||||
.minimum_zig_version = "0.17.0",
|
.minimum_zig_version = "0.17.0",
|
||||||
.dependencies = .{
|
.dependencies = .{
|
||||||
.slugger = .{ .path = "lib/slugger" },
|
.slugger = .{ .path = "pkg/slugger" },
|
||||||
.p2 = .{ .path = "lib/p2" },
|
.p2 = .{ .path = "pkg/p2" },
|
||||||
.lz4 = .{ .path = "lib/lz4" },
|
.lz4 = .{ .path = "pkg/lz4" },
|
||||||
|
.xxd = .{ .path = "pkg/xxd" },
|
||||||
},
|
},
|
||||||
.paths = .{ "build.zig", "build.zig.zon", "src", "lib", "tools", "programs" },
|
.paths = .{ "build.zig", "build.zig.zon", "src", "lib", "tools", "programs" },
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,7 @@ pub const BumpArenaAdvanced = @import("structures/bump-arena.zig").BumpArenaAdva
|
||||||
pub const BumpCheckpoint = @import("structures/bump-arena.zig").BumpCheckpoint;
|
pub const BumpCheckpoint = @import("structures/bump-arena.zig").BumpCheckpoint;
|
||||||
|
|
||||||
pub const pscopes = @import("structures/pscopes.zig");
|
pub const pscopes = @import("structures/pscopes.zig");
|
||||||
|
pub const ScopesContext = pscopes.ScopesContext;
|
||||||
pub const xxd = @import("structures/xxd.zig");
|
|
||||||
|
|
||||||
pub const xxdWrite = xxd.xxdWrite;
|
|
||||||
|
|
||||||
const ArrayList = std.ArrayList;
|
const ArrayList = std.ArrayList;
|
||||||
const ArrayListUnmanaged = std.ArrayListUnmanaged;
|
const ArrayListUnmanaged = std.ArrayListUnmanaged;
|
||||||
|
|
@ -78,7 +75,6 @@ pub fn sharedLibNameAlloc(allocator: std.mem.Allocator, s: []const u8) ![]u8 {
|
||||||
}
|
}
|
||||||
|
|
||||||
comptime {
|
comptime {
|
||||||
std.testing.refAllDecls(xxd);
|
|
||||||
std.testing.refAllDecls(ring_queue);
|
std.testing.refAllDecls(ring_queue);
|
||||||
std.testing.refAllDecls(index_pool);
|
std.testing.refAllDecls(index_pool);
|
||||||
std.testing.refAllDecls(concurrent_queue);
|
std.testing.refAllDecls(concurrent_queue);
|
||||||
|
|
@ -262,6 +262,7 @@ pub fn xxdWrite(writer: *std.Io.Writer, bytes: []const u8, options: XxdOptions)
|
||||||
addr += stride;
|
addr += stride;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fn writeByteAsHex2(byte: u8, writer: *std.Io.Writer, lower: bool) std.Io.Writer.Error!void {
|
inline fn writeByteAsHex2(byte: u8, writer: *std.Io.Writer, lower: bool) std.Io.Writer.Error!void {
|
||||||
const digits = if (lower) "0123456789abcdef" else "0123456789ABCDEF";
|
const digits = if (lower) "0123456789abcdef" else "0123456789ABCDEF";
|
||||||
try writer.writeAll(&.{ digits[byte >> 4], digits[byte & 0xf] });
|
try writer.writeAll(&.{ digits[byte >> 4], digits[byte & 0xf] });
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
// hand bombed zig version of the api
|
||||||
|
pub const Verbosity = enum(c_int) {
|
||||||
|
verbose,
|
||||||
|
info,
|
||||||
|
warn,
|
||||||
|
err,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// desc: a thing representing a zig slice
|
||||||
|
pub const Slice = extern struct {
|
||||||
|
ptr: [*c]const u8,
|
||||||
|
len: usize,
|
||||||
|
|
||||||
|
pub fn make(s: []const u8) @This() {
|
||||||
|
return .{ .ptr = s.ptr, .len = s.len };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bytes(self: @This()) []const u8 {
|
||||||
|
var s: []const u8 = undefined;
|
||||||
|
s.len = self.len;
|
||||||
|
s.ptr = @ptrCast(self.ptr);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const expected_version: u32 = 0;
|
||||||
|
|
||||||
|
const core_api = extern struct {
|
||||||
|
version: u32,
|
||||||
|
get_engine: *const fn () callconv(.c) ?*anyopaque,
|
||||||
|
get_tag_registry: *const fn () callconv(.c) ?*anyopaque,
|
||||||
|
start: *const fn () callconv(.c) void,
|
||||||
|
shutdown: *const fn () callconv(.c) void,
|
||||||
|
declare_logging_category: *const fn (name: Slice, default_verbosity: Verbosity) callconv(.c) u32,
|
||||||
|
log_puts: *const fn (category: u32, verbosity: Verbosity, string: Slice) callconv(.c) void,
|
||||||
|
};
|
||||||
|
|
||||||
|
var core: *core_api = undefined;
|
||||||
|
// -- end of handbomb
|
||||||
|
|
||||||
|
pub export fn entrypoint(_core: *core_api) callconv(.c) i32 {
|
||||||
|
core = _core;
|
||||||
|
const category = core.declare_logging_category(Slice.make("test"), .warn);
|
||||||
|
core.log_puts(category, .info, Slice.make("Game startup is called"));
|
||||||
|
core.log_puts(category, .info, Slice.make("Game startup is called2"));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
# tiny api definition - tadpole
|
||||||
|
# reads linearly from top to down 4 spaces denotes an indent
|
||||||
|
#
|
||||||
|
# comments are skipped/stripped,
|
||||||
|
#
|
||||||
|
# comment := # [.*] \n
|
||||||
|
#
|
||||||
|
# root := [declaration+]
|
||||||
|
# declaration := [comment] (enum | struct | moduledef | functiondef)
|
||||||
|
# enum := "enum" label [enumdef+]
|
||||||
|
# enumdef := label ["=" integer]
|
||||||
|
#
|
||||||
|
# struct := "struct" label [field+]
|
||||||
|
# field := [docstring](label ":" typedef)
|
||||||
|
# typedef := ([pointertype] builtin) | label
|
||||||
|
#
|
||||||
|
# pointertype := c_ptr | const_c_ptr
|
||||||
|
# builtin := "usize" | "i8" | "u8" | "i32" | "u32" | "i64" | "u64" | "opaque"
|
||||||
|
#
|
||||||
|
# moduledef := "module" label [moduleoptions]
|
||||||
|
# moduleoptions := label string | integer | label
|
||||||
|
#
|
||||||
|
# functiondef := "fn" label "->" [([pointertype] builtin) | "void"] [field+]
|
||||||
|
#
|
||||||
|
|
||||||
|
module core
|
||||||
|
@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
|
||||||
|
len: usize
|
||||||
|
|
||||||
|
struct Slice
|
||||||
|
ptr: const_c_ptr u8
|
||||||
|
len: usize
|
||||||
|
|
||||||
|
fn get_engine -> c_ptr opaque
|
||||||
|
fn get_tag_registry -> c_ptr opaque
|
||||||
|
fn start -> void
|
||||||
|
fn shutdown -> void
|
||||||
|
|
||||||
|
fn declare_logging_category -> u32
|
||||||
|
name: Slice
|
||||||
|
default_verbosity: Verbosity
|
||||||
|
|
||||||
|
fn log_puts -> void
|
||||||
|
category: u32
|
||||||
|
verbosity: Verbosity
|
||||||
|
string: Slice
|
||||||
|
|
@ -4,17 +4,33 @@
|
||||||
// - snake_case for functions intended to be exported to the api
|
// - snake_case for functions intended to be exported to the api
|
||||||
//
|
//
|
||||||
// a file like core.zig (this file) exists so we can generate a c api from it later.
|
// a file like core.zig (this file) exists so we can generate a c api from it later.
|
||||||
|
//
|
||||||
|
// some notes about the c api generation
|
||||||
|
//
|
||||||
|
// /// 3 comment slashes are used to denote that it is
|
||||||
|
|
||||||
|
/// General verbosity level of logging
|
||||||
|
pub const Verbosity = enum(c_int) {
|
||||||
|
verbose,
|
||||||
|
info,
|
||||||
|
warn,
|
||||||
|
err,
|
||||||
|
|
||||||
|
pub fn toString(self: @This()) []const u8 {
|
||||||
|
return @tagName(self);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// desc: a thing representing a zig slice
|
/// desc: a thing representing a zig slice
|
||||||
pub const Slice = extern struct {
|
pub const SliceMutable = extern struct {
|
||||||
ptr: [*c]u8,
|
ptr: [*c]u8,
|
||||||
len: usize,
|
len: usize,
|
||||||
|
|
||||||
pub fn fromZ(s: []const u8) @This() {
|
pub fn make(s: []const u8) @This() {
|
||||||
return .{ .ptr = s.ptr, .len = s.len };
|
return .{ .ptr = s.ptr, .len = s.len };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn toSlice(self: @This()) []u8 {
|
pub fn bytes(self: @This()) []u8 {
|
||||||
var s: []u8 = undefined;
|
var s: []u8 = undefined;
|
||||||
s.len = self.len;
|
s.len = self.len;
|
||||||
s.ptr = @ptrCast(self.ptr);
|
s.ptr = @ptrCast(self.ptr);
|
||||||
|
|
@ -22,6 +38,23 @@ pub const Slice = extern struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// desc: a thing representing a zig slice
|
||||||
|
pub const Slice = extern struct {
|
||||||
|
ptr: [*c]const u8,
|
||||||
|
len: usize,
|
||||||
|
|
||||||
|
pub fn make(s: []const u8) @This() {
|
||||||
|
return .{ .ptr = s.ptr, .len = s.len };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bytes(self: @This()) []const u8 {
|
||||||
|
var s: []const u8 = undefined;
|
||||||
|
s.len = self.len;
|
||||||
|
s.ptr = @ptrCast(self.ptr);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// desc: gets a pointer to the actual engine instance
|
/// desc: gets a pointer to the actual engine instance
|
||||||
///
|
///
|
||||||
/// this will be used by dynamically loaded modules discover and register functionality
|
/// this will be used by dynamically loaded modules discover and register functionality
|
||||||
|
|
@ -35,7 +68,9 @@ pub export fn get_tag_registry() callconv(.c) ?*anyopaque {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub export fn core_start() callconv(.c) void {
|
pub export fn start() callconv(.c) void {
|
||||||
|
// eng.init();
|
||||||
|
//
|
||||||
// const eng = engine.get();
|
// const eng = engine.get();
|
||||||
// const io = eng.io;
|
// const io = eng.io;
|
||||||
// const allocator = eng.systemAllocator;
|
// const allocator = eng.systemAllocator;
|
||||||
|
|
@ -43,35 +78,103 @@ pub export fn core_start() callconv(.c) void {
|
||||||
// _ = allocator;
|
// _ = allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub export fn core_declare_logging_category(name: [*c]u8, default_verbosity: Verbosity) void {
|
pub export fn declare_logging_category(name: Slice, default_verbosity: Verbosity) callconv(.c) u32 {
|
||||||
_ = name;
|
println("logging category defined: {s} default verbosity: {s}", .{ name.bytes(), @tagName(default_verbosity) });
|
||||||
_ = default_verbosity;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const Verbosity = enum(c_int) {
|
var writeBuffer: [4096]u8 = undefined;
|
||||||
verbose,
|
var fwriter: std.Io.File.Writer = undefined;
|
||||||
info,
|
|
||||||
warn,
|
pub fn setupPrint(io: std.Io) void {
|
||||||
err,
|
fwriter = std.Io.File.stdout().writer(io, &writeBuffer);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
pub fn print(comptime fmt: []const u8, args: anytype) void {
|
||||||
|
fwriter.interface.print(fmt, args) catch return;
|
||||||
|
fwriter.interface.flush() catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn println(comptime fmt: []const u8, args: anytype) void {
|
||||||
|
fwriter.interface.print(fmt ++ "\n", args) catch return;
|
||||||
|
fwriter.interface.flush() catch {};
|
||||||
|
}
|
||||||
|
|
||||||
/// desc: pushes a string to the logging system
|
/// desc: pushes a string to the logging system
|
||||||
pub export fn core_log_puts(category: u32, verbosity: Verbosity, string: [*c]u8) callconv(.c) void {
|
pub export fn log_puts(category: u32, verbosity: Verbosity, string: Slice) callconv(.c) void {
|
||||||
_ = category;
|
_ = category;
|
||||||
_ = verbosity;
|
// using this for now.
|
||||||
_ = string;
|
println("[{s:>6}][{s:>4}]: {s}", .{ "debug", verbosity.toString(), string.bytes() });
|
||||||
}
|
}
|
||||||
|
|
||||||
pub export fn core_shutdown() callconv(.c) void {}
|
pub export fn shutdown() callconv(.c) void {
|
||||||
|
engine.gEngine.deinit();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const core_api = extern struct {
|
||||||
|
version: u32,
|
||||||
|
get_engine: *const fn () callconv(.c) ?*anyopaque,
|
||||||
|
get_tag_registry: *const fn () callconv(.c) ?*anyopaque,
|
||||||
|
start: *const fn () callconv(.c) void,
|
||||||
|
shutdown: *const fn () callconv(.c) void,
|
||||||
|
declare_logging_category: *const fn (name: Slice, default_verbosity: Verbosity) callconv(.c) u32,
|
||||||
|
log_puts: *const fn (category: u32, verbosity: Verbosity, string: Slice) callconv(.c) void,
|
||||||
|
};
|
||||||
|
pub var gApi: core_api = undefined;
|
||||||
|
|
||||||
// non exported functions below here, used by the zig runtime.
|
// non exported functions below here, used by the zig runtime.
|
||||||
pub fn startEngine(io: std.Io, systemAllocator: std.mem.Allocator) void {
|
// get minimal from juicymain's init.minimal
|
||||||
|
pub fn startEngine(io: std.Io, systemAllocator: std.mem.Allocator, minimal: std.process.Init.Minimal) !void {
|
||||||
// create the engine registry
|
// create the engine registry
|
||||||
engine.initialize(io, systemAllocator);
|
try engine.gEngine.init(io, systemAllocator, minimal);
|
||||||
|
|
||||||
|
gApi = .{
|
||||||
|
.version = 0, // these really won't matter until much later.
|
||||||
|
.shutdown = shutdown,
|
||||||
|
.get_engine = get_engine,
|
||||||
|
.get_tag_registry = get_tag_registry,
|
||||||
|
.start = start,
|
||||||
|
.declare_logging_category = declare_logging_category,
|
||||||
|
.log_puts = log_puts,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const engine = @import("engine.zig");
|
pub fn getEngine() *engine.Engine {
|
||||||
|
return &engine.gEngine;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const SmallString = extern struct {
|
||||||
|
len: u8,
|
||||||
|
buffer: [64]u8,
|
||||||
|
pub fn bytes(self: *const @This()) []const u8 {
|
||||||
|
return self.buffer[0..self.len];
|
||||||
|
}
|
||||||
|
pub fn fmt(comptime format: []const u8, args: anytype) !@This() {
|
||||||
|
var s: @This() = undefined;
|
||||||
|
const x = try std.fmt.bufPrint(&s.buffer, format, args);
|
||||||
|
s.len = @intCast(x.len);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const TinyString = extern struct {
|
||||||
|
len: u8,
|
||||||
|
buffer: [32]u8,
|
||||||
|
pub inline fn bytes(self: *const @This()) []const u8 {
|
||||||
|
return self.buffer[0..self.len];
|
||||||
|
}
|
||||||
|
pub inline fn fmt(comptime format: []const u8, args: anytype) !@This() {
|
||||||
|
var s: @This() = undefined;
|
||||||
|
const x = try std.fmt.bufPrint(&s.buffer, format, args);
|
||||||
|
s.len = @intCast(x.len);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const engine = @import("engine.zig");
|
||||||
pub const slugger = @import("slugger");
|
pub const slugger = @import("slugger");
|
||||||
pub const p2 = @import("p2");
|
pub const p2 = @import("p2");
|
||||||
|
pub const WinLib = @import("winlib.zig");
|
||||||
|
|
||||||
|
pub const DeclareTagPool = p2.DeclareTagPool;
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
// engine is the core registry for our hot
|
// engine is the core registry for our hot
|
||||||
// reloading
|
// reloading
|
||||||
//
|
//
|
||||||
// unlike old ribtech the engine does not bring a runtime.
|
// you as the user of the engine bring your own runtime. You can think of tadpole like an interface
|
||||||
// you as the user of the engine bring your own,
|
// description for an operating system
|
||||||
//
|
//
|
||||||
// I can provide a standard interface for accessing and
|
// I can provide a standard interface for accessing and
|
||||||
// creating threadpools and memory,
|
// creating threadpools and memory,
|
||||||
|
|
@ -15,10 +15,53 @@
|
||||||
// modify and tear down systems for modification
|
// modify and tear down systems for modification
|
||||||
pub const Engine = struct {
|
pub const Engine = struct {
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
|
scopes: *p2.ScopesContext,
|
||||||
|
backing: std.mem.Allocator,
|
||||||
|
safe: std.heap.SafeAllocator, // safe validating allocator for most things.
|
||||||
|
arena: std.heap.ArenaAllocator, // TODO [assign tags]: build a memory profiling tracker that wraps this one
|
||||||
|
engineObjects: std.ArrayList(EngineObject) = .empty,
|
||||||
|
|
||||||
// engineObjects: std.ArrayList(EngineObjectRef),
|
lastFrameStampNs: i96,
|
||||||
|
dt: f64 = 0,
|
||||||
|
minimal: std.process.Init.Minimal,
|
||||||
|
|
||||||
|
frameCount: u64 = 0,
|
||||||
|
|
||||||
|
// incomming allocator must be threadsafe
|
||||||
|
pub fn init(self: *@This(), io: std.Io, allocator: std.mem.Allocator, minimal: std.process.Init.Minimal) !void {
|
||||||
|
self.* = .{
|
||||||
|
.io = io,
|
||||||
|
.backing = allocator,
|
||||||
|
.minimal = minimal,
|
||||||
|
.arena = std.heap.ArenaAllocator.init(allocator),
|
||||||
|
.safe = std.heap.SafeAllocator.init(allocator, .{
|
||||||
|
.stack_trace_frames = 20,
|
||||||
|
}),
|
||||||
|
.scopes = undefined,
|
||||||
|
.lastFrameStampNs = std.Io.Clock.Timestamp.now(io, .awake).raw.nanoseconds,
|
||||||
|
};
|
||||||
|
self.scopes = try .create(self.safe.allocator(), io);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tick(self: *@This()) void {
|
||||||
|
const newTime = std.Io.Clock.Timestamp.now(self.io, .awake);
|
||||||
|
const delta = newTime.raw.nanoseconds - self.lastFrameStampNs;
|
||||||
|
self.lastFrameStampNs = newTime.raw.nanoseconds;
|
||||||
|
self.dt = @as(f64, @floatFromInt(delta)) / 1e9;
|
||||||
|
|
||||||
|
std.debug.print("frame: {} deltaTime: {}\n", .{ self.frameCount, self.dt });
|
||||||
|
self.frameCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *@This()) void {
|
||||||
|
self.scopes.destroy();
|
||||||
|
self.arena.deinit();
|
||||||
|
_ = self.safe.deinitLog(true);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const EngineObjectRef = struct {};
|
pub var gEngine: Engine = undefined;
|
||||||
|
|
||||||
|
const EngineObject = @import("types/EngineObject.zig");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const p2 = @import("p2");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,13 @@
|
||||||
ptr: *anyopaque,
|
ptr: *anyopaque,
|
||||||
|
interface: *EngineObjectInterface,
|
||||||
name: TagPool.Tag,
|
name: TagPool.Tag,
|
||||||
|
|
||||||
|
const EngineObjectInterface = extern struct {
|
||||||
|
init: *const fn (*anyopaque, *EngineContext) callconv(.c) bool,
|
||||||
|
deinit: *const fn (*anyopaque) callconv(.c) void,
|
||||||
|
};
|
||||||
|
|
||||||
|
const EngineContext = opaque {};
|
||||||
|
|
||||||
const TagPool = core.DeclareTagPool("EngineObject");
|
const TagPool = core.DeclareTagPool("EngineObject");
|
||||||
const core = @import("../core.zig");
|
const core = @import("../core.zig");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
handle: windows.PVOID,
|
||||||
|
|
||||||
|
pub fn open(path: []const u8) !@This() {
|
||||||
|
var fbab: [4096]u8 = undefined;
|
||||||
|
var fba = std.heap.FixedBufferAllocator.init(&fbab);
|
||||||
|
const path_w = try std.unicode.wtf8ToWtf16LeAlloc(fba.allocator(), path);
|
||||||
|
|
||||||
|
var handle: windows.PVOID = undefined;
|
||||||
|
switch (windows.ntdll.LdrLoadDll(null, null, &.init(path_w), &handle)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.DLL_NOT_FOUND => return error.DllNotFound,
|
||||||
|
else => |status| return windows.unexpectedStatus(status),
|
||||||
|
}
|
||||||
|
return .{ .handle = handle };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lookup(self: *@This(), comptime T: type, name: [:0]const u8) ?T {
|
||||||
|
var addr: windows.PVOID = undefined;
|
||||||
|
switch (windows.ntdll.LdrGetProcedureAddress(self.handle, &.init(name), 0, &addr)) {
|
||||||
|
.SUCCESS => return @ptrCast(@alignCast(addr)),
|
||||||
|
else => return null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn close(self: *@This()) void {
|
||||||
|
_ = windows.ntdll.LdrUnloadDll(self.handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const windows = std.os.windows;
|
||||||
|
|
@ -0,0 +1,236 @@
|
||||||
|
fn ensureInputTxt(io: std.Io) !void {
|
||||||
|
const cwd = std.Io.Dir.cwd();
|
||||||
|
const fpath = try cwd.createDirPathOpen(io, "zig-out", .{});
|
||||||
|
const s = fpath.statFile(io, "input.txt", .{}) catch {
|
||||||
|
_ = try fpath.createFile(io, "input.txt", .{});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
_ = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main(init: std.process.Init) !void {
|
||||||
|
const io = init.io;
|
||||||
|
const allocator = std.heap.page_allocator;
|
||||||
|
|
||||||
|
// engine level initialization
|
||||||
|
try core.startEngine(io, allocator, init.minimal);
|
||||||
|
defer core.shutdown();
|
||||||
|
|
||||||
|
core.setupPrint(io);
|
||||||
|
|
||||||
|
core.print("press h or 'help' for help \n> ", .{});
|
||||||
|
|
||||||
|
const argv = try init.minimal.args.toSlice(init.arena.allocator());
|
||||||
|
|
||||||
|
var cliTest: bool = false;
|
||||||
|
for (argv) |a| {
|
||||||
|
if (std.mem.startsWith(u8, a, "--cli-test")) cliTest = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var readBuffer: [4096]u8 = undefined;
|
||||||
|
var openedFile: ?std.Io.File = null;
|
||||||
|
const f = f: {
|
||||||
|
if (!cliTest) {
|
||||||
|
break :f std.Io.File.stdin();
|
||||||
|
}
|
||||||
|
try ensureInputTxt(io);
|
||||||
|
openedFile = try std.Io.Dir.cwd().openFile(io, "zig-out/input.txt", .{});
|
||||||
|
break :f openedFile.?;
|
||||||
|
};
|
||||||
|
var freader = f.reader(io, &readBuffer);
|
||||||
|
var r = &freader.interface;
|
||||||
|
|
||||||
|
loadedFingerPrints = .init(init.arena.allocator());
|
||||||
|
|
||||||
|
loop: while (true) {
|
||||||
|
if (cliTest and core.engine.gEngine.frameCount == 10)
|
||||||
|
break :loop;
|
||||||
|
|
||||||
|
const l = try r.takeDelimiter('\n') orelse {
|
||||||
|
core.engine.gEngine.tick();
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
const line = std.mem.trim(u8, l, "\r\n ");
|
||||||
|
|
||||||
|
core.engine.gEngine.tick();
|
||||||
|
|
||||||
|
if (line.len > 0) {
|
||||||
|
if (Command.parse(line)) |c| {
|
||||||
|
if (mem.eql(u8, c.name, "quit")) {
|
||||||
|
break :loop;
|
||||||
|
} else if (mem.eql(u8, c.name, "help")) {
|
||||||
|
usage();
|
||||||
|
} else if (c.func) |func| {
|
||||||
|
const args = blk: {
|
||||||
|
const x = std.mem.cutScalar(u8, line, ' ') orelse break :blk "";
|
||||||
|
break :blk x[1];
|
||||||
|
};
|
||||||
|
const result = func(args);
|
||||||
|
if (result != 0)
|
||||||
|
println("Error! code: {d}", .{result});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println("Error! unknown command {s}", .{line});
|
||||||
|
usage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
core.print("> ", .{});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const commands = [_]Command{
|
||||||
|
.{
|
||||||
|
.name = "quit",
|
||||||
|
.desc = "exits the engine",
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.name = "help",
|
||||||
|
.desc = "prints this help dialogue",
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.name = "list",
|
||||||
|
.desc = "lists available pods for hot reloading (default; searches zig-out/pods)",
|
||||||
|
.func = listPods,
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.name = "load",
|
||||||
|
.desc = "loads the selected pod by name",
|
||||||
|
.func = loadPod,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const Command = struct {
|
||||||
|
name: []const u8,
|
||||||
|
desc: []const u8,
|
||||||
|
func: ?*const fn (line: []const u8) i32 = null,
|
||||||
|
|
||||||
|
pub fn parse(line: []const u8) ?*const @This() {
|
||||||
|
for (&commands) |*cmd| {
|
||||||
|
if (checkCommand(line, cmd.name)) {
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn usage() void {
|
||||||
|
for (commands) |cmd| {
|
||||||
|
println("{s}/{s} - {s}", .{ cmd.name, cmd.name[0..1], cmd.desc });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn checkCommand(line: []const u8, arg: []const u8) bool {
|
||||||
|
const startsWith = std.mem.startsWith;
|
||||||
|
return startsWith(u8, line, arg) or (startsWith(u8, line, arg[0..1]) and line.len == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var podLexoCountId: u32 = 1;
|
||||||
|
var loadedFingerPrints: std.AutoHashMap(u32, void) = undefined;
|
||||||
|
|
||||||
|
fn loadPod(args: []const u8) i32 {
|
||||||
|
var buffer: [64]u8 = undefined;
|
||||||
|
const podName = args;
|
||||||
|
|
||||||
|
const io = core.getEngine().io;
|
||||||
|
const cwd = std.Io.Dir.cwd();
|
||||||
|
println("loading pod `{s}` ...", .{args});
|
||||||
|
|
||||||
|
const podsDir = cwd.openDir(io, "zig-out/pods", .{}) catch return -1;
|
||||||
|
|
||||||
|
const podFile = std.fmt.bufPrint(&buffer, "{s}.pod{s}", .{ args, builtin.target.dynamicLibSuffix() }) catch {
|
||||||
|
println("pod file name too long", .{});
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
const stat = podsDir.statFile(io, podFile, .{}) catch {
|
||||||
|
println("Unable to load pod: '{s}' '{s}'", .{ args, podFile });
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
// write me a function to turn a u32 into a 8 digit hex
|
||||||
|
const fingerprint: u32 = @intCast(((stat.mtime.nanoseconds >> 32) & 0xfffffff) ^ (stat.mtime.nanoseconds & 0xffffff));
|
||||||
|
|
||||||
|
if (loadedFingerPrints.contains(fingerprint)) {
|
||||||
|
println("skipping load, already loaded fingerprint: {s}", .{hex8(fingerprint)});
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
loadedFingerPrints.put(fingerprint, {}) catch return -1;
|
||||||
|
const sizeinfloat: f64 = @floatFromInt(stat.size);
|
||||||
|
|
||||||
|
println("pod found {s} mtimeHash: {s} size: {d} kb", .{ podFile, hex8(fingerprint)[0..], sizeinfloat / 1024 });
|
||||||
|
|
||||||
|
const modCachePath = core.TinyString.fmt(".modulecache/{s}", .{podName}) catch return -1;
|
||||||
|
|
||||||
|
const targetDir = cwd.createDirPathOpen(io, modCachePath.bytes(), .{}) catch return -1;
|
||||||
|
|
||||||
|
const lexograph = blk: {
|
||||||
|
var lexo: core.TinyString = core.TinyString.fmt("a{d}", .{podLexoCountId}) catch return -1;
|
||||||
|
const digits: u8 = @intCast(std.math.log10(podLexoCountId));
|
||||||
|
lexo.buffer[0] = 'a' + digits;
|
||||||
|
break :blk lexo;
|
||||||
|
};
|
||||||
|
|
||||||
|
const outputPath = core.SmallString.fmt("{s}-{s}", .{ lexograph.bytes(), podFile }) catch return -1;
|
||||||
|
println("will be installed as {s}", .{outputPath.bytes()});
|
||||||
|
podsDir.copyFile(podFile, targetDir, outputPath.bytes(), io, .{}) catch return -1;
|
||||||
|
|
||||||
|
podLexoCountId += 1;
|
||||||
|
|
||||||
|
var fullFileBuffer: [4096]u8 = undefined;
|
||||||
|
var ffba = std.heap.FixedBufferAllocator.init(&fullFileBuffer);
|
||||||
|
const fullPath = targetDir.realPathFileAlloc(io, outputPath.bytes(), ffba.allocator()) catch return -1;
|
||||||
|
println("loading file {s}", .{fullPath});
|
||||||
|
|
||||||
|
const entrypointFn = *const fn (_core: *core.core_api) callconv(.c) i32;
|
||||||
|
|
||||||
|
var lib = blk: {
|
||||||
|
if (builtin.os.tag == .windows) {
|
||||||
|
break :blk core.WinLib.open(fullPath) catch return -1;
|
||||||
|
} else {
|
||||||
|
break :blk std.DynLib.open(fullPath) catch return -1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (lib.lookup(entrypointFn, "entrypoint")) |func| {
|
||||||
|
if (func(&core.gApi) == 0)
|
||||||
|
println("module load success!", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hex8(value: u32) [8]u8 {
|
||||||
|
var out: [8]u8 = undefined;
|
||||||
|
_ = std.fmt.bufPrint(&out, "{x:0>8}", .{value}) catch unreachable; // always exactly 8 bytes
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn listPods(args: []const u8) i32 {
|
||||||
|
_ = args; // not used for this one
|
||||||
|
const io = core.getEngine().io;
|
||||||
|
const cwd = std.Io.Dir.cwd();
|
||||||
|
|
||||||
|
const podsDir = cwd.openDir(io, "zig-out/pods", .{ .iterate = true }) catch return -1;
|
||||||
|
var podCount: usize = 0;
|
||||||
|
var iterate = podsDir.iterate();
|
||||||
|
while (iterate.next(io) catch return -1) |entry| {
|
||||||
|
if (entry.kind != .file) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mem.find(u8, entry.name, ".pod")) |offset| {
|
||||||
|
_ = offset;
|
||||||
|
println("pod found stat: {s}", .{entry.name});
|
||||||
|
podCount += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println("{d} pod(s) found", .{podCount});
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mem = std.mem;
|
||||||
|
const core = @import("core");
|
||||||
|
const println = core.println;
|
||||||
|
const std = @import("std");
|
||||||
|
const builtin = @import("builtin");
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
generate-api
|
||||||
|
|
||||||
|
takes an input file and outputs source and output zig files
|
||||||
|
input-file - input .tad file to parse
|
||||||
|
help - shows the help dialogue
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
pub fn SimpleArgParse(comptime prog_name: []const u8, comptime help_text: []const u8) type {
|
||||||
|
return struct {
|
||||||
|
var gArgsMap: std.StringHashMap([]const u8) = undefined;
|
||||||
|
pub var parsedOne: bool = false;
|
||||||
|
|
||||||
|
pub fn initArgsMap(gpa: std.mem.Allocator) void {
|
||||||
|
gArgsMap = .init(gpa);
|
||||||
|
}
|
||||||
|
|
||||||
|
const helpfile = prog_name ++ ".help.txt";
|
||||||
|
|
||||||
|
pub fn writeArgsMap(io: std.Io) !void {
|
||||||
|
var d = try std.Io.Dir.cwd().createDirPathOpen(io, ".helpfiles/", .{});
|
||||||
|
var f = try d.createFile(io, helpfile, .{});
|
||||||
|
defer f.close(io);
|
||||||
|
|
||||||
|
var b: [1024]u8 = undefined;
|
||||||
|
var fwriter = f.writer(io, &b);
|
||||||
|
const w = &fwriter.interface;
|
||||||
|
try w.writeAll(help_text);
|
||||||
|
var iter = gArgsMap.iterator();
|
||||||
|
|
||||||
|
while (iter.next()) |e| {
|
||||||
|
try w.print("{s:>16} - {s}\n", .{ e.key_ptr.*, e.value_ptr.* });
|
||||||
|
}
|
||||||
|
try w.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn printHelp(io: std.Io) !void {
|
||||||
|
var d = try std.Io.Dir.cwd().createDirPathOpen(io, ".helpfiles/", .{});
|
||||||
|
|
||||||
|
d.access(io, helpfile, .{}) catch {
|
||||||
|
writeArgsMap(io) catch {
|
||||||
|
std.debug.print("help file generation failed", .{});
|
||||||
|
return error.FailedToWriteFile;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var rbuf: [64 * 1024]u8 = undefined;
|
||||||
|
const helpcontents = try d.readFile(io, helpfile, &rbuf);
|
||||||
|
|
||||||
|
var b: [1024]u8 = undefined;
|
||||||
|
var fwriter = std.Io.File.stdout().writer(io, &b);
|
||||||
|
const w = &fwriter.interface;
|
||||||
|
|
||||||
|
try w.writeAll(helpcontents);
|
||||||
|
try w.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cliArgParse(x: [*:0]const u8, arg: []const u8, description: []const u8) ?[]const u8 {
|
||||||
|
const s = std.mem.span(x);
|
||||||
|
|
||||||
|
if (!gArgsMap.contains(arg)) {
|
||||||
|
gArgsMap.put(arg, description) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const search = mem.trim(u8, s, "-");
|
||||||
|
|
||||||
|
var right: []const u8 = "";
|
||||||
|
const left = blk: {
|
||||||
|
if (mem.find(u8, search, "=")) |sep| {
|
||||||
|
right = search[sep + 1 ..];
|
||||||
|
break :blk search[0..sep];
|
||||||
|
}
|
||||||
|
break :blk search;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (mem.eql(u8, left, arg)) {
|
||||||
|
parsedOne = true;
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const mem = std.mem;
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
// 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
|
||||||
|
\\
|
||||||
|
\\takes an input file and outputs source and output zig files
|
||||||
|
\\
|
||||||
|
;
|
||||||
|
const AP = SimpleArgParse("generate-api", helpText);
|
||||||
|
|
||||||
|
pub fn main(init: std.process.Init) !void {
|
||||||
|
AP.initArgsMap(init.arena.allocator());
|
||||||
|
|
||||||
|
const argv = init.minimal.args.vector;
|
||||||
|
|
||||||
|
for (argv) |a| {
|
||||||
|
if (AP.cliArgParse(a, "help", "shows the help dialogue")) |arg| {
|
||||||
|
_ = arg;
|
||||||
|
try AP.printHelp(init.io);
|
||||||
|
}
|
||||||
|
if (AP.cliArgParse(a, "input-file", "input .tad file to parse")) |arg| {
|
||||||
|
log.info("parsing input file: {s}", .{arg});
|
||||||
|
try tad.compile(init.io, init.arena.allocator(), arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AP.writeArgsMap(init.io) catch unreachable;
|
||||||
|
if (!AP.parsedOne) {
|
||||||
|
try AP.printHelp(init.io);
|
||||||
|
}
|
||||||
|
log.info("done :)", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
const log = std.log.scoped(.generateApi);
|
||||||
|
const std = @import("std");
|
||||||
|
const mem = std.mem;
|
||||||
|
const SimpleArgParse = @import("common/args.zig").SimpleArgParse;
|
||||||
|
const tad = @import("tad/tad.zig");
|
||||||
|
|
@ -1,11 +1,42 @@
|
||||||
// this thing eats a rb top level zig file and generates a C header
|
// this thing eats a top level zig file and generates a C header
|
||||||
|
|
||||||
pub fn main(init: std.process.Init) !void {
|
pub fn main(init: std.process.Init) !void {
|
||||||
const args = init.minimal.args;
|
const args = init.minimal.args;
|
||||||
const argsSlice = try args.toSlice(init.arena.allocator());
|
const argsSlice = try args.toSlice(init.arena.allocator());
|
||||||
const io = init.io;
|
|
||||||
_ = io;
|
|
||||||
_ = argsSlice;
|
_ = argsSlice;
|
||||||
|
const io = init.io;
|
||||||
|
core.setupPrint(io);
|
||||||
|
const gpa = init.arena.allocator();
|
||||||
|
const source = try std.Io.Dir.cwd().readFileAllocOptions(io, args[1], gpa, .unlimited, .of(u8), 0);
|
||||||
|
|
||||||
|
var tree = try zig.Ast.parse(gpa, source, .{ .mode = .zig });
|
||||||
|
defer tree.deinit(gpa);
|
||||||
|
|
||||||
|
for (tree.rootDecls()) |decl| {
|
||||||
|
var buf: [12]zig.Ast.Node.Index = undefined;
|
||||||
|
const proto = tree.fullFnProto(&buf, decl) orelse continue;
|
||||||
|
const export_tok = proto.extern_export_inline_token orelse continue; // not an extern, move on
|
||||||
|
if (tree.tokenTag(export_tok) != .keyword_export) continue;
|
||||||
|
|
||||||
|
var first = tree.firstToken(decl);
|
||||||
|
while (first > 0 and tree.tokenTag(first - 1) == .doc_comment) first -= 1;
|
||||||
|
var t = first;
|
||||||
|
while (tree.tokenTag(t) == .doc_comment) : (t += 1) {
|
||||||
|
// skip over all docstrings
|
||||||
|
}
|
||||||
|
core.println("func detected: ", .{tree.tokenSlice(proto.name_token.?)});
|
||||||
|
|
||||||
|
var iter = proto.iterate(&tree);
|
||||||
|
while (iter.next()) |p| {
|
||||||
|
const name = if (p.name_token) |nametok| tree.tokenSlice(nametok) else "_";
|
||||||
|
core.printn(" {s}: {s}", .{ name, tree.getNodeSource(p.type_expr.?) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const ret = if (proto.ast.return_type.unwrap()) |r| tree.getNodeSource(r) else "?";
|
||||||
|
core.println(" returns {s}\n", .{ret});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const core = @import("core");
|
||||||
|
const zig = std.zig;
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,4 @@
|
||||||
pub var gargs: std.process.Args = undefined;
|
pub var gargs: []const [:0]const u8 = undefined;
|
||||||
pub threadlocal var iter: std.process.Args.Iterator = undefined;
|
|
||||||
|
|
||||||
pub fn beginArgs() void {
|
|
||||||
iter = gargs.iterate();
|
|
||||||
}
|
|
||||||
pub fn nextArg() ?[:0]const u8 {
|
|
||||||
return iter.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const Opts = struct {
|
pub const Opts = struct {
|
||||||
targetFile: ?[]const u8 = null,
|
targetFile: ?[]const u8 = null,
|
||||||
|
|
@ -18,11 +10,10 @@ pub const Opts = struct {
|
||||||
end,
|
end,
|
||||||
};
|
};
|
||||||
|
|
||||||
beginArgs();
|
|
||||||
var opts: Opts = .{};
|
var opts: Opts = .{};
|
||||||
|
|
||||||
var state: ParseState = .exe;
|
var state: ParseState = .exe;
|
||||||
while (nextArg()) |arg| {
|
for (gargs) |arg| {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
.exe => state = .targetFile,
|
.exe => state = .targetFile,
|
||||||
.targetFile => {
|
.targetFile => {
|
||||||
|
|
@ -61,7 +52,7 @@ pub fn usage() void {}
|
||||||
pub fn main(init: std.process.Init) !void {
|
pub fn main(init: std.process.Init) !void {
|
||||||
const io = init.io;
|
const io = init.io;
|
||||||
|
|
||||||
gargs = init.minimal.args;
|
gargs = try init.minimal.args.toSlice(init.arena.allocator());
|
||||||
|
|
||||||
const opts = Opts.parse();
|
const opts = Opts.parse();
|
||||||
if (opts.targetFile == null) {
|
if (opts.targetFile == null) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,590 @@
|
||||||
|
// normalizes a buffer in-place
|
||||||
|
fn normalizeContents(buffer: []u8) []u8 {
|
||||||
|
var cursor: usize = 0;
|
||||||
|
var wcursor: usize = 0;
|
||||||
|
while (cursor < buffer.len) : (cursor += 1) {
|
||||||
|
if (cursor + 2 > buffer.len) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (mem.eql(u8, buffer[cursor .. cursor + 2], "\r\n")) {
|
||||||
|
buffer[wcursor] = '\n';
|
||||||
|
cursor += 2;
|
||||||
|
wcursor += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buffer[wcursor] = buffer[cursor];
|
||||||
|
wcursor += 1;
|
||||||
|
}
|
||||||
|
return buffer[0..wcursor];
|
||||||
|
}
|
||||||
|
|
||||||
|
const BuiltinType = enum(u8) { usize, int, @"opaque", i8, u8, i32, u32, i64, u64 };
|
||||||
|
|
||||||
|
const StructDefinition = struct {
|
||||||
|
name: []const u8,
|
||||||
|
fields: std.ArrayList(Field),
|
||||||
|
};
|
||||||
|
|
||||||
|
const FunctionDefinition = struct {
|
||||||
|
name: []const u8,
|
||||||
|
args: std.ArrayList(Field) = .empty,
|
||||||
|
returnType: TypeRef,
|
||||||
|
};
|
||||||
|
|
||||||
|
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 {
|
||||||
|
name: []const u8,
|
||||||
|
typeReference: TypeRef,
|
||||||
|
docstring: ?[]const u8 = null,
|
||||||
|
};
|
||||||
|
|
||||||
|
const EnumDefnition = struct {
|
||||||
|
name: []const u8,
|
||||||
|
values: std.ArrayList(EnumValue),
|
||||||
|
docstring: ?[]const u8,
|
||||||
|
backingType: BuiltinType = .int,
|
||||||
|
allowAnyInt: bool = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return mem.eql(u8, a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
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(enum(u8)) {
|
||||||
|
term: TerminalKind,
|
||||||
|
label: void,
|
||||||
|
comment: []const u8,
|
||||||
|
directive: Directive,
|
||||||
|
keyword: Keyword,
|
||||||
|
integerValue: i64,
|
||||||
|
floatValue: f64,
|
||||||
|
},
|
||||||
|
|
||||||
|
fn slice(self: @This(), contents: []const u8) []const u8 {
|
||||||
|
return contents[self.start..self.end];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Line = struct {
|
||||||
|
num: u32,
|
||||||
|
offset: u32,
|
||||||
|
contents: []const u8, // sans \n
|
||||||
|
tokens: []Token,
|
||||||
|
docstring: ?[]const u8, // leading docstring without a space dominates trailing comment as docstring
|
||||||
|
indent: u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const terminals: []const []const u8 = &.{ "->", ";", ":", ")", "(", ">", "<", "[", "]", "=" };
|
||||||
|
const TerminalKind = enum { arrow, semicolon, colon, rparen, lparen, rangle, langle, lsqbrack, rsqbrack, equals };
|
||||||
|
|
||||||
|
const Directive = enum {
|
||||||
|
import,
|
||||||
|
backing,
|
||||||
|
};
|
||||||
|
const directives = @typeInfo(Directive).@"enum".field_names;
|
||||||
|
|
||||||
|
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,
|
||||||
|
contents: []const u8,
|
||||||
|
|
||||||
|
fn countIndent(x: []const u8) u8 {
|
||||||
|
var s = x;
|
||||||
|
var i: u8 = 0;
|
||||||
|
while (mem.startsWith(u8, s, " ")) {
|
||||||
|
s = s[4..];
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Tokenizer = struct {
|
||||||
|
line: []const u8,
|
||||||
|
cursor: u32,
|
||||||
|
|
||||||
|
fn init(line: []const u8) @This() {
|
||||||
|
return .{ .cursor = 0, .line = line };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isLabelChar(x: u8) bool {
|
||||||
|
switch (x) {
|
||||||
|
'a'...'z', 'A'...'Z', '_', '0'...'9' => return true,
|
||||||
|
else => return false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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]) {
|
||||||
|
'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;
|
||||||
|
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) {}
|
||||||
|
|
||||||
|
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)) {
|
||||||
|
const tok: Token = .{
|
||||||
|
.start = self.cursor,
|
||||||
|
.end = self.cursor + @as(u32, @intCast(t.len)),
|
||||||
|
.tokenKind = .{ .term = @fromBackingInt(@intCast(i)) },
|
||||||
|
};
|
||||||
|
self.cursor += @intCast(t.len);
|
||||||
|
return tok;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn tokenize(gpa: std.mem.Allocator, line: []const u8) ![]Token {
|
||||||
|
var rvl: ArrayList(Token) = .empty;
|
||||||
|
var tokenizer = Tokenizer.init(line);
|
||||||
|
while (try tokenizer.next()) |n| {
|
||||||
|
try rvl.append(gpa, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
return try rvl.toOwnedSlice(gpa);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lex(self: *@This(), gpa: std.mem.Allocator, file: []const u8, contents: []const u8) !void {
|
||||||
|
self.* = .{
|
||||||
|
.file = file,
|
||||||
|
.contents = contents,
|
||||||
|
.gpa = gpa,
|
||||||
|
.module = null,
|
||||||
|
.lines = undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
var lines: std.ArrayList(Line) = .empty;
|
||||||
|
|
||||||
|
var toks = mem.tokenizeScalar(u8, contents, '\n');
|
||||||
|
var linenum: u32 = 0;
|
||||||
|
while (toks.next()) |line| {
|
||||||
|
linenum += 1;
|
||||||
|
log.debug("(line: {s})", .{line});
|
||||||
|
const nl: Line = .{
|
||||||
|
.offset = @intCast(toks.index - line.len),
|
||||||
|
.contents = line,
|
||||||
|
.indent = countIndent(line),
|
||||||
|
.docstring = null,
|
||||||
|
.tokens = try tokenize(self.gpa, line),
|
||||||
|
.num = linenum,
|
||||||
|
};
|
||||||
|
for (nl.tokens) |tok| {
|
||||||
|
log.debug(
|
||||||
|
" (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(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 lineRef = cursor.*;
|
||||||
|
const line, const body = try self.setupBodyState(cursor, .@"struct");
|
||||||
|
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");
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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..] },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
.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,
|
||||||
|
// });
|
||||||
|
|
||||||
|
var newdef: Definition = undefined;
|
||||||
|
switch (line.tokens[0].tokenKind) {
|
||||||
|
.comment => {
|
||||||
|
return .{ .comment = {} };
|
||||||
|
},
|
||||||
|
.keyword => |kw| {
|
||||||
|
switch (kw) {
|
||||||
|
.@"enum" => {
|
||||||
|
newdef = .{ .@"enum" = try self.parseEnum(cursor) };
|
||||||
|
try self.enums.append(self.gpa, newdef.@"enum");
|
||||||
|
},
|
||||||
|
.@"fn" => {
|
||||||
|
newdef = .{ .@"fn" = try self.parseFunction(cursor) };
|
||||||
|
try self.functions.append(self.gpa, newdef.@"fn");
|
||||||
|
},
|
||||||
|
.@"struct" => {
|
||||||
|
newdef = .{ .@"struct" = try self.parseStruct(cursor) };
|
||||||
|
try self.structs.append(self.gpa, newdef.@"struct");
|
||||||
|
},
|
||||||
|
.module => {
|
||||||
|
newdef = .{ .module = try self.parseModule(cursor) };
|
||||||
|
self.module = newdef.module;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => return error.UnexpectedToken,
|
||||||
|
}
|
||||||
|
log.info("{f}", .{newdef});
|
||||||
|
return newdef;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(self: *@This()) !void {
|
||||||
|
var cursor: u32 = 0;
|
||||||
|
while (cursor < @as(u32, @intCast(self.lines.len))) {
|
||||||
|
const line = try self.parseLine(&cursor);
|
||||||
|
_ = line;
|
||||||
|
cursor += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
|
defer arena.deinit();
|
||||||
|
|
||||||
|
var mod: Module = undefined;
|
||||||
|
try mod.lex(arena.allocator(), path, normalized);
|
||||||
|
try mod.parse();
|
||||||
|
}
|
||||||
|
|
||||||
|
const log = std.log.scoped(.tad);
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const ArrayList = std.ArrayList;
|
||||||
|
const mem = std.mem;
|
||||||
20
roadmap.md
20
roadmap.md
|
|
@ -6,19 +6,23 @@ Chapter 1: core tech
|
||||||
- structured logging - done
|
- structured logging - done
|
||||||
- compression and streaming - done
|
- compression and streaming - done
|
||||||
- structured logging loading utilities - done
|
- structured logging loading utilities - done
|
||||||
- logging cli and tools directory
|
- simple repl - done
|
||||||
- logging task (game thread)
|
|
||||||
- API generation
|
|
||||||
- Hot Reloading
|
- Hot Reloading
|
||||||
- Command API
|
- module loading via cli infrastructure - done
|
||||||
|
- API generation
|
||||||
|
- generating the handbomb from the parser
|
||||||
|
- API generation
|
||||||
|
- logging cli and tools directory - done
|
||||||
|
- logging task (game thread)
|
||||||
- IO and threadpool
|
- IO and threadpool
|
||||||
- logging task
|
- logging task
|
||||||
- Allocations and memory pooling
|
- Allocations and memory pooling
|
||||||
- tracy and performance profiling
|
- performance profiling
|
||||||
|
|
||||||
Chapter 2: graphics
|
Chapter 2: graphics
|
||||||
|
|
||||||
- slang shader compiler
|
- GL Backend
|
||||||
- runtime reflection
|
- runtime reflection
|
||||||
- renderer api
|
- gpu-one renderer api
|
||||||
- gameplay scripting
|
|
||||||
|
Chapter 3: Game 1 - Cognesia
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue