Skip to content

Commit

Permalink
add anything?, minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Jul 11, 2023
1 parent f187562 commit dfc19c2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub fn defineAll(machine: *DtMachine) !void {
try machine.define("rev", "reverse a quote or string", .{ .builtin = rev });
try machine.define("quote", "quote a value", .{ .builtin = quoteVal });
try machine.define("quote-all", "quote all current context", .{ .builtin = quoteAll });
try machine.define("anything?", "true if any value at all precedes", .{ .builtin = @"anything?" });
try machine.define("concat", "concatenate two quotes. Values are coerced into quotes. (For String concatenation, see join)", .{ .builtin = concat });
try machine.define("push", "move an item into a quote", .{ .builtin = push });
try machine.define("pop", "move the last item of a quote to top of stack", .{ .builtin = pop });
Expand Down Expand Up @@ -436,7 +437,10 @@ pub fn norm(dt: *DtMachine) !void {
pub fn dotS(dt: *DtMachine) !void {
try stdout.print("[ ", .{});

var top = dt.nest.first orelse return;
var top = dt.nest.first orelse {
try stdout.print("]", .{});
return;
};

for (top.data.items) |val| {
try val.print(dt.alloc);
Expand Down Expand Up @@ -1210,6 +1214,11 @@ pub fn quoteAll(dt: *DtMachine) !void {
try dt.quoteContext();
}

pub fn @"anything?"(dt: *DtMachine) !void {
const top = dt.nest.first orelse return Error.ContextStackUnderflow;
try dt.push(.{ .bool = top.data.items.len != 0 });
}

pub fn concat(dt: *DtMachine) !void {
const vals = try dt.popN(2);

Expand Down
1 change: 1 addition & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn handlePipedStdoutOnly(dt: *DtMachine) !void {
fn readEvalPrintLoop(dt: *DtMachine) !void {
dt.handleCmd("run-args") catch |e| return doneOrDie(dt, e);

// TODO: Can this catch be done in the stdlib? Other people need to catch errors too!
while (true) dt.handleCmd("main-repl") catch |e| switch (e) {
error.EndOfStream => {
try stderr.print("\n", .{});
Expand Down
4 changes: 2 additions & 2 deletions src/stdlib.dt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
[ p nl ] \pl def
[ p ] \print def
[ pl ] \println def
[ \pl map drop ] \pls def
[ \pl each ] \pls def

[ ep enl ] \epl def
[ ep ] \eprint def
[ epl ] \eprintln def
[ \epl map drop ] \epls def
[ \epl each ] \epls def

[ .s ] \status def

Expand Down

0 comments on commit dfc19c2

Please sign in to comment.