From 77d7c4366ee09726a2d492feae99c9c8539a012c Mon Sep 17 00:00:00 2001 From: otya128 Date: Sun, 11 Dec 2016 20:27:24 +0900 Subject: [PATCH] Implement PUSH --- SMILEBASIC/builtinfunctions.d | 33 +++++++++++++++++++++++++++++++++ SMILEBASIC/type.d | 29 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/SMILEBASIC/builtinfunctions.d b/SMILEBASIC/builtinfunctions.d index 62dc722..09f20cf 100644 --- a/SMILEBASIC/builtinfunctions.d +++ b/SMILEBASIC/builtinfunctions.d @@ -2355,6 +2355,39 @@ class BuiltinFunction } return std.math.log(a) / std.math.log(b); } + static void PUSH(Value ary, Value exp) + { + if (!ary.isArray) + { + throw new TypeMismatch("PUSH", 1); + } + if (!exp.canCast(ary.elementType)) + { + throw new TypeMismatch("PUSH"); + } + switch (ary.type) + { + case ValueType.String: + throw new IllegalFunctionCall("NOTIMPL:PUSH string"); + case ValueType.IntegerArray: + if (ary.integerArray.dimCount != 1) + throw new TypeMismatch("PUSH", 1); + ary.integerArray.push(exp.castInteger); + break; + case ValueType.DoubleArray: + if (ary.doubleArray.dimCount != 1) + throw new TypeMismatch("PUSH", 1); + ary.doubleArray.push(exp.castDouble); + break; + case ValueType.StringArray: + if (ary.stringArray.dimCount != 1) + throw new TypeMismatch("PUSH", 1); + ary.stringArray.push(exp.castString); + break; + default: + throw new TypeMismatch(); + } + } //alias void function(PetitComputer, Value[], Value[]) BuiltinFunc; static BuiltinFunctions[wstring] builtinFunctions; static wstring getBasicName(BFD)(const wstring def) diff --git a/SMILEBASIC/type.d b/SMILEBASIC/type.d index d2a7fd3..abb40a7 100644 --- a/SMILEBASIC/type.d +++ b/SMILEBASIC/type.d @@ -235,6 +235,26 @@ struct Value throw new TypeMismatch(); } } + ValueType elementType() + { + switch(this.type) + { + case ValueType.String: + return ValueType.String; + case ValueType.IntegerArray: + return ValueType.Integer; + case ValueType.DoubleArray: + return ValueType.Double; + case ValueType.StringArray: + return ValueType.String; + default: + throw new TypeMismatch(); + } + } + bool canCast(ValueType t) + { + return t == this.type || ((t == ValueType.Integer || t == ValueType.Double) && isNumber); + } } //import std.experimental.ndslice; //ndsliceがこれに相当? @@ -359,5 +379,14 @@ class Array(T) if(i1 >= dim[0] && i2 >= dim[1] && i3 >= dim[2] && i4 >= dim[3]) throw new SubscriptOutOfRange(); return array[i1] = v;//array[i1 * dim[0] * dim[1] * dim[2] + i2 * dim[1] + i3] = v; } + void push(T v) + { + if (dimCount != 1) + { + throw new TypeMismatch(); + } + array ~= v; + dim[0]++; + } }