Ejemplo n.º 1
0
 public override void Visit(GlobalDclNode node)
 {
     Console.Write("global " + node.Type + " "); // global int
     node.Id.Accept(this);
     Console.Write(" = ");
     node.InitialValue.Accept(this);
     Console.Write(";");
     PrettyPrintNewLine();
 }
        public override void Visit(GlobalDclNode node)
        {
            CSharpString.Append("static ");
            //if (node.Type.Contains("[]"))
            if (node.IsArray)
            {
                CSharpString.Append("List<");
                CSharpString.Append(node.Type.Replace("[]", "")); // global int example
                CSharpString.Append("> ");
            }
            else
            {
                CSharpString.Append(node.Type);
                if (!IsPrimitiveType(node.Type))
                {
                    CSharpString.Append("_");
                }
                CSharpString.Append(" ");
            }



            node.Id.Accept(this);
            CSharpString.Append(" = ");
            if (node.InitialValue != null)
            {
                if (!IsPrimitiveType(node.Type))
                {
                    if (node.InitialValue is FuncCallExpressionNode StructConstructerCall)
                    {
                        if (SymTbl.GlobalScope.Symbols[StructConstructerCall.Id.Id] is StructDclNode)
                        {
                            CSharpString.Append("new ");
                        }
                    }
                    node.InitialValue.Accept(this);
                }
                else
                {
                    if (node.Type == "string")
                    {
                        CSharpString.Append("(");
                        node.InitialValue.Accept(this);
                        CSharpString.Append(").ToString()");
                    }
                    else if (node.Type == "int" && node.InitialValue.Type == "float")
                    {
                        CSharpString.Append("(int)(");
                        node.InitialValue.Accept(this);
                        CSharpString.Append(")");
                    }
                    else
                    {
                        node.InitialValue.Accept(this);
                    }
                }
            }
            else
            {
                //Default values if a variable isn't initialized
                //if (node.Type.Contains("[]"))
                if (node.IsArray)
                {
                    string type = node.Type.Replace("[]", "");
                    if (IsPrimitiveType(type))
                    {
                        CSharpString.Append("new List<" + type + ">()");
                    }
                    else
                    {
                        CSharpString.Append("new List<" + type + "_>()");
                    }
                }
                else
                {
                    switch (node.Type)
                    {
                    case "int":
                        CSharpString.Append("0");
                        break;

                    case "float":
                        CSharpString.Append("0.0f");
                        break;

                    case "string":
                        CSharpString.Append("\"\"");
                        break;

                    case "bool":
                        CSharpString.Append("false");
                        break;

                    default:
                        CSharpString.Append("new " + node.Type + "_()");
                        break;
                    }
                }
            }
            CSharpString.Append(";");
            PrettyPrintNewLine();
        }
Ejemplo n.º 3
0
 internal abstract void Visit(GlobalDclNode node);
Ejemplo n.º 4
0
 public abstract void Visit(GlobalDclNode node);