Implement SHIFT/UNSHIFT
This commit is contained in:
@@ -2720,6 +2720,62 @@ class BuiltinFunction
|
|||||||
throw new TypeMismatch();
|
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)
|
static void BACKTRACE(PetitComputer p)
|
||||||
{
|
{
|
||||||
auto bt = p.vm.backTrace;
|
auto bt = p.vm.backTrace;
|
||||||
|
|||||||
@@ -487,6 +487,35 @@ class Array(T)
|
|||||||
dim[0]--;
|
dim[0]--;
|
||||||
return last;
|
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)
|
@property void length(int size)
|
||||||
{
|
{
|
||||||
if (dimCount != 1)
|
if (dimCount != 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user