-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp.c
69 lines (52 loc) · 1.15 KB
/
lisp.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lisp.h"
#include "assert.h"
#define LISP_LEN (1 << 20)
static int iter = 1;
int get_iter(void)
{
return iter;
}
i64 *stack_top;
int main(void)
{
i64 dummy = 0xC0FFEE;
stack_top = &dummy;
// lisp source to be interpreted
static char lisp[LISP_LEN] = {0};
FILE *f = fopen("lisp", "rb");
assert(f);
fseek(f, 0, SEEK_END);
size_t fsize = (size_t)ftell(f);
fseek(f, 0, SEEK_SET);
assert(fsize < LISP_LEN);
fread(lisp, fsize, 1, f);
fclose(f);
lisp[fsize] = 0;
init();
// dump();
char *lisp_ptr = &lisp[0];
char **cursor = &lisp_ptr;
while (**cursor)
{
ptr parsed = parse(cursor);
ptr evaled = eval(parsed);
println(evaled);
strip(cursor);
iter++;
}
gc();
int memory = mem_usage();
char *unit[] = {"", "K", "M", "G", "T"};
char **mem_unit = &unit[0];
while (memory >= (1 << 13))
{
mem_unit++;
memory /= 1024;
}
printf("We used %d%sB of memory for the lisp values\n", memory, *mem_unit);
return 0;
}