diff --git a/SMILEBASIC/builtinfunctions.d b/SMILEBASIC/builtinfunctions.d index a8f76fb..54a7caa 100644 --- a/SMILEBASIC/builtinfunctions.d +++ b/SMILEBASIC/builtinfunctions.d @@ -2720,6 +2720,62 @@ class BuiltinFunction throw new TypeMismatch(); } } + static void UNSHIFT(Value ary, Value exp) + { + if (!ary.isArray) + { + throw new TypeMismatch("UNSHIFT", 1); + } + if (!exp.canCast(ary.elementType)) + { + throw new TypeMismatch("UNSHIFT"); + } + if (ary.dimCount != 1) + throw new TypeMismatch("UNSHIFT", 1); + switch (ary.type) + { + case ValueType.String: + ary.stringValue.unshift(exp.castString); + break; + case ValueType.IntegerArray: + ary.integerArray.unshift(exp.castInteger); + break; + case ValueType.DoubleArray: + ary.doubleArray.unshift(exp.castDouble); + break; + case ValueType.StringArray: + ary.stringArray.unshift(exp.castString); + break; + default: + throw new TypeMismatch(); + } + } + static Value SHIFT(Value ary) + { + if (!ary.isArray) + { + throw new TypeMismatch("SHIFT", 1); + } + if (ary.dimCount != 1) + throw new TypeMismatch("SHIFT", 1); + if (ary.length == 0) + { + throw new SubscriptOutOfRange("SHIFT"); + } + switch (ary.type) + { + case ValueType.String: + return Value(ary.stringValue.shift()); + case ValueType.IntegerArray: + return Value(ary.integerArray.shift()); + case ValueType.DoubleArray: + return Value(ary.doubleArray.shift()); + case ValueType.StringArray: + return Value(ary.stringArray.shift()); + default: + throw new TypeMismatch(); + } + } static void BACKTRACE(PetitComputer p) { auto bt = p.vm.backTrace; diff --git a/SMILEBASIC/type.d b/SMILEBASIC/type.d index e41af29..acb0c70 100644 --- a/SMILEBASIC/type.d +++ b/SMILEBASIC/type.d @@ -487,6 +487,35 @@ class Array(T) dim[0]--; return last; } + void unshift(T v) + { + if (dimCount != 1) + { + throw new TypeMismatch(); + } + array = v ~ array; + dim[0]++; + } + void unshift(Array!T v) + { + if (dimCount != 1) + { + throw new TypeMismatch(); + } + array = v.array ~ array; + dim[0] = cast(int)length; + } + T shift() + { + if (dimCount != 1) + { + throw new TypeMismatch(); + } + auto l = array[0]; + array = array[1..$]; + dim[0]--; + return l; + } @property void length(int size) { if (dimCount != 1)