예제 #1
0
 public SetVariableAction(ITTEVariable variable, string op, ITTEExpression value, ITTEExpression index)
 {
     Variable = variable;
     Operator = op;
     Value    = value;
     Index    = index;
 }
예제 #2
0
        bool ContainExpression(out ITTEExpression expr)
        {
            _operators.Push(TTEOperator.Sentinel);
            bool result = Expression(out expr);

            _operators.Pop();
            return(result);
        }
 private void WriteSide(DecompileRule decompiler, ITTEExpression expression)
 {
     if (expression is TernaryExpression || (expression is BinaryOperatorExpression bop && bop.Operator.Precedence < Operator.Precedence))
     {
         decompiler.Append("(");
         expression.Decompile(decompiler);
         decompiler.Append(")");
     }
예제 #4
0
 // Commons as ITTEExpression
 bool Number(out ITTEExpression numberExpression)
 {
     if (Double(out double value))
     {
         numberExpression = new NumberExpression(value);
         return(true);
     }
     numberExpression = null;
     return(false);
 }
예제 #5
0
        bool VariableIndex(out ITTEExpression index)
        {
            if (!Match("["))
            {
                index = null;
                return(false);
            }

            ContainExpression(out index);

            Match("]");
            return(true);
        }
예제 #6
0
        void PopOperator()
        {
            var op = _operators.Pop();

            if (op.Type == OperatorType.Binary)
            {
                // Binary
                if (op.Contain == ContainGroup.Left)
                {
                    var right = _operands.Pop();
                    var left  = _operands.Pop();
                    _operands.Push(new BinaryOperatorExpression(left, right, op));
                    return;
                }

                Stack <ITTEExpression> exprs = new Stack <ITTEExpression>();
                exprs.Push(_operands.Pop());
                exprs.Push(_operands.Pop());

                while (_operators.Peek() == op)
                {
                    _operators.Pop();
                    exprs.Push(_operands.Pop());
                }

                ITTEExpression result = exprs.Pop();
                while (exprs.Count != 0)
                {
                    result = new BinaryOperatorExpression(result, exprs.Pop(), op);
                }

                _operands.Push(result);
            }
            else if (op.Type == OperatorType.Unary)
            {
                // Unary
                var value = _operands.Pop();
                _operands.Push(new UnaryOperatorExpression(value, op));
            }
            else
            {
                // Ternary
                var op2    = _operators.Pop();
                var rhs    = _operands.Pop();
                var middle = _operands.Pop();
                var lhs    = _operands.Pop();
                _operands.Push(new TernaryExpression(lhs, middle, rhs));
            }
        }
예제 #7
0
        private CsvPart PartFromExpression(ITTEExpression expression)
        {
            if (expression is NumberExpression num)
            {
                return(new CsvNumber(num.Value));
            }
            else if (expression is StringExpression str)
            {
                return(new CsvString(str.Value));
            }
            // Others
            else if (expression is FunctionExpression func)
            {
                switch (func.Function.WorkshopName)
                {
                // Array
                case "Array": return(new CsvArray(func.Values.Select(v => PartFromExpression(v)).ToArray()));

                // True
                case "True": return(new CsvBoolean(true));

                // False
                case "False": return(new CsvBoolean(false));

                // Null
                case "Null": return(new CsvNull());

                // Vector
                case "Vector":
                    return(new CsvVector(new Models.Vertex(
                                             ExtractComponent(func, 0, "X"),
                                             ExtractComponent(func, 1, "Y"),
                                             ExtractComponent(func, 2, "Z")
                                             )));

                // Default
                default:
                    Error("Unsure of how to handle function '" + func.Function.WorkshopName);
                    return(null);
                }
            }

            Error("Unsure of how to handle expression of type '" + expression.GetType().Name + "'.");
            return(null);
        }
예제 #8
0
        bool LegacyExpression(out ITTEExpression expr)
        {
            if (LegacyOperator(out expr))
            {
                return(true);
            }
            else if (Match("Value In Array"))
            {
                Match("(");
                ContainExpression(out var array);
                Match(",");
                ContainExpression(out var index);
                Match(")");

                expr = new IndexerExpression(array, index);
                return(true);
            }
            return(false);
        }
 private void WriteSide(DecompileRule decompiler, ITTEExpression expression, bool left)
 {
     if (expression is TernaryExpression || (
             expression is BinaryOperatorExpression bop &&
             (
                 bop.Operator.Precedence < Operator.Precedence ||
                 (
                     bop.Operator == Operator && (
                         bop.Operator.Contain != ContainGroup.None &&
                         left == (Operator.Contain == ContainGroup.Left)
                         )
                 )
             )
             ))
     {
         decompiler.Append("(");
         expression.Decompile(decompiler);
         decompiler.Append(")");
     }
예제 #10
0
        // Workshop string function
        bool WorkshopString(out ITTEExpression expr)
        {
            bool localized; // Determines if the string is localized.

            // Custom string
            if (Match(Kw("Custom String")))
            {
                localized = false;
            }
            // Localized string
            else if (Match(Kw("String")))
            {
                localized = true;
            }
            else
            {
                // Not a string
                expr = null;
                return(false);
            }

            Match("(");

            // Get the actual string.
            MatchString(out string str);

            // Get the format parameters.
            List <ITTEExpression> formats = new List <ITTEExpression>();

            while (Match(","))
            {
                if (ContainExpression(out ITTEExpression value))
                {
                    formats.Add(value);
                }
            }

            Match(")");

            expr = new StringExpression(str, formats.ToArray(), localized);
            return(true);
        }
예제 #11
0
        // Variables
        bool GlobalVariable(out ITTEExpression expr)
        {
            int c = Position; // Revert

            string name   = null;
            bool   result = Match("Global") &&
                            Match(".") &&
                            Identifier(out name);

            if (!result)
            {
                expr     = null;
                Position = c;
                return(false);
            }

            AddIfOmitted(name, true);
            expr = new GlobalVariableExpression(name);
            return(true);
        }
예제 #12
0
        bool MatchPlayerVariable(ITTEExpression parent, out ITTEExpression playerVariable)
        {
            playerVariable = parent;
            bool matched = false;

            while (Match("."))
            {
                matched = true;
                Identifier(out string name);
                AddIfOmitted(name, false);
                playerVariable = new PlayerVariableExpression(name, playerVariable);

                // Array index
                while (VariableIndex(out ITTEExpression index))
                {
                    playerVariable = new IndexerExpression(playerVariable, index);
                }
            }

            return(matched);
        }
예제 #13
0
        // Enumerator Values
        bool EnumeratorValue(out ITTEExpression expr)
        {
            if (Match(Kw("All Teams")))
            {
                expr = new ConstantEnumeratorExpression(ElementEnumMember.Team(Team.All));
                return(true);
            }
            if (Match(Kw("Team 1")))
            {
                expr = new ConstantEnumeratorExpression(ElementEnumMember.Team(Team.Team1));
                return(true);
            }
            if (Match(Kw("Team 2")))
            {
                expr = new ConstantEnumeratorExpression(ElementEnumMember.Team(Team.Team2));
                return(true);
            }
            // TODO: Gamemode, map, button, etc

            expr = null;
            return(false);
        }
 public TTECondition(bool disabled, ITTEExpression expression)
 {
     Disabled   = disabled;
     Expression = expression;
 }
예제 #15
0
 public TTECondition(string comment, bool disabled, ITTEExpression expression)
 {
     Comment    = comment;
     Disabled   = disabled;
     Expression = expression;
 }
예제 #16
0
        bool LegacyOperator(out ITTEExpression expr)
        {
            TTEOperator op = null;

            if (MatchAll(true, Kw("Add"), "("))
            {
                op = TTEOperator.Add;
            }
            else if (MatchAll(true, Kw("Subtract"), "("))
            {
                op = TTEOperator.Subtract;
            }
            else if (MatchAll(true, Kw("Multiply"), "("))
            {
                op = TTEOperator.Multiply;
            }
            else if (MatchAll(true, Kw("Divide"), "("))
            {
                op = TTEOperator.Divide;
            }
            else if (MatchAll(true, Kw("Modulo"), "("))
            {
                op = TTEOperator.Modulo;
            }
            else if (MatchAll(true, Kw("Raise To Power"), "("))
            {
                op = TTEOperator.Power;
            }
            else if (MatchAll(true, Kw("And"), "("))
            {
                op = TTEOperator.And;
            }
            else if (MatchAll(true, Kw("Or"), "("))
            {
                op = TTEOperator.Or;
            }
            else if (MatchAll(true, Kw("Compare"), "("))
            {
                ContainExpression(out ITTEExpression compareLeft);
                Match(",");

                if (Match("=="))
                {
                    op = TTEOperator.Equal;
                }
                else if (Match("!="))
                {
                    op = TTEOperator.NotEqual;
                }
                else if (Match(">="))
                {
                    op = TTEOperator.GreaterThanOrEqual;
                }
                else if (Match("<="))
                {
                    op = TTEOperator.LessThanOrEqual;
                }
                else if (Match(">"))
                {
                    op = TTEOperator.GreaterThan;
                }
                else if (Match("<"))
                {
                    op = TTEOperator.LessThan;
                }

                Match(",");
                ContainExpression(out ITTEExpression compareRight);
                Match(")");

                expr = new BinaryOperatorExpression(compareLeft, compareRight, op);
                return(true);
            }
            else if (MatchAll(true, Kw("If-Then-Else"), "("))
            {
                ContainExpression(out ITTEExpression condition);
                Match(",");
                ContainExpression(out ITTEExpression consequent);
                Match(",");
                ContainExpression(out ITTEExpression alternative);
                Match(")");

                expr = new TernaryExpression(condition, consequent, alternative);
                return(true);
            }

            if (op == null)
            {
                expr = null;
                return(false);
            }

            ContainExpression(out ITTEExpression left);
            Match(",");
            ContainExpression(out ITTEExpression right);
            Match(")");

            expr = new BinaryOperatorExpression(left, right, op);
            return(true);
        }
 public PlayerVariableExpression(string name, ITTEExpression player)
 {
     Name   = name;
     Player = player;
 }
예제 #18
0
        // Expressions
        bool Expression(out ITTEExpression expr, bool root = true)
        {
            expr = null;

            // Group
            if (Match("("))
            {
                ContainExpression(out expr);
                Match(")");
            }
            // Number
            else if (Number(out expr))
            {
            }
            // Enum value
            else if (EnumeratorValue(out expr))
            {
            }
            // Variable
            else if (GlobalVariable(out expr))
            {
            }
            // Legacy
            else if (LegacyExpression(out expr))
            {
            }
            // Function
            else if (Function(false, out FunctionExpression value))
            {
                expr = value;
            }
            // String
            else if (WorkshopString(out expr))
            {
            }
            // Unary operator
            else if (MatchUnary(out TTEOperator unaryOperator))
            {
                PushOperator(unaryOperator);
                Expression(out expr);
            }
            // No matches
            else
            {
                return(false);
            }

            // Array index
            while (VariableIndex(out ITTEExpression index))
            {
                expr = new IndexerExpression(expr, index);
            }

            // Player variable
            if (MatchPlayerVariable(expr, out ITTEExpression playerVariable))
            {
                expr = playerVariable;
            }

            // Push the expression
            _operands.Push(expr);

            // Binary operator
            while (MatchOperator(out TTEOperator op))
            {
                PushOperator(op);
                Expression(out ITTEExpression right, false);
            }
            while (_operators.Peek().Precedence > 0)
            {
                PopOperator();
            }

            // If this is the root, return the top operand.
            if (root)
            {
                expr = _operands.Pop();
            }

            return(true);
        }
예제 #19
0
        bool Action(out ITTEAction action)
        {
            action = null;

            // Comment
            MatchString(out string comment);
            bool isDisabled = Match(Kw("disabled"));

            // Subroutine
            if (Match(Kw("Call Subroutine")))
            {
                Match("(");
                Identifier(out string name);
                Match(")");
                action = new CallSubroutine(name, Parse.CallParallel.NoParallel);
            }
            // Start Rule Subroutine
            else if (Match(Kw("Start Rule")))
            {
                Match("(");
                Identifier(out string name);
                Match(",");

                if (Match(Kw("Restart Rule")))
                {
                    Match(")");
                    action = new CallSubroutine(name, Parse.CallParallel.AlreadyRunning_RestartRule);
                }
                else if (Match(Kw("Do Nothing")))
                {
                    Match(")");
                    action = new CallSubroutine(name, Parse.CallParallel.AlreadyRunning_DoNothing);
                }
                else
                {
                    throw new Exception("Expected 'Restart Rule' or 'Do Nothing'.");
                }
            }
            // Set variable.
            else if (Expression(out ITTEExpression expr))
            {
                // Unfold the index if required.
                ITTEExpression index = null;
                if (expr is IndexerExpression indexer)
                {
                    index = indexer.Index;
                    expr  = indexer.Expression;
                }

                // Make sure the expression is a variable.
                if (expr is ITTEVariable == false)
                {
                    throw new Exception("Expression is not a variable.");
                }

                string   op        = null;
                string[] operators = new string[] { "=", "+=", "-=", "/=", "*=", "%=", "^=" };
                foreach (string it in operators)
                {
                    if (Match(it))
                    {
                        op = it;
                        break;
                    }
                }

                Expression(out ITTEExpression value);
                action = new SetVariableAction((ITTEVariable)expr, op, value, index);
            }
            // Function.
            else if (Function(true, out FunctionExpression func))
            {
                action = func;
            }
            // Unknown.
            else
            {
                return(false);
            }

            action.Disabled = isDisabled;
            action.Comment  = comment;
            Match(";");
            return(true);
        }
 public BinaryOperatorExpression(ITTEExpression left, ITTEExpression right, TTEOperator op)
 {
     Left     = left;
     Right    = right;
     Operator = op;
 }
예제 #21
0
        private static Vertex[] ExtractVertexArray(string variableName, IPathmapErrorHandler errorHandler, ITTEExpression expression)
        {
            if (expression is FunctionExpression arrayFunction && arrayFunction.Function.Name == "Array")
            {
                Vertex[] vertices = new Vertex[arrayFunction.Values.Length];
                // Loop through each value.
                for (int i = 0; i < arrayFunction.Values.Length; i++)
                {
                    if (arrayFunction.Values[i] is FunctionExpression vectorFunction && vectorFunction.Function.Name == "Vector")
                    {
                        double x = ExtractVertexComponent(errorHandler, 0, vectorFunction),
                               y = ExtractVertexComponent(errorHandler, 1, vectorFunction),
                               z = ExtractVertexComponent(errorHandler, 2, vectorFunction);

                        vertices[i] = new Vertex(x, y, z);
                    }