diff --git a/SMILEBASIC/VM.d b/SMILEBASIC/VM.d index 88005d5..be59165 100644 --- a/SMILEBASIC/VM.d +++ b/SMILEBASIC/VM.d @@ -12,11 +12,13 @@ class VM int pc; Value[] stack; Value[] global; - this(Code[] code, int len) + int[wstring] debugTable; + this(Code[] code, int len, int[wstring] debugTable) { this.code = code; this.stack = new Value[1024 * 1024]; this.global = new Value[len]; + this.debugTable = debugTable; } void run() { @@ -33,6 +35,10 @@ class VM { value = stack[--stacki]; } + Value testGetGlobaVariable(wstring name) + { + return global[debugTable[name]]; + } } enum CodeType { @@ -134,8 +140,8 @@ class Operate : Code { Value l; Value r; - vm.pop(l); vm.pop(r); + vm.pop(l); double ld = l.integerValue; double rd = r.integerValue; //とりあえずInteger @@ -154,6 +160,7 @@ class Operate : Code ld /= rd; break; default: + writeln("NotImpl: ", operator); break; } l.integerValue = cast(int)ld; diff --git a/SMILEBASIC/compiler.d b/SMILEBASIC/compiler.d index 0642587..10bf727 100644 --- a/SMILEBASIC/compiler.d +++ b/SMILEBASIC/compiler.d @@ -89,6 +89,7 @@ class Compiler } break; default: + stderr.writeln("Compile:NotImpl ", exp.type); break; } } @@ -132,6 +133,16 @@ class Compiler stderr.writeln("Compile:NotImpl ", i.type); } } - return new VM(code, globalIndex + 1); + return new VM(code, globalIndex + 1, global); } } + +unittest +{ + import otya.smilebasic.parser; + auto parser = new Parser("A=1+2+3+4*5/4-5+(6+6)*7"); + auto vm = parser.compile(); + vm.run(); + writeln(vm.testGetGlobaVariable("A").integerValue); + assert(vm.testGetGlobaVariable("A").integerValue == 1+2+3+4*5/4-5+(6+6)*7); +} diff --git a/SMILEBASIC/main.d b/SMILEBASIC/main.d index 4a7e94f..1da1eed 100644 --- a/SMILEBASIC/main.d +++ b/SMILEBASIC/main.d @@ -8,7 +8,7 @@ int main(string[] argv) writeln(parser.calc()); } - auto parser = new Parser("A=1+2+3+4\nPRINT 1+1,2+3;10-5,A"); + auto parser = new Parser("A=1+2+3+4\nPRINT 1+1,2+3;10-5,A:A=A*2:PRINT A"); auto vm = parser.compile(); vm.run(); readln(); diff --git a/SMILEBASIC/parser.d b/SMILEBASIC/parser.d index bd7361b..15f5a4e 100644 --- a/SMILEBASIC/parser.d +++ b/SMILEBASIC/parser.d @@ -359,6 +359,11 @@ class Parser } else { + if(token.type == TokenType.Colon || token.type == TokenType.NewLine) + { + print.addLine(); + break; + } lex.popFront(); if(token.type == TokenType.Semicolon) { @@ -514,4 +519,5 @@ unittest //Eval("test(2+5)") = 2+5; //Eval("test(2+5, test(2, 3+5))") = 3+5; Test!"2-3*4-5"(); + Test!"1+2+3+4*5/4-5+(6+6)*7"(); }