# tyvm An experimental bytecode interpreter for type-level Typescript. ## About `tyvm` is another Typescript type-checker project, but this time with a focus on nailing _type-level_ Typescript first. The idea is that type-level Typescript is a very simple, purely functional programming language. Tackling type-level Typescript alone is a lot less complex of a task then trying to reach feature parity with tsc in implementing the entirety of Typescript. This would, theoretically, allow `tyvm` to be useful much more quickly. It could start being used as a converter for `Typescript types -> X`, where X is GraphQL, Prisma, JSON Schema, etc. I'm also just fascinated by this kind of stuff, so it's also a fun side project for me. ## Architectural Overview This project is comprised of 2 main parts: 1. Compiler: Written in Rust, takes Typescript type-level source code -> tyvm bytecode 2. VM: Written in Zig, takes tyvm bytecode and executes it. ### Compiler The compiler uses the [oxc project's parser](https://github.com/web-infra-dev/oxc/) to parse Typescript source code into an AST, which is then converted to a specialized intermediate representation (IR). The IR makes compilation into bytecode much easier. It also doesn't constrain us to any one particular AST, conceptually, any AST from any of the JS/TS compiler projects (e.g. SWC, Babel, etc.) could be converted to the IR, allowing us to support many different compiler frontends. ### VM The VM is a stack based machine that runs the generated bytecode from the compiler. Because of the simplicity and immutable nature of type-level Typescript, there many cool performance optimizations we can borrow from FP: - Structurally shared data structures: [hash array mapped tries](https://en.wikipedia.org/wiki/Hash_array_mapped_trie), [RRB trees](https://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=F16EC1235F2CFF0ED612C3C1ADC87EAF?doi=10.1.1.592.5377&rep=rep1&type=pdf) - Static reference count optimizations ([Perceus](https://www.microsoft.com/en-us/research/publication/perceus-garbage-free-reference-counting-with-reuse/))