Search code examples
pythoncode-generationhardware-interfacehardware

How to design a code generator that generates Python code


My current project requires that I read data that is stored in configuration files and generate hardware-abstraction-layer code from it. The configuration data structures describe digital hardware in a low-level way (components with registers/bitfields, power on reset values and so on). From this data I need to autogenerate Python code that can be used by other application developers in order to write applications on top of it. Like I said before, the idea is to provide a HAL for the higher-level application.

My question is: What would be your approach to solve this problem? To me it feels natural to use Python for the code generator. I don't know if there are better tools for this task (yacc/bison?). My design idea would be to have a static program component gets redistributed and handles the communication handling, logging and all the generic stuff. The other component that is hardware-dependent would be completely autogenerated and use the API of the static component.


Solution

  • What you want to do is use is Program Transformation System (PTS).

    These are tools/frameworks that accept descriptions of one or more computer languages, and then allow you write rules that map one language to another. In your case, you want to define a language for the "configuration files" for your hardware, and write rules that map various patterns of the configuration files to Python code fragments (maybe declarations, maybe statements, maybe sets of functions) that provide your desired hardware API.

    Insisting the PTS be in Python will simply limit you choices to pretty much the empty set, unless you think you are going to build a PTS yourself. That won't happen either. Thinking that using a parser is a good step is right, but it is only a small part of really workable solution; see my essay on Life After Parsing on why you want a full PTS.