Skip to main content

A Python framework for creating compilers that target the Varphi language

Project description

Varphi Development Kit

A Python framework for creating compilers that target the Varphi language - a domain-specific language for describing Turing machine transition rules.

Overview

Varphi is a minimalist language designed to represent Turing machine programs using simple transition rules. The Varphi Development Kit provides a flexible compiler framework that allows you to build custom compilers to transform Varphi programs into any target format.

Installation

pip install varphi-devkit

Requirements:

  • Python ≥ 3.10

Varphi Language Syntax

Varphi programs consist of transition rules with the following syntax:

STATE TAPE_CHARACTER STATE TAPE_CHARACTER HEAD_DIRECTION

Where:

  • STATE: Current/target state (format: q followed by alphanumeric characters, e.g., q0, q_start, q1_accept)
  • TAPE_CHARACTER: Tape symbol (0 for blank, 1 for marked)
  • HEAD_DIRECTION: Head movement (L for left, R for right)

Example Varphi Program

// Simple addition-by-one program
q0 1 q0 1 R
q0 0 qHalt 1 R

Language Features

  • Comments: Single-line (//) and multi-line (/* */) comments are supported
  • Whitespace: Flexible whitespace handling (spaces, tabs, newlines)
  • States: Flexible state naming with q prefix

Core Architecture

The framework is built around these key components:

Data Model

  • VarphiTapeCharacter: Enum for tape symbols (BLANK="0", TALLY="1")
  • VarphiHeadDirection: Enum for head movement (LEFT="L", RIGHT="R")
  • VarphiLine: Dataclass representing a transition rule with fields:
    • if_state: Current state
    • if_character: Current tape character
    • then_state: Next state
    • then_character: Character to write
    • then_direction: Direction to move

Compiler Framework

  • VarphiCompiler: Abstract base class for implementing custom compilers
  • compile_varphi(): Function to parse and compile Varphi programs
  • VarphiSyntaxError: Exception for syntax errors

Usage

Creating a Custom Compiler

To create a Varphi compiler, subclass VarphiCompiler and implement three methods:

from varphi_devkit import VarphiCompiler, VarphiLine, compile_varphi

class MyCompiler(VarphiCompiler):
    def __init__(self):
        # Initialize your compiler's state
        self.output = []
    
    def handle_line(self, line: VarphiLine):
        # Process each transition rule
        self.output.append(f"Transition: {line.if_state} -> {line.then_state}")
    
    def generate_compiled_program(self) -> str:
        # Return the final compiled output
        return "\n".join(self.output)

# Use your compiler
program = """
q0 0 q1 1 R
q1 1 q_halt 0 L
"""

compiler = MyCompiler()
result = compile_varphi(program, compiler)
print(result)

Example Toy Compilers

The framework's test suite includes several example compilers that demonstrate different use cases.

Error Handling

The framework provides comprehensive syntax error reporting out of the box:

from varphi_devkit import VarphiSyntaxError, compile_varphi
from your_compiler import YourCompiler

try:
    result = compile_varphi("invalid syntax here", YourCompiler())
except VarphiSyntaxError as e:
    print(f"Syntax error at line {e.line}, column {e.column}: {e.message}")

API Reference

Core Functions

compile_varphi(program: str, compiler: VarphiCompiler) -> str

Parses and compiles a Varphi program using the provided compiler.

  • Parameters:
    • program: Varphi source code as a string
    • compiler: VarphiCompiler instance to process the program
  • Returns: Compiled program output from the compiler
  • Raises: VarphiSyntaxError for invalid syntax

Abstract Base Class

VarphiCompiler

Abstract base class for implementing custom Varphi compilers.

Abstract Methods:

  • __init__(self) -> None: Initialize compiler state
  • handle_line(self, line: VarphiLine) -> None: Process a transition rule (line in the Varphi program)
  • generate_compiled_program(self) -> str: Return final compiled output

Data Classes

VarphiLine

Represents a single transition rule with attributes:

  • if_state: str - Current state
  • if_character: VarphiTapeCharacter - Current tape character
  • then_state: str - Next state
  • then_character: VarphiTapeCharacter - Character to write
  • then_direction: VarphiHeadDirection - Head movement direction

VarphiTapeCharacter

Enum for tape characters:

  • BLANK = "0" - Empty tape cell
  • TALLY = "1" - Marked tape cell

VarphiHeadDirection

Enum for head movement:

  • LEFT = "L" - Move head left
  • RIGHT = "R" - Move head right

Exceptions

VarphiSyntaxError

Exception raised for syntax errors in Varphi programs.

Attributes:

  • message: str - Error description
  • line: int - Line number where error occurred
  • column: int - Column position of error

License

This project is available under the BSD-3-Clause License (see LICENSE).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

varphi_devkit-1.4.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

varphi_devkit-1.4.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file varphi_devkit-1.4.0.tar.gz.

File metadata

  • Download URL: varphi_devkit-1.4.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for varphi_devkit-1.4.0.tar.gz
Algorithm Hash digest
SHA256 9f1d4dfa448a40512f682bd4c01815d5b9c8fecf1e053dc7a5cc62bf366e5e41
MD5 c22bf8f9d7b4bec3d67a0f7b4354d0c1
BLAKE2b-256 677fb0cfec053963f3f554d3f259d9bfbb2272d18039168375d69e2c849d07c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for varphi_devkit-1.4.0.tar.gz:

Publisher: ci.yml on varphi-lang/varphi-devkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file varphi_devkit-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: varphi_devkit-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for varphi_devkit-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab7e271a540d1c9181955e79441ab1644d69ab83814974564f7e1775202d0f6d
MD5 9209efee8f05998153ac0428062f219a
BLAKE2b-256 ad25248c70e4b35d7e9aab6b43363c4946b71523ba18a85a2c982bd4101820f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for varphi_devkit-1.4.0-py3-none-any.whl:

Publisher: ci.yml on varphi-lang/varphi-devkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page