Quick start
Build Typhon from source, run the samples, publish the typhon command and run the test and benchmark suites.
Requirements
Typhon targets .NET 11, so building it needs the .NET 11 SDK. The compiler and the CLI are built from source. Python is needed for the benchmarks, which run every program under CPython and NumPy as well, and for the conformance check that the expected outputs still match CPython.
Building the solution
git clone https://github.com/typhon-net/typhon-lang
cd typhon-lang
dotnet build Typhon.slnx
The solution contains the compiler, the typhon CLI, the Typhon.NumPy library and the samples.
Projects and scripts
Typhon code compiles in two ways. In a .NET project, dotnet build and dotnet run compile the .ty files together with the rest of the project. For a single script, the typhon command compiles and runs it, like python script.py. The Typhon code is the same in both cases. The difference is what the code can reference and what the build produces.
Project with dotnet build and dotnet run | Script with typhon run | |
|---|---|---|
| What is compiled | A .csproj whose sources include .ty files, alone or next to .cs files. | One .ty file and the sibling modules it imports. |
| What compiles it | MSBuild runs the Typhon source generator inside the project’s Roslyn compilation. | The typhon command compiles in memory and reuses its cache and compile server. |
| What the code can use | The code can use everything the project references: NuGet packages, frameworks such as ASP.NET Core, other projects and C# code in the same assembly. | The code can use the .NET base class library, the Typhon runtime, Typhon.NumPy and the NuGet packages named in its #:package lines. A script that imports from ASP.NET Core also gets the ASP.NET Core framework. |
| Result | dotnet build produces the project’s normal build output, and dotnet run starts it. | The script runs at once. typhon build writes a .dll, or with --aot a native executable. Native AOT does not support ASP.NET Core scripts. |
| Settings | MSBuild properties such as TyphonTypeMode and TyphonIntMode configure the compiler. | Options such as --strict, --permissive and --bigint configure the compiler. |
| Suited for | Applications, libraries, web APIs and code mixed with C# | Scripts, experiments, and the test and benchmark programs |
Code that needs another project, Razor components or C# code in the same assembly belongs in a project. NuGet packages work in both: a project references them, and a script names them in #:package lines. A web API with ASP.NET Core also runs both ways: typhon run samples/MinimalApi/main.ty starts the same web API as dotnet run --project samples/MinimalApi.
Projects with dotnet run
The samples are projects, so dotnet run builds and starts them. samples/HelloTyphon is a console application whose sources are .ty files only. Its project file is an ordinary .csproj:
dotnet run --project samples/HelloTyphon
samples/CSharpApp is a C# application. It uses a class library written in Typhon (samples/TyphonLib), and it mixes .cs and .ty files in one project:
dotnet run --project samples/CSharpApp
samples/MinimalApi is an ASP.NET Core minimal API written in Typhon, with routes, JSON bodies and dependency injection. The page ASP.NET Core minimal API walks through it:
dotnet run --project samples/MinimalApi -- --urls http://localhost:5000
samples/BlazorApp is a Blazor web app whose components keep their markup in .razor files and their logic in .ty files. The page Blazor apps explains it, together with its counterpart on Blazor WebAssembly:
dotnet run --project samples/BlazorApp
samples/WasmLife runs Conway’s Game of Life, written in plain Python, in the browser on the .NET WebAssembly runtime. The development server listens on http://localhost:5080, and the page WebAssembly in the browser explains the sample:
dotnet run --project samples/WasmLife
The CSharpApp sample prints, among other lines:
Typhon raised ValueError → C# caught ArgumentException: radius must be positive
subtotal 37.00, total incl. VAT after 'student -10%': 36.00
Samples gives an overview of all samples in the repository.
Scripts with the typhon command
The typhon command compiles and runs single scripts, like python script.py. Publishing it for the current platform precompiles it with ReadyToRun, which roughly halves its compile time. Typhon has been tested on Windows and Linux; the macOS commands are untested.
dotnet publish src/Typhon.Cli -c Release -r linux-x64 -o tools/typhon
tools/typhon/typhon run samples/scripts/tour.ty dotnet publish src/Typhon.Cli -c Release -r win-x64 -o tools/typhon
tools/typhon/typhon run samples/scripts/tour.ty dotnet publish src/Typhon.Cli -c Release -r osx-arm64 -o tools/typhon
tools/typhon/typhon run samples/scripts/tour.ty Further commands show the generated C# and build assemblies or native executables:
tools/typhon/typhon emit samples/scripts/tour.ty
tools/typhon/typhon build samples/scripts/tour.ty -o out/tour
tools/typhon/typhon build samples/scripts/tour.ty --aot -o out/tour-aot
The typhon command describes every command and option.
Tests written in Typhon
Typhon code can be tested in two ways. typhon test runs pytest-style tests from test_*.ty and *_test.ty files, and a test project of .ty files runs with dotnet test like a C# test project:
tools/typhon/typhon test tests/pytest
dotnet test samples/XunitTests
tests/pytest/test_sample.ty uses fixtures, parametrize, raises and approx, and one of its tests fails on purpose to show the report. samples/XunitTests uses the xUnit attributes as decorators, such as @Fact and @Theory with @InlineData. The typhon command describes typhon test.
Conformance tests
dotnet test tests/Typhon.Conformance.Tests
Each test case is a small Python program. A case passes when Typhon prints exactly what CPython printed for it. The test project looks for CPython in TYPHON_CPYTHON, in the repository’s .venv and on the PATH. Conformance tests explains the suite.
Benchmarks
The benchmark runners time every program under CPython and Typhon and check that both print the same result.
python -m venv .venv; .venv\Scripts\pip install numpy
./benchmarks/run.ps1 -Repeat 5 python3 -m venv ~/bench-venv && ~/bench-venv/bin/pip install numpy pythran
bash benchmarks/linux/run_linux.sh -n 5 The published results use five runs per configuration. The Windows runner uses the virtual environment in the repository and writes benchmarks/RESULTS.md. The Linux runner expects its virtual environment in ~/bench-venv, Codon in ~/.codon/bin/codon and the .NET SDK in ~/.dotnet; the environment variables PYTHON, CODON and DOTNET_ROOT point it elsewhere. It needs /usr/bin/time and rsync, copies the repository to ~/typhon and builds there. It skips a tool it cannot find and writes benchmarks/RESULTS_LINUX.md. The benchmark page summarises the published results.