示例#1
0
 private static void Unindent()
 {
     if (IndentString.Length >= 2)
     {
         IndentString = IndentString.Remove(IndentString.Length - 2);
     }
 }
示例#2
0
        public string GenerateCode(int indent)
        {
            string        istr = IndentString.GetIndent(indent);
            StringBuilder sb   = new StringBuilder();

            sb.AppendLine(istr + "//temp variables , should be optimized by C++/cli compiler.");
            foreach (string s in tempVars.Keys)
            {
                if (tempVars[s].IsReference)
                {
                    continue;
                }
                string   initVal = ";";
                TypeInfo ti      = tempVars[s];
                if (ti.IsBoolean)
                {
                    initVal = " = false;";
                }
                else if (ti.IsRef)
                {
                    initVal = " = nullptr;";
                }
                else if (ti.IsEnum)
                {
                    initVal = " = (" + tempVars[s].TypeClause + ")0;";
                }
                else if (ti.IsStruct)
                {
                    initVal = ";";
                }
                else if (ti.IsSimple)
                {
                    if (ti.IsCastSimple)
                    {
                        initVal = " = (" + tempVars[s] + ")0;";
                    }
                    else
                    {
                        initVal = " = 0;";
                    }
                }
                sb.AppendLine(istr + tempVars[s].TypeDefClause + " " + s + initVal);
            }
            sb.AppendLine(istr + "//local variables.");
            if (locals != null)
            {
                foreach (Param p in locals)
                {
                    if (p.Type.IsReference)
                    {
                        continue;
                    }
                    string   initVal = ";";
                    TypeInfo ti      = p.Type;
                    if (ti.IsBoolean)
                    {
                        initVal = " = false;";
                    }
                    else if (ti.IsRef)
                    {
                        initVal = " = nullptr;";
                    }
                    else if (ti.IsEnum)
                    {
                        initVal = " = (" + p.Type + ")0;";
                    }
                    else if (ti.IsStruct)
                    {
                        initVal = ";";
                    }
                    else if (ti.IsSimple)
                    {
                        if (ti.IsCastSimple)
                        {
                            initVal = " = (" + p.Type + ")0;";
                        }
                        else
                        {
                            initVal = " = 0;";
                        }
                    }
                    sb.AppendLine(istr + p.CalcClause() + initVal);
                }
            }
            sb.AppendLine(istr + "//method body ------- ");
            foreach (IlIns il in ilLines.Values)
            {
                sb.AppendLine(istr + il.GenerateCode());
            }
            return(sb.ToString());
        }
示例#3
0
        public string GenerateCode()
        {
            StringBuilder sb = new StringBuilder();

            //if(csharps.Count<=0)
            //    return "";
            if (label.Length > 0)
            {
                sb.Append(label);
                if (sb.Length < 20)
                {
                    sb.Append(IndentString.GetSpace(20 - sb.Length));
                }
                foreach (DAGNode node in csharps)
                {
                    List <string> exps = new List <string>();
                    string        s    = node.Generate(exps);
                    if (node.IsComment)
                    {
                        sb.Append("/*");
                    }
                    sb.Append(string.Join("", exps.ToArray()));
                    if (node.IsStatement && node.Identifier.Length <= 0)
                    {
                        sb.Append(s + node.StatementSemicolon);
                    }
                    if (node.IsComment)
                    {
                        sb.Append("*/");
                    }
                }
                if (sb.Length < 80)
                {
                    sb.Append(IndentString.GetSpace(80 - sb.Length));
                }
                sb.Append("//");
                sb.Append(opcode);
                if (operand.Value.Length > 0)
                {
                    sb.Append("\t\t\t\t" + operand.Value);
                }
            }
            else if (csharps.Count > 0)
            {
                if (opcode == ".try")
                {
                    sb.Append("IL_" + LabelValue.ToString("X6") + ":");
                    if (sb.Length < 20)
                    {
                        sb.Append(IndentString.GetSpace(20 - sb.Length));
                    }
                }
                else
                {
                    sb.Append(IndentString.GetSpace(20));
                }
                if (csharps.Count > 0)
                {
                    foreach (DAGNode node in csharps)
                    {
                        List <string> exps = new List <string>();
                        string        s    = node.Generate(exps);
                        sb.Append(string.Join("", exps.ToArray()));
                        if (node.IsStatement && node.Identifier.Length <= 0)
                        {
                            sb.Append(s + node.StatementSemicolon);
                        }
                    }
                }
            }
            else
            {
                sb.Append(IndentString.GetSpace(80) + "//" + opcode);
            }
            return(sb.ToString());
        }