Exemple #1
0
    def visit_VarDecl(self, node):
        type_name = node.type_node.value
        type_symbol = self.cur_scope.lookup(type_name)

        var_name = node.var_node.value
        var_symbol = VarSymbol(var_name, type_symbol)
        self.cur_scope.insert(var_symbol)
Exemple #2
0
    def visit_VarDecl(self, node):
        type_name = node.type_node.value
        type_symbol = self.cur_scope.lookup(type_name)

        var_name = node.var_node.value
        var_symbol = VarSymbol(var_name, type_symbol)

        #if there is already a symbol with var_name in the current scope
        #symbol table this error will be raised
        if self.cur_scope.lookup(var_name, current_scope_only=True):
            raise Exception(f'Error: Duplicate indentifier {var_name} found')

        self.cur_scope.insert(var_symbol)
Exemple #3
0
    def visit_Procedure(self, node):
        proc_str = self.tl * 4 * ' '

        proc_name = node.name
        proc_symbol = ProcedureSymbol(proc_name)
        self.cur_scope.insert(proc_symbol)

        proc_str += f'procedure {proc_name}{self.cur_scope.scope_level}'

        proc_scope = ScopedSymbolTable(scope_name=proc_name,
                                       scope_level=self.cur_scope.scope_level +
                                       1,
                                       enclosing_scope=self.cur_scope)
        self.cur_scope = proc_scope

        if len(node.params) > 0:
            c = 0
            proc_str += '('
            for param in node.params:
                param_type = self.cur_scope.lookup(param.type_node.value)
                param_name = param.var_node.value
                var_symbol = VarSymbol(param_name, param_type)
                self.cur_scope.insert(var_symbol)
                proc_symbol.params.append(var_symbol)

                proc_str += (f'{param_name}{var_symbol.scope.scope_level} : '
                             f'{param_type}{param_type.scope.scope_level}')

                if c != 0 and c < len(node.params) - 1:
                    proc_str += ','

                c += 1

            proc_str += ')'

        proc_str += f';'

        self.lines.append(proc_str)

        self.visit(node.block)

        self.lines.append(self.tl * 4 * ' ' + f'end; {{END OF {proc_name}}}')

        self.cur_scope = self.cur_scope.enclosing_scope
Exemple #4
0
    def visit_Procedure(self, node):
        proc_name = node.name
        proc_symbol = ProcedureSymbol(proc_name)
        self.cur_scope.insert(proc_symbol)

        print('ENTER scope: ' + proc_name)
        proc_scope = ScopedSymbolTable(proc_name, 2)
        self.cur_scope = proc_scope

        for param in node.params:
            param_type = self.cur_scope.lookup(param.type_node.value)
            param_name = param.var_node.value
            var_symbol = VarSymbol(param_name, param_type)
            self.cur_scope.insert(var_symbol)
            proc_symbol.params.append(var_symbol)

        self.visit(node.block)

        print(proc_scope)
        print('LEAVE scope: ' + proc_name)