Tython is a transpiler that converts type-annotated Python-like code to standard Python with type hints.
- Static type checking
- Python type hints generation
- Function declarations with type annotations
- Variable declarations with type inference
- String concatenation and arithmetic operations
- Comparison operators
- Single-line and multi-line comments
- Configurable output formatting
- Docstring generation
str: String typeint: Integer typefloat: Float typebool: Boolean type
tython -input file.ty [options]-input string: Input Tython file (required)-output string: Output Python file (default: input filename with .py extension)-no-type-hints: Disable type hints in output-python-version string: Target Python version (default: "3.8")-indent-size int: Number of spaces for indentation (default: 4)-docstrings: Add docstrings to functions-debug: Enable debug output
Input file (example.ty):
# Variable declarations
name: str = "John"
age: int = 25
/* Function with type annotations */
def calculate_bmi(weight: float, height: float) -> float {
bmi: float = weight / (height * height)
}Generated output:
#!/usr/bin/env python3.8
name = "John" # type: str
age = 25 # type: int
def calculate_bmi(weight: float, height: float) -> float:
"""
Input parameters:
weight: float
height: float
Returns: float
"""
bmi = weight / (height * height) # type: float
return bmicmd/tython/main.go: Command-line interface and program entry pointpkg/: Core packageslexer/: Tokenizes the input codeparser/: Parses tokens into an ASTtypechecker/: Performs static type checkingcodegen/: Generates Python code with type hints
-
Adding a New Type:
- Add the type in
parser/parser.gounder theTypeconstants - Update type checking rules in
typechecker/typechecker.go - Add default value in
codegen/codegen.go
- Add the type in
-
Adding New Operators:
- Add token in
lexer/lexer.go - Update operator handling in
parser/parser.go - Add type checking rules in
typechecker/typechecker.go
- Add token in
-
Adding New Statements:
- Define new statement type in
parser/parser.go - Add parsing logic in
parser.go - Implement type checking in
typechecker.go - Add code generation in
codegen.go
- Define new statement type in
- Go 1.24.2 or higher
go build -o tython.exe ./cmd/tython