Esempio n. 1
0
 public override void CaseACharConstExp(ACharConstExp node)
 {
     Value += node.GetCharLiteral().Text;
 }
Esempio n. 2
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. 3
0
 public override void CaseACharConstExp(ACharConstExp node)
 {
     Write(node.GetCharLiteral().Text);
 }
Esempio n. 4
0
        public override void DefaultIn(Node node)
        {
            if (!canMerge)
            {
                return;
            }
            if (node is AMethodDecl)
            {
                //First node - no need to fetch
                if (((AMethodDecl)node).GetFormals().Count != ((AMethodDecl)otherNode).GetFormals().Count)
                {
                    canMerge = false;
                }
                return;
            }
            //Fetch corrosponding other node
            int index = 0;
            GetChildTypeIndex getChildTypeIndex = new GetChildTypeIndex()
            {
                Parent = node.Parent(), Child = node
            };

            node.Parent().Apply(getChildTypeIndex);
            index = getChildTypeIndex.Index;
            GetChildTypeByIndex getChildTypeByIndex = new GetChildTypeByIndex()
            {
                Child = node, Index = index, Parent = otherNode
            };

            otherNode.Apply(getChildTypeByIndex);
            otherNode = getChildTypeByIndex.Child;

            if (otherNode.GetType() != node.GetType())
            {
                canMerge = false;
                return;
            }

            if (node is AALocalDecl)
            {
                locals.Add((AALocalDecl)node);
                otherLocals.Add((AALocalDecl)otherNode);
                return;
            }

            if (node is ANamedType)
            {
                ANamedType aNode  = (ANamedType)node;
                ANamedType aOther = (ANamedType)otherNode;
                if (data.StructTypeLinks.ContainsKey(aNode) != data.StructTypeLinks.ContainsKey(aOther))
                {
                    canMerge = false;
                    return;
                }
                if (data.StructTypeLinks.ContainsKey(aNode) && data.StructTypeLinks[aNode] != data.StructTypeLinks[aOther])
                {
                    canMerge = false;
                }
                if (!data.StructTypeLinks.ContainsKey(aNode) && aNode.IsSame(aOther, true))//aNode.GetName().Text != aOther.GetName().Text)
                {
                    canMerge = false;
                }
                if (aNode.IsPrimitive() && !aOther.IsPrimitive(aNode.AsIdentifierString()))
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AABlock)
            {
                AABlock aNode  = (AABlock)node;
                AABlock aOther = (AABlock)otherNode;
                if (aNode.GetStatements().Count != aOther.GetStatements().Count)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AIntConstExp)
            {
                AIntConstExp aNode  = (AIntConstExp)node;
                AIntConstExp aOther = (AIntConstExp)otherNode;
                if (aNode.GetIntegerLiteral().Text != aOther.GetIntegerLiteral().Text)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AFixedConstExp)
            {
                AFixedConstExp aNode  = (AFixedConstExp)node;
                AFixedConstExp aOther = (AFixedConstExp)otherNode;
                if (aNode.GetFixedLiteral().Text != aOther.GetFixedLiteral().Text)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AStringConstExp)
            {
                AStringConstExp aNode  = (AStringConstExp)node;
                AStringConstExp aOther = (AStringConstExp)otherNode;
                if (aNode.GetStringLiteral().Text != aOther.GetStringLiteral().Text)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is ACharConstExp)
            {
                ACharConstExp aNode  = (ACharConstExp)node;
                ACharConstExp aOther = (ACharConstExp)otherNode;
                if (aNode.GetCharLiteral().Text != aOther.GetCharLiteral().Text)
                {
                    canMerge = false;
                }
                return;
            }

            if (node is ASimpleInvokeExp)
            {
                ASimpleInvokeExp aNode  = (ASimpleInvokeExp)node;
                ASimpleInvokeExp aOther = (ASimpleInvokeExp)otherNode;
                if (data.SimpleMethodLinks[aNode] != data.SimpleMethodLinks[aOther] &&
                    !(data.SimpleMethodLinks[aNode] == Util.GetAncestor <AMethodDecl>(aNode) &&
                      data.SimpleMethodLinks[aOther] == Util.GetAncestor <AMethodDecl>(aOther)))
                {
                    canMerge = false;
                }
                return;
            }

            if (node is ALocalLvalue)
            {
                ALocalLvalue aNode  = (ALocalLvalue)node;
                ALocalLvalue aOther = (ALocalLvalue)otherNode;
                if (locals.IndexOf(data.LocalLinks[aNode]) != otherLocals.IndexOf(data.LocalLinks[aOther]))
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AFieldLvalue)
            {
                AFieldLvalue aNode  = (AFieldLvalue)node;
                AFieldLvalue aOther = (AFieldLvalue)otherNode;
                if (data.FieldLinks[aNode] != data.FieldLinks[aOther])
                {
                    canMerge = false;
                }
                return;
            }

            if (node is AStructLvalue)
            {
                AStructLvalue aNode  = (AStructLvalue)node;
                AStructLvalue aOther = (AStructLvalue)otherNode;
                if (data.StructFieldLinks[aNode] != data.StructFieldLinks[aOther])
                {
                    canMerge = false;
                }
                return;
            }
        }
Esempio n. 5
0
 public static bool ReturnsTheSame(PExp left, PExp right, SharedData data)
 {
     if (left.GetType() != right.GetType())
     {
         return(false);
     }
     if (left is ABinopExp)
     {
         ABinopExp aLeft  = (ABinopExp)left;
         ABinopExp aRight = (ABinopExp)right;
         if (aLeft.GetBinop().GetType() != aRight.GetBinop().GetType())
         {
             return(false);
         }
         return(ReturnsTheSame(aLeft.GetLeft(), aRight.GetLeft(), data) &&
                ReturnsTheSame(aLeft.GetRight(), aRight.GetRight(), data));
     }
     if (left is AUnopExp)
     {
         AUnopExp aLeft  = (AUnopExp)left;
         AUnopExp aRight = (AUnopExp)right;
         if (aLeft.GetUnop().GetType() != aRight.GetUnop().GetType())
         {
             return(false);
         }
         return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data));
     }
     if (left is AIntConstExp)
     {
         AIntConstExp aLeft  = (AIntConstExp)left;
         AIntConstExp aRight = (AIntConstExp)right;
         return(int.Parse(aLeft.GetIntegerLiteral().Text) == int.Parse(aRight.GetIntegerLiteral().Text));
     }
     if (left is AFixedConstExp)
     {
         AFixedConstExp aLeft  = (AFixedConstExp)left;
         AFixedConstExp aRight = (AFixedConstExp)right;
         return(aLeft.GetFixedLiteral().Text == aRight.GetFixedLiteral().Text);
     }
     if (left is AStringConstExp)
     {
         AStringConstExp aLeft  = (AStringConstExp)left;
         AStringConstExp aRight = (AStringConstExp)right;
         return(aLeft.GetStringLiteral().Text == aRight.GetStringLiteral().Text);
     }
     if (left is ACharConstExp)
     {
         ACharConstExp aLeft  = (ACharConstExp)left;
         ACharConstExp aRight = (ACharConstExp)right;
         return(aLeft.GetCharLiteral().Text == aRight.GetCharLiteral().Text);
     }
     if (left is ABooleanConstExp)
     {
         ABooleanConstExp aLeft  = (ABooleanConstExp)left;
         ABooleanConstExp aRight = (ABooleanConstExp)right;
         return(aLeft.GetBool().GetType() == aRight.GetBool().GetType());
     }
     if (left is ASimpleInvokeExp)
     {
         //A method might not return the same thing each time it is called
         return(false);
     }
     if (left is ALvalueExp)
     {
         ALvalueExp aLeft  = (ALvalueExp)left;
         ALvalueExp aRight = (ALvalueExp)right;
         return(ReturnsTheSame(aLeft.GetLvalue(), aRight.GetLvalue(), data));
     }
     if (left is AParenExp)
     {
         AParenExp aLeft  = (AParenExp)left;
         AParenExp aRight = (AParenExp)right;
         return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data));
     }
     throw new Exception("Util.ReturnsTheSame. Unexpected type, got " + left.GetType());
 }