Пример #1
0
 public ForLoop(Parser yyp, ForLoopStatement flsa, Expression e, ForLoopStatement flsb, Statement s)
     : base((yyp))
 {
     kids.Add(flsa);
     kids.Add(e);
     kids.Add(flsb);
     if (0 < s.kids.Count && s.kids.Top is CompoundStatement) kids.Add(s.kids.Pop());
     else kids.Add(s);
 }
        /// <summary>
        ///     Generates the code for a Statement node.
        /// </summary>
        /// <param name="s">The Statement node.</param>
        /// <returns>String containing C# code for Statement s.</returns>
        string GenerateStatement(Statement s)
        {
            StringBuilder retVal = new StringBuilder();
            bool printSemicolon = true;

            bool marc = FuncCallsMarc();
            retVal.Append(Indent());

            if (0 < s.kids.Count)
            {
                // Jump label prints its own colon, we don't need a semicolon.
                printSemicolon = !(s.kids.Top is JumpLabel);

                // If we encounter a lone Ident, we skip it, since that's a C#
                // (MONO) error.
                if (!(s.kids.Top is IdentExpression && 1 == s.kids.Count))
                {
                    foreach (SYMBOL kid in s.kids)
                    {
                        //                        if (kid is Assignment && m_SLCompatabilityMode)
                        if (kid is Assignment)
                        {
                            Assignment a = kid as Assignment;
                            List<string> identifiers = new List<string>();
                            checkForMultipleAssignments(identifiers, a);

                            SYMBOL firstChild = (SYMBOL) a.kids[0];
                            bool retStrChanged = false;
                            if (firstChild is Declaration &&
                                DuplicatedLocalVariables[GetLocalDeclarationKey()].ContainsKey(
                                    ((Declaration) firstChild).Id))
                            {
                                Declaration dec = ((Declaration) firstChild);
                                if (a.kids.Count == 2)
                                {
                                    SYMBOL assignmentChild = (SYMBOL) a.kids[1];
                                tryAgain:
                                    if (assignmentChild is TypecastExpression)
                                    {
                                        TypecastExpression typecast = (TypecastExpression)assignmentChild;
                                        assignmentChild = (SYMBOL)typecast.kids[0];
                                        goto tryAgain;
                                    }
                                    else if (assignmentChild is FunctionCall || assignmentChild is FunctionCallExpression)
                                    {
                                        retStrChanged = true;
                                        retVal.Append(dec.Id);
                                        a.kids.Pop();
                                    }
                                    else if (assignmentChild is IdentExpression)
                                    {
// 20131224 not used                                        IdentExpression identEx = (IdentExpression) assignmentChild;
                                    }
                                    else if (assignmentChild is ListConstant)
                                    {
                                        ListConstant listConst = (ListConstant) assignmentChild;
                                        foreach (SYMBOL listChild in listConst.kids)
                                        {
                                            if (listChild is ArgumentList)
                                            {
                                                ArgumentList argList = (ArgumentList) listChild;
                                                int i = 0;
                                                //bool changed = false;
                                                object[] pObj = new object[argList.kids.Count];
                                                foreach (SYMBOL objChild in argList.kids)
                                                {
                                                    pObj[i] = objChild;
                                                    if (objChild is IdentExpression)
                                                    {
// 20131224 not used                                                        IdentExpression identEx = (IdentExpression) objChild;
                                                    }
                                                    i++;
                                                }
                                                // TODO: 20160607 -greythane- This is never executed, check implmentation
                                                /*if (changed)
                                                {
                                                    argList.kids = new ObjectList();
                                                    foreach (object o in pObj)
                                                        argList.kids.Add(o);
                                                }
                                                */
                                            }
                                        }
                                    }
                                    else if (assignmentChild is Constant)
                                    {
                                        Constant identEx = (Constant) assignmentChild;
                                        string value = GetValue(identEx);
                                        Constant dupConstant =
                                            (Constant) DuplicatedLocalVariables[GetLocalDeclarationKey()][dec.Id];
                                        if(dupConstant != null)
                                            dupConstant.Value = (dupConstant == null || dupConstant.Value == null)
                                                                    ? GetValue(dupConstant)
                                                                    : dupConstant.Value;
                                        if (dupConstant == null || value != dupConstant.Value)
                                        {
                                            retStrChanged = true;
                                            retVal.Append(dec.Id);
                                            a.kids.Pop();
                                        }
                                    }
                                }
                            }
                            if (!retStrChanged)
                                retVal.Append(GenerateNode((SYMBOL) a.kids.Pop()));
                            retVal.Append(Generate(string.Format(" {0} ", a.AssignmentType), a));
                            foreach (SYMBOL akid in a.kids)
                            {
                                if (akid is BinaryExpression)
                                {
                                    BinaryExpression be = akid as BinaryExpression;
                                    if (be.ExpressionSymbol.Equals("&&") || be.ExpressionSymbol.Equals("||"))
                                    {
                                        // special case handling for logical and/or, see Mantis 3174
                                        retVal.Append("((bool)(");
                                        retVal.Append(GenerateNode((SYMBOL) be.kids.Pop()));
                                        retVal.Append("))");
                                        retVal.Append(Generate(string.Format(" {0} ", be.ExpressionSymbol.Substring(0, 1)), be));
                                        retVal.Append("((bool)(");
                                        foreach (SYMBOL bkid in be.kids)
                                            retVal.Append(GenerateNode(bkid));
                                        retVal.Append("))");
                                    }
                                    else
                                    {
                                        retVal.Append(GenerateNode((SYMBOL) be.kids.Pop()));
                                        retVal.Append(Generate(string.Format(" {0} ", be.ExpressionSymbol), be));
                                        foreach (SYMBOL kidb in be.kids)
                                        {
                                            //                                            if (kidb is FunctionCallExpression)
                                            {
                                                retVal.Append(GenerateNode(kidb));
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    retVal.Append(GenerateNode(akid));
                                }
                            }
                        }
                        else
                        {
                            if (kid is FunctionCallExpression)
                            {
                                foreach (SYMBOL akid in kid.kids)
                                {
                                    if (akid is FunctionCall)
                                        retVal.Append(GenerateFunctionCall(akid as FunctionCall, false));
                                    else
                                        retVal.Append(GenerateNode(akid));
                                }
                            }
                            else
                            {
                                // this kids will not need to dump in current string position
                                // so save what we have in dump and let kids have their own then take it again
                                retVal.Append(DumpFunc(marc));
                                retVal.Append(GenerateNode(kid));
                                marc = FuncCallsMarc();
                            }
                        }
                    }
                }
            }

            //Nasty hack to fix if statements with yield return and yield break;
            if (retVal[retVal.Length - 1] == '}')
                printSemicolon = false;

            if (printSemicolon)
                retVal.Append(GenerateLine(";"));

            return DumpFunc(marc) + retVal + DumpAfterFunc(marc);
        }
Пример #3
0
 private void AddStatement(Statement s)
 {
     if (0 < s.kids.Count && s.kids.Top is CompoundStatement) kids.Add(s.kids.Pop());
     else kids.Add(s);
 }
Пример #4
0
 public DoWhileStatement(Parser yyp, SYMBOL s, Statement st) : base((yyp))
 {
     if (0 < st.kids.Count && st.kids.Top is CompoundStatement) kids.Add(st.kids.Pop());
     else kids.Add(st);
     kids.Add(s);
 }
Пример #5
0
 public IfStatement(Parser yyp, SYMBOL s, Statement ifs, Statement es) : base((yyp))
 {
     kids.Add(s);
     AddStatement(ifs);
     if (0 < es.kids.Count && es.kids.Top is IfStatement) kids.Add(es.kids.Pop());
     else AddStatement(es);
 }
Пример #6
0
 public IfStatement(Parser yyp, SYMBOL s, Statement ifs) : base((yyp))
 {
     kids.Add(s);
     AddStatement(ifs);
 }
Пример #7
0
 private void AddStatement(Statement s)
 {
     if (s.kids.Top is IfStatement || s.kids.Top is WhileStatement || s.kids.Top is DoWhileStatement ||
         s.kids.Top is ForLoop) kids.Add(s.kids.Pop());
     else kids.Add(s);
 }
Пример #8
0
 public StatementList(Parser yyp, StatementList sl, Statement s) : base((yyp))
 {
     while (0 < sl.kids.Count) kids.Add(sl.kids.Pop());
     AddStatement(s);
 }
Пример #9
0
 public StatementList(Parser yyp, Statement s) : base((yyp))
 {
     AddStatement(s);
 }