Python to Wasm Compiler (Up to Chapter 2)#78
Merged
loyaltypollution merged 93 commits intosource-academy:mainfrom Mar 3, 2026
Merged
Python to Wasm Compiler (Up to Chapter 2)#78loyaltypollution merged 93 commits intosource-academy:mainfrom
loyaltypollution merged 93 commits intosource-academy:mainfrom
Conversation
previously, type tags were stored as wasm global consts. this changes it them to be inlined consts instead
Contributor
|
looks good to me! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements a new Python to Wasm compilation and execution pipeline along with supporting infrastructure and some tests. All new files are placed in the
src/wasm-compilerdirectory, except the Conductor executor and awasm-compiler.specs.tstest file. Thewabtdependency for compiling WAT (Wasm text format) into Wasm binary is also added.An IR is also implemented to aid debugging and increase robustness of the compilation; WAT instructions are represented with structured TypeScript objects rather than with raw strings in the Python compilation. A builder API for building these objects is published (wasm-util) as a separate package and imported for use here.
Overall pipeline of compiler:
Python code
→ parsed into Python AST (existing parser)
→ translated to WAT IR
→ translated to raw WAT
→ compiled to Wasm binary (with wabt)
For Python compilation, a basic runtime is required to handle dynamic values, as well as defunctionalisation and lexical addressing to handle HOFs and closures. A suite of WebAssembly functions to take care of these, written with the builder API, can be found in
src/wasm-compiler/constants.ts.The translation from Python AST to WAT IR takes place in a generator class in
src/wasm-compiler/builderGenerator.ts, which uses the visitor pattern. WAT IR to raw WAT translation, on the other hand, is handled bywasm-util, and is performed with another visitor generator. This PR targets up to SICP Chapter 2, which includes conditionals and pairs. List support is still WIP.The endpoint for the entire pipeline is the
compileToWasmAndRunfunction undersrc/wasm-compiler/index.ts.