Example #1
0
        public override object Result(LogicExecutionState state)
        {
            var obj = this.Expression.Result(state);

            if (obj is float)
            {
                switch (this.Op)
                {
                case "-":
                    return(-(float)obj);

                default:
                    throw new InvalidOperationException();
                }
            }

            switch (this.Op)
            {
            case "-":
                return(LogicBuiltins.Negate(new List <object> {
                    obj
                }));

            default:
                throw new InvalidOperationException();
            }
        }
        public static object DoAddition(object leftObj, string op, object rightObj)
        {
            if (leftObj is float && rightObj is float)
            {
                switch (op)
                {
                case "+":
                    return((float)leftObj + (float)rightObj);

                case "-":
                    return((float)leftObj - (float)rightObj);

                default:
                    throw new InvalidOperationException();
                }
            }

            if (leftObj is string)
            {
                switch (op)
                {
                case "+":
                    return((string)leftObj + rightObj);

                default:
                    throw new InvalidOperationException();
                }
            }

            if (rightObj is string)
            {
                switch (op)
                {
                case "+":
                    return(leftObj + (string)rightObj);

                default:
                    throw new InvalidOperationException();
                }
            }

            switch (op)
            {
            case "+":
                return(LogicBuiltins.Add(new List <object> {
                    leftObj, rightObj
                }));

            case "-":
                return(LogicBuiltins.Subtract(new List <object> {
                    leftObj, rightObj
                }));

            default:
                throw new InvalidOperationException();
            }
        }
Example #3
0
        public override Expression Compile(ParameterExpression stateParameterExpression, LabelTarget returnTarget)
        {
            switch (this.Op)
            {
            case "-":
                return
                    (Expr.Invoke(
                         (Expression <Func <object, object> >)
                             (x => x is float? - (float)x: LogicBuiltins.Negate(new List <object> {
                    x
                })),
                         Expr.Convert(this.Expression.Compile(stateParameterExpression, returnTarget), typeof(object))));

            default:
                throw new InvalidOperationException();
            }
        }
        public static object DoMultiply(object leftObj, string op, object rightObj)
        {
            if (leftObj is float && rightObj is float)
            {
                switch (op)
                {
                case "*":
                    return((float)leftObj * (float)rightObj);

                case "/":
                    return((float)leftObj / (float)rightObj);

                default:
                    throw new InvalidOperationException();
                }
            }

            if (op == "*")
            {
                if ((leftObj is Vector2 || leftObj is Vector3 || leftObj is Vector4) && rightObj is Matrix)
                {
                    return(LogicBuiltins.Transform(leftObj, rightObj));
                }

                if (leftObj is Matrix && (rightObj is Vector2 || rightObj is Vector3 || rightObj is Vector4))
                {
                    return(LogicBuiltins.Transform(rightObj, leftObj));
                }
            }

            switch (op)
            {
            case "*":
                return(LogicBuiltins.Multiply(new List <object> {
                    leftObj, rightObj
                }));

            case "/":
                return(LogicBuiltins.Divide(new List <object> {
                    leftObj, rightObj
                }));

            default:
                throw new InvalidOperationException();
            }
        }
Example #5
0
        public static object DoCall(string name, IEnumerable <object> arguments, LogicExecutionState state)
        {
            var structType = state.Structures.FirstOrDefault(x => x.Name == name);

            if (structType != null)
            {
                return(new LogicStructureInstance(structType));
            }

            var values = arguments.ToList();

            switch (name)
            {
            case "float":
                return(Convert.ToSingle(values[0]));

            case "string":
                return(Convert.ToString(values[0]));

            case "float2":
                return(new Vector2(Convert.ToSingle(values[0]), Convert.ToSingle(values[1])));

            case "float3":
                if (values[0] is Vector2)
                {
                    return(new Vector3((Vector2)values[0], Convert.ToSingle(values[1])));
                }

                return(new Vector3(Convert.ToSingle(values[0]), Convert.ToSingle(values[1]), Convert.ToSingle(values[2])));

            case "float4":
                if (values[0] is Vector3)
                {
                    return(new Vector4((Vector3)values[0], Convert.ToSingle(values[1])));
                }

                return(new Vector4(Convert.ToSingle(values[0]), Convert.ToSingle(values[1]), Convert.ToSingle(values[2]), Convert.ToSingle(values[3])));

            case "matrix":
                return(new Matrix());
            }

            switch (name)
            {
            case "max":
                return(LogicBuiltins.Max(values));

            case "min":
                return(LogicBuiltins.Min(values));

            case "sin":
                return(LogicBuiltins.Sin(values));

            case "cos":
                return(LogicBuiltins.Cos(values));

            case "tan":
                return(LogicBuiltins.Tan(values));

            case "atan":
                return(LogicBuiltins.Atan(values));

            case "atan2":
                return(LogicBuiltins.Atan2(values));

            case "lerp":
                return(LogicBuiltins.Lerp(values));

            case "abs":
                return(LogicBuiltins.Abs(values));

            case "normalize":
                return(LogicBuiltins.Normalize(values));

            case "ceil":
                return(LogicBuiltins.Ceiling(values));

            case "floor":
                return(LogicBuiltins.Floor(values));

            case "round":
                return(LogicBuiltins.Round(values));

            case "distance":
                return(LogicBuiltins.Distance(values));

            case "distancesqr":
                return(LogicBuiltins.DistanceSquared(values));

            case "matident":
                return(Matrix.Identity);

            case "mattrans":
                return(Matrix.CreateTranslation((Vector3)values[0]));

            case "matrotx":
                return(Matrix.CreateRotationX(Convert.ToSingle(values[0])));

            case "matroty":
                return(Matrix.CreateRotationY(Convert.ToSingle(values[0])));

            case "matrotz":
                return(Matrix.CreateRotationZ(Convert.ToSingle(values[0])));

            case "matscale":
                return(Matrix.CreateScale(Convert.ToSingle(values[0])));

            case "pi":
                return(MathHelper.Pi);

            default:
                if (state.AppFunctions.ContainsKey(name))
                {
                    return(state.AppFunctions[name](values.ToArray()));
                }
                break;
            }

            var userFunction = state.Functions.FirstOrDefault(x => x.Name == name);

            if (userFunction != null)
            {
                throw new NotImplementedException();
            }

            throw new InvalidOperationException("Function missing: " + name);
        }