Esempio n. 1
0
 def visitNegationExpr(self, ctx: DecafParser.NegationExprContext):
     t = self.visit(ctx.expression())
     if t != type_enum.Boolean:
         error = genericError('Can only negate (!) boolean expressions',
                              ctx.start.line)
         self.errors.append(error)
         return type_enum.Error
     return type_enum.Boolean
Esempio n. 2
0
 def visitNegativeExpr(self, ctx: DecafParser.NegativeExprContext):
     t = self.visit(ctx.expression())
     if t != type_enum.Integer:
         error = genericError(
             'Can only apply negative (-) to integer expressions',
             ctx.start.line)
         self.errors.append(error)
         return type_enum.Error
     return type_enum.Integer
Esempio n. 3
0
    def visitAssignStmt(self, ctx: DecafParser.AssignStmtContext):
        left_type = self.visit(ctx.left)
        right_type = self.visit(ctx.right)

        if right_type != left_type:
            error = genericError(
                'Can only assign to two expressions with the same type',
                ctx.start.line)
            self.errors.append(error)
            return type_enum.Error

        return type_enum.Boolean
Esempio n. 4
0
    def visitIfStmt(self, ctx: DecafParser.IfStmtContext):
        self.enterScope('ifblock' + str(self.anonCounter), 'if')
        self.anonCounter += 1

        expt = self.TypeValidator.visit(ctx.expression())
        if expt != type_enum.Boolean:
            error = genericError('Expected boolean expression for if',
                                 ctx.start.line)
            self.TypeValidator.errors.append(error)

        visit = self.visitChildren(ctx)

        self.exitScope()
        return visit
Esempio n. 5
0
    def visitEqualityOp(self, ctx: DecafParser.EqualityOpContext):
        op = ctx.op.getText()

        left_type = self.visit(ctx.left)
        right_type = self.visit(ctx.right)

        if right_type != left_type:
            error = genericError(
                'Can only apply %s to two expressions with the same type' % op,
                ctx.start.line)
            self.errors.append(error)
            return type_enum.Error

        return type_enum.Boolean
Esempio n. 6
0
    def visitLocation(self, ctx: DecafParser.LocationExprContext):
        var_name = ctx.name.text
        scope = self.scopes.peek()
        var, _scope = scope.lookup(var_name)
        value = type_enum.Error

        if not var:
            error = notDefinedError('Variable', var_name, ctx.start.line)
            self.errors.append(error)
            return type_enum.Error

        value = var.stype

        if ctx.expr:

            if not var.listSize:
                error = genericError(
                    'symbol of type %s is non suscriptable' % var.stype,
                    ctx.start.line)
                self.errors.append(error)
                return type_enum.Error

            num_expr = ctx.expr.getText()

            try:
                num = int(num_expr)
                if num < 0 or (num > var.listSize - 1):
                    error = genericError('Index out of range', ctx.start.line)
                    self.errors.append(error)
                    return type_enum.Error
            except ValueError:
                pass

            visit = self.visit(ctx.expr)

            if visit != type_enum.Integer:
                error = genericError('Index must be an integer, got',
                                     ctx.start.line)
                self.errors.append(error)
                return type_enum.Error

        if ctx.loc:
            struct = scope.typeExists(var.stype, type_enum.Struct)

            if not struct:
                error = genericError(
                    'Location passed but %s is not a struct' % var_name,
                    ctx.start.line)
                self.errors.append(error)
                return type_enum.Error

            if ctx.loc.name.text not in struct.paramlist:
                error = genericError(
                    'Location %s not defined in struct of %s of type %s' %
                    (ctx.loc.name.text, var_name, var.stype), ctx.start.line)
                self.errors.append(error)
                return type_enum.Error

            value = struct.paramlist[ctx.loc.name.text].stype

        return value