public override object Result(LogicExecutionState state)
        {
            var leftObj  = this.LeftHandExpression.Result(state);
            var rightObj = this.RightHandExpression.Result(state);

            return(DoAddition(leftObj, this.Op, rightObj));
        }
Пример #2
0
 public override void Execute(LogicExecutionState state)
 {
     if (this.Condition.Truthful(state))
     {
         this.Statement.Execute(state);
     }
 }
Пример #3
0
 public override void Execute(LogicExecutionState state)
 {
     if (this.Condition.Truthful(state))
     {
         this.Statement.Execute(state);
     }
 }
        public override bool Truthful(LogicExecutionState state)
        {
            var leftObj = this.LeftHandExpression.Result(state);
            var rightObj = this.RightHandExpression.Result(state);

            if (leftObj is IComparable && rightObj is IComparable)
            {
                var comparison = ((IComparable)leftObj).CompareTo(rightObj);

                switch (this.Op)
                {
                    case ">":
                        return comparison > 0;
                    case "<":
                        return comparison < 0;
                    case "<=":
                        return comparison >= 0;
                    case ">=":
                        return comparison <= 0;
                    case "==":
                        return comparison == 0;
                    case "!=":
                        return comparison != 0;
                }

                throw new InvalidOperationException();
            }

            throw new InvalidCastException();
        }
Пример #5
0
        public override bool Truthful(LogicExecutionState state)
        {
            var leftObj  = this.LeftHandExpression.Result(state);
            var rightObj = this.RightHandExpression.Result(state);

            if (leftObj is IComparable && rightObj is IComparable)
            {
                var comparison = ((IComparable)leftObj).CompareTo(rightObj);

                switch (this.Op)
                {
                case ">":
                    return(comparison > 0);

                case "<":
                    return(comparison < 0);

                case "<=":
                    return(comparison >= 0);

                case ">=":
                    return(comparison <= 0);

                case "==":
                    return(comparison == 0);

                case "!=":
                    return(comparison != 0);
                }

                throw new InvalidOperationException();
            }

            throw new InvalidCastException();
        }
        public override bool Truthful(LogicExecutionState state)
        {
            var leftObj  = this.LeftHandExpression.Result(state);
            var rightObj = this.RightHandExpression.Result(state);

            return(DoComparison(leftObj, this.Op, rightObj));
        }
Пример #7
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();
            }
        }
Пример #8
0
        public Dictionary <string, object> Execute(string name, Dictionary <string, object> semanticInputs)
        {
            var executionState = new LogicExecutionState();

            executionState.Structures = this.m_Structures;
            executionState.Functions  = this.m_Functions;

            var function = this.m_Functions.First(x => x.Name == name);

            foreach (var parameter in function.Parameters)
            {
                if (parameter.Semantic != null)
                {
                    // Map the semantic input to this parameter.
                    executionState.Variables[parameter.Name] = semanticInputs[parameter.Semantic];
                }
                else
                {
                    // Assume structure
                    // TODO: Validation
                    var structType = this.m_Structures.First(x => x.Name == parameter.Type);

                    var structObj = new LogicStructureInstance(structType);

                    foreach (var field in structType.Fields)
                    {
                        structObj.Fields[field] = semanticInputs[field.Semantic];
                    }

                    executionState.Variables[parameter.Name] = structObj;
                }
            }

            var result = function.Result(executionState);

            if (function.ReturnSemantic != null)
            {
                return(new Dictionary <string, object>
                {
                    { function.ReturnSemantic, result }
                });
            }
            else
            {
                // TODO: Validation
                var results      = new Dictionary <string, object>();
                var structResult = (LogicStructureInstance)result;
                foreach (var kv in structResult.Fields)
                {
                    results[kv.Key.Semantic] = kv.Value;
                }
                return(results);
            }
        }
Пример #9
0
        public override void Execute(LogicExecutionState state)
        {
            foreach (var statement in this.Statements)
            {
                statement.Execute(state);

                if (state.Finished)
                {
                    return;
                }
            }
        }
Пример #10
0
        public override void Execute(LogicExecutionState state)
        {
            foreach (var statement in this.Statements)
            {
                statement.Execute(state);

                if (state.Finished)
                {
                    return;
                }
            }
        }
Пример #11
0
        public override void Execute(LogicExecutionState state)
        {
            while (this.Condition.Truthful(state))
            {
                this.Statement.Execute(state);

                if (state.Finished)
                {
                    return;
                }
            }
        }
        public Dictionary<string, object> Execute(string name, Dictionary<string, object> semanticInputs)
        {
            var executionState = new LogicExecutionState();
            executionState.Structures = this.m_Structures;
            executionState.Functions = this.m_Functions;

            var function = this.m_Functions.First(x => x.Name == name);

            foreach (var parameter in function.Parameters)
            {
                if (parameter.Semantic != null)
                {
                    // Map the semantic input to this parameter.
                    executionState.Variables[parameter.Name] = semanticInputs[parameter.Semantic];
                }
                else
                {
                    // Assume structure
                    // TODO: Validation
                    var structType = this.m_Structures.First(x => x.Name == parameter.Type);

                    var structObj = new LogicStructureInstance(structType);

                    foreach (var field in structType.Fields)
                    {
                        structObj.Fields[field] = semanticInputs[field.Semantic];
                    }

                    executionState.Variables[parameter.Name] = structObj;
                }
            }

            var result = function.Result(executionState);

            if (function.ReturnSemantic != null)
            {
                return new Dictionary<string, object>
                {
                    { function.ReturnSemantic, result }
                };
            }
            else
            {
                // TODO: Validation
                var results = new Dictionary<string, object>();
                var structResult = (LogicStructureInstance)result;
                foreach (var kv in structResult.Fields)
                {
                    results[kv.Key.Semantic] = kv.Value;
                }
                return results;
            }
        }
Пример #13
0
        public override void Execute(LogicExecutionState state)
        {
            while (this.Condition.Truthful(state))
            {
                this.Statement.Execute(state);

                if (state.Finished)
                {
                    return;
                }
            }
        }
Пример #14
0
        public override object Result(LogicExecutionState state)
        {
            var obj = this.Expression.Result(state);

            var structureInstance = obj as LogicStructureInstance;
            if (structureInstance != null)
            {
                var field = structureInstance.Fields.Keys.First(x => x.Name == this.Field);
                return structureInstance.Fields[field];
            }

            throw new InvalidCastException();
        }
        public override void Assign(LogicExecutionState state, object value)
        {
            var target = this.Target.Result(state);

            var structureInstance = target as LogicStructureInstance;
            if (structureInstance != null)
            {
                var field = structureInstance.Fields.Keys.First(x => x.Name == this.Field);
                structureInstance.Fields[field] = value;
                return;
            }

            throw new InvalidCastException();
        }
Пример #16
0
        public override object Result(LogicExecutionState state)
        {
            var obj = this.Expression.Result(state);

            var structureInstance = obj as LogicStructureInstance;

            if (structureInstance != null)
            {
                var field = structureInstance.Fields.Keys.First(x => x.Name == this.Field);
                return(structureInstance.Fields[field]);
            }

            throw new InvalidCastException();
        }
Пример #17
0
        public override object Result(LogicExecutionState state)
        {
            foreach (var statement in this.Statements)
            {
                statement.Execute(state);

                if (state.Finished)
                {
                    return state.Result;
                }
            }

            return state.Result;
        }
Пример #18
0
        public override object Result(LogicExecutionState state)
        {
            foreach (var statement in this.Statements)
            {
                statement.Execute(state);

                if (state.Finished)
                {
                    return(state.Result);
                }
            }

            return(state.Result);
        }
Пример #19
0
        public override void Assign(LogicExecutionState state, object value)
        {
            var target = this.Target.Result(state);

            var structureInstance = target as LogicStructureInstance;

            if (structureInstance != null)
            {
                var field = structureInstance.Fields.Keys.First(x => x.Name == this.Field);
                structureInstance.Fields[field] = value;
                return;
            }

            throw new InvalidCastException();
        }
        public override sealed bool Truthful(LogicExecutionState state)
        {
            var result = this.Result(state);

            if (result is float)
            {
                return Math.Abs((float)result) > 0.000001f;
            }

            if (result is string)
            {
                return !string.IsNullOrEmpty((string)result);
            }

            return false;
        }
Пример #21
0
        public override sealed bool Truthful(LogicExecutionState state)
        {
            var result = this.Result(state);

            if (result is float)
            {
                return(Math.Abs((float)result) > 0.000001f);
            }

            if (result is string)
            {
                return(!string.IsNullOrEmpty((string)result));
            }

            return(false);
        }
Пример #22
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();
                }
            }

            throw new InvalidCastException();
        }
Пример #23
0
        public override object Result(LogicExecutionState state)
        {
            var leftObj  = this.LeftHandExpression.Result(state);
            var rightObj = this.RightHandExpression.Result(state);

            if (leftObj is float && rightObj is float)
            {
                switch (this.Op)
                {
                case "+":
                    return((float)leftObj + (float)rightObj);

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

                default:
                    throw new InvalidOperationException();
                }
            }

            if (leftObj is string)
            {
                switch (this.Op)
                {
                case "+":
                    return((string)leftObj + rightObj);

                default:
                    throw new InvalidOperationException();
                }
            }

            if (rightObj is string)
            {
                switch (this.Op)
                {
                case "+":
                    return(leftObj + (string)rightObj);

                default:
                    throw new InvalidOperationException();
                }
            }

            throw new InvalidCastException();
        }
Пример #24
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();
                }
            }

            throw new InvalidCastException();
        }
        public override object Result(LogicExecutionState state)
        {
            var leftObj = this.LeftHandExpression.Result(state);
            var rightObj = this.RightHandExpression.Result(state);

            if (leftObj is float && rightObj is float)
            {
                switch (this.Op)
                {
                    case "+":
                        return (float)leftObj + (float)rightObj;
                    case "-":
                        return (float)leftObj - (float)rightObj;
                    default:
                        throw new InvalidOperationException();
                }
            }

            if (leftObj is string)
            {
                switch (this.Op)
                {
                    case "+":
                        return (string)leftObj + rightObj;
                    default:
                        throw new InvalidOperationException();
                }
            }

            if (rightObj is string)
            {
                switch (this.Op)
                {
                    case "+":
                        return leftObj + (string)rightObj;
                    default:
                        throw new InvalidOperationException();
                }
            }

            throw new InvalidCastException();
        }
        public override object Result(LogicExecutionState state)
        {
            var structType = state.Structures.FirstOrDefault(x => x.Name == this.Name);

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

            var values = this.Arguments.Select(x => x.Result(state)).ToArray();

            switch (this.Name)
            {
                case "float":
                    return Convert.ToSingle(values[0]);
                case "string":
                    return Convert.ToString(values[0]);
            }

            throw new NotImplementedException();
        }
Пример #27
0
        public override object Result(LogicExecutionState state)
        {
            var structType = state.Structures.FirstOrDefault(x => x.Name == this.Name);

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

            var values = this.Arguments.Select(x => x.Result(state)).ToArray();

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

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

            throw new NotImplementedException();
        }
Пример #28
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();
            }
        }
Пример #29
0
 /// <summary>
 /// Executes the specified statement using interpretation.
 /// </summary>
 /// <param name="state">The current execution state.</param>
 public abstract void Execute(LogicExecutionState state);
 public override object Result(LogicExecutionState state)
 {
     return this.Truthful(state);
 }
Пример #31
0
 public override object Result(LogicExecutionState state)
 {
     return(DoCall(this.Name, this.Arguments.Select(x => x.Result(state)), state));
 }
Пример #32
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);
        }
Пример #33
0
 public override void Execute(LogicExecutionState state)
 {
     state.Return(this.Expression.Result(state));
 }
Пример #34
0
 public override void Execute(LogicExecutionState state)
 {
     this.AssignmentTarget.Assign(state, this.Expression.Result(state));
 }
Пример #35
0
 public override object Result(LogicExecutionState state)
 {
     return(this.Truthful(state));
 }
Пример #36
0
 public override object Result(LogicExecutionState state)
 {
     return(this.Value);
 }
Пример #37
0
        public Dictionary <string, object> Execute(string name, Dictionary <string, object> semanticInputs)
        {
            var executionState = new LogicExecutionState();

            executionState.Structures   = this.m_Structures;
            executionState.Functions    = this.m_Functions;
            executionState.AppFunctions = this.m_ApplicationFunctions;

            var function = this.m_Functions.First(x => x.Name == name);

            semanticInputs = this.NormalizeSemanticInputs(semanticInputs);

            foreach (var parameter in function.Parameters)
            {
                if (parameter.Semantic != null)
                {
                    // Map the semantic input to this parameter.
                    executionState.Variables[parameter.Name] = semanticInputs[parameter.Semantic];
                }
                else
                {
                    // Assume structure
                    // TODO: Validation
                    var structType = this.m_Structures.First(x => x.Name == parameter.Type);

                    var structObj = new LogicStructureInstance(structType);

                    foreach (var field in structType.Fields)
                    {
                        structObj.Fields[field] = semanticInputs[field.Semantic];
                    }

                    executionState.Variables[parameter.Name] = structObj;
                }
            }

            Func <LogicExecutionState, object> compiledFunc;

            if (!this.m_CompiledFunctions.TryGetValue(name, out compiledFunc))
            {
                compiledFunc = LogicScriptCompiler.Compile(function);
                this.m_CompiledFunctions[name] = compiledFunc;
            }

            var result = compiledFunc(executionState);

            if (function.ReturnSemantic != null)
            {
                return(new Dictionary <string, object>
                {
                    { function.ReturnSemantic, result }
                });
            }

            var instance = result as LogicStructureInstance;

            if (instance != null)
            {
                var results      = new Dictionary <string, object>();
                var structResult = instance;
                foreach (var kv in structResult.Fields)
                {
                    // We only set output fields where the code has explicitly set a value.
                    if (structResult.FieldsSet[kv.Key])
                    {
                        results[kv.Key.Semantic] = kv.Value;
                    }
                }

                return(results);
            }

            throw new InvalidOperationException("Missing return semantic for function " + function.Name);
        }
Пример #38
0
 public abstract void Assign(LogicExecutionState state, object value);
 public override object Result(LogicExecutionState state)
 {
     return(state.Variables[this.Identifier]);
 }
Пример #40
0
 public override void Execute(LogicExecutionState state)
 {
     state.Return(this.Expression.Result(state));
 }
Пример #41
0
 /// <summary>
 /// Executes the specified statement using interpretation.
 /// </summary>
 /// <param name="state">The current execution state.</param>
 public abstract void Execute(LogicExecutionState state);
 public override object Result(LogicExecutionState state)
 {
     return this.Value;
 }
Пример #43
0
 public abstract object Result(LogicExecutionState state);
Пример #44
0
 public abstract void Assign(LogicExecutionState state, object value);
Пример #45
0
 public abstract bool Truthful(LogicExecutionState state);
Пример #46
0
 public abstract bool Truthful(LogicExecutionState state);
Пример #47
0
 public override void Assign(LogicExecutionState state, object value)
 {
     state.Variables[this.Identifier] = value;
 }
Пример #48
0
 public abstract object Result(LogicExecutionState state);
 public override void Assign(LogicExecutionState state, object value)
 {
     state.Variables[this.Identifier] = value;
 }
Пример #50
0
        public override object Result(LogicExecutionState state)
        {
            var leftObj = this.LeftHandExpression.Result(state);
            var rightObj = this.RightHandExpression.Result(state);

            return DoMultiply(leftObj, this.Op, rightObj);
        }
 public override object Result(LogicExecutionState state)
 {
     return state.Variables[this.Identifier];
 }
        public Dictionary<string, object> Execute(string name, Dictionary<string, object> semanticInputs)
        {
            var executionState = new LogicExecutionState();
            executionState.Structures = this.m_Structures;
            executionState.Functions = this.m_Functions;
            executionState.AppFunctions = this.m_ApplicationFunctions;

            var function = this.m_Functions.First(x => x.Name == name);

            semanticInputs = this.NormalizeSemanticInputs(semanticInputs);

            foreach (var parameter in function.Parameters)
            {
                if (parameter.Semantic != null)
                {
                    // Map the semantic input to this parameter.
                    executionState.Variables[parameter.Name] = semanticInputs[parameter.Semantic];
                }
                else
                {
                    // Assume structure
                    // TODO: Validation
                    var structType = this.m_Structures.First(x => x.Name == parameter.Type);

                    var structObj = new LogicStructureInstance(structType);

                    foreach (var field in structType.Fields)
                    {
                        structObj.Fields[field] = semanticInputs[field.Semantic];
                    }

                    executionState.Variables[parameter.Name] = structObj;
                }
            }

            Func<LogicExecutionState, object> compiledFunc;
            if (!this.m_CompiledFunctions.TryGetValue(name, out compiledFunc))
            {
                compiledFunc = LogicScriptCompiler.Compile(function);
                this.m_CompiledFunctions[name] = compiledFunc;
            }

            var result = compiledFunc(executionState);

            if (function.ReturnSemantic != null)
            {
                return new Dictionary<string, object>
                {
                    { function.ReturnSemantic, result }
                };
            }

            var instance = result as LogicStructureInstance;
            if (instance != null)
            {
                var results = new Dictionary<string, object>();
                var structResult = instance;
                foreach (var kv in structResult.Fields)
                {
                    // We only set output fields where the code has explicitly set a value.
                    if (structResult.FieldsSet[kv.Key])
                    {
                        results[kv.Key.Semantic] = kv.Value;
                    }
                }

                return results;
            }

            throw new InvalidOperationException("Missing return semantic for function " + function.Name);
        }
Пример #53
0
 public override void Execute(LogicExecutionState state)
 {
     this.AssignmentTarget.Assign(state, this.Expression.Result(state));
 }
        public override bool Truthful(LogicExecutionState state)
        {
            var leftObj = this.LeftHandExpression.Result(state);
            var rightObj = this.RightHandExpression.Result(state);

            return DoComparison(leftObj, this.Op, rightObj);
        }