1
0

Implement SHIFT/UNSHIFT

This commit is contained in:
otya128
2016-12-20 21:24:05 +09:00
parent c03ab93395
commit e00cee0344
2 changed files with 85 additions and 0 deletions

View File

@@ -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;

View File

@@ -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)