From 6c7be9b92e689a446bab63366bfd2e87c31f1f5a Mon Sep 17 00:00:00 2001 From: otya128 Date: Thu, 9 Jul 2015 21:28:15 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=82=B0=E3=83=A9=E3=83=A0?= =?UTF-8?q?=E5=AE=9F=E8=A1=8C=E3=81=A8=E3=82=B9=E3=82=AF=E3=83=AD=E3=83=BC?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E5=AE=9F=E8=A3=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SMILEBASIC/VM.d | 25 ++++++++++++++- SMILEBASIC/petitcomputer.d | 63 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/SMILEBASIC/VM.d b/SMILEBASIC/VM.d index 5f9747f..6c8bf16 100644 --- a/SMILEBASIC/VM.d +++ b/SMILEBASIC/VM.d @@ -3,6 +3,7 @@ import otya.smilebasic.type; import otya.smilebasic.token; import otya.smilebasic.error; import otya.smilebasic.compiler; +import otya.smilebasic.petitcomputer; import std.uni; import std.utf; import std.conv; @@ -27,6 +28,7 @@ class VM VMVariable[wstring] globalTable; Function[wstring] functions; int bp; + PetitComputer petitcomputer; this(Code[] code, int len, VMVariable[wstring] globalTable, Function[wstring] functions) { this.code = code; @@ -41,7 +43,7 @@ class VM } void run() { - bp = 0;//globalを実行なのでbaseは0(グローバル変数をスタックに取るようにしない限り) + bp = 0;//globalを実行なのでbaseは0(グローバル変数をスタックに取るようにしない限り)(挙動的にスタックに確保していなさそう) for(pc = 0; pc < this.code.length; pc++) { code[pc].execute(this); @@ -51,6 +53,21 @@ class VM stderr.writeln("CompilerBug?:stack"); } } + void init(PetitComputer petitcomputer) + { + this.petitcomputer = petitcomputer; + bp = 0;//globalを実行なのでbaseは0(グローバル変数をスタックに取るようにしない限り)(挙動的にスタックに確保していなさそう) + } + bool runStep() + { + if(pc < this.code.length) + { + code[pc].execute(this); + pc++; + return true; + } + return false; + } void push(ref Value value) { stack[stacki++] = value; @@ -122,12 +139,18 @@ class PrintCode : Code { case ValueType.Integer: write(arg.integerValue); + if(vm.petitcomputer) + vm.petitcomputer.printConsole(arg.integerValue); break; case ValueType.Double: write(arg.doubleValue); + if(vm.petitcomputer) + vm.petitcomputer.printConsole(arg.doubleValue); break; case ValueType.String: write(arg.stringValue); + if(vm.petitcomputer) + vm.petitcomputer.printConsole(arg.stringValue); break; default: //type mismatch diff --git a/SMILEBASIC/petitcomputer.d b/SMILEBASIC/petitcomputer.d index 511f4ea..058a6f6 100644 --- a/SMILEBASIC/petitcomputer.d +++ b/SMILEBASIC/petitcomputer.d @@ -7,6 +7,7 @@ import std.stdio; import std.conv; import std.string; import std.c.stdio; +import otya.smilebasic.parser; class GraphicPage { SDL_Surface* surface; @@ -175,9 +176,43 @@ class PetitComputer SDL_WINDOW_SHOWN); renderer = SDL_CreateRenderer(window, -1, 0); GRPF.createTexture(renderer); - printConsole("Hello, World!!v('ω')v"); + //とりあえず + auto parser = new Parser(readText("FIZZBUZZ.TXT").to!wstring); + auto vm = parser.compile(); + bool running = true; + vm.init(this); while (true) { + auto startTicks = SDL_GetTicks(); + uint elapse; + do + { + try + { + if(running) running = vm.runStep(); + } + catch(SmileBasicError sbe) + { + try + { + printConsole(sbe.to!string); + } + catch + { + } + } + catch(Throwable t) + { + try + { + printConsole(t.to!string); + } + catch + { + } + } + elapse = SDL_GetTicks() - startTicks; + } while(elapse <= 1000 / 60); SDL_Event event; SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); @@ -212,10 +247,21 @@ class PetitComputer SDL_RenderCopy(renderer, GRPF.texture, &fontTable[console[y][x]], &rect); } } - void printConsole(wstring text) + void printConsole(T...)(T args) + { + foreach(i; args) + { + printConsoleString(i.to!wstring); + } + } + void printConsoleString(wstring text) { foreach(wchar c; text) { + if(CSRY >= consoleHeight) + { + CSRY = consoleHeight - 1; + } if(c != '\r' && c != '\n') { console[CSRY][CSRX] = c; @@ -226,6 +272,19 @@ class PetitComputer CSRX = 0; CSRY++; } + if(CSRY >= consoleHeight) + { + auto tmp = console[0]; + for(int i = 0; i < consoleHeight - 1; i++) + { + console[i] = console[i + 1]; + } + console[consoleHeight - 1] = tmp; + for(int j = 0; j < tmp.length; j++) + tmp[j] = '\0'; + assert(console[0] != console[2]); + CSRY = consoleHeight - 1; + } } } }