pyccel.codegen.codegen module#

Module containing the Codegen class which handles the generation of code for a Python program or module. It takes the Pyccel semantic parser, which contains the Pyccel AST annotated through the semantic stage as well as the scoping information, and uses the appropriate CodePrinter to generate code in the target language. See developer_docs/codegen_stage.md for more details on the codegen stage.

class pyccel.codegen.codegen.Codegen(parser, name, language, verbose)[source]View on GitHub#

Bases: object

Class which handles the generation of code.

The class which handles the generation of code. This is done by creating an appropriate class inheriting from CodePrinter and using it to create strings describing the code that should be printed. This class then takes care of creating the necessary files.

Parameters:
  • parser (SemanticParser) – The Pyccel semantic parser for a Python program or module. This contains the annotated AST and additional information about the variables scope.

  • name (str) – Name of the generated module or program.

  • language (str) – The language which the printer should print to.

  • verbose (int) – The level of verbosity.

property astView on GitHub#

Returns the AST.

export(filename)[source]View on GitHub#

Export code to a file with the requested name.

Generate the code in the target language from the AST and print this code to file. Between 1 and 3 files are generated depending on the AST and the target language. A source file is always generated. In languages with header files, a header file is also generated. Finally if the AST includes a program and the target language is not Python a program source file is also generated. The source and header files are named by appending the extension to the requested filename. The program source file is named by additionally prepending prog_ to the requested filename.

Parameters:

filename (str) – The base (i.e. no extensions) of the filename of the file where the code should be printed to.

Returns:

  • filename (str) – The name of the file where the source code was printed.

  • prog_filename (str) – The name of the file where the source code for the program was printed.

get_printer_imports()[source]View on GitHub#

Get the objects that were imported by the current codeprinter.

Get the objects that were imported by the current codeprinter. These imports may affect the necessary compiler commands.

Returns:

A dictionary mapping the include strings to the import module.

Return type:

dict[str, Import]

property is_programView on GitHub#

True if the file is a program.

True if the file is a program, in other words True if the file contains a if __name__ == ‘__main__’ statement.

property nameView on GitHub#

Returns the name associated to the source code

property parserView on GitHub#

The parser which generated the AST printed by this class.

The parser which generated the AST printed by this class.

property printerView on GitHub#

The printer which is used to generate code.

The printer which is used by this class to generate code in the target language.