aboutsummaryrefslogtreecommitdiff
path: root/main.d
diff options
context:
space:
mode:
Diffstat (limited to 'main.d')
-rw-r--r--main.d118
1 files changed, 118 insertions, 0 deletions
diff --git a/main.d b/main.d
new file mode 100644
index 0000000..18927d8
--- /dev/null
+++ b/main.d
@@ -0,0 +1,118 @@
+import std.stdio;
+import std.string;
+
+import parser;
+/*
+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();
+}