コード例 #1
0
        BasicNode Compare(Func <int, bool> resCheck, BasicEnvironment env, ConstantNode[] args)
        {
            var       x            = args[0];
            var       y            = args[1];
            const int InvalidValue = -99;
            int       realRes      = InvalidValue;

            if (x is NumericConstantNode || y is NumericConstantNode)
            {
                var nx = x.ToNumericConstant()?.Value;
                var ny = y.ToNumericConstant()?.Value;
                if (nx.HasValue && ny.HasValue)
                {
                    realRes = nx.Value.CompareTo(ny.Value);
                }
            }
            if (realRes == InvalidValue && (x is StringConstantNode || y is StringConstantNode))
            {
                var sx = x.StringValue;
                var sy = y.StringValue;
                realRes = string.Compare(sx, sy, StringComparison.CurrentCulture);
            }
            if (realRes == InvalidValue && (x is BoolConstantNode || y is BoolConstantNode))
            {
                var bx = x.BoolValue;
                var by = y.BoolValue;
                realRes = bx.CompareTo(by);
            }
            if (realRes == InvalidValue)
            {
                return(env.RuntimeError("incompatible types for comparison"));
            }
            return(new BoolConstantNode(resCheck(realRes)));
        }
コード例 #2
0
        BasicNode NumFunc(BasicEnvironment env, ConstantNode[] args, Func <double, double> func)
        {
            var num = args[0].ToNumericConstant()?.Value;

            if (!num.HasValue)
            {
                return(env.RuntimeError($"internal function called with invalid argument {args[0].ToString()}"));
            }
            return(new NumericConstantNode(func(num.Value)));
        }
コード例 #3
0
ファイル: Property.cs プロジェクト: t-stf/TsBasic
        public BasicNode Redim(BasicEnvironment env, int[] newDims)
        {
            if (newDims.Length != dims.Length)
            {
                return(env.RuntimeError("dimension count mismatch"));
            }
            bool sameDims = true;

            for (int i = 0; i < newDims.Length; i++)
            {
                if (dims[i] == newDims[i])
                {
                    continue;
                }
                sameDims = false;
                break;
            }
            if (sameDims)
            {
                return(ControlNode.Make(EvalResultKind.Ok, null));
            }

            int[] curIndex = new int[dims.Length];
            var   dst      = new ArrayProperty <T>(newDims);

            while (true)
            {
                curIndex[0]++;
                for (int i = 0; i < curIndex.Length - 1; i++)
                {
                    if (curIndex[i] < dims[i])
                    {
                        break;
                    }
                    if (i == curIndex.Length - 1)
                    {
                        break;
                    }
                    curIndex[i] = 0;
                    curIndex[i + 1]++;
                }
                if (curIndex[curIndex.Length - 1] >= dims[curIndex.Length - 1])
                {
                    break;
                }
                T val;
                if (GetValue(curIndex, out val) && val != null)
                {
                    dst.SetValue(curIndex, val);
                }
            }

            return(ControlNode.Make(EvalResultKind.Ok, null));
        }