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
|
import std.stdio;
import std.string;
import parser;
import dbg;
/*
import compiler;
import obj;
import dbg;
import chunk;
import vm;
*/
void printForm(Form f, string prefix) {
switch (f.type) {
case FormType.EOF:
writeln("eof");
return;
case FormType.ATOM:
writefln("%s atom: %s", prefix, atomAsString(cast(Atom)f));
break;
case FormType.CONS:
Cons c = cast(Cons)f;
writef("%s cons <", prefix);
if (c.evaluate) {
writeln("true>");
} else {
writeln("false>");
}
printForm(c.head, format("%s>", prefix));
foreach (Form i ; c.tail) {
printForm(i, format("%s>", prefix));
}
break;
case FormType.NIL:
writefln("%s NIL", prefix);
break;
case FormType.PARSE_ERROR:
ParseError pe = cast(ParseError)f;
writefln("ERROR: %s", pe.message);
break;
case FormType.SYMBOL:
Symbol s = cast(Symbol)f;
writefln("%s sym: %s", prefix, s.name);
break;
case FormType.FUNC:
Func func = cast(Func)f;
writefln("%s <fn %s>", prefix, func.name.name);
printForm(func.args, format("%s -", prefix));
writefln("%s with %d body lines", prefix, func.funcBody.length);
writefln("%s <end fn %s>", prefix, func.name.name);
break;
default:
writeln("printFormDefault");
break;
}
}
void repl() {
while(true) {
write("> ");
string input = strip(stdin.readln());
if (input.length == 0) {
continue;
}
Parser parser = new Parser(input);
Form f;
while (true) {
f = parser.parseForm();
printForm(f, "");
if (f.type == FormType.EOF) {
break;
}
/*
if (is(typeof(f) == Eof)) {
writeln("eof");
break;
} else if (is(typeof(f) == Atom)) {
writeln("atom");
} else {
writeln("other");
}
*/
/*
if (typeof(f) == EOF) {
writeln("reached the end");
break;
} else if (typeof(f) == ParseError) {
writefln("got a parse error: %s", f.message);
break;
}
*/
}
/*
Compiler compiler = new Compiler(lex);
ObjFunction func = compiler.compile();
VM vm = new VM(func);
vm.run();
*/
}
}
void main() {
repl();
}
|