summaryrefslogtreecommitdiff
path: root/lib.c
blob: fe87abfab899923c960fe8f27aa01164c1213a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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));
	}
}