-
Notifications
You must be signed in to change notification settings - Fork 10
/
repl.c
163 lines (125 loc) · 3.71 KB
/
repl.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdlib.h>
#include <time.h>
#include <string.h>
// Disable asserts?
// #define NDEBUG
//#define LISP_DEBUG
#define LISP_IMPLEMENTATION
#include "lisp.h"
#include "lisp_lib.h"
#define LINE_MAX 4096
static Lisp sch_load(Lisp args, LispError* e, LispContext ctx)
{
Lisp path = lisp_car(args);
Lisp result = lisp_read_path(lisp_string(path), e, ctx);
if (*e != LISP_ERROR_NONE) return lisp_null();
return lisp_eval(result, e, ctx);
}
int main(int argc, const char* argv[])
{
const char* file_path = NULL;
int run_script = 0;
int verbose;
#ifdef LISP_DEBUG
verbose = 1;
#else
verbose = 0;
#endif
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "--load") == 0)
{
file_path = argv[i + 1];
}
if (strcmp(argv[i], "--script") == 0)
{
file_path = argv[i + 1];
run_script = 1;
}
}
//LispContext ctx = lisp_init();
LispContext ctx = lisp_init_with_lib();
lisp_env_define(
lisp_cdr(lisp_env(ctx)),
lisp_make_symbol("LOAD", ctx),
lisp_make_func(sch_load),
ctx
);
// Load as a macro is called "include" and can be used to load files containing macros.
lisp_table_set(
lisp_macro_table(ctx),
lisp_make_symbol("INCLUDE", ctx),
lisp_make_func(sch_load),
ctx
);
clock_t start_time, end_time;
if (file_path)
{
if (verbose)
{
printf("loading: %s\n", file_path);
}
start_time = clock();
LispError error;
Lisp l = lisp_read_path(file_path, &error, ctx);
if (error != LISP_ERROR_NONE)
{
fprintf(stderr, "%s. %s\n", file_path, lisp_error_string(error));
}
end_time = clock();
if (verbose)
printf("read (us): %lu\n", 1000000 * (end_time - start_time) / CLOCKS_PER_SEC);
start_time = clock();
Lisp code = lisp_macroexpand(l, &error, ctx);
if (error != LISP_ERROR_NONE)
{
fprintf(stderr, "%s\n", lisp_error_string(error));
exit(1);
}
end_time = clock();
if (verbose)
printf("expand (us): %lu\n", 1000000 * (end_time - start_time) / CLOCKS_PER_SEC);
start_time = clock();
lisp_eval(code, &error, ctx);
end_time = clock();
if (error != LISP_ERROR_NONE)
{
fprintf(stderr, "%s\n", lisp_error_string(error));
exit(1);
}
lisp_collect(lisp_null(), ctx);
if (verbose)
printf("eval (us): %lu\n", 1000000 * (end_time - start_time) / CLOCKS_PER_SEC);
}
if (!run_script)
{
// REPL
while (!feof(stdin))
{
printf("> ");
char line[LINE_MAX];
if (!fgets(line, LINE_MAX, stdin)) break;
clock_t start_time = clock();
LispError error;
Lisp code = lisp_read(line, &error, ctx);
if (error != LISP_ERROR_NONE)
{
fprintf(stderr, "%s\n", lisp_error_string(error));
continue;
}
Lisp l = lisp_eval(code, &error, ctx);
clock_t end_time = clock();
if (error != LISP_ERROR_NONE)
{
fprintf(stderr, "%s\n", lisp_error_string(error));
}
lisp_printf(stdout, l);
printf("\n");
lisp_collect(lisp_null(), ctx);
if (verbose)
printf("(us): %lu\n", 1000000 * (end_time - start_time) / CLOCKS_PER_SEC);
}
}
lisp_shutdown(ctx);
return 0;
}