Beispiel #1
0
    def createType(self, line, typeName):
        type = symtab.Type(typeName, self.T)
        if not type.isValid():
            ErrorHandler.nameError(line, typeName)
            self.error()

        return type
Beispiel #2
0
    def visitMethodCall(self, ctx: decafParser.MethodCallContext):
        name = ctx.ID().getText()

        methodSymbol = self.currentScope.resolve(name)

        # Check that the symbol is defined
        if not methodSymbol:
            ErrorHandler.nameError(ctx.start.line, name)
            self.error()

        # Check that the symbol is a method
        isMethod = isinstance(methodSymbol, symtab.MethodSymbol)
        if not isMethod:
            ErrorHandler.typeError(ctx.start.line, name, 3)
            self.error()

        # Check method signature
        signature = methodSymbol.args
        args = list()

        for arg in ctx.arg():
            type = self.visit(arg)
            args.append(type)

        if not (signature == args):
            ErrorHandler.typeError(ctx.start.line, name, 4)
            self.error()

        # Return the type of the method
        # to be able to make methods calls from conditional statements
        return methodSymbol.type.name
Beispiel #3
0
    def visitLocation(self, ctx: decafParser.LocationContext):
        name = ctx.ID().getText()
        symbol = self.currentScope.resolve(name)

        if not symbol:
            ErrorHandler.nameError(ctx.start.line, name)
            self.error()
            return _error

        # Check if it is a built-in type
        builtIn = False
        if symbol.type.name in self.T.builtIn:
            builtIn = True

        if builtIn:
            # Declared as a variable
            if isinstance(symbol, symtab.VariableSymbol):
                if ctx.expr is not None:
                    ErrorHandler.typeError(ctx.start.line, symbol.name, 5)
                    self.error()

                if ctx.loc is not None:
                    ErrorHandler.attributeError(ctx.start.line, symbol.name,
                                                ctx.loc.getText())
                    self.error()

                return symbol.type.name

            # Declared as an array
            elif isinstance(symbol, symtab.ArraySymbol):
                try:
                    index = int(ctx.expr.getText())
                except ValueError:
                    index = None

                if (index is None) or (index < 0) or (index >= int(
                        symbol.num)):
                    ErrorHandler.arrayError(ctx.start.line, symbol.name, 2)
                    self.error()

                if ctx.loc is not None:
                    ErrorHandler.attributeError(ctx.start.line, symbol.name,
                                                ctx.loc.getText())
                    self.error()

                return symbol.type.name

        if not builtIn:
            # Save the actual scope
            actualScope = self.currentScope
            # Because it has a member, we declare the struct as the current scope
            self.currentScope = self.structs.get(symbol.type.name)
            # Visit location
            type = self.visit(ctx.loc)
            # Restore actual scope
            self.currentScope = actualScope
            # Return the type of the location
            return type
Beispiel #4
0
    def mainCheker(self, line, scope):
        symbol = None
        isMain = False

        for symbolName in scope.symbols:
            if symbolName == "main":
                symbol = scope.symbols.get(symbolName)
                break

        if isinstance(symbol, symtab.MethodSymbol):
            isMain = True

        if not isMain:
            ErrorHandler.nameError(line, "main()")
            self.error()