Exemplo n.º 1
0
    def test_manifest_typing(self, get_visitor):
        file = "test_cases/header/typing_known.bs"
        tree = get_visitor(file)
        st = FrontEndBase.run_globals(tree, SymbolTable())

        aaa = st.get_global('bbb')
        assert aaa.types == {ChemTypes.REDUCING_AGENTS_WEAK, ChemTypes.SALTS_ACIDIC}
Exemplo n.º 2
0
    def test_manifest_typing_unknown(self, get_visitor):
        file = "test_cases/header/typing_unknown.bs"
        tree = get_visitor(file)
        st = FrontEndBase.run_globals(tree, SymbolTable())

        aaa = st.get_global('aaa')

        assert aaa.types == {ChemTypes.SILOXANES, ChemTypes.UNKNOWN}
Exemplo n.º 3
0
    def test_manifests(self, get_visitor):
        file = "test_cases/header/manifest.bs"
        tree = get_visitor(file)
        st = FrontEndBase.run_globals(tree, SymbolTable())

        mod = st.get_global('mod')
        stat = st.get_global('stat')
        mani = st.get_global('aaa')

        assert ChemTypes.MODULE in mod.types and len(mod.types) == 1
        assert ChemTypeResolver.is_mat_in_set(stat.types)
        assert ChemTypeResolver.is_mat_in_set(mani.types)
Exemplo n.º 4
0
    def translate(self, filename: str) -> Program:
        """
        Translates the program from the AST into the corresponding IR.
        :param filename: name of file to parse.
        :return:
        """
        file_stream = FileStream(filename)
        lexer = BSLexer(file_stream)
        stream = CommonTokenStream(lexer)
        parser = BSParser(stream)
        tree = parser.program()

        # We can rely on Python's shallow copy and pass by reference semantics
        # to create only one object and allow all the passes to update it.
        symbol_table = SymbolTable()
        identifier = self.config.identify.get_identifier()

        # Order matters, don't mess with the order, it should be Header, Symbol, Method.
        visitor_passes = [
            HeaderVisitor(symbol_table, identifier),
            SymbolTableVisitor(symbol_table, identifier),
            MethodVisitor(symbol_table),
            IRVisitor(symbol_table)
        ]

        for visitor in visitor_passes:
            if self.config.debug:
                self.log.debug("Running {} pass.".format(visitor.visitor_name))
            visitor.visit(tree)

        ir = visitor_passes[-1]
        self.program = Program(functions=ir.functions,
                               config=self.config,
                               symbol_table=ir.symbol_table,
                               bb_graph=ir.graph,
                               name=self.config.input_file,
                               calls=ir.calls)

        self.visit_type_check(tree, ir.symbol_table)

        if self.config.write_cfg:
            for root in self.program.functions:
                self.program.write['{}_basic_block_graph'.format(
                    root)] = Writable(
                        self.program.name, "{}/{}_{}_basic_blocks.dot".format(
                            self.config.output, self.program.name,
                            root), self.program.functions[root]['graph'],
                        WritableType.GRAPH)
        return self.program
 def get_ir(self, tree):
     st = FrontEndBase.run_globals(tree, SymbolTable())
     st = FrontEndBase.run_symbols(tree, st)
     st = FrontEndBase.run_methods(tree, st)
     return FrontEndBase.run_ir(tree, st)
 def run_globals(
     tree, symbol_table: SymbolTable = SymbolTable()) -> SymbolTable:
     header_visitor = HeaderVisitor(symbol_table, NaiveIdentifier())
     header_visitor.visit(tree)
     return header_visitor.symbol_table