1
0

COMMON DEF(WIP)

This commit is contained in:
otya128
2016-12-02 21:27:03 +09:00
parent 2ae805d35c
commit ccc9da91e1
4 changed files with 21 additions and 9 deletions

View File

@@ -42,6 +42,7 @@ class VMSlot
class VM
{
VMSlot[5] slots;
Function[wstring] commonFunctions;
int slot;
VMSlot currentSlot;
int stacki;
@@ -1264,12 +1265,18 @@ class CallFunctionCode : Code
this.argCount = argCount;
this.outArgCount = outArgCount;
}
Function func;
void resolve(VM vm)
{
func = vm.currentSlot.functions.get(name, null);
throw new SyntaxError(name);
}
override void execute(VM vm)
{
Function func = vm.currentSlot.functions.get(name, null);
if(!func)
{
throw new SyntaxError(name);
//楽だし実行時に解決させる
resolve(vm);
}
if(func.argCount != this.argCount)
{

View File

@@ -1227,6 +1227,7 @@ class Compiler
genCode(new EndVM());
return code;
}
VM vm;
void compileDirectMode(VM vm)
{
isDirectMode = true;
@@ -1242,8 +1243,9 @@ class Compiler
VM compile()
{
isDirectMode = false;
compileProgram();
VM vm = new VM();
this.vm = vm;
compileProgram();
vm.loadSlot(0, code, globalIndex + 1, global, functions, globalScope.data, globalLabel, debugInfo);
vm.setCurrentSlot(0);
registerSystemVariable(vm);

View File

@@ -426,19 +426,22 @@ class DefineFunction : Statement
wstring name;
Statements functionBody;
bool returnExpr;
this(wstring name, bool returnExpr, SourceLocation loc)
bool isCommon;
this(wstring name, bool returnExpr, bool isCommon, SourceLocation loc)
{
super.location = loc;
this.type = NodeType.DefineFunction;
this.name = name;
this.returnExpr = returnExpr;
this.isCommon = isCommon;
}
this(wstring name, SourceLocation loc)
this(wstring name, bool isCommon, SourceLocation loc)
{
super.location = loc;
this.type = NodeType.DefineFunction;
this.name = name;
this.returnExpr = false;
this.isCommon = isCommon;
}
void addArgument(wstring name)
{

View File

@@ -558,12 +558,12 @@ class Parser
}
else
{
statement = defineFunction();
statement = defineFunction(true);
}
}
else if(token.type == TokenType.Def)
{
statement = defineFunction();
statement = defineFunction(false);
}
else
{
@@ -598,7 +598,7 @@ class Parser
}
return token.value.stringValue;
}
DefineFunction defineFunction()
DefineFunction defineFunction(bool isCommon)
{
lex.popFront();
auto token = lex.front();
@@ -607,7 +607,7 @@ class Parser
syntaxError();
return null;
}
DefineFunction node = new DefineFunction(token.value.stringValue, lex.location);
DefineFunction node = new DefineFunction(token.value.stringValue, isCommon, lex.location);
lex.popFront();
token = lex.front();
if(token.type == TokenType.LParen)