1
0

ラベルとGOTOを実�

This commit is contained in:
otya128
2015-06-06 14:51:45 +09:00
parent 88ad0392a1
commit 2eaab2ef55
6 changed files with 91 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ class Compiler
Code[] code;
int[wstring] global;
int[wstring] globalLabel;
int globalIndex = 0;
void genCodeImm(Value value)
{
@@ -32,6 +33,10 @@ class Compiler
{
code ~= new Operate(op);
}
void genCodeGoto(wstring label)
{
code ~= new GotoS(label);
}
int defineGlobalVarIndex(wstring name)
{
int global = this.global.get(name, 0);
@@ -129,10 +134,28 @@ class Compiler
genCodePopGlobal(getGlobalVarIndex(assign.name));
}
break;
case NodeType.Label:
{
auto label = cast(Label)i;
globalLabel[label.label] = code.length;
}
break;
case NodeType.Goto:
{
genCodeGoto((cast(Goto)i).label);
}
break;
default:
stderr.writeln("Compile:NotImpl ", i.type);
}
}
foreach(int i, Code c; code)
{
if(c.type == CodeType.GotoS)
{
code[i] = new GotoAddr(globalLabel[(cast(GotoS)c).label]);
}
}
return new VM(code, globalIndex + 1, global);
}
}