Skip to content

Commit 87fc2ed

Browse files
committed
change Lisp.stack to []Expr and remove N constant
1 parent 4869786 commit 87fc2ed

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

src/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn main() anyerror!void {
66
var writer = std.fs.File.stdout().writer(&.{});
77

88
var lisp: tinylisp.Lisp = undefined;
9-
var stack: [tinylisp.Lisp.N]f64 = undefined;
9+
var stack: [1024]f64 = undefined;
1010
lisp.initPinned(&writer.interface, &stack);
1111
try lisp.repl(std.fs.File.stdin());
1212
}

src/tinylisp.zig

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,26 @@ const nil = box(NIL, 0);
6161

6262
pub const Lisp = struct {
6363
writer: *Io.Writer,
64-
/// cell[N] array of Lisp expressions, shared by the stack and atom heap
65-
stack: *[N]Expr,
64+
/// array of Lisp expressions, shared by the stack and atom heap. suggested
65+
/// length 1024 or more.
66+
stack: []Expr,
6667
/// heap pointer, heap+hp with hp=0 points to the first atom string in stack[]
6768
heap_ptr: u32,
68-
/// stack pointer, the stack starts at the top of stack[] with sp=N
69+
/// stack pointer, the stack starts at the top of stack[] with sp=stack.len
6970
stack_ptr: u32,
7071
err: Expr,
7172
tru: Expr,
7273
env: Expr,
7374

74-
// TODO: make this configurable via build.zig
75-
/// number of cells for the shared stack and atom heap, increase N as desired
76-
pub const N: I = 1024;
77-
78-
pub fn initPinned(l: *Lisp, writer: *Io.Writer, stack: *[N]Expr) void {
75+
/// `stack`: cells for the shared stack and atom heap. suggested stack
76+
/// length: 1024 or more.
77+
pub fn initPinned(l: *Lisp, writer: *Io.Writer, stack: []Expr) void {
7978
@memset(stack, 0);
8079
l.* = .{
8180
.writer = writer,
8281
.stack = stack,
8382
.heap_ptr = 0,
84-
.stack_ptr = N,
83+
.stack_ptr = @intCast(stack.len),
8584
.err = undefined,
8685
.tru = undefined,
8786
.env = undefined,
@@ -645,9 +644,9 @@ pub const Lisp = struct {
645644
);
646645

647646
var counter: usize = 0;
648-
var sp: usize = N;
647+
var sp: usize = l.stack.len;
649648
while (sp > l.stack_ptr) : (counter += 1) {
650-
try l.writer.print("| {:>5} |", .{N - counter});
649+
try l.writer.print("| {:>5} |", .{l.stack.len - counter});
651650
sp -= 1;
652651
const x = l.stack[sp];
653652
switch (tag(x)) {
@@ -729,7 +728,7 @@ test "tinylisp - cons" {
729728
fn testExprTag(source: [:0]const u8, expected_expr_tag: I) !void {
730729
var stdout_w = std.fs.File.stdout().writer(&.{}); // TODO use discarding writer?
731730
var lisp: Lisp = undefined;
732-
var stack: [Lisp.N]Expr = undefined;
731+
var stack: [1024]Expr = undefined;
733732
lisp.initPinned(&stdout_w.interface, &stack);
734733
const eval_expr = lisp.run(source) orelse lisp.err;
735734
try std.testing.expectEqual(tag(eval_expr), expected_expr_tag);

src/wasm.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export fn _wasm_free(ptr: [*]u8, len: usize) void {
1616
}
1717

1818
var lisp: tinylisp.Lisp = undefined;
19-
var stack: [tinylisp.Lisp.N]f64 = undefined;
19+
var stack: [1024]f64 = undefined;
2020
var writer = JS.Terminal.writer();
2121

2222
export fn tinylisp_init() bool {

0 commit comments

Comments
 (0)