Skip to content

Latest commit

History

124 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

TwoPy

A toy Python interpreter written in C++26.

Usage

  • Ensure you have CMake 3.20+ and a C++26 supporting compiler.
  • Enable helper.sh.
  • Do ./helper.sh help for usage info. Use ./helper.sh build debug-build <your build tool> to create the binary.

Design

This is based off typical compiler stages:

  • Lexer: Tokenizes Python source code.
  • Parser: Builds an AST via recursive descent with Pratt's parsing
  • Bytecode Compiler: Compiles the AST into bytecode with constant/name pooling, scope-aware variable access, and jump patching
  • Stack-Based VM: Executes bytecode with a global/local variable env with its own stack and instruction pointer.

Supported Python Features (around 3.8 - 3.9)

  • Basic variable store/load, comparisons, arithmetic.
  • print??
Screenshot_20260308_202950

Contributors

Special thanks to DrkWithT for helping refactor the match and consume functions in the Parser namespace to use metaprogramming, eliminating verbose consume(T) || consume(T) chains for larger conditionals.

TODO

  • VM Refactor 1
    • New stack representation supporting arbitrary peeking (presized vector with SP, BP)
    • Stack of call frames for recursion support
  • Get Fibonacci working as microbenchmark 1
  • Support nested functions
  • For loops
  • Ternary expressions?
  • Support lists
  • Support dictionaries

About

No description, website, or topics provided.

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages