Example #1
0
    def typeof(self):
        if (self.__typeof == None):
            # resolve the field name first
            btype = self.base.typeof()
            if btype.isok():
                if btype.kind not in ['user', 'class_literal']:
                    signal_type_error(
                        "User-defined class/instance type expected, found {0}".
                        format(str(btype)), self.lines)
                    self.__typeof = Type('error')
                else:
                    if btype.kind == 'user':
                        # user-defined instance type:
                        acc = 'instance'
                    else:
                        # user-defined class type
                        acc = 'static'

                    baseclass = btype.baseclass
                    j = resolve_field(acc, baseclass, self.fname,
                                      Config.current_class)
                    if (j == None):
                        signal_type_error(
                            "No accessible field with name {0} in class {1}".
                            format(self.fname, baseclass.name), self.lines)
                        self.__typeof = Type('error')
                    else:
                        self.field = j
                        self.__typeof = j.type

        return self.__typeof
    def typeof(self):
        if (self.__typeof == None):
            # resolve the method name first
            btype = self.base.typeof()
            if btype.isok():
                if btype.kind not in ['user', 'class_literal']:
                    signal_type_error(
                        "User-defined class/instance type expected, found {0}".
                        format(str(btype)), self.lines)
                    self.__typeof = Type('error')
                else:
                    if btype.kind == 'user':
                        # user-defined instance type:
                        acc = 'instance'
                    else:
                        # user-defined class type
                        acc = 'static'

                    baseclass = btype.baseclass
                    argtypes = [a.typeof() for a in self.args]
                    if (all([a.isok() for a in argtypes])):
                        j = resolve_method(acc, baseclass, self.mname,
                                           argtypes, Config.current_class,
                                           self.lines)

                        if (j == None):
                            self.__typeof = Type('error')
                        else:
                            self.method = j
                            self.__typeof = j.rtype
                    else:
                        self.__typeof = Type('error')
        return self.__typeof
Example #3
0
 def typeof(self):
     if (self.__typeof == None):
         if (Config.current_class.superclass != None):
             self.current_class = Config.current_class
             self.__typeof = Type(self.current_class.superclass)
         else:
             self.__typeof = Type('error')
             signal_type_error(
                 "Type error in Super expression: class {0} has no superclass"
                 .format(str(self.current_class)), self.lines)
     return self.__typeof
 def type_spec(self):
     # TYPESPEC : INTEGER | REAL
     if self.current_token.type == Token_Type.INTEGER:
         node = Type(self.current_token)
         self.eat(Token_Type.INTEGER)
     elif self.current_token.type == Token_Type.REAL:
         node = Type(self.current_token)
         self.eat(Token_Type.REAL)
     else:
         raise Exception('Error : unknown type {}'.format(self.current_token.value))
     return node
Example #5
0
    def typeof(self):
        if (self.__typeof == None):
            arg1type = self.arg1.typeof()
            arg2type = self.arg2.typeof()
            self.__typeof = Type('error')
            if (self.bop in ['add', 'sub', 'mul', 'div']):
                if (arg1type.isint()) and (arg2type.isint()):
                    self.__typeof = arg1type
                elif (arg1type.isnumeric()) and (arg2type.isnumeric()):
                    self.__typeof = Type('float')
                else:
                    if (arg1type.isok() and arg2type.isok()):
                        signal_bop_error('first', self.bop, arg1type,
                                         self.arg1, ['int', 'float'],
                                         'int/float')
                        signal_bop_error('second', self.bop, arg2type,
                                         self.arg2, ['int', 'float'],
                                         'int/float')

            elif (self.bop in ['lt', 'leq', 'gt', 'geq']):
                if ((arg1type.isnumeric()) and (arg2type.isnumeric())):
                    self.__typeof = Type('boolean')
                else:
                    if (arg1type.isok() and arg2type.isok()):
                        signal_bop_error('first', self.bop, arg1type,
                                         self.arg1, ['int', 'float'],
                                         'int/float')
                        signal_bop_error('second', self.bop, arg2type,
                                         self.arg2, ['int', 'float'],
                                         'int/float')

            elif (self.bop in ['and', 'or']):
                if ((arg1type.isboolean()) and (arg2type.isboolean())):
                    self.__typeof = Type('boolean')
                else:
                    if (arg1type.isok() and arg2type.isok()):
                        signal_bop_error('first', self.bop, arg1type,
                                         self.arg1, ['boolean'], 'boolean')
                        signal_bop_error('second', self.bop, arg2type,
                                         self.arg2, ['boolean'], 'boolean')
            else:
                # equality/disequality
                if ((arg1type.isok()) and (arg2type.isok())):
                    if ((arg1type.is_subtype_of(arg2type))
                            or (arg2type.is_subtype_of(arg1type))):
                        self.__typeof = Type('boolean')
                    else:
                        signal_type_error(
                            'Type error in arguments of binary {0} expression: compatible types expected, found {1} and {2}'
                            .format(self.bop, str(arg1type),
                                    str(arg2type)), self.lines)

        return self.__typeof
Example #6
0
 def typeof(self):
     if (self.__typeof == None):
         lhstype = self.lhs.typeof()
         rhstype = self.rhs.typeof()
         if (lhstype.isok() and rhstype.isok()):
             if (rhstype.is_subtype_of(lhstype)):
                 self.__typeof = rhstype
             else:
                 self.__typeof = Type('error')
                 signal_type_error(
                     'Type error in assign expression: compatible types expected, found {0} and {1}'
                     .format(str(lhstype), str(rhstype)), self.lines)
         else:
             self.__typeof = Type('error')
     return self.__typeof
Example #7
0
 def typeof(self):
     if (self.__typeof == None):
         if (not self.index.typeof().isint()):
             signal_type_error(
                 "Type error in index of Array Index expression: integer expected, found {0}"
                 .format(str(self.index.typeof())), self.index.lines)
             mytype = Type('error')
         if (self.base.typeof().kind != 'array'):
             signal_type_error(
                 "Type error in base of Array Index expression: array type expected, found {0}"
                 .format(str(self.base.typeof())), self.base.lines)
             mytype = Type('error')
         else:
             mytype = self.base.typeof().basetype
         self.__typeof = mytype
     return self.__typeof
Example #8
0
 def typeof(self):
     if (self.__typeof == None):
         # resolve the constructor name first
         argtypes = [a.typeof() for a in self.args]
         if (all([a.isok() for a in argtypes])):
             j = resolve_constructor(Config.current_class, self.classref,
                                     argtypes, self.lines)
             if (j == None):
                 self.__typeof = Type('error')
             else:
                 self.constructor = j
                 self.__typeof = Type(self.classref)
         else:
             # type error in some argument; already signaled before
             self.__typeof = Type('error')
     return self.__typeof
Example #9
0
def p_primitive_type_specifier(p):
    """
    primitive_type : INT
                   | CHAR
                   | FLOAT
                   | DOUBLE
    """
    p[0] = Type(p[1])
Example #10
0
 def typeof(self):
     if (self.__typeof == None):
         mytype = Type(self.basetype, len(self.args))
         for a in self.args:
             if (not a.typeof().isok()):
                 # previous error, so mark and pass
                 mytype = Type('error')
                 break
             if (not a.typeof().isint()):
                 # int arg type expected
                 signal_type_error(
                     "Type error in argument to New Array expression: int expected, found {0}"
                     .format(str(a.typeof())), a.lines)
                 mytype = Type('error')
                 break
         self.__typeof = mytype
     return self.__typeof
Example #11
0
def p_declaration_aggregate_field(p):
    """
    declaration : opt_field_annotations opt_qualifiers user_defined \
                                        opt_attribute IDENT \
                                        opt_qualifiers variables \
                                        semicolons
    """
    p[0] = create_fields(p[2] + Type("%s %s" % (p[3], p[5])) + p[6], p[1],
                         p[7])
Example #12
0
def p_declaration_named_type_field(p):
    """
    declaration : opt_field_annotations opt_qualifiers primitive_type \
                                        opt_qualifiers variables \
                                        semicolons
                | opt_field_annotations opt_qualifiers IDENT opt_qualifiers \
                                        variables semicolons
    """
    p[0] = create_fields(p[2] + Type(p[3]) + p[4], p[1], p[5])
Example #13
0
def p_type_qualifier(p):
    """
    type_qualifier : CONST
                   | VOLATILE
                   | SHORT
                   | LONG
                   | UNSIGNED
                   | SIGNED
    """
    p[0] = Type(p[1])
Example #14
0
 def typeof(self):
     if (self.__typeof == None):
         argtype = self.arg.typeof()
         if (argtype.isnumeric()):
             self.__typeof = argtype
         else:
             self.__typeof = Type('error')
             if (argtype.isok()):
                 signal_type_error('Type error in auto expression: int/float expected, found {0}'.format(str(argtype)), self.lines)
     return self.__typeof
Example #15
0
 def typecheck(self):
     global current_method
     if (self.__typecorrect == None):
         if (self.expr == None):
             argtype = Type('void')
         else:
             argtype = self.expr.typeof()
         self.__typecorrect = argtype.is_subtype_of(
             Config.current_method.rtype)
         if (argtype.isok() and (not self.__typecorrect)):
             signal_type_error(
                 "Type error in Return statement: {0} expected, found {1}".
                 format(str(Config.current_method.rtype),
                        str(argtype)), self.lines)
     return self.__typecorrect
Example #16
0
 def typeof(self):
     if (self.__typeof == None):
         if (self.kind == 'int'):
             self.__typeof = Type('int')
         elif (self.kind == 'float'):
             self.__typeof = Type('float')
         elif (self.kind == 'string'):
             self.__typeof = Type('string')
         elif (self.kind == 'Null'):
             self.__typeof = Type('null')
         elif (self.kind == 'True'):
             self.__typeof = Type('boolean')
         elif (self.kind == 'False'):
             self.__typeof = Type('boolean')
     return self.__typeof
Example #17
0
 def typeof(self):
     if (self.__typeof == None):
         argtype = self.arg.typeof()
         self.__typeof = Type('error')
         if (self.uop == 'uminus'):
             if (argtype.isnumeric()):
                 self.__typeof = argtype
             elif (argtype.kind != 'error'):
                 # not already in error
                 signal_type_error(
                     "Type error in unary minus expression: int/float expected, found {0}"
                     .format(str(argtype)), self.arg.lines)
         elif (self.uop == 'neg'):
             if (argtype.isboolean()):
                 self.__typeof = argtype
             elif (argtype.kind != 'error'):
                 # not already in error
                 signal_type_error(
                     "Type error in unary negation expression: boolean expected, found {0}"
                     .format(str(argtype)), self.arg.lines)
     return self.__typeof
Example #18
0
 def handleType(p):
     return Type(p[0].value)
Example #19
0
 def typeof(self):
     if (self.__typeof == None):
         self.__typeof = Type(self.classref, literal=True)
     return self.__typeof
Example #20
0
    def typeof(self):
        if (self.__typeof == None):
            self.current_class = Config.current_class
            self.__typeof = Type(self.current_class)

        return self.__typeof