1
0

Implement VAR(x:VAR)

This commit is contained in:
otya128
2016-12-29 16:58:14 +09:00
parent 33f8f67d13
commit 8a65b44002
2 changed files with 74 additions and 35 deletions

View File

@@ -340,7 +340,7 @@ class VM
static Name parse(wstring input) static Name parse(wstring input)
{ {
import std.array; import std.array;
sizediff_t index = cast(sizediff_t)input.indexOf(':'); sizediff_t index = input.indexOf(':');
if (index == -1) if (index == -1)
{ {
return Name(false, -1, input); return Name(false, -1, input);
@@ -406,7 +406,21 @@ class VM
} }
bool chkvar(wstring var) bool chkvar(wstring var)
{ {
if (var in currentSlot.globalTable) auto name = parse(var);
VMSlot slot;
if (name.hasSlot)
{
if (!checkSlotNumber(name.slot))
{
return false;
}
slot = slots[name.slot];
}
else
{
slot = currentSlot;
}
if (var in slot.globalTable)
return true; return true;
if (currentFunction && var in currentFunction.variable) if (currentFunction && var in currentFunction.variable)
return true; return true;
@@ -2288,99 +2302,124 @@ class SwapCode : Code
class PushVarRefExpression : Code class PushVarRefExpression : Code
{ {
Scope sc;
this(Scope sc)
{
this.sc = sc;
}
override void execute(VM vm) override void execute(VM vm)
{ {
Value valv; Value valv;
vm.pop(valv); vm.pop(valv);
auto name = valv.castDString; auto name = VM.parse(valv.castDString);
VMSlot slot;
if (name.hasSlot)
{
if (!vm.checkSlotNumber(name.slot))
{
throw new UndefinedVariable();
}
slot = vm.slots[name.slot];
}
else
{
slot = vm.currentSlot;
}
VMVariable vmv = VMVariable(int.min); VMVariable vmv = VMVariable(int.min);
if (sc.func) if (!name.hasSlot && vm.currentFunction)
{ {
vmv = sc.func.variable.get(name, vmv); vmv = vm.currentFunction.variable.get(name.name, vmv);
if (vmv.index != int.min) if (vmv.index != int.min)
{ {
vm.push(Value(&vm.stack[vm.bp + vmv.index])); vm.push(Value(&vm.stack[vm.bp + vmv.index]));
return; return;
} }
} }
vmv = vm.currentSlot.globalTable.get(name, vmv); vmv = slot.globalTable.get(name.name, vmv);
if (vmv.index == int.min) if (vmv.index == int.min)
{ {
throw new UndefinedVariable(); throw new UndefinedVariable();
} }
vm.push(Value(&vm.currentSlot.global[vmv.index])); vm.push(Value(&slot.global[vmv.index]));
} }
} }
class PushVarExpression : Code class PushVarExpression : Code
{ {
Scope sc;
this(Scope sc)
{
this.sc = sc;
}
override void execute(VM vm) override void execute(VM vm)
{ {
Value valv; Value valv;
vm.pop(valv); vm.pop(valv);
auto name = valv.castDString; auto name = VM.parse(valv.castDString);
VMSlot slot;
if (name.hasSlot)
{
if (!vm.checkSlotNumber(name.slot))
{
throw new UndefinedVariable();
}
slot = vm.slots[name.slot];
}
else
{
slot = vm.currentSlot;
}
VMVariable vmv = VMVariable(int.min); VMVariable vmv = VMVariable(int.min);
if (sc.func) if (!name.hasSlot && vm.currentFunction)
{ {
vmv = sc.func.variable.get(name, vmv); vmv = vm.currentFunction.variable.get(name.name, vmv);
if (vmv.index != int.min) if (vmv.index != int.min)
{ {
vm.push(vm.stack[vm.bp + vmv.index]); vm.push(vm.stack[vm.bp + vmv.index]);
return; return;
} }
} }
vmv = vm.currentSlot.globalTable.get(name, vmv); vmv = slot.globalTable.get(name.name, vmv);
if (vmv.index == int.min) if (vmv.index == int.min)
{ {
throw new UndefinedVariable(); throw new UndefinedVariable();
} }
vm.push(vm.currentSlot.global[vmv.index]); vm.push(slot.global[vmv.index]);
} }
} }
class PopVarExpression : Code class PopVarExpression : Code
{ {
Scope sc;
this(Scope sc)
{
this.sc = sc;
}
override void execute(VM vm) override void execute(VM vm)
{ {
Value valv; Value valv;
vm.pop(valv); vm.pop(valv);
Value expr; Value expr;
vm.pop(expr); vm.pop(expr);
auto name = valv.castDString; auto name = VM.parse(valv.castDString);
VMSlot slot;
if (name.hasSlot)
{
if (!vm.checkSlotNumber(name.slot))
{
throw new UndefinedVariable();
}
slot = vm.slots[name.slot];
}
else
{
slot = vm.currentSlot;
}
VMVariable vmv = VMVariable(int.min); VMVariable vmv = VMVariable(int.min);
if (sc.func) if (!name.hasSlot && vm.currentFunction)
{ {
vmv = sc.func.variable.get(name, vmv); vmv = vm.currentFunction.variable.get(name.name, vmv);
if (vmv.index != int.min) if (vmv.index != int.min)
{ {
vm.stack[vm.bp + vmv.index] = expr; vm.stack[vm.bp + vmv.index] = expr;
return; return;
} }
} }
vmv = vm.currentSlot.globalTable.get(name, vmv); vmv = slot.globalTable.get(name.name, vmv);
if (vmv.index == int.min) if (vmv.index == int.min)
{ {
throw new UndefinedVariable(); throw new UndefinedVariable();
} }
vm.currentSlot.global[vmv.index] = expr; slot.global[vmv.index] = expr;
} }
} }

View File

@@ -586,7 +586,7 @@ class Compiler
{ {
auto varref = cast(VarRef)exp; auto varref = cast(VarRef)exp;
compileExpression(varref.expression, sc); compileExpression(varref.expression, sc);
genCode(new PushVarExpression(sc)); genCode(new PushVarExpression());
} }
break; break;
default: default:
@@ -643,7 +643,7 @@ class Compiler
{ {
auto varref = cast(VarRef)exp; auto varref = cast(VarRef)exp;
compileExpression(varref.expression, sc); compileExpression(varref.expression, sc);
genCode(new PushVarRefExpression(sc)); genCode(new PushVarRefExpression());
} }
break; break;
default: default:
@@ -692,7 +692,7 @@ class Compiler
{ {
auto varref = cast(VarRef)expr; auto varref = cast(VarRef)expr;
compileExpression(varref.expression, sc); compileExpression(varref.expression, sc);
genCode(new PopVarExpression(sc)); genCode(new PopVarExpression());
} }
break; break;
case NodeType.VoidExpression: case NodeType.VoidExpression: