Compiler pipeline
The stages from a .ty file to IL: parser, analysis and type inference, C# generation, Roslyn, and the runtime that every Typhon assembly contains.
Typhon compiles in two steps. Its own front end turns Python source into C#, and Roslyn, the C# compiler, turns that C# into IL. The C# is an intermediate form, and a build writes it to disk only when asked to.
- .ty source Python 3.12 syntax with type annotations
- Lexer and parser Hand-written, with INDENT and DEDENT tokens
- Analysis Scopes, names and types resolved against Roslyn symbols; whole-program type inference
- C# with #line Generated source that maps back to the .ty file
- Roslyn The same compilation as the project's own .cs files
- IL In a .dll or .exe; Native AOT compiles it further into a native executable
.ty file to IL. Roslyn compiles the generated C# together with the project's C# code. Front end
The tokenizer and the recursive-descent parser in src/Typhon.Compiler/Syntax are written by hand for the Python subset that Typhon supports. The tokenizer produces Python’s INDENT and DEDENT tokens. The front end does not depend on CPython. A second front end parses GDScript into the same syntax tree.
Analysis and lowering to C#
The code in src/Typhon.Compiler/Emit resolves names and types and writes C#.
Python has no new keyword. Foo(1) is a function call or a construction, depending on what Foo is. The compiler therefore resolves every name against the Typhon modules and against the Roslyn compilation, which contains the referenced assemblies and the C# code of the same project.
Python locals belong to a function, and C# locals belong to a block. LocalsAnalyzer computes, for each variable, the innermost block that contains all of its uses. The compiler declares the variable there with var, splits it into independent variables where its uses do not interact, and hoists the declaration otherwise.
Python’s protocol methods (__add__, __eq__, __str__, __getitem__, __iter__, __len__ and others) stay in the generated class, and the compiler connects them to .NET operators, Equals, ToString, indexers and IEnumerable<T>.
Whole-program type inference runs in this stage as well. It repeats the emitter until the inferred signatures stop changing.
Roslyn as back end
Roslyn compiles the generated C#. It provides overload resolution, generic type inference, lambdas and closures, the state machines for async and iterators, debug symbols and IL emission. #line directives in the generated code map every error, warning, stack trace line and breakpoint back to the .ty file.
typhon emit prints the generated C# for a single file. In a project with the MSBuild property EmitCompilerGeneratedFiles set to true, the build writes it to the obj folder. The samples set this property.
Build integration
An incremental Roslyn source generator in src/Typhon.Compiler/Generator receives the .ty files as additional files. Because of the generator, Typhon and C# code can live in one project and reference each other in both directions, and Typhon types appear in C# IntelliSense. Source generators run on every keystroke in the IDE, so the generator has to stay fast.
Runtime
The Python builtins and the standard library modules live in src/Typhon.Compiler/Runtime/. They are compiled into every Typhon assembly as internal types, so a Typhon program has no runtime dependency of its own. Scripts that typhon run compiles in memory reference a precompiled copy in src/Typhon.Runtime instead, which saves compiling the runtime on every run.
Compile time
Once warm, Typhon’s parser, type inference and C# generation take 6 to 30 ms for the benchmark programs. Most of a compilation is Roslyn. Before the CLI was published with ReadyToRun, a cold compilation spent about 1.2 s in Roslyn, mostly because Roslyn itself had to be JIT-compiled first. Before the runtime was precompiled, compiling the runtime’s own source took about 180 ms of a 200 ms warm compilation. The typhon command describes how the script cache and the compile server avoid these costs.