From d3c43920872602ead46b7bd8f8a972d757dbf765 Mon Sep 17 00:00:00 2001 From: otya128 Date: Sat, 24 Dec 2016 19:29:37 +0900 Subject: [PATCH] Add function data to stack frame --- SMILEBASIC/VM.d | 15 +++++++++++++++ SMILEBASIC/compiler.d | 2 +- SMILEBASIC/type.d | 7 +++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/SMILEBASIC/VM.d b/SMILEBASIC/VM.d index 882478b..f129d76 100644 --- a/SMILEBASIC/VM.d +++ b/SMILEBASIC/VM.d @@ -276,6 +276,7 @@ class VM return "undefined variable"; } otya.smilebasic.type.Data currentData;//DATAの位置は全スロット共通 + Function currentFunction; Value readData() { Value value; @@ -1342,6 +1343,8 @@ class ReturnFunction : Code vm.pop(bp); Value hae; vm.pop(hae); + Value cfunc; + vm.pop(cfunc); if (hae.type != ValueType.Data) { throw new TypeMismatch(); @@ -1350,6 +1353,14 @@ class ReturnFunction : Code { vm.currentData = hae.data; } + if (cfunc.type != ValueType.Function) + { + throw new TypeMismatch(); + } + else + { + vm.currentFunction = cfunc.func; + } vm.stacki -= func.argCount; if(func.returnExpr) { @@ -1420,6 +1431,8 @@ class CallFunctionCode : Code vm.pushBackTrace(name); //TODO:args auto bp = vm.stacki; + vm.push(Value(vm.currentFunction)); + vm.currentFunction = func; vm.push(Value(vm.currentData)); vm.currentData.index = 0; vm.currentData.table = func.scope_.data; @@ -1495,6 +1508,8 @@ class CallFunctionS : Code } //TODO:args auto bp = vm.stacki; + vm.push(Value(vm.currentFunction)); + vm.currentFunction = func; vm.push(Value(vm.currentData)); vm.currentData.index = 0; vm.currentData.table = func.scope_.data; diff --git a/SMILEBASIC/compiler.d b/SMILEBASIC/compiler.d index 71437d3..0465ac0 100644 --- a/SMILEBASIC/compiler.d +++ b/SMILEBASIC/compiler.d @@ -85,7 +85,7 @@ class DataTable } class Function { - static const frameSize = 3; + static const frameSize = 4; int address; wstring name; int argCount; diff --git a/SMILEBASIC/type.d b/SMILEBASIC/type.d index 0f0d63d..b0abd87 100644 --- a/SMILEBASIC/type.d +++ b/SMILEBASIC/type.d @@ -18,6 +18,7 @@ enum ValueType : byte StringReference, StringArrayReference, Data, + Function, } import otya.smilebasic.compiler; import otya.smilebasic.vm; @@ -56,6 +57,7 @@ struct Value ArrayReference!wchar stringReference; ArrayReference!(Array!wchar) stringArrayReference; Data data; + Function func; } this(int value) { @@ -129,6 +131,11 @@ struct Value this.type = ValueType.Data; this.data = data; } + this(Function func) + { + this.type = ValueType.Function; + this.func = func; + } void castOp(ValueType type) { switch(type)