Skip to content

saksham-45/Mathon

Repository files navigation

DSLcompiler: A Scientific Computing DSL with Symbolic Math

Python 3.11+ License: MIT

A compiler for a domain-specific language focused on scientific computing and mathematical exploration. Features symbolic differentiation, automatic differentiation (AD), numerical integration, and interactive plotting capabilities.

Core Features

Compiler Pipeline

  • Lexer and parser supporting mathematical expressions, assignments, and function calls
  • AST-based intermediate representation with transformation hooks
  • LLVM IR generation via llvmlite
  • JIT compilation using MCJIT for native code execution
  • Interactive REPL with plotting support

Mathematical Capabilities

  • Built-in math functions: sin, cos, tan, exp, log, pow
  • Symbolic differentiation engine that works directly on AST
  • Forward-mode automatic differentiation using dual numbers
  • Numerical integration using composite Simpson's rule
  • Interactive function plotting with matplotlib integration

Developer Experience

  • Explain mode (--explain) showing compilation pipeline stages:
    • Token stream from lexer
    • AST structure and transformations
    • Optimization passes and simplifications
    • Generated LLVM IR
    • Execution results
  • Interactive plotting via plot(expr, a, b) in REPL or scripts
  • Comprehensive test suite for compiler and math features

Usage Examples

Basic Math and Assignment

# Run with: python -m dslc.cli run --expr "..."
x = 2 + 3 * 4;
y = sin(x);
print(y);

Symbolic Differentiation

# Find derivative of sin(x)
python -m dslc.cli calculus derivative "sin(x);" x
# Output: cos(x)

# Find derivative of x^3
python -m dslc.cli calculus derivative "pow(x,3);" x
# Output: 3 * pow(x,2)

Numerical Derivatives (using Dual Numbers)

# Evaluate derivative of x^3 at x=2
python -m dslc.cli calculus gradient "pow(x,3);" x 2.0
# Output: 12.0

Integration

# Integrate sin(x) from 0 to π
python -m dslc.cli calculus integrate "sin(x)" 0 3.14159
# Output: 2.0

Interactive Plotting

# Plot sin(x) from 0 to 10
python -m dslc.cli run --expr "plot(sin(x), 0, 10);"

# Plot a custom function
python -m dslc.cli run --expr "plot(sin(x) * exp(-x/3), 0, 10);"

Explain Mode Example

python -m dslc.cli run --expr "x = 2 * 3; sin(x);" --explain

Output:

[Lexer] Tokens: IDENT(x), EQUAL(=), NUMBER(2), STAR(*), NUMBER(3), SEMICOLON(;), ...
[Parser] AST:
  x = (2.0 * 3.0);
  sin(x);
[Optimizer] Simplified (2.0 * 3.0) -> 6.0
[CodeGen] Emitted LLVM IR:
; ModuleID = "dsl_module"
...
[Result] 0.279415498

Setup and Installation

Requirements:

  • Python 3.11+ (recommended for llvmlite wheel compatibility)
  • pip for package management
  • Optional: LLVM toolchain if building llvmlite from source

Quick start:

# Create and activate virtual environment
python3.11 -m venv .venv
source .venv/bin/activate

# Install package and dependencies
pip install -e .
pip install pytest matplotlib

# Run tests
pytest -q

# Start REPL
python -m dslc.cli repl --explain

Architecture

The compiler follows a traditional pipeline architecture:

Source → Lexer → Parser → AST → Optimizations → LLVM IR → JIT → Execution

Key components:

  • dslc/lexer.py: Tokenizer implementation
  • dslc/parser.py: Recursive descent parser
  • dslc/ast.py: AST node definitions and visitors
  • dslc/codegen.py: LLVM IR generation via llvmlite
  • dslc/jit.py: JIT compilation and runtime
  • dslc/calculus.py: Symbolic/numeric math utilities
  • dslc/cli.py: Command-line interface and REPL

Roadmap

Near-term goals:

  1. Type system with int/float/bool support
  2. User-defined functions and scoping
  3. IR-level optimizations
  4. Enhanced Python interoperability
  5. Expanded mathematical library

Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Add tests under tests/
  4. Update documentation if needed
  5. Open a PR with a clear description

Guidelines:

  • Run tests before submitting: pytest -q
  • Include examples for new features
  • Update README for CLI changes
  • Keep PR scope focused

License

MIT License - see LICENSE for details.

Acknowledgments

About

a math domain language slightly efficient than std python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors