変数を実�
This commit is contained in:
@@ -9,17 +9,20 @@ class VM
|
|||||||
{
|
{
|
||||||
Code[] code;
|
Code[] code;
|
||||||
int stacki;
|
int stacki;
|
||||||
|
int pc;
|
||||||
Value[] stack;
|
Value[] stack;
|
||||||
this(Code[] code)
|
Value[] global;
|
||||||
|
this(Code[] code, int len)
|
||||||
{
|
{
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.stack = new Value[1024 * 1024];
|
this.stack = new Value[1024 * 1024];
|
||||||
|
this.global = new Value[len];
|
||||||
}
|
}
|
||||||
void run()
|
void run()
|
||||||
{
|
{
|
||||||
for(int i = 0; i < this.code.length; i++)
|
for(pc = 0; pc < this.code.length; pc++)
|
||||||
{
|
{
|
||||||
code[i].execute(this);
|
code[pc].execute(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void push(ref Value value)
|
void push(ref Value value)
|
||||||
@@ -34,6 +37,7 @@ class VM
|
|||||||
enum CodeType
|
enum CodeType
|
||||||
{
|
{
|
||||||
Push,
|
Push,
|
||||||
|
PushG,
|
||||||
Operate,
|
Operate,
|
||||||
Return,
|
Return,
|
||||||
Goto,
|
Goto,
|
||||||
@@ -65,7 +69,7 @@ class PrintCode : Code
|
|||||||
write(arg.integerValue);
|
write(arg.integerValue);
|
||||||
break;
|
break;
|
||||||
case ValueType.String:
|
case ValueType.String:
|
||||||
write(arg.stringValue.to!string);
|
write(arg.stringValue);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
//type missmatch
|
//type missmatch
|
||||||
@@ -90,6 +94,20 @@ class Push : Code
|
|||||||
vm.push(imm);
|
vm.push(imm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class PushG : Code
|
||||||
|
{
|
||||||
|
int var;
|
||||||
|
this(int var)
|
||||||
|
{
|
||||||
|
this.type = CodeType.PushG;
|
||||||
|
this.var = var;
|
||||||
|
}
|
||||||
|
override void execute(VM vm)
|
||||||
|
{
|
||||||
|
vm.push(vm.global[var]);
|
||||||
|
}
|
||||||
|
}
|
||||||
class Operate : Code
|
class Operate : Code
|
||||||
{
|
{
|
||||||
TokenType operator;
|
TokenType operator;
|
||||||
@@ -111,7 +129,16 @@ class Operate : Code
|
|||||||
}
|
}
|
||||||
class Goto : Code
|
class Goto : Code
|
||||||
{
|
{
|
||||||
|
int address;
|
||||||
|
this(int addr)
|
||||||
|
{
|
||||||
|
this.type = CodeType.Goto;
|
||||||
|
address = addr;
|
||||||
|
}
|
||||||
|
override void execute(VM vm)
|
||||||
|
{
|
||||||
|
vm.pc = address - 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
class Gosub : Code
|
class Gosub : Code
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,10 +14,16 @@ class Compiler
|
|||||||
}
|
}
|
||||||
|
|
||||||
Code[] code;
|
Code[] code;
|
||||||
|
int[wstring] global;
|
||||||
|
int globalIndex = 0;
|
||||||
void genCodeImm(Value value)
|
void genCodeImm(Value value)
|
||||||
{
|
{
|
||||||
code ~= new Push(value);
|
code ~= new Push(value);
|
||||||
}
|
}
|
||||||
|
void genCodePushGlobal(int ind)
|
||||||
|
{
|
||||||
|
code ~= new PushG(ind);
|
||||||
|
}
|
||||||
void genCodeOP(TokenType op)
|
void genCodeOP(TokenType op)
|
||||||
{
|
{
|
||||||
code ~= new Operate(op);
|
code ~= new Operate(op);
|
||||||
@@ -39,6 +45,15 @@ class Compiler
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case NodeType.Variable:
|
case NodeType.Variable:
|
||||||
|
auto var = cast(Variable)exp;
|
||||||
|
int global = this.global.get(var.name, 0);
|
||||||
|
if(global == 0)
|
||||||
|
{
|
||||||
|
//local変数をあたる
|
||||||
|
//それでもだめならOPTION STRICTならエラー
|
||||||
|
this.global[var.name] = ++globalIndex = global;
|
||||||
|
}
|
||||||
|
genCodePushGlobal(global);
|
||||||
break;
|
break;
|
||||||
case NodeType.CallFunction:
|
case NodeType.CallFunction:
|
||||||
{
|
{
|
||||||
@@ -89,6 +104,6 @@ class Compiler
|
|||||||
stderr.writeln("Compile:NotImpl ", i.type);
|
stderr.writeln("Compile:NotImpl ", i.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new VM(code);
|
return new VM(code, globalIndex + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ int main(string[] argv)
|
|||||||
writeln(parser.calc());
|
writeln(parser.calc());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto parser = new Parser("PRINT 1+1,2+3;10-5");
|
auto parser = new Parser("PRINT 1+1,2+3;10-5,A");
|
||||||
auto vm = parser.compile();
|
auto vm = parser.compile();
|
||||||
vm.run();
|
vm.run();
|
||||||
readln();
|
readln();
|
||||||
|
|||||||
Reference in New Issue
Block a user