-
Notifications
You must be signed in to change notification settings - Fork 10
/
sample.c
55 lines (42 loc) · 1.32 KB
/
sample.c
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
45
46
47
48
49
50
51
52
53
54
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define LISP_IMPLEMENTATION
#include "lisp.h"
#include "lisp_lib.h"
Lisp integer_range(Lisp args, LispError* e, LispContext ctx)
{
// first argument
LispInt start = lisp_int(lisp_car(args));
args = lisp_cdr(args);
// second argument
LispInt end = lisp_int(lisp_car(args));
if (end < start)
{
*e = LISP_ERROR_OUT_OF_BOUNDS;
return lisp_null();
}
LispInt n = end - start;
Lisp numbers = lisp_make_vector(n, ctx);
for (LispInt i = 0; i < n; ++i)
lisp_vector_set(numbers, i, lisp_make_int(start + i));
return numbers;
}
int main(int argc, const char* argv[])
{
LispContext ctx = lisp_init();
lisp_lib_load(ctx);
// wrap in Lisp object
Lisp func = lisp_make_func(integer_range);
// add to enviornment with symbol INTEGER-RANGE
Lisp env = lisp_env(ctx);
lisp_env_define(env, lisp_make_symbol("INTEGER-RANGE", ctx), func, ctx);
Lisp pi = lisp_make_real(3.141592);
lisp_env_define(env, lisp_make_symbol("PI", ctx), pi, ctx);
LispError e;
Lisp result = lisp_eval(lisp_read("(INTEGER-RANGE 5 15)", &e, ctx), &e, ctx);
lisp_printf(stdout, result);
if (e != LISP_ERROR_NONE) fprintf(stderr, "error: %s\n", lisp_error_string(e));
lisp_shutdown(ctx);
return 0;
}