-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFACT_shell.c
366 lines (313 loc) · 8.62 KB
/
FACT_shell.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* This file is part of FACT.
*
* FACT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FACT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FACT. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FACT.h"
#include "FACT_shell.h"
#include "FACT_types.h"
#include "FACT_lexer.h"
#include "FACT_parser.h"
#include "FACT_comp.h"
#include "FACT_vm.h"
#include "FACT_error.h"
#include "FACT_mpc.h"
#include "FACT_alloc.h"
#include "FACT_hash.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <histedit.h>
static void print_num (FACT_num_t);
static void print_scope (FACT_scope_t);
static int is_blank (const char *str) /* Returns 1 if the rest of a string is junk (comment or whitespace). */
{
for (; *str != '\0' && *str != '#'; str++) {
if (!isspace (*str))
return 0;
}
return 1;
}
static int is_complete (const char *line) /* Check to see if a line forms a complete statement. */
{
/* This is not reentrant. */
static int p_count, b_count, c_count;
static enum {
NO_QUOTE = 0,
IN_DQUOTE,
IN_SQUOTE,
} quote_stat;
bool bslash;
size_t i;
/* If line is NULL, take that as a signal to reset all the counts. */
if (line == NULL) {
p_count = b_count = c_count = 0;
return 1;
}
bslash = false;
for (i = 0; line[i] != '\0'; i++) {
switch (line[i]) {
case '(':
if (quote_stat == NO_QUOTE)
p_count++;
break;
case ')':
if (quote_stat == NO_QUOTE
&& p_count > 0)
p_count--;
break;
case '[':
if (quote_stat == NO_QUOTE)
b_count++;
break;
case ']':
if (quote_stat == NO_QUOTE
&& b_count > 0)
b_count--;
break;
case '{':
if (quote_stat == NO_QUOTE)
c_count++;
break;
case '}':
if (quote_stat == NO_QUOTE) {
if (c_count > 0)
c_count--;
if (p_count == 0
&& b_count == 0
&& c_count == 0
&& is_blank (line + i + 1))
return 1;
}
break;
case ';':
if (p_count == 0
&& b_count == 0
&& c_count == 0
&& is_blank (line + i + 1))
return 1;
break;
case '\'':
if (quote_stat == NO_QUOTE)
quote_stat = IN_SQUOTE;
else if (quote_stat == IN_SQUOTE) {
if (!bslash)
quote_stat = NO_QUOTE;
}
break;
case '"':
if (quote_stat == NO_QUOTE)
quote_stat = IN_DQUOTE;
else if (quote_stat == IN_DQUOTE) {
if (!bslash)
quote_stat = NO_QUOTE;
}
break;
case '\\':
bslash = (bslash ? false : true);
continue; /* Oh hohoho! I'm tricky aren't I! */
case '#':
if (quote_stat == NO_QUOTE)
/* The very fact that we reached this point means that
* the statement is incomplete.
*/
return 0;
break;
default:
break;
}
/* This is skipped over when bslash is set to true, thus
* it will reset bslash after one iteration.
*/
bslash = false;
}
return 0; /* If we got here, it's an incomplete statement. */
}
static bool new_stmt = true;
static size_t curr_line = 1;
char *shell_prompt (EditLine *e) /* Return the shell prompt. */
{
static char *prev = NULL;
static size_t prev_len;
size_t i, j, k, line;
/* Free the previous prompt or reuse it. */
if (prev != NULL) {
if (prev[0] == ' ' && !new_stmt)
return prev;
FACT_free (prev);
}
/* Reallocate and set the prompt. */
if (new_stmt) {
i = sizeof (SHELL_START) - 1;
prev = FACT_malloc_atomic (i + 1);
strcpy (prev, SHELL_START);
/* Write the line number to the string. */
line = curr_line;
do {
prev[i++] = (line % 10) + '0';
/* We aren't trying to be very speedy here. */
prev = FACT_realloc (prev, i + sizeof (SHELL_END));
} while ((line /= 10) > 0);
for (j = i - 1, k = sizeof (SHELL_START) - 1; k < j; j--, k++) {
char hold;
hold = prev[j];
prev[j] = prev[k];
prev[k] = hold;
}
/* Set the end prompt. */
strcpy (prev + i, SHELL_END);
prev_len = i + sizeof (SHELL_END) - 1;
} else {
prev = FACT_malloc_atomic (prev_len - 1 + sizeof (SHELL_CONT));
memset (prev, ' ', prev_len - 2);
strcpy (prev + prev_len - 2, SHELL_CONT);
}
return prev;
}
void FACT_shell (void)
{
char *input;
const char *line;
size_t i;
volatile size_t last_ip;
FACT_t *ret_val;
FACT_tree_t parsed;
FACT_lexed_t tokenized;
struct cstack_t frame;
/* Editline variables. */
int ignore;
EditLine *el;
History *hist;
HistEvent hev;
input = NULL;
last_ip = 0;
/* Print out some info and then set up the prompt. */
printf ("FACT version %s\n", FACT_VERSION);
el = el_init ("/usr/bin/FACT", stdin, stdout, stderr);
el_set (el, EL_PROMPT, &shell_prompt);
el_set (el, EL_EDITOR, "emacs");
hist = history_init ();
history (hist, &hev, H_SETSIZE, REMEMBER_CMDS);
el_set (el, EL_HIST, history, hist);
/* Set error recovery. */
reset_error:
if (setjmp (recover)) {
/* Print out the error and a stack trace. */
fprintf (stderr, "Caught unhandled error: %s\n", curr_thread->curr_err.what);
while (curr_thread->cstackp - curr_thread->cstack >= 0) {
frame = pop_c ();
if (FACT_is_BIF (frame.this->extrn_func))
fprintf (stderr, "\tat built-in function %s\n", frame.this->name);
else
fprintf (stderr, "\tat scope %s (%s:%zu)\n", frame.this->name, FACT_get_file (frame.ip), FACT_get_line (frame.ip));
}
push_c (Furlow_offset(), frame.this);
goto reset_error;
}
for (;;) { /* This can be simplified a lot I think. */
new_stmt = true;
input = NULL;
i = 1;
do {
line = el_gets (el, &ignore);
new_stmt = false;
if (line != NULL && ignore > 0) {
i += strlen (line);
history (hist, &hev, H_ENTER, line);
if (input == NULL) {
input = FACT_malloc_atomic (i);
strcpy (input, line);
} else {
input = FACT_realloc (input, i);
strcat (input, line);
}
} else
break;
} while (!is_complete (line));
if (input == NULL)
break;
/* Tokenize and parse the code. */
tokenized = FACT_lex_string (input);
tokenized.line = curr_line;
/* Go through every token and get to the correct line. */
for (i = 0; tokenized.tokens[i].id != E_END; i++)
curr_line += tokenized.tokens[i].lines;
curr_line += tokenized.tokens[i].lines;
if (setjmp (tokenized.handle_err)) {
/* There was a parsing error, print it and skip. */
printf ("Parsing error (%s:%zu): %s.\n", "<stdin>", tokenized.line, tokenized.err);
continue;
}
parsed = FACT_parse (&tokenized);
/* There was no error, continue to compilation. */
FACT_compile (parsed, "<stdin>", true);
/* Run the code. */
Furlow_run ();
/* The X register contains the return value of the last expression. */
ret_val = Furlow_register (R_X);
printf ("%%");
if (ret_val->type == UNSET_TYPE) {
FACT_t *t;
if (ret_val->home != NULL &&
(t = FACT_find_in_table_nohash (ret_val->home, ret_val->ap)) != NULL)
ret_val = t;
}
if (ret_val->type == UNSET_TYPE)
printf (" Undef");
else if (ret_val->type == NUM_TYPE)
print_num ((FACT_num_t) ret_val->ap);
else if (FACT_is_BIF (((FACT_scope_t) ret_val->ap)->extrn_func))
printf (" Built-in function %s", ((FACT_scope_t) ret_val->ap)->name);
else /* Scope. */
print_scope ((FACT_scope_t) ret_val->ap);
printf ("\n");
Furlow_register (R_X)->type = UNSET_TYPE;
Furlow_register (R_X)->home = NULL;
}
history_end (hist);
el_end (el);
}
static void print_num (FACT_num_t val)
{
size_t i;
if (val->array_up != NULL) {
printf (" [");
for (i = 0; i < val->array_size; i++) {
if (i)
printf (", ");
print_num (val->array_up[i]);
}
printf (" ]");
} else
printf (" %s", mpc_get_str (val->value));
}
static void print_scope (FACT_scope_t val)
{
size_t i;
if (*val->marked)
return;
if (*val->array_up != NULL) {
printf (" [");
for (i = 0; i < *val->array_size; i++) {
if (i)
printf (", ");
print_scope ((*val->array_up)[i]);
}
printf (" ]");
} else {
printf ("{ %s%s ", val->name, ((val->vars->num_entries == 0) ? "\0" : ":"));
FACT_table_digest (val->vars);
printf ("}");
}
}