Пример #1
0
    def visit(self, node):
        if isinstance(node, ASTImport):
            # Import statements make library functions available to PyPE
            imp = LibraryImporter(node.module.name)
            print(imp.add_symbols(self.symbol_table))

        # TODO
        # Add symbols for the following types of names:
        #   inputs: anything in an input expression
        #     the SymbolType should be input, and the ref can be None
        #     the scope should be the enclosing component
        #   assigned names: the bound name in an assignment expression
        #     the SymbolType should be var, and the ref can be None
        #     the scope should be the enclosing component
        #   components: the name of each component
        #     the SymbolType should be component, and the ref can be None
        #     the scope sould be *global*

        # Note, you'll need to track scopes again for some of these.
        # You may need to add class state to handle this.

        if isinstance(node, ASTInputExpr):
            for input_expression in node.children:
                name = input_expression.name
                self.symbol_table.addsym((name, SymbolType.input, None),
                                         self.currentComponent)
        elif isinstance(node, ASTAssignmentExpr):
            name = node.binding.name
            self.symbol_table.addsym((name, SymbolType.var, None),
                                     self.currentComponent)
        elif isinstance(node, ASTComponent):
            name = node.name.name
            self.currentComponent = name
            self.symbol_table.addsym((name, SymbolType.component, None),
                                     'global')
Пример #2
0
    def visit(self, node):

        # import statements make library functions available to PYPE
        if isinstance(node, ASTImport):
            imp = LibraryImporter(node.module)
            imp.add_symbols(self.symbol_table)

        # traverse the rest of the nodes in the tree
        elif isinstance(node, ASTProgram):
            if node.children is not None:
                for child in node.children:
                    self.visit(child)

        # add symbols for inputs, i.e. anything in an expression_list
        # SymbolType: inputs
        # ref: None
        # scope: enclosing component
        elif isinstance(node, ASTInputExpr):
            if node.children is not None:
                for child in node.children:
                    sym = Symbol(child.name, SymbolType.input, None)
                    scope = node.parent.name.name
                    self.symbol_table.addsym(sym, scope=scope)

        # add symbols for assigned names, i.e. the bound name in an
        # assignment expression
        # SymbolType: var
        # ref: None
        # scope: enclosing component
        elif isinstance(node, ASTAssignmentExpr):
            sym = Symbol(node.binding.name, SymbolType.var, None)
            scope = node.parent.name.name
            self.symbol_table.addsym(sym, scope=scope)

        # add symbols for components, i.e. the name of each components
        # SymbolType: components
        # ref: None
        # scope: global
        elif isinstance(node, ASTComponent):
            sym = Symbol(node.name.name, SymbolType.component, None)
            self.symbol_table.addsym(sym)

            # traverse the rest of the nodes in the tree
            if node.children is not None:
                for child in node.children:
                    self.visit(child)