Skip to content

Python to Wasm Compiler (Up to Chapter 2)#78

Merged
loyaltypollution merged 93 commits intosource-academy:mainfrom
jonasongg:wasm-compiler-chapter-2
Mar 3, 2026
Merged

Python to Wasm Compiler (Up to Chapter 2)#78
loyaltypollution merged 93 commits intosource-academy:mainfrom
jonasongg:wasm-compiler-chapter-2

Conversation

@jonasongg
Copy link
Contributor

@jonasongg jonasongg commented Feb 22, 2026

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-compiler directory, except the Conductor executor and a wasm-compiler.specs.ts test file. The wabt dependency 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 by wasm-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 compileToWasmAndRun function under src/wasm-compiler/index.ts.

@lucasnyc
Copy link
Contributor

lucasnyc commented Mar 2, 2026

looks good to me!

Copy link
Contributor

@lucasnyc lucasnyc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@loyaltypollution loyaltypollution merged commit 9c30dba into source-academy:main Mar 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants