One of the final projects that I did during my 4-year Electrical/Computer Engineering degree was to write a compiler with a WebAssembly target. The compiler is written in C, and was implemented using compiler construction tools (Flex, Bison). The scanner, parser, semantic checker and code generation were all developed as part of this compiler. The source language is J-- (subset of Java), and the compiler targets WebAssembly by outputting WebAssembly Text Format (WAT).
A J-- (subset of Java) compiler written in C, that compiles J-- into WebAssembly Text Format (WAT).
This project is a J-- (subset of Java) compiler written in C, that compiles J-- into WebAssembly Text Format (WAT).
See the full J-- specification here: https://pages.cpsc.ucalgary.ca/~aycock/411/spec.html
There are 4 parts to the compiler:
- Scanner (Flex)
- Parser (Bison)
- Semantic Checker
- Code Generation
When the compiler is run, it generates WAT files from J-- files. WASM files are then generated from the WAT files, and then the WASM files are run.
All the provided J-- test cases can found here: J-- Files
All the generated WAT files can be found here: WAT Files
This test case makes use of the
prints run-time function, and outputs a string.
Provided J-- File: gen.t1
Generated WAT File: gen_t1.wat
Provided J-- File: gen.t5
Generated WAT File: gen_t5.wat
42 42 42
Provided J-- File: gen.t10
Generated WAT File: gen_t10.wat…
The compiler was implemented over the course of 4 months. The biggest challenge in this project was integrating all the parts of the compiler together in order to be able to do code generation. I learned the importance of doing thorough testing of each project milestone, and I also learned how to write useful test cases.
Overall, the experience of implementing a compiler that was able to generate correct WAT code was very rewarding. I really enjoyed learning about various parts of a compiler and how those parts work.