-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrepl.c
167 lines (156 loc) · 4.66 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
163
164
165
166
167
#include <stdio.h>
#include <string.h>
#include "vm.h"
#include "parser.h"
#include "compiler.h"
#include "scanner.h"
#include "object.h"
#include "debug.h"
#include "memory.h"
#include "linenoise.h"
static ObjFunction *Func = NULL;
static char *lines[50];
static int numLines = 0;
static const char *prompt = "> ";
static void scannerAddLine(Scanner *scan, char *src) {
ASSERT(src);
ASSERT(scan->source);
size_t newsz = strlen(scan->source)+1+strlen(src)+1;
char *buf = calloc(1, newsz);
ASSERT_MEM(buf);
strcpy(buf, scan->source);
strcat(buf, src);
strcat(buf, "\n");
xfree(scan->source);
scan->source = buf;
}
static void getMoreSourceFn(Scanner *scan, Parser *p) {
char *line = NULL;
line = linenoise(prompt);
if (!line && p) { // CTRL-D
p->aborted = true;
return;
}
linenoiseHistoryAdd(line);
/*fprintf(stderr, "add src called\n");*/
scannerAddLine(scan, line);
/*fprintf(stderr, "/add src called\n");*/
lines[numLines++] = line;
}
static bool evalNode(Node *program) {
resetStack();
vm.exited = false;
THREAD()->hadError = false;
CompileErr cerr = COMPILE_ERR_NONE;
/*fprintf(stderr, "compiling code: '%s'", code);*/
Func = compile_node(program, &cerr);
if (cerr != COMPILE_ERR_NONE) {
fprintf(stderr, "%s", "Compilation error\n");
return false;
}
/*fprintf(stderr, "interpreting code\n");*/
InterpretResult ires = interpret(Func, "(repl)");
unhideFromGC(TO_OBJ(Func));
resetStack();
if (ires != INTERPRET_OK) {
fprintf(stderr, "%s", "Error evaluating code\n");
return false;
}
return true;
}
static void freeLines(char *lines[], int numLines) {
for (int i = 0; i < numLines; i++) {
xfree(lines[i]);
}
}
static void _resetScanner(void) {
Scanner *scan = getScanner();
if (scan->source) xfree(scan->source);
initScanner(scan, strdup(""));
scannerSetMoreSourceFn(scan, getMoreSourceFn);
}
NORETURN void repl(void) {
setScanner(&scanner);
_resetScanner();
initVM();
linenoiseHistorySetMaxLen(500);
// we want to evaluate unused expressions, like the statement `1+1`,
// because we print the last VM value after the user types in an
// expression or statement.
compilerOpts.noRemoveUnusedExpressions = true;
char *line = NULL;
while ((line = linenoise(prompt)) != NULL) { // NOTE: chomps newline
linenoiseHistoryAdd(line);
if (numLines == 0 && strcmp(line, "exit") == 0) {
xfree(line);
line = NULL;
break;
}
if (numLines == 0 && strcmp(line, "pstack") == 0) {
printVMStack(stderr, THREAD());
xfree(line);
line = NULL;
continue;
}
// resets the VM, re-inits the code chunk
if (numLines == 0 && strcmp(line, "reset") == 0) {
fprintf(stderr, "Resetting VM... ");
freeVM();
initVM();
_resetScanner();
xfree(line);
line = NULL;
fprintf(stderr, "done.\n");
continue;
}
ASSERT(line);
if (numLines == 50) {
fprintf(stderr, "Too many lines");
ASSERT(0); // FIXME: don't just error out
}
lines[numLines++] = line;
scannerAddLine(getScanner(), line);
Parser p;
initParser(&p);
Node *n = parseMaybePartialStatement(&p, getMoreSourceFn);
if (p.hadError) {
fprintf(stderr, "%s", "Parser error\n");
outputParserErrors(&p, stderr);
freeParser(&p);
freeLines(lines, numLines);
numLines = 0;
_resetScanner();
line = NULL;
continue;
}
freeParser(&p);
ASSERT(n);
bool compileOk = evalNode(n);
if (!compileOk) {
freeLines(lines, numLines);
numLines = 0;
_resetScanner();
line = NULL;
continue;
}
Value *val = getLastValue();
fprintf(stderr, "%s", " => ");
if (val) {
// Add first callframe in case there are none, because
// printValue may call native methods (toString()), which
// rely on the framecount to be at least 1.
if (THREAD()->ec->frameCount == 0) {
THREAD()->ec->frameCount++;
}
printInspectValue(stderr, *val);
} else {
printValue(stderr, NIL_VAL, false, -1);
}
fprintf(stderr, "%s", "\n");
freeLines(lines, numLines);
numLines = 0;
_resetScanner();
line = NULL;
}
stopVM(0);
}