summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-06 21:02:55 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-06 21:02:55 +0200
commit90af1ed17d317435fb56ea041fa0d937f5043726 (patch)
tree7e0997c3bcc8911240cda7ca5cd434eda97ced02
parent0d989d4b7c5507899721ae208b4ca605b4017e37 (diff)
More cleanup.
-rw-r--r--src/zisp.zig1
-rw-r--r--src/zisp/gc.zig4
-rw-r--r--src/zisp/gc/PairPool.zig9
-rw-r--r--src/zisp/io/Parser.zig6
-rw-r--r--src/zisp/io/parse.zig8
-rw-r--r--src/zisp/io/print.zig113
-rw-r--r--src/zisp/value.zig2
-rw-r--r--src/zisp/value/array.zig13
-rw-r--r--src/zisp/value/istr.zig4
-rw-r--r--src/zisp/value/pair.zig2
-rw-r--r--src/zisp/value/string.zig13
11 files changed, 74 insertions, 101 deletions
diff --git a/src/zisp.zig b/src/zisp.zig
index ae4aabc..dff410f 100644
--- a/src/zisp.zig
+++ b/src/zisp.zig
@@ -1,4 +1,3 @@
-const builtin = @import("builtin");
const std = @import("std");
pub const gc = @import("zisp/gc.zig");
diff --git a/src/zisp/gc.zig b/src/zisp/gc.zig
index 7fc431d..9267087 100644
--- a/src/zisp/gc.zig
+++ b/src/zisp/gc.zig
@@ -1,10 +1,10 @@
const builtin = @import("builtin");
const std = @import("std");
-const value = @import("value.zig");
-
const Alloc = std.mem.Allocator;
+const value = @import("value.zig");
+
pub const PairPool = @import("gc/PairPool.zig");
pub const IstrSet = @import("gc/IstrSet.zig");
diff --git a/src/zisp/gc/PairPool.zig b/src/zisp/gc/PairPool.zig
index 8a437ae..7995098 100644
--- a/src/zisp/gc/PairPool.zig
+++ b/src/zisp/gc/PairPool.zig
@@ -1,9 +1,8 @@
const std = @import("std");
-const value = @import("../value.zig");
-
const Alloc = std.mem.Allocator;
-const PairMemPool = std.heap.MemoryPool(value.pair.Pair);
+
+const value = @import("../value.zig");
const Value = value.Value;
const PairPtr = value.pair.PairPtr;
@@ -11,7 +10,7 @@ const PairPtr = value.pair.PairPtr;
const PairPool = @This();
alloc: Alloc,
-pair_pool: PairMemPool,
+pair_pool: std.heap.MemoryPool(value.pair.Pair),
const default_init_cap = 1024;
@@ -22,7 +21,7 @@ pub fn init(alloc: Alloc) !PairPool {
pub fn initCustom(alloc: Alloc, init_cap: usize) !PairPool {
return .{
.alloc = alloc,
- .pair_pool = try PairMemPool.initCapacity(alloc, init_cap),
+ .pair_pool = try .initCapacity(alloc, init_cap),
};
}
diff --git a/src/zisp/io/Parser.zig b/src/zisp/io/Parser.zig
index 3a610dd..3b5aa2a 100644
--- a/src/zisp/io/Parser.zig
+++ b/src/zisp/io/Parser.zig
@@ -35,13 +35,13 @@
const builtin = @import("builtin");
const std = @import("std");
+const Alloc = std.mem.Allocator;
+const List = std.ArrayListUnmanaged;
+
const gc = @import("../gc.zig");
const lib = @import("../lib.zig");
const value = @import("../value.zig");
-const Alloc = std.mem.Allocator;
-const List = std.ArrayListUnmanaged;
-
const IstrSet = gc.IstrSet;
const PairPool = gc.PairPool;
const Value = value.Value;
diff --git a/src/zisp/io/parse.zig b/src/zisp/io/parse.zig
index 06dfea0..9b6d32c 100644
--- a/src/zisp/io/parse.zig
+++ b/src/zisp/io/parse.zig
@@ -1,13 +1,13 @@
const builtin = @import("builtin");
const std = @import("std");
-const value = @import("../value.zig");
-
-const Parser = @import("Parser.zig");
-
const Alloc = std.mem.Allocator;
const Reader = *std.Io.Reader;
+const io = @import("../io.zig");
+const value = @import("../value.zig");
+
+const Parser = io.Parser;
const Value = value.Value;
const is_test = builtin.is_test;
diff --git a/src/zisp/io/print.zig b/src/zisp/io/print.zig
index 4312c98..e5caab7 100644
--- a/src/zisp/io/print.zig
+++ b/src/zisp/io/print.zig
@@ -1,10 +1,10 @@
const std = @import("std");
+const Writer = *std.Io.Writer;
+
const io = @import("../io.zig");
const value = @import("../value.zig");
-const Writer = *std.Io.Writer;
-
const Parser = io.Parser;
const Value = value.Value;
const PairPtr = value.pair.PairPtr;
@@ -12,36 +12,35 @@ const IstrPtr = value.istr.IstrPtr;
const ArrayPtr = value.array.ArrayPtr;
pub fn toWriter(w: Writer, v: Value) anyerror!void {
- if (v.isSstr()) return sstr(w, v);
- if (v.isRune()) return rune(w, v);
- if (v.isMisc()) return misc(w, v);
- if (v.getPtr(.istr)) |p| return istr(w, p);
- if (v.getPtr(.array)) |p| return array(w, p);
- if (v.getPtr(.pair)) |p| return pair(w, p);
- @panic("unhandled type");
-}
-
-pub fn double(w: Writer, v: Value) !void {
- _ = w;
- _ = v;
- @panic("not implemented");
+ if (v.isSstr()) return printBareStr(w, value.sstr.unpack(&v));
+ if (v.isRune()) return printRune(w, v);
+ if (v.isMisc()) return printMisc(w, v);
+ if (value.istr.check(v)) |p| return printBareStr(w, p.str());
+ if (value.array.check(.str, v)) |p| return printBareStr(w, p.str());
+ if (value.pair.check(v)) |p| return printPair(w, p);
+ @panic("Unsupported type to print.");
}
-pub fn fixnum(w: Writer, v: Value) !void {
- try w.print("{d}", .{value.fixnum.unpack(v)});
+pub fn printBareStr(w: Writer, s: []const u8) !void {
+ if (s.len == 0) @panic("Empty string must be quoted.");
+ const no_joins = Parser.isSpecialBareChar(s[0]);
+ for (s) |c| {
+ if (Parser.isBareChar(c)) {
+ try w.writeByte(c);
+ } else if (no_joins and (c == '.' or c == ':')) {
+ try w.writeByte(c);
+ } else {
+ @panic("String needs quoting.");
+ }
+ }
}
-pub fn rune(w: Writer, v: Value) !void {
+pub fn printRune(w: Writer, v: Value) !void {
try w.writeByte('#');
try w.writeAll(value.rune.unpack(&v));
}
-pub fn sstr(w: Writer, v: Value) !void {
- // TODO: Check if pipes/escapes necessary.
- try w.writeAll(value.sstr.unpack(&v));
-}
-
-pub fn misc(w: Writer, v: Value) !void {
+pub fn printMisc(w: Writer, v: Value) !void {
try switch (v.bits) {
value.f.bits => w.writeAll("#f"),
value.t.bits => w.writeAll("#t"),
@@ -49,24 +48,23 @@ pub fn misc(w: Writer, v: Value) !void {
value.eof.bits => w.writeAll("#EOF"),
value.none.bits => w.writeAll("#NONE"),
value.undef.bits => w.writeAll("#UNDEF"),
- else => @panic("not implemented"),
+ else => @panic("Unsupported misc value to print."),
};
}
-pub fn pair(w: Writer, p: PairPtr) !void {
+pub fn printPair(w: Writer, p: PairPtr) !void {
try switch (p.car.bits) {
- Parser.PQSTR.bits => quotString(w, p.cdr, '|'),
- Parser.DQSTR.bits => quotString(w, p.cdr, '"'),
- Parser.ATSTR.bits => atString(w, p.cdr),
- Parser.LABEL.bits => label(w, p.cdr),
- else => list(w, p),
+ Parser.PQSTR.bits => printQuotString(w, p.cdr, '|'),
+ Parser.DQSTR.bits => printQuotString(w, p.cdr, '"'),
+ Parser.ATSTR.bits => printAtString(w, p.cdr),
+ Parser.LABEL.bits => printLabel(w, p.cdr),
+ else => printList(w, p),
};
}
-fn quotString(w: Writer, s: Value, comptime qchar: u8) !void {
+fn printQuotString(w: Writer, s: Value, comptime qchar: u8) !void {
try w.writeByte(qchar);
- // TODO: use string.zig (when it exists) to get []u8 generically
- const str = try getStr(&s);
+ const str = try value.string.getString(&s);
for (str) |c| switch (c) {
qchar => {
try w.writeByte('\\');
@@ -83,32 +81,18 @@ fn quotString(w: Writer, s: Value, comptime qchar: u8) !void {
try w.writeByte(qchar);
}
-pub fn atString(w: Writer, at_str_pair: Value) !void {
+pub fn printAtString(w: Writer, at_str_pair: Value) !void {
const p = value.pair.assert(at_str_pair);
const b = value.fixnum.unpack(p.car);
std.debug.assert(b <= 255);
- const str = try getStr(&p.cdr);
+ const str = try value.string.getString(&p.cdr);
try w.writeByte('@');
try w.writeByte(@intCast(b));
try w.writeAll(str);
try w.writeByte(@intCast(b));
}
-// TODO: belongs in string.zig
-fn getStr(s: *const Value) ![]const u8 {
- return if (value.sstr.check(s.*))
- value.sstr.unpack(s)
- else if (value.istr.check(s.*)) |istr_ptr|
- istr_ptr.str()
- else if (value.array.check(s.*)) |array_ptr|
- array_ptr.str()
- else {
- s.dump();
- @panic("This is not a string.");
- };
-}
-
-pub fn label(w: Writer, v: Value) !void {
+pub fn printLabel(w: Writer, v: Value) !void {
var num: Value = undefined;
var dat: ?Value = undefined;
if (value.pair.check(v)) |p| {
@@ -135,32 +119,7 @@ pub fn label(w: Writer, v: Value) !void {
}
}
-pub fn istr(w: Writer, p: IstrPtr) !void {
- try string(w, p.str());
-}
-
-pub fn array(w: Writer, s: ArrayPtr) !void {
- switch (s.type) {
- .str => try string(w, s.str()),
- else => @panic("not implemented"),
- }
-}
-
-pub fn string(w: Writer, s: []const u8) !void {
- if (s.len == 0) @panic("Empty string must be quoted.");
- const no_joins = Parser.isSpecialBareChar(s[0]);
- for (s) |c| {
- if (Parser.isBareChar(c)) {
- try w.writeByte(c);
- } else if (no_joins and (c == '.' or c == ':')) {
- try w.writeByte(c);
- } else {
- @panic("String needs quoting.");
- }
- }
-}
-
-pub fn list(w: Writer, p: PairPtr) !void {
+pub fn printList(w: Writer, p: PairPtr) !void {
try w.writeByte('(');
try toWriter(w, p.car);
var cdr = p.cdr;
diff --git a/src/zisp/value.zig b/src/zisp/value.zig
index d39e225..5402b16 100644
--- a/src/zisp/value.zig
+++ b/src/zisp/value.zig
@@ -164,6 +164,8 @@ pub const pair = @import("value/pair.zig");
pub const istr = @import("value/istr.zig");
pub const array = @import("value/array.zig");
+pub const string = @import("value/string.zig");
+
const endian = builtin.target.cpu.arch.endian();
const max = std.math.maxInt;
diff --git a/src/zisp/value/array.zig b/src/zisp/value/array.zig
index f8e87bf..6be3c9c 100644
--- a/src/zisp/value/array.zig
+++ b/src/zisp/value/array.zig
@@ -1,11 +1,11 @@
const builtin = @import("builtin");
const std = @import("std");
+const Alloc = std.mem.Allocator;
+
const gc = @import("../gc.zig");
const value = @import("../value.zig");
-const Alloc = std.mem.Allocator;
-
const Value = value.Value;
/// Pointer to header for an array of various element types and sizes.
@@ -54,7 +54,7 @@ pub const ArrayHeader = packed struct(u64) {
len_or_ptr: u48,
is_slice: bool = false,
is_ptr: bool = false,
- type: enum(u2) { int, flt, val, str },
+ type: ArrayType,
info: packed union {
int: packed struct(u12) {
size: u10,
@@ -154,6 +154,8 @@ pub const ArrayHeader = packed struct(u64) {
}
};
+const ArrayType = enum(u2) { int, flt, val, str };
+
const Endian = enum(u1) {
little,
big,
@@ -179,6 +181,7 @@ pub fn newString(alloc: Alloc, s: []const u8) !Value {
return value.ptr.pack(.array, arr);
}
-pub fn check(v: Value) ?ArrayPtr {
- return v.getPtr(.array) orelse null;
+pub fn check(comptime t: ArrayType, v: Value) ?ArrayPtr {
+ if (v.getPtr(.array)) |p| if (p.type == t) return p;
+ return null;
}
diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig
index 9accd27..35093ed 100644
--- a/src/zisp/value/istr.zig
+++ b/src/zisp/value/istr.zig
@@ -4,11 +4,11 @@
const std = @import("std");
+const Alloc = std.mem.Allocator;
+
const gc = @import("../gc.zig");
const value = @import("../value.zig");
-const Alloc = std.mem.Allocator;
-
const IstrSet = gc.IstrSet;
const Value = value.Value;
diff --git a/src/zisp/value/pair.zig b/src/zisp/value/pair.zig
index f2a27a0..4ae38a3 100644
--- a/src/zisp/value/pair.zig
+++ b/src/zisp/value/pair.zig
@@ -1,5 +1,3 @@
-const std = @import("std");
-
const gc = @import("../gc.zig");
const value = @import("../value.zig");
diff --git a/src/zisp/value/string.zig b/src/zisp/value/string.zig
new file mode 100644
index 0000000..129e35b
--- /dev/null
+++ b/src/zisp/value/string.zig
@@ -0,0 +1,13 @@
+//! Abstract Data Type over Sstr, Istr, Array
+
+const value = @import("../value.zig");
+
+const Value = value.Value;
+
+pub fn getString(s: *const Value) ![]const u8 {
+ if (value.sstr.check(s.*)) return value.sstr.unpack(s);
+ if (value.istr.check(s.*)) |istr_ptr| return istr_ptr.str();
+ if (value.array.check(.str, s.*)) |arr_ptr| return arr_ptr.str();
+ s.dump();
+ @panic("This is not a string.");
+}