Пример #1
0
        private void CreateStatement(ParseTreeNode statementNode, Event evt, CompositeStatement compositeStatement = null)
        {
            AbstractStatement statement = null;
            if (statementNode.Term == grammar.Action)
            {
                statement = createAction(statementNode);
            }
            else if (statementNode.Term == grammar.Condition)
            {
                ConditionGroup conditionGroup = new ConditionGroup();
                conditionGroup.If.Expression = getStrVal(statementNode.ChildNodes.First(child => child.Term == grammar.Expr));
                foreach (ParseTreeNode child in grammar.GetOfType(statementNode, grammar.Actions)) CreateStatement(child, evt, conditionGroup.If);
                foreach (ParseTreeNode elseNode in grammar.GetOfType(statementNode, grammar.Else))
                {
                    string expr = getStrVal(elseNode.ChildNodes.FirstOrDefault(child => child.Term == grammar.Expr));
                    AbstractCondition cond;

                    if (null == expr)
                    {
                        conditionGroup.Else = new BasicCondition();
                        cond = conditionGroup.Else;
                    }
                    else
                    {
                        cond = new ExpressionCondition() { Expression = expr };
                        conditionGroup.AddStatement(cond);
                    }

                    foreach (ParseTreeNode child in grammar.GetOfType(elseNode, grammar.Actions)) CreateStatement(child, evt, cond);
                }
                statement = conditionGroup;
            }
            else if (statementNode.Term == grammar.Assignment)
            {
                Assignment assignment = new Assignment();
                assignment.Key = getStrVal(statementNode.ChildNodes[0]);
                assignment.Value = getStrVal(statementNode.ChildNodes.Last());

                assignment.Operator = statementNode.ChildNodes.Count == 2 ? AssignmentOperator.Assign :
                    AbstractAssignment.AssignmentValues[grammar.OpLookup[statementNode.ChildNodes[1].Term]];

                statement = assignment;
            }
            else
            {
                CompositeStatement loop = null;
                switch (statementNode.ChildNodes.Count)
                {
                    case 2:
                        WhileLoop wl = new WhileLoop();
                        wl.Expression = getStrVal(statementNode.ChildNodes[0]);
                        statement = loop = wl;
                        break;
                    case 3:
                        ForLoop fl = new ForLoop();
                        fl.Expression = getStrVal(statementNode.ChildNodes[0]);
                        //First assignment, expression, assignment, actions
                        fl.PostExpression = getStrVal(statementNode.ChildNodes[1]);
                        statement = loop = fl;
                        break;
                    case 4:
                        ForLoop forLoop = new ForLoop();
                        forLoop.Expression = getStrVal(statementNode.ChildNodes[1]);
                        //First assignment, expression, assignment, actions
                        forLoop.PostExpression = getStrVal(statementNode.ChildNodes[2]);
                        forLoop.PreExpression = getStrVal(statementNode.ChildNodes[0]);
                        statement = loop = forLoop;
                        break;
                }
                foreach (ParseTreeNode child in grammar.GetOfType(statementNode, grammar.Actions))
                {
                    // TODO: This is a hack to skip the Actions found in the first line of the for() statement.
                    if (!statementNode.ChildNodes.Contains(child))
                    {
                        CreateStatement(child, evt, loop);
                    }
                }
            }
            if (compositeStatement == null) evt.AddStatement(statement);
            else compositeStatement.AddStatement(statement);
        }