diff options
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 44 |
1 files changed, 44 insertions, 0 deletions
@@ -0,0 +1,44 @@ +#include <time.h> + +#include "lib.h" +#include "state.h" +#include "val.h" +#include "util.h" + +static Val fn_clock(State *S, int nargs, Val *args) { + return VAL_NUM((double)clock() / CLOCKS_PER_SEC); +} + +static Val fn_write(State *S, int nargs, Val *args) { + CHECK(nargs>0, "need 1 arg to write"); + print_val(args[0]); + return VAL_NIL; +} +static Val fn_say(State *S, int nargs, Val *args) { + CHECK(nargs>0, "need 1 arg to say"); + println_val(args[0]); + return VAL_NIL; +} + + + + +typedef struct { + char *name; + CFunc func; +} BuiltinFunc; +static BuiltinFunc builtin_funcs[] = { + { "clock", fn_clock }, + { "say", fn_say }, + { "write", fn_write }, + { 0 }, +}; + +void load_stdlib(State *S) { + for (BuiltinFunc *b = builtin_funcs; b->name != NULL; b++) { + ObjString *oname = objstring_copy_cstr(S, b->name); + ht_put(S, &S->globals, oname, VAL_CFUNC(b->func)); + } +} + + |