Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tython - A Typed Python Transpiler

Tython is a transpiler that converts type-annotated Python-like code to standard Python with type hints.

Features

  • 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

Supported Types

  • str: String type
  • int: Integer type
  • float: Float type
  • bool: Boolean type

Usage

tython -input file.ty [options]

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

Example

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 bmi

Project Structure

  • cmd/tython/main.go: Command-line interface and program entry point
  • pkg/: Core packages
    • lexer/: Tokenizes the input code
    • parser/: Parses tokens into an AST
    • typechecker/: Performs static type checking
    • codegen/: Generates Python code with type hints

How to Add New Features

  1. Adding a New Type:

    • Add the type in parser/parser.go under the Type constants
    • Update type checking rules in typechecker/typechecker.go
    • Add default value in codegen/codegen.go
  2. Adding New Operators:

    • Add token in lexer/lexer.go
    • Update operator handling in parser/parser.go
    • Add type checking rules in typechecker/typechecker.go
  3. 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

Requirements

  • Go 1.24.2 or higher

Building from Source

go build -o tython.exe ./cmd/tython

About

Transpilador de python com tipos(Tython) para python

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages