Exemple #1
0
        public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
        {
            var syntax = (ScriptIfStatement)syntaxNode;

            var code = new CodeIfStatement(
                AstDomCompiler.Compile <CodeExpression>(syntax.Condition.Expression, prog),
                AstDomCompiler.Compile <CodeStatement>(syntax.Statement, prog),
                syntax.ElseStatement == null ? null : AstDomCompiler.Compile <CodeStatement>(syntax.ElseStatement, prog));


            return(code);
        }
    public CodeObject Compile(AstNode syntaxNode, CodeProgram prog)
    {
      var syntax = (ScriptIfStatement)syntaxNode;

      var code = new CodeIfStatement(
         AstDomCompiler.Compile<CodeExpression>(syntax.Condition.Expression, prog),
         AstDomCompiler.Compile<CodeStatement>(syntax.Statement, prog),
         syntax.ElseStatement == null ? null : AstDomCompiler.Compile<CodeStatement>(syntax.ElseStatement, prog));


      return code;
    }
    private static CodeStatement BuildNextIf(IList<CodeSwitchCase> list, int index)
    {
      if (index < 0 || list.Count <= index) return null;

      var current = list[index];

      //Default case
      if (current.Condition == null) return current.Statement;

      CodeStatement next = new CodeIfStatement(
        new CodeBinaryOperator { Left = new CodeVariableReference("#switch_" + _sid), Right = current.Condition, Type = OperatorType.Eq },
        current.Statement,
        BuildNextIf(list, index + 1));

      return next;
    }
Exemple #4
0
        private static CodeStatement BuildNextIf(IList <CodeSwitchCase> list, int index)
        {
            if (index < 0 || list.Count <= index)
            {
                return(null);
            }

            var current = list[index];

            //Default case
            if (current.Condition == null)
            {
                return(current.Statement);
            }

            CodeStatement next = new CodeIfStatement(
                new CodeBinaryOperator {
                Left = new CodeVariableReference("#switch_" + _sid), Right = current.Condition, Type = OperatorType.Eq
            },
                current.Statement,
                BuildNextIf(list, index + 1));

            return(next);
        }
        private bool WriteIf(BasicBlock block, Conditional conditional, List<CodeStatement> stmts, Context context)
        {
            var preStmts = block.Statements.Take(block.Statements.Count - 1);
            AddManyStatements(stmts, preStmts);

            var last = (CodeExpressionStatement)block.Statements.Last();
            var ifStmt = new CodeIfStatement {
                Condition = last.Expression
            };

            AddStatement(stmts, ifStmt);

            bool emptyThen = false;
            // is there a follow?
            if (conditional.Follow != null && conditional.Follow != context.LoopFollow) {
                // process the THEN part
                bool isBreak = false;
                var succ = block.ThenEdge;
                if (succ.DfsTraversed != DfsTraversal.Alpha) {
                    if (succ != conditional.Follow) {
                        // THEN part
                        ifStmt.Condition = ifStmt.Condition.Invert();
                        isBreak = WriteCode(block, (BasicBlock)succ, ifStmt.TrueStatements, context.NewUntil(conditional.Follow));
                    }
                    else {
                        // empty THEN part => negate ELSE part
                        isBreak = WriteCode(block, (BasicBlock)block.ElseEdge, ifStmt.TrueStatements, context.NewUntil(conditional.Follow));
                        emptyThen = true;
                    }
                }

                // process the ELSE part
                succ = block.ElseEdge;
                if (succ.DfsTraversed != DfsTraversal.Alpha) {
                    if (succ != conditional.Follow) {
                        // ELSE part
                        List<CodeStatement> output;
                        if (isBreak)
                            output = stmts;
                        else
                            output = ifStmt.FalseStatements;
                        isBreak = WriteCode(block, (BasicBlock)succ, output, context.NewUntil(conditional.Follow));
                        if (block == conditional.Follow)
                            return isBreak;
                    }
                }
                else if (!emptyThen) {
                    // already visited => emit label
                    throw new InvalidOperationException();
                }

                // Continue with the follow
                return WriteCode(null, (BasicBlock)conditional.Follow, stmts, context);
            }
            else {
                // no follow => if..then..else
                ifStmt.Condition = ifStmt.Condition.Invert();
                var isBreak = WriteCode(block, (BasicBlock)block.ThenEdge, ifStmt.TrueStatements, context);
                var output = ifStmt.FalseStatements;
                if (isBreak)
                    output = stmts;
                return WriteCode(block, (BasicBlock)block.ElseEdge, output, context);
            }
        }