From ab6deb5eb1244f566c8a8b22022c68d50d5eb4d7 Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Mon, 22 May 2023 20:26:27 -0400 Subject: make the REPL actually remember things --- compiler.d | 11 ----------- main.d | 5 +++-- vm.d | 4 +--- 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/compiler.d b/compiler.d index 76f6c04..4b7775e 100644 --- a/compiler.d +++ b/compiler.d @@ -50,12 +50,9 @@ class Compiler { } void compileAdd(Form[] args) { - writeln("compiling add"); int line = args[0].line; resolve(args[0]); - writeln("resolved the first argument"); func.chunk.writeOp(OpCode.OP_TYPE_CHECK_NUMBER, line); - writeln("wrote the typecheck op"); // (+ n) always returns n if (args.length == 1) { @@ -208,7 +205,6 @@ class Compiler { } void compileIf(Form form) { - writeln("compiling if..."); If if_ = cast(If)form; resolve(if_.cond); @@ -281,9 +277,7 @@ class Compiler { Block block = cast(Block)form; beginScope(); foreach (Form inner; block.blockBody) { - writeln("about to compile an inner form"); resolve(inner); - writeln("finished compiling inner form"); } endScope(); } @@ -314,7 +308,6 @@ class Compiler { } void compileFunc(Form form) { - writeln("compiling func"); Func f = cast(Func)form; // name the function @@ -335,8 +328,6 @@ class Compiler { } } - writeln("got the arguments"); - /* // compile each inner Form foreach (Form inner; f.funcBody) { @@ -350,8 +341,6 @@ class Compiler { b.blockBody = f.funcBody; compiler.compileBlock(b); - writeln("compiled the body"); - // write the function as a Value Function outFunc = compiler.finish(); func.chunk.writeOp(OpCode.OP_CONSTANT, f.line); diff --git a/main.d b/main.d index eeff97d..b540058 100644 --- a/main.d +++ b/main.d @@ -21,6 +21,7 @@ import vm; void repl() { + VM vm = new VM(); while(true) { write("> "); @@ -41,7 +42,7 @@ void repl() { VM vm = new VM(func); vm.run(); */ - interpret(input); + interpret(input, vm); } } @@ -70,7 +71,7 @@ int main(string[] args) { string data = readFile(fname); - interpret(data); + interpret(data, new VM()); } return 0; } diff --git a/vm.d b/vm.d index 5b69a23..ea6a1ae 100644 --- a/vm.d +++ b/vm.d @@ -457,13 +457,11 @@ class VM { } } -InterpretResult interpret(string source) { +InterpretResult interpret(string source, VM vm) { Parser parser = new Parser(source); Compiler compiler = new Compiler(ObjType.SCRIPT, &parser); Function func = compiler.compile(); - VM vm = new VM(); - // vm.globals[":int"] = makeTypeValue(":int"); vm.pushA(makeObjValue(func)); -- cgit v1.2.3