プログラム実行とスクロールの実装.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user