1
0

RETURN without GOSUBするようにした

This commit is contained in:
otya128
2015-06-07 21:03:55 +09:00
parent f7c4f06eae
commit 9794c2f01c
2 changed files with 19 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ class VM
Value[] stack; Value[] stack;
Value[] global; Value[] global;
int[wstring] globalTable; int[wstring] globalTable;
int bp;
ValueType getType(wstring name) ValueType getType(wstring name)
{ {
wchar s = name[name.length - 1]; wchar s = name[name.length - 1];
@@ -42,6 +43,7 @@ class VM
} }
void run() void run()
{ {
bp = 0;//globalを実行なのでbaseは0(グローバル変数をスタックに取るようにしない限り)
for(pc = 0; pc < this.code.length; pc++) for(pc = 0; pc < this.code.length; pc++)
{ {
code[pc].execute(this); code[pc].execute(this);
@@ -55,9 +57,13 @@ class VM
{ {
stack[stacki++] = value; stack[stacki++] = value;
} }
bool canPop()
{
return stacki > bp;
}
void pop(out Value value) void pop(out Value value)
{ {
if(stacki <= 0) if(stacki <= bp)
{ {
writeln("Stack underflow"); writeln("Stack underflow");
readln(); readln();
@@ -462,6 +468,10 @@ class ReturnSubroutine : Code
override void execute(VM vm) override void execute(VM vm)
{ {
Value pc; Value pc;
if(!vm.canPop())
{
throw new ReturnWithoutGosub();
}
vm.pop(pc); vm.pop(pc);
if(pc.type != ValueType.Integer || pc.integerValue < 0 || pc.integerValue >= vm.code.length) if(pc.type != ValueType.Integer || pc.integerValue < 0 || pc.integerValue >= vm.code.length)
{ {

View File

@@ -35,4 +35,11 @@ class DuplicateVariable : SmileBasicError
super("Duplicate variable"); super("Duplicate variable");
} }
} }
class ReturnWithoutGosub : SmileBasicError
{
this()
{
this.errnum = 30;
super("RETURN without GOSUB");
}
}