1
0

Add VAR(expr)=expr

This commit is contained in:
otya128
2016-12-08 22:32:51 +09:00
parent 879d28c9bf
commit 609bc0bcd5
3 changed files with 79 additions and 1 deletions

View File

@@ -1943,3 +1943,67 @@ class SwapCode : Code
*dp1 = num2;
}
}
class PushVarRefExpression : Code
{
Scope sc;
this(Scope sc)
{
this.sc = sc;
}
override void execute(VM vm)
{
Value valv;
vm.pop(valv);
auto name = valv.castString;
VMVariable vmv = VMVariable(int.min);
if (sc.func)
{
vmv = sc.func.variable.get(name, vmv);
if (vmv.index != int.min)
{
vm.push(Value(&vm.stack[vm.bp + vmv.index]));
return;
}
}
vmv = vm.currentSlot.globalTable.get(name, vmv);
if (vmv.index == int.min)
{
throw new UndefinedVariable();
}
vm.push(Value(&vm.currentSlot.global[vmv.index]));
}
}
class PushVarExpression : Code
{
Scope sc;
this(Scope sc)
{
this.sc = sc;
}
override void execute(VM vm)
{
Value valv;
vm.pop(valv);
auto name = valv.castString;
VMVariable vmv = VMVariable(int.min);
if (sc.func)
{
vmv = sc.func.variable.get(name, vmv);
if (vmv.index != int.min)
{
vm.push(vm.stack[vm.bp + vmv.index]);
return;
}
}
vmv = vm.currentSlot.globalTable.get(name, vmv);
if (vmv.index == int.min)
{
throw new UndefinedVariable();
}
vm.push(vm.currentSlot.global[vmv.index]);
}
}

View File

@@ -578,6 +578,13 @@ class Compiler
}
}
break;
case NodeType.VarRef:
{
auto varref = cast(VarRef)exp;
compileExpression(varref.expression, sc);
genCode(new PushVarExpression(sc));
}
break;
default:
stderr.writeln("Compile:NotImpl ", exp.type);
break;
@@ -628,6 +635,13 @@ class Compiler
}
}
break;
case NodeType.VarRef:
{
auto varref = cast(VarRef)exp;
compileExpression(varref.expression, sc);
genCode(new PushVarRefExpression(sc));
}
break;
default:
stderr.writeln("Compile:NotImpl ", exp.type);
break;

View File

@@ -628,6 +628,6 @@ class VarRef : Expression
{
super.location = loc;
this.type = NodeType.VarRef;
this.expression = expression;
this.expression = expr;
}
}