From ccc9da91e1c238221c1a40be2a758843dade4a13 Mon Sep 17 00:00:00 2001 From: otya128 Date: Fri, 2 Dec 2016 21:27:03 +0900 Subject: [PATCH] COMMON DEF(WIP) --- SMILEBASIC/VM.d | 11 +++++++++-- SMILEBASIC/compiler.d | 4 +++- SMILEBASIC/node.d | 7 +++++-- SMILEBASIC/parser.d | 8 ++++---- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/SMILEBASIC/VM.d b/SMILEBASIC/VM.d index a0ab458..af23faa 100644 --- a/SMILEBASIC/VM.d +++ b/SMILEBASIC/VM.d @@ -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) { diff --git a/SMILEBASIC/compiler.d b/SMILEBASIC/compiler.d index 7802220..ab53a6e 100644 --- a/SMILEBASIC/compiler.d +++ b/SMILEBASIC/compiler.d @@ -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); diff --git a/SMILEBASIC/node.d b/SMILEBASIC/node.d index 6205364..8616488 100644 --- a/SMILEBASIC/node.d +++ b/SMILEBASIC/node.d @@ -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) { diff --git a/SMILEBASIC/parser.d b/SMILEBASIC/parser.d index 05bba9d..5e42faa 100644 --- a/SMILEBASIC/parser.d +++ b/SMILEBASIC/parser.d @@ -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)