diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-01-06 07:45:07 +0100 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-01-06 07:45:07 +0100 |
| commit | 75185f9fa26b5fc49036008df16c8bb4f1a51a13 (patch) | |
| tree | cb4f7a260f4e83bfbff302a926e12df2ae7007fa /src/zisp | |
| parent | e24978c647188c9b80563b535462a794e6eaa52b (diff) | |
Writergate, other 0.16 changes, and test fixes for new grammar.
Diffstat (limited to 'src/zisp')
| -rw-r--r-- | src/zisp/gc.zig | 10 | ||||
| -rw-r--r-- | src/zisp/io/Parser.zig | 10 | ||||
| -rw-r--r-- | src/zisp/io/parser.zig | 5 | ||||
| -rw-r--r-- | src/zisp/io/unparser.zig | 30 | ||||
| -rw-r--r-- | src/zisp/value.zig | 9 | ||||
| -rw-r--r-- | src/zisp/value/rune.zig | 2 | ||||
| -rw-r--r-- | src/zisp/value/sstr.zig | 2 |
7 files changed, 42 insertions, 26 deletions
diff --git a/src/zisp/gc.zig b/src/zisp/gc.zig index 2f51b9b..4fc2c90 100644 --- a/src/zisp/gc.zig +++ b/src/zisp/gc.zig @@ -13,10 +13,16 @@ pub const alloc = std.heap.smp_allocator; // Cons cells -var cons_pool = std.heap.MemoryPool([2]Value).init(alloc); +const ConsPool = std.heap.MemoryPool([2]Value); +var cons_pool: ConsPool = undefined; +var need_init = true; pub fn cons(v1: Value, v2: Value) *[2]Value { - const mem = cons_pool.create() catch @panic("OOM"); + if (need_init) { + cons_pool = ConsPool.initCapacity(alloc, 64) catch @panic("OOM"); + need_init = false; + } + const mem = cons_pool.create(alloc) catch @panic("OOM"); mem[0] = v1; mem[1] = v2; return mem; diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig index 57cdc14..2c381e1 100644 --- a/src/zisp/io/Parser.zig +++ b/src/zisp/io/Parser.zig @@ -119,7 +119,8 @@ pub const Alloc = struct { }; alloc: Alloc, -input: std.io.AnyReader = undefined, +io: std.Io, +input: *std.Io.Reader = undefined, context: Context = .{}, stack: List(Context) = undefined, chars: List(u8) = undefined, @@ -130,10 +131,11 @@ err_msg: []const u8 = undefined, pub fn init( alloc: Alloc, + io: std.Io, init_stack_capacity: usize, init_chars_capacity: usize, ) !Parser { - var p: Parser = .{ .alloc = alloc }; + var p: Parser = .{ .alloc = alloc, .io = io }; p.stack = try .initCapacity(alloc.stack, init_stack_capacity); p.chars = try .initCapacity(alloc.chars, init_chars_capacity); return p; @@ -155,7 +157,7 @@ fn read(p: *Parser) !?u8 { .{p.unread_char.?}, ); } - const c = p.input.readByte() catch |e| switch (e) { + const c = p.input.takeByte() catch |e| switch (e) { error.EndOfStream => return null, else => return p.err(.ReadError, "???"), }; @@ -242,7 +244,7 @@ fn call(p: *Parser, f: Fn) !void { }; } -pub fn run(p: *Parser, input: std.io.AnyReader) !Value { +pub fn run(p: *Parser, input: *std.Io.Reader) !Value { p.input = input; p.context.next = .parseUnit; while (p.context.next) |next| { diff --git a/src/zisp/io/parser.zig b/src/zisp/io/parser.zig index cfe2bf1..49d260c 100644 --- a/src/zisp/io/parser.zig +++ b/src/zisp/io/parser.zig @@ -44,17 +44,18 @@ pub fn initSfa(alloc: anytype) !Parser { const SfaType = @typeInfo(@TypeOf(alloc)).pointer.child; return Parser.init( alloc.allocator(), + std.Io.Threaded.global_single_threaded.io(), SfaType.init_stack_capacity, SfaType.init_chars_capacity, ); } -pub fn parse(input: std.io.AnyReader) Value { +pub fn parse(input: *std.Io.Reader) Value { return _parse(input, true); } pub fn _parse( - input: std.io.AnyReader, + input: *std.Io.Reader, comptime panic: bool, ) if (panic) Value else error{ ParseError, OutOfMemory }!Value { const alloc = std.heap.smp_allocator; diff --git a/src/zisp/io/unparser.zig b/src/zisp/io/unparser.zig index 64e18d0..04184fa 100644 --- a/src/zisp/io/unparser.zig +++ b/src/zisp/io/unparser.zig @@ -6,10 +6,9 @@ const value = @import("../value.zig"); const istr = value.istr; const seq = value.seq; -const ShortString = value.ShortString; const Value = value.Value; -pub fn unparse(w: anytype, v: Value) anyerror!void { +pub fn unparse(w: *std.Io.Writer, v: Value) anyerror!void { // zig fmt: off try if (v.isDouble()) unparseDouble(w, v) else if (v.isFixnum()) unparseFixnum(w, v) @@ -22,40 +21,41 @@ pub fn unparse(w: anytype, v: Value) anyerror!void { else if (v.isSstr()) unparseSstr(w, v) ; // zig fmt: on + try w.flush(); } -fn unparseDouble(w: anytype, v: Value) !void { +fn unparseDouble(w: *std.Io.Writer, v: Value) !void { _ = w; _ = v; @panic("not implemented"); } -fn unparseFixnum(w: anytype, v: Value) !void { +fn unparseFixnum(w: *std.Io.Writer, v: Value) !void { _ = w; _ = v; @panic("not implemented"); } -fn unparseRune(w: anytype, v: Value) !void { +fn unparseRune(w: *std.Io.Writer, v: Value) !void { const name = value.rune.unpack(v); try w.writeByte('#'); - try w.writeAll(name.constSlice()); + try w.writeAll(name.slice()); } -fn unparseSstr(w: anytype, v: Value) !void { +fn unparseSstr(w: *std.Io.Writer, v: Value) !void { // TODO: Check if pipes/escapes necessary. const str = value.sstr.unpack(v); - try w.writeAll(str.constSlice()); + try w.writeAll(str.slice()); } -fn unparseChar(w: anytype, v: Value) !void { +fn unparseChar(w: *std.Io.Writer, v: Value) !void { const uc: u21 = value.char.unpack(v); var buf: [4]u8 = undefined; const len = try std.unicode.utf8Encode(uc, &buf); try w.writeAll(buf[0..len]); } -fn unparseMisc(w: anytype, v: Value) !void { +fn unparseMisc(w: *std.Io.Writer, v: Value) !void { try switch (v.bits) { value.f.bits => w.writeAll("#f"), value.t.bits => w.writeAll("#t"), @@ -67,13 +67,13 @@ fn unparseMisc(w: anytype, v: Value) !void { }; } -fn unparseSrat(w: anytype, v: Value) !void { +fn unparseSrat(w: *std.Io.Writer, v: Value) !void { _ = w; _ = v; @panic("not implemented"); } -fn unparsePair(w: anytype, p: *[2]Value) !void { +fn unparsePair(w: *std.Io.Writer, p: *[2]Value) !void { try w.writeByte('('); try unparse(w, p[0]); var cdr = p[1]; @@ -83,21 +83,21 @@ fn unparsePair(w: anytype, p: *[2]Value) !void { } if (!value.nil.eq(cdr)) { try w.writeByte(' '); - try w.writeByte('.'); + try w.writeByte('&'); try w.writeByte(' '); try unparse(w, cdr); } try w.writeByte(')'); } -fn unparseSeq(w: anytype, s: *seq.Header) !void { +fn unparseSeq(w: *std.Io.Writer, s: *seq.Header) !void { switch (s.type) { .string => try unparseString(w, s), else => @panic("not implemented"), } } -fn unparseString(w: anytype, s: *seq.Header) !void { +fn unparseString(w: *std.Io.Writer, s: *seq.Header) !void { // TODO: Check if pipes/escapes necessary. try w.writeByte('|'); try w.writeAll(s.bytes()); diff --git a/src/zisp/value.zig b/src/zisp/value.zig index 449a577..2d148ed 100644 --- a/src/zisp/value.zig +++ b/src/zisp/value.zig @@ -170,7 +170,14 @@ const endian = builtin.target.cpu.arch.endian(); const max = std.math.maxInt; /// Used when dealing with runes and short strings. -pub const ShortString = std.BoundedArray(u8, 6); +pub const ShortString = struct { + len: u8, + buf: [6]u8, + + pub fn slice(this: @This()) []const u8 { + return this.buf[0..this.len]; + } +}; /// Used to find the length of a rune or short string. pub fn sstrLen(x: u64) u8 { diff --git a/src/zisp/value/rune.zig b/src/zisp/value/rune.zig index e75c276..f49feb4 100644 --- a/src/zisp/value/rune.zig +++ b/src/zisp/value/rune.zig @@ -51,7 +51,7 @@ pub fn unpack(v: Value) ShortString { assert(v); const s: [6]u8 = @bitCast(v.rune.name); const l = value.sstrLen(v.bits); - return .{ .buffer = s, .len = l }; + return .{ .buf = s, .len = l }; } // Zisp API diff --git a/src/zisp/value/sstr.zig b/src/zisp/value/sstr.zig index 4f0336e..b4cabff 100644 --- a/src/zisp/value/sstr.zig +++ b/src/zisp/value/sstr.zig @@ -45,7 +45,7 @@ pub fn unpack(v: Value) ShortString { assert(v); const s: [6]u8 = @bitCast(v.sstr.string); const l = value.sstrLen(v.bits); - return .{ .buffer = s, .len = l }; + return .{ .buf = s, .len = l }; } // No Zisp API for sstr specifically, since it's a string. See string.zig. |
