Esempio n. 1
0
        public static string TypeToIdentifierString(PType type)
        {
            if (type is AVoidType)
            {
                return("void");
            }

            /*if (type is AArrayType)
             * {
             *  AArrayType aType = (AArrayType)type;
             *  return TypeToString(aType.GetType()) + "[" + aType.GetDimention().Text + "]";
             * }*/
            if (type is AArrayTempType)
            {
                AArrayTempType aType = (AArrayTempType)type;
                return(TypeToIdentifierString(aType.GetType()) + "Ar" + (aType.GetIntDim() == null ? "" : aType.GetIntDim().Text));
            }
            if (type is ANamedType)
            {
                ANamedType aType = (ANamedType)type;
                return(((AAName)aType.GetName()).AsString().Replace('.', '_'));
            }
            if (type is APointerType)
            {
                APointerType aType = (APointerType)type;
                return("p" + TypeToIdentifierString(aType.GetType()));
            }
            if (type is ADynamicArrayType)
            {
                ADynamicArrayType aType = (ADynamicArrayType)type;
                return(TypeToIdentifierString(aType.GetType()) + "DAr");
            }
            if (type is AGenericType)
            {
                AGenericType aType = (AGenericType)type;
                string       ret   = TypeToIdentifierString(aType.GetBase()) + "G_";
                bool         first = true;
                foreach (PType t in aType.GetGenericTypes())
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        ret += "_";
                    }
                    ret += TypeToIdentifierString(t);
                }
                return(ret + "_G");
            }
            throw new Exception("Unknown type");
        }
Esempio n. 2
0
        private void MakeAssignments(AABlock block, PType type, PLvalue leftSide, bool onEnhritedFields)
        {
            if (type is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)type))
            {
                AStructDecl str = data.StructTypeLinks[(ANamedType)type];
                foreach (AALocalDecl field in str.GetLocals().OfType <AALocalDecl>())
                {
                    if (!onEnhritedFields && data.EnheritanceLocalMap.ContainsKey(field))
                    {
                        continue;
                    }

                    ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data));
                    data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide];
                    AStructLvalue newLeftSide = new AStructLvalue(lvalueExp, new ADotDotType(new TDot(".")), new TIdentifier(field.GetName().Text));
                    data.StructFieldLinks[newLeftSide] = field;
                    data.LvalueTypes[newLeftSide]      = field.GetType();

                    if (field.GetInit() != null)
                    {
                        AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), newLeftSide,
                                                                       Util.MakeClone(field.GetInit(), data));
                        data.ExpTypes[assignment] = data.LvalueTypes[newLeftSide];

                        block.GetStatements().Add(new AExpStm(new TSemicolon(";"),
                                                              assignment));
                    }
                    else
                    {
                        MakeAssignments(block, field.GetType(), newLeftSide, onEnhritedFields);
                    }
                }
            }
            else if (type is AArrayTempType)
            {
                AArrayTempType aType = (AArrayTempType)type;
                for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++)
                {
                    AIntConstExp index = new AIntConstExp(new TIntegerLiteral(i.ToString()));
                    data.ExpTypes[index] = new ANamedType(new TIdentifier("int"), null);

                    ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data));
                    data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide];
                    AArrayLvalue newLeftSide = new AArrayLvalue(new TLBracket("["), lvalueExp, index);
                    data.LvalueTypes[newLeftSide] = aType.GetType();

                    MakeAssignments(block, aType.GetType(), newLeftSide, onEnhritedFields);
                }
            }
        }
        public override void InAArrayTempType(AArrayTempType node)
        {
            if (node.GetIntDim() == null && !Util.HasAncestor <ANewExp>(node))
            {
                node = node;

                /*bool valid = true;
                 * int val = FoldInt(node.GetDimention(), ref valid);
                 * if (!valid)
                 * {
                 *  errors.Add(new ErrorCollection.Error(node.GetToken(), "The dimension must be a constant expression."));
                 *  throw new ParserException(null, null);
                 * }
                 * node.SetIntDim(new TIntegerLiteral(val.ToString()));*/
            }
            base.InAArrayTempType(node);
        }
Esempio n. 4
0
        private List <PStm> AssignDefault(PLvalue lvalue)
        {
            List <PStm> returner  = new List <PStm>();
            PType       type      = data.LvalueTypes[lvalue];
            PExp        rightSide = null;

            if (type is ANamedType)
            {
                ANamedType aType = (ANamedType)type;
                if (aType.IsPrimitive("string"))//aType.GetName().Text == "string")
                {
                    rightSide = new AStringConstExp(new TStringLiteral("\"\""));
                    data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("string"), null);
                }
                else if (aType.IsPrimitive(GalaxyKeywords.NullablePrimitives.words)) //GalaxyKeywords.NullablePrimitives.words.Contains(aType.GetName().Text))
                {
                    rightSide = new ANullExp();
                    data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("null"), null);
                }
                else if (aType.IsPrimitive(new [] { "int", "byte", "fixed" }))

                /*aType.GetName().Text == "int" ||
                *  aType.GetName().Text == "byte" ||
                *  aType.GetName().Text == "fixed")*/
                {
                    rightSide = new AIntConstExp(new TIntegerLiteral("0"));
                    data.ExpTypes[rightSide] = type;
                }
                else if (aType.IsPrimitive("bool"))//aType.GetName().Text == "bool")
                {
                    rightSide = new ABooleanConstExp(new AFalseBool());
                    data.ExpTypes[rightSide] = type;
                }
                else if (aType.IsPrimitive("color"))//aType.GetName().Text == "color")
                {
                    PExp             arg1   = new AIntConstExp(new TIntegerLiteral("0"));
                    PExp             arg2   = new AIntConstExp(new TIntegerLiteral("0"));
                    PExp             arg3   = new AIntConstExp(new TIntegerLiteral("0"));
                    ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Color"), new ArrayList()
                    {
                        arg1, arg2, arg3
                    });
                    rightSide = invoke;
                    data.ExpTypes[rightSide]       = type;
                    data.ExpTypes[arg1]            =
                        data.ExpTypes[arg2]        =
                            data.ExpTypes[arg3]    = new ANamedType(new TIdentifier("int"), null);
                    data.SimpleMethodLinks[invoke] =
                        data.Libraries.Methods.First(func => func.GetName().Text == invoke.GetName().Text);
                }
                else if (aType.IsPrimitive("char"))//aType.GetName().Text == "char")
                {
                    //Dunno?!
                    rightSide = new ACharConstExp(new TCharLiteral("'\0'"));
                    data.ExpTypes[rightSide] = type;
                }
                else //Struct
                {
                    AStructDecl str = data.StructTypeLinks[aType];
                    foreach (AALocalDecl localDecl in str.GetLocals())
                    {
                        ALvalueExp    reciever  = new ALvalueExp(Util.MakeClone(lvalue, data));
                        AStructLvalue newLvalue = new AStructLvalue(reciever, new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text));
                        data.StructFieldLinks[newLvalue] = localDecl;
                        data.ExpTypes[reciever]          = type;
                        data.LvalueTypes[newLvalue]      = localDecl.GetType();
                        returner.AddRange(AssignDefault(newLvalue));
                    }
                    return(returner);
                }
                AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(lvalue, data), rightSide);
                data.ExpTypes[assignment] = type;
                return(new List <PStm>()
                {
                    new AExpStm(new TSemicolon(";"), assignment)
                });
            }
            if (type is AArrayTempType)
            {
                AArrayTempType aType = (AArrayTempType)type;
                for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++)
                {
                    ALvalueExp   reciever  = new ALvalueExp(Util.MakeClone(lvalue, data));
                    AArrayLvalue newLvalue = new AArrayLvalue(new TLBracket("["), reciever, new AIntConstExp(new TIntegerLiteral(i.ToString())));
                    data.ExpTypes[reciever]             = type;
                    data.LvalueTypes[newLvalue]         = aType.GetType();
                    data.ExpTypes[newLvalue.GetIndex()] = new ANamedType(new TIdentifier("int"), null);
                    returner.AddRange(AssignDefault(newLvalue));
                }
                return(returner);
            }

            throw new Exception("Unexpected type. (LivenessAnalasys.AssignDefault), got " + type);
        }
Esempio n. 5
0
 public override void CaseAArrayTempType(AArrayTempType node)
 {
     node.GetType().Apply(this);
     Write("[" + node.GetIntDim().Text + "]");
 }
Esempio n. 6
0
        public static StringBuilder TypeToStringBuilder(PType type)
        {
            if (type is AVoidType)
            {
                return(new StringBuilder("void"));
            }

            /*if (type is AArrayType)
             * {
             *  AArrayType aType = (AArrayType)type;
             *  return TypeToString(aType.GetType()) + "[" + aType.GetDimention().Text + "]";
             * }*/
            if (type is AArrayTempType)
            {
                AArrayTempType aType = (AArrayTempType)type;

                StringBuilder builder = new StringBuilder();
                builder.Append(TypeToStringBuilder(aType.GetType()));
                builder.Append("[");
                if (aType.GetIntDim() != null)
                {
                    builder.Append(aType.GetIntDim().Text);
                }
                builder.Append("]");
                return(builder);
            }
            if (type is ANamedType)
            {
                ANamedType    aType   = (ANamedType)type;
                StringBuilder builder = new StringBuilder();
                foreach (TIdentifier identifier in ((AAName)aType.GetName()).GetIdentifier())
                {
                    if (builder.Length != 0)
                    {
                        builder.Append(".");
                    }
                    builder.Append(identifier.Text);
                }
                return(builder);
            }
            if (type is APointerType)
            {
                APointerType  aType   = (APointerType)type;
                StringBuilder builder = new StringBuilder();
                builder.Append(TypeToStringBuilder(aType.GetType()));
                builder.Append("*");
                return(builder);
            }
            if (type is ADynamicArrayType)
            {
                ADynamicArrayType aType   = (ADynamicArrayType)type;
                StringBuilder     builder = new StringBuilder();
                builder.Append(TypeToStringBuilder(aType.GetType()));
                builder.Append("[]");
                return(builder);
            }
            if (type is AGenericType)
            {
                AGenericType  aType   = (AGenericType)type;
                StringBuilder builder = new StringBuilder();
                builder.Append(TypeToStringBuilder(aType.GetBase()));
                builder.Append("<");
                bool first = true;
                foreach (PType t in aType.GetGenericTypes())
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        builder.Append(", ");
                    }
                    builder.Append(TypeToStringBuilder(t));
                }
                builder.Append(">");
                return(builder);
            }
            throw new Exception("Unknown type. Got " + type);
        }