Exemplo n.º 1
0
        private void VisitNode(PrimaryExpression node)
        {
            AbstractNode child = node.Child;

            child.Accept(this);
            node.TypeDescriptor = child.TypeDescriptor;
        }
Exemplo n.º 2
0
        private TypeDescriptor AssignmentExp(AbstractNode qualName, AbstractNode exp)
        {
            QualifiedName     name       = qualName as QualifiedName;
            Expression        expression = exp as Expression;
            PrimaryExpression primaryExp = exp as PrimaryExpression;

            TypeDescriptor nameDesc;

            if (name != null && (expression != null || primaryExp != null))
            {
                Attributes nameAttr = Table.lookup(name.GetStringName());
                qualName.AttributesRef  = nameAttr;
                qualName.TypeDescriptor = nameAttr.TypeDescriptor;

                exp.Accept(this);

                // Check for errors
                // if both types are Error Descriptors, combine errors
                ErrorDescriptor nameErrDesc =
                    nameAttr.TypeDescriptor as ErrorDescriptor;
                ErrorDescriptor expErrDesc =
                    exp.TypeDescriptor as ErrorDescriptor;
                if (nameErrDesc != null && expErrDesc != null)
                {
                    nameDesc = nameErrDesc.CombineErrors(expErrDesc);
                }
                // if one or the other is an error, propagate the error up
                else if (nameErrDesc != null)
                {
                    nameDesc = nameAttr.TypeDescriptor;
                }
                else if (expErrDesc != null)
                {
                    nameDesc = exp.TypeDescriptor;
                }
                // check that the variable being assigned to is assignable
                else if (nameAttr.IsAssignable)
                {
                    // if types compatible, assign successfully assigned type
                    if (TypesCompatible(nameAttr.TypeDescriptor,
                                        exp.TypeDescriptor))
                    {
                        nameDesc = GetAssignmentDesc(nameAttr.TypeDescriptor,
                                                     exp.TypeDescriptor);
                    }
                    // otherwise, assign new error for incompatible types
                    else
                    {
                        nameDesc = new ErrorDescriptor("Incompatible types: " +
                                                       "cannot assign " + GetSimpleName(exp.TypeDescriptor)
                                                       + " to " + GetSimpleName(nameAttr.TypeDescriptor) +
                                                       " variable");
                    }
                }
                // variable is not assignable
                else
                {
                    nameDesc = new ErrorDescriptor(nameAttr +
                                                   " is not assigable. Cannot assign as " +
                                                   GetSimpleName(exp.TypeDescriptor));
                }
            }
            // Assignment not made up of correct parts
            else
            {
                string message = "";
                if (name == null && expression == null)
                {
                    message += "EQUALS expression expects 'QualifiedName' " +
                               "on LHS, but has: " + qualName.GetType().Name +
                               " & 'Expression' or 'PrimaryExression' on " +
                               "RHS, but has " + exp.GetType().Name;
                }
                else if (name == null)
                {
                    message += "EQUALS expression expects 'QualifiedName' on" +
                               " LHS, but has: " + qualName.GetType().Name;
                }
                else
                {
                    message += "EQUALS expression expects 'Expression' or " +
                               "'PrimaryExpression' on RHS, but has: " +
                               exp.GetType().Name;
                }
                nameDesc = new ErrorDescriptor(message);
            }
            return(nameDesc);
        }
Exemplo n.º 3
0
 private void VisitNode(PrimaryExpression node)
 {
     node.TypeDescriptor = PrimaryExp(node);
 }