Skip to content

Commit

Permalink
Add basic implementations of assert(), error()
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
darrylabbate committed Nov 14, 2022
1 parent fe78e4f commit 238a0e4
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/lib_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@
#include <errno.h>
#include <string.h>

static void err(const char *msg) {
fprintf(stderr, "riff: %s\n", msg);
exit(1);
}

#include "vm_ops.h"

LIB_FN(error);

// assert(e[,s])
LIB_FN(assert) {
if (UNLIKELY(!argc)) {
err("expected expression for assertion");
}
if (UNLIKELY(!vm_test(fp))) {
l_error(fp+1, argc-1);
}
return 0;
}

// error([s])
LIB_FN(error) {
if (argc && !is_null(fp)) {
fputs_val(stderr, fp);
}
exit(1);
}

// eval(s)
LIB_FN(eval) {
if (!is_str(fp)) {
Expand Down Expand Up @@ -109,10 +137,12 @@ LIB_FN(type) {
}

static riff_lib_fn_reg baselib[] = {
LIB_FN_REG(eval, 1),
LIB_FN_REG(num, 1),
LIB_FN_REG(print, 1),
LIB_FN_REG(type, 1)
LIB_FN_REG(assert, 0),
LIB_FN_REG(error, 0),
LIB_FN_REG(eval, 1),
LIB_FN_REG(num, 1),
LIB_FN_REG(print, 1),
LIB_FN_REG(type, 1),
};

void riff_lib_register_base(riff_htab *g) {
Expand Down

0 comments on commit 238a0e4

Please sign in to comment.