diff options
| author | Ben Winston | 2023-05-20 15:12:30 -0400 |
|---|---|---|
| committer | Ben Winston | 2023-05-20 15:12:30 -0400 |
| commit | 0c70372774297272dd14133d48e40e9a3624420a (patch) | |
| tree | 4e66fb1b97a0fd537a20dca4ddcd26003f83c94d /main.d | |
| parent | 6902cc5abe09da9f6f2d86f22d06684d97cfa9f3 (diff) | |
initial commit of compiler/VM with a couple basic instructions
Diffstat (limited to 'main.d')
| -rw-r--r-- | main.d | 90 |
1 files changed, 23 insertions, 67 deletions
@@ -3,6 +3,10 @@ import std.string; import parser; import dbg; +import chunk; +import compiler; +import vm; + /* import compiler; import obj; @@ -11,53 +15,6 @@ 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) { @@ -69,37 +26,36 @@ void repl() { } Parser parser = new Parser(input); + + /* + Chunk chunk = new Chunk(); Form f; while (true) { f = parser.parseForm(); - printForm(f, ""); + + f.compile(chunk); + + if (f.type == FormType.EOF) { break; } + } + */ + Compiler compiler = new Compiler(FunctionType.SCRIPT, &parser); + Function func = compiler.compile(); - /* - if (is(typeof(f) == Eof)) { - writeln("eof"); - break; - } else if (is(typeof(f) == Atom)) { - writeln("atom"); - } else { - writeln("other"); - } - */ + VM vm = new VM(func); + vm.run(); - /* - if (typeof(f) == EOF) { - writeln("reached the end"); - break; - } else if (typeof(f) == ParseError) { - writefln("got a parse error: %s", f.message); - break; - } - */ + /* + writeln("== disassembling chunk =="); + int off = 0; + while (off < func.chunk.code.length) { + off = disassemble(func.chunk, off); } + */ /* Compiler compiler = new Compiler(lex); |
