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.
- 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
- 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
- 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
# Run with: python -m dslc.cli run --expr "..."
x = 2 + 3 * 4;
y = sin(x);
print(y);# 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)# Evaluate derivative of x^3 at x=2
python -m dslc.cli calculus gradient "pow(x,3);" x 2.0
# Output: 12.0# Integrate sin(x) from 0 to π
python -m dslc.cli calculus integrate "sin(x)" 0 3.14159
# Output: 2.0# 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);"python -m dslc.cli run --expr "x = 2 * 3; sin(x);" --explainOutput:
[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
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 --explainThe compiler follows a traditional pipeline architecture:
Source → Lexer → Parser → AST → Optimizations → LLVM IR → JIT → Execution
Key components:
dslc/lexer.py: Tokenizer implementationdslc/parser.py: Recursive descent parserdslc/ast.py: AST node definitions and visitorsdslc/codegen.py: LLVM IR generation via llvmlitedslc/jit.py: JIT compilation and runtimedslc/calculus.py: Symbolic/numeric math utilitiesdslc/cli.py: Command-line interface and REPL
Near-term goals:
- Type system with int/float/bool support
- User-defined functions and scoping
- IR-level optimizations
- Enhanced Python interoperability
- Expanded mathematical library
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Add tests under
tests/ - Update documentation if needed
- 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
MIT License - see LICENSE for details.
- LLVM integration via llvmlite
- CLI framework: Click
- Plotting: matplotlib
- Inspired by the LLVM Kaleidoscope tutorial