Architecture for a JavaScript to C compiler
Lots of the behaviour we’ll need to implement JavaScript cannot be implemented without runtime support:
Lets manually walk the following JS code through our compiler tool-chain:
First we run esprima to parse the source and give us a syntax-tree:
This will be input to , which would output a target C program. You can see we’re implementing JS’s ternary expression with C’s :
Once our compiler has run we’re left with a valid C program (hopefully). To use the runtime library our output program use C’s macro to include our runtime library’s header files1, and get access to the definitions of the functions our library has defined elsewhere:
In my next post, we’ll look at our how our compiler takes a syntax tree and outputs C source code.
Source: timr.co