Esempio n. 1
0
    def check(self):
        e = self.expr.resolve()
        if mjp.isToken(e):
            if mjp.literalToType(e.get_type()) != BOOLEAN_TYPE:
                raise SemanticError(e.get_line(), e.get_col(), "La expresion debe ser de tipo boolean")
        elif isVariable(e):
            if e.type.get_type() != BOOLEAN_TYPE:
                raise SemanticError(e.name.get_line(), e.name.get_col(), "La expresion debe ser de tipo boolean")
        elif isMethod(e):
            if e.is_constructr() or e.ret_type.get_type() != BOOLEAN_TYPE:
                raise SemanticError(
                    self.expr.ref.get_line(), self.expr.ref.get_col(), "La expresion debe ser de tipo boolean"
                )
        else:
            raise Exception()

        rand = "".join(random.choice(string.letters + string.digits) for i in xrange(10))
        label_true = "while_true_%s" % rand
        label_false = "while_false_%s" % rand

        code = "%s: nop\n" % label_true
        code += self.expr.check()
        code += "bf %s\n" % label_false
        code += self.statement.check()
        code += "jump %s\n" % label_true
        code += "%s: nop\n" % label_false

        return code
Esempio n. 2
0
    def check(self):
        e = self.expr.resolve()
        if mjp.isToken(e):
            if mjp.literalToType(e.get_type()) != BOOLEAN_TYPE:
                raise SemanticError(e.get_line(), e.get_col(),
                                    "La expresion debe ser de tipo boolean")
        elif isVariable(e):
            if e.type.get_type() != BOOLEAN_TYPE:
                raise SemanticError(e.name.get_line(), e.name.get_col(),
                                    "La expresion debe ser de tipo boolean")
        elif isMethod(e):
            if e.is_constructr() or e.ret_type.get_type() != BOOLEAN_TYPE:
                raise SemanticError(self.expr.ref.get_line(),
                                    self.expr.ref.get_col(),
                                    "La expresion debe ser de tipo boolean")
        else:
            raise Exception()

        rand = "".join(
            random.choice(string.letters + string.digits) for i in xrange(10))
        label_true = "while_true_%s" % rand
        label_false = "while_false_%s" % rand

        code = "%s: nop\n" % label_true
        code += self.expr.check()
        code += "bf %s\n" % label_false
        code += self.statement.check()
        code += "jump %s\n" % label_true
        code += "%s: nop\n" % label_false

        return code
Esempio n. 3
0
    def check(self):
        if self.method.is_constructor():
            if not self.expr is None:
                raise SemanticError(
                    self.ret.get_line(), self.ret.get_col(), "Los return en constructores no deben retornar valores"
                )
            else:
                return ""

        if self.expr is None:
            if self.method.ret_type.get_type() != VOID_TYPE:
                raise SemanticError(
                    self.ret.get_line(),
                    self.ret.get_col(),
                    "La sentencia return debe contener una expresion de tipo %s" % self.method.ret_type.get_lexeme(),
                )
        else:
            t = self.expr.resolve()
            # raise Exception("Checkear compatibilidad de herencia tambien!")
            if mjp.isToken(t):
                rt = mjp.literalToType(t.get_type())
                if rt != self.method.ret_type.get_type():
                    raise SemanticError(
                        self.ret.get_line(),
                        self.ret.get_col(),
                        "Se esta retornando %s en un metodo de tipo %s"
                        % (mjp.typeToStr(rt), self.method.ret_type.get_lexeme()),
                    )
            else:
                if t.type.get_lexeme() != self.method.ret_type.get_lexeme():
                    raise SemanticError(
                        self.ret.get_line(),
                        self.ret.get_col(),
                        "Se esta retornando %s en un metodo de tipo %s"
                        % (t.type.get_lexeme(), self.method.ret_type.get_lexeme()),
                    )

            offset = 3
            if self.method.isStatic():
                offset = 2  # no hay this
            return self.expr.check() + "store %d ; offset a ret_val\n" % (offset + len(self.method.params) + 1)
        return ""
Esempio n. 4
0
    def check(self):
        if self.method.is_constructor():
            if not self.expr is None:
                raise SemanticError(
                    self.ret.get_line(), self.ret.get_col(),
                    "Los return en constructores no deben retornar valores")
            else:
                return ""

        if self.expr is None:
            if self.method.ret_type.get_type() != VOID_TYPE:
                raise SemanticError(
                    self.ret.get_line(), self.ret.get_col(),
                    "La sentencia return debe contener una expresion de tipo %s"
                    % self.method.ret_type.get_lexeme())
        else:
            t = self.expr.resolve()
            #raise Exception("Checkear compatibilidad de herencia tambien!")
            if mjp.isToken(t):
                rt = mjp.literalToType(t.get_type())
                if rt != self.method.ret_type.get_type():
                    raise SemanticError(
                        self.ret.get_line(), self.ret.get_col(),
                        "Se esta retornando %s en un metodo de tipo %s" %
                        (mjp.typeToStr(rt), self.method.ret_type.get_lexeme()))
            else:
                if t.type.get_lexeme() != self.method.ret_type.get_lexeme():
                    raise SemanticError(
                        self.ret.get_line(), self.ret.get_col(),
                        "Se esta retornando %s en un metodo de tipo %s" %
                        (t.type.get_lexeme(),
                         self.method.ret_type.get_lexeme()))

            offset = 3
            if self.method.isStatic():
                offset = 2  # no hay this
            return self.expr.check() + "store %d ; offset a ret_val\n" % (
                offset + len(self.method.params) + 1)
        return ""