diff options
author | ubq323 <ubq323@ubq323.website> | 2024-06-26 14:44:19 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-06-26 14:44:19 +0100 |
commit | a8519434f058d0ab60bf7f90acc61997cb982cfa (patch) | |
tree | e1858d42805e61afbd5c6c0b9d2f3b47bc8411ee /lib.c | |
parent | 20584e520dfd44393aa66350b442e8a23e46c5a3 (diff) |
add cfunc type and rudimentary stdlib
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)); + } +} + + |