-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
168 lines (145 loc) · 3.77 KB
/
main.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
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include "wannabe-lisp.h"
int interactive; /* interactive (repl) mode flag */
env_t *global; /* pointer to global environment */
jmp_buf repl_jmp; /* jump pointer to REPL used to
* recover from in-code errors */
int save_mode = 0; /* logging-to-file mode flag */
FILE *save_file; /* log file handle */
void final_clean_up()
{
gc();
gc_selfdestroy();
}
int main(int argc, char **argv)
{
char *buf = malloc(LOGICAL_LINE_BUFSIZ);
char out[LINEBUFSIZ];
list_t *expr;
if (!buf) {
printf("could not allocate a buffer\n");
exit(1);
}
/*
* Setup the debug-output-log module.
* Must make sure to run this before running
* the prefix-loading code, because that goes
* through the interpreter, which pushes
* debug data
*/
stacktracer_init();
/*
* It's always good to initialize strings, otherwise strange
* random garbage appears when you least expect it.
*/
*buf = 0;
/* Create the global environment */
global = new_env();
/*
* Set up the built-in procedure symbols
* for arithmetic, comparisons, etc. These
* map to code written in C that is within
* the interpreter itself (in `primitives.c').
*/
install_primitives(global);
/*
* Load up some composite primitives, stored
* in the file `prefix.txt', which is Scheme code.
* `prefix.txt' is partly hand-written (`preprefix.txt'),
* and partly generated by the script `gen-prefix.py'.
*/
load_code_from_file("prefix.txt");
/*
* Check for a "./lisp -i" invocation --
* that means interactive mode
*/
interactive = argc == 2 && !strcmp(argv[1], "-i");
/*
* In interactive mode, code errors jump to here.
* If the user writes invalid code, the program
* does not exit but rather gives a new fresh prompt
* (and also makes debug logs etc. available upon
* request)
*/
if (setjmp(repl_jmp))
*buf = 0;
stacktracer_reset();
/* This `while' is the REPL */
while (1) {
/*
* Clean up `buf', which holds input lines,
* and clean up `expr', which is the structure
* to which input gets parsed
*/
*buf = 0;
expr = new_list();
/* The flag GC_STRESS_TEST causes the program
* to constantly repeat the same code in a loop.
* Because the GC is in reasonable working order,
* the host machine will *not* run out of memory
* after a minute or so.
*/
#ifdef GC_STRESS_TEST
sprintf(buf, "((lambda (x) x) (+ 1 2 3))");
#else
if (save_mode)
fprintf(save_file, "\n");
/*
* Read a syntactic/logical, i.e. parenthesis-complete
* "line". do_read(), owned by `linereader.c', waits
* for the user to close all parentheses even
* if it takes several input-lines.
*/
if (!do_read_file(buf, stdin, interactive))
break;
#endif
if (!*buf)
break;
/* Parse the input string into a tree */
build(expr, buf);
/*
* arghhh -- this is what was left behind after
* some painful debugging... getting the CLI
* just right is a burden.
*/
if (feof(stdin))
break;
/* Evaluate the parsed input and print the result */
if (interactive) {
*out = 0;
printout(call_eval(expr, global), out);
puts(out);
/*
* If log mode set, log REPL
* printback as a comment
*/
if (save_mode) {
fprintf(save_file, ";; ");
fflush(save_file);
fputs(out, save_file);
fprintf(save_file, "\n");
}
} else {
/*
* In non-interactive mode, don't print the
* result. (You can use `display' to print
* things then).
*/
call_eval(expr, global);
}
/* Clean up leftover goo */
gc();
/* Clear debug trace after every input line */
stacktracer_reset();
}
/* clean up lots of stuff */
if (save_mode)
fclose(save_file);
final_clean_up();
free(buf);
stacktracer_destroy();
return 0;
}