Add abs function. Fix "at" operator.
This commit is contained in:
@@ -114,6 +114,7 @@ namespace Cryville.Crtr {
|
||||
_ctxops.Add(IdentifierManager.SharedInstance.Request("clamp"), new func_clamp(() => ContextSelfValue));
|
||||
_ctxops.Add(IdentifierManager.SharedInstance.Request("min"), new func_min(() => ContextSelfValue));
|
||||
_ctxops.Add(IdentifierManager.SharedInstance.Request("max"), new func_max(() => ContextSelfValue));
|
||||
_ctxops.Add(IdentifierManager.SharedInstance.Request("abs"), new func_abs(() => ContextSelfValue));
|
||||
}
|
||||
static PdtEvaluator() {
|
||||
_shortops.Add(new PdtOperatorSignature("@", 2), new op_at_2());
|
||||
@@ -237,12 +238,12 @@ namespace Cryville.Crtr {
|
||||
class op_at_2 : PdtOperator {
|
||||
public op_at_2() : base(2) { }
|
||||
protected override void Execute() {
|
||||
int _, pc;
|
||||
int _;
|
||||
var op0 = GetOperand(0);
|
||||
var op1 = (int)Math.Round(GetOperand(1).AsNumber());
|
||||
if (op0.Type != PdtInternalType.Vector) throw new InvalidOperationException("Not a vector");
|
||||
op0.GetArraySuffix(out _, out pc);
|
||||
if (op1 >= pc) throw new IndexOutOfRangeException();
|
||||
op0.GetArraySuffix(out _, out _);
|
||||
if (op1 >= (op0.Length - sizeof(int)) / sizeof(float)) throw new IndexOutOfRangeException();
|
||||
float result = GetOperand(0).AsNumber(op1 * sizeof(float));
|
||||
GetReturnFrame(PdtInternalType.Number, sizeof(float)).SetNumber(result);
|
||||
}
|
||||
@@ -366,6 +367,22 @@ namespace Cryville.Crtr {
|
||||
ret.SetNumber(Mathf.Max(a, b));
|
||||
}
|
||||
}
|
||||
class func_abs : PdtOperator {
|
||||
readonly Func<PropSrc> _ctxcb;
|
||||
public func_abs(Func<PropSrc> ctxcb) : base(1) {
|
||||
_ctxcb = ctxcb;
|
||||
}
|
||||
protected override unsafe void Execute() {
|
||||
var ret = GetReturnFrame(PdtInternalType.Number, sizeof(float));
|
||||
float arg;
|
||||
switch (LoadedOperandCount) {
|
||||
case 0: arg = oputil.AsNumber(_ctxcb()); break;
|
||||
case 1: arg = GetOperand(0).AsNumber(); break;
|
||||
default: throw new ArgumentException("Argument count not 0 or 1");
|
||||
}
|
||||
ret.SetNumber(Mathf.Abs(arg));
|
||||
}
|
||||
}
|
||||
unsafe static class oputil {
|
||||
public static float AsNumber(PropSrc src) {
|
||||
if (src == null)
|
||||
|
Reference in New Issue
Block a user