OptiLang
A Python-inspired interpreter with built-in profiling, static complexity analysis, optimization detection, and scoring.
OptiLang 1.0.3 ships the full source-to-insight pipeline:
source -> tokens -> AST -> semantic checks -> execution -> profiling -> static complexity -> optimization suggestions -> score
Release Highlights
- Python-like language core with variables, control flow, functions, recursion, lists, dictionaries, tuple unpacking, method calls, slicing, and exception handling
- Runtime execution with line-level and function-level profiling
- Static Big-O complexity analysis using AST structure — no runtime guessing, confidence scores included
- Extended complexity class support:
O(1)throughO(n!),O(2^n),O(∞),O(n+m),O(n*m), and more - Ten optimization detectors for performance and maintainability issues
- Five-dimension scoring system (correctness, efficiency, complexity, quality, maintainability) with a final
0–100score, grade, and narrative explanation - Reorganized package layout (
core,analysis,runtime,types,utils) with full backward compatibility
Quick Start
Install from PyPI
pip install optilang
Install from Source
git clone https://github.com/Sthamanik/optilang.git
cd optilang
python3 -m venv .venv
source .venv/bin/activate
pip install -e .
End-to-End Example
from optilang import analyze, calculate_score, execute
from optilang.lexer import tokenize
from optilang.parser import parse
source = """
total = 0
for i in range(10):
total += i
print(total)
"""
result = execute(source)
ast = parse(tokenize(source))
report = analyze(ast, result.profiling, result.symbol_table)
score = calculate_score(
profiling_data=result.profiling.to_dict() if result.profiling else None,
optimizer_report=report,
source_lines=source.count("\n") + 1,
errors=result.errors,
)
print(result.output) # 45
print(score.grade, score.score) # e.g. Excellent 95.0
print(score.complexity_class) # e.g. O(n)
for suggestion in report.suggestions:
print(f"{suggestion.pattern}: {suggestion.description}")
For quick optimization suggestions from source text only:
from optilang import analyze_source
report = analyze_source(source)
Static Complexity Analysis
from optilang import analyze_complexity, analyze_function_complexity
from optilang.lexer import tokenize
from optilang.parser import parse
source = """
def bubble_sort(arr):
for i in range(len(arr)):
for j in range(len(arr) - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
"""
ast = parse(tokenize(source))
result = analyze_complexity(ast)
print(result.complexity) # O(n²)
print(result.confidence) # 1.0
print(result.explanation)
# Per-function breakdown
fn_results = analyze_function_complexity(ast)
for name, r in fn_results.items():
print(f"{name}: {r.complexity} (confidence={r.confidence})")
Architecture
┌──────────┐ ┌──────────┐ ┌─────────────┐ ┌──────────┐ ┌──────────┐
│ Lexer │ -> │ Parser │ -> │ Semantic │ -> │ Executor │ -> │ Profiler │
│ (Tokens) │ │ (AST) │ │ (Annot AST) │ │ (Runtime)│ │ (Metrics)│
└──────────┘ └──────────┘ └─────────────┘ └──────────┘ └──────────┘
│ │ │
v v v
┌──────────┐ ┌──────────┐ ┌──────────┐
│Complexity│ │Optimizer │ │ Scorer │
│ (Big-O) │ │(Patterns)│ │ (0-100) │
└──────────┘ └──────────┘ └──────────┘
What OptiLang Supports
Language Features
- Numbers, strings, booleans, and
None - Arithmetic, comparison, logical, unary, and augmented assignment operators (
+=,-=,*=, etc.) - Variables and lexical scoping
if/elif/elsewhileandfor ... in ...break,continue, andpass- Function definitions, calls, default parameters, returns, and recursion
- Lists, dictionaries, and index access
- Tuple unpacking:
a, b = 1, 2and swap syntaxarr[i], arr[j] = arr[j], arr[i] - Method calls:
obj.method(args) - List and string slicing:
arr[1:3],s[::-1] - Indexed augmented assignment:
arr[i] += 1 try/except/finally
Built-In Functions and Types
| Name | Description |
|---|---|
print |
Print values to output |
range |
Integer range iterator |
len |
Length of a list or string |
str |
Convert to string |
int |
Convert to integer |
float |
Convert to float |
bool |
Convert to boolean |
list |
Convert to list |
dict |
Create/convert a dictionary |
Analysis Features
Profiling
Execution returns ExecutionResult, which can include:
- Captured program output
- Execution time
- Line execution counts and timings
- Function call counts and recursion depth
- Peak memory estimate
- Heuristic runtime complexity estimate
- Final symbol table
Static Complexity Analysis
analyze_complexity(ast) returns a ComplexityResult with:
- A
Complexityenum value representing the Big-O class - Confidence score (
1.0= provably derived from AST structure, lower = heuristic) - Human-readable explanation of the derivation
- Uses symbolic
ComplexityExpralgebra for exact reasoning about loop nesting, parameters, and recursion
Supported complexity classes:
| Class | Label |
|---|---|
| Constant | O(1) |
| Logarithmic | O(log n) |
| Linear | O(n) |
| Linearithmic | O(n log n) |
| Quadratic | O(n²) |
| Quadratic-log | O(n² log n) |
| Cubic | O(n³) |
| Quartic | O(n⁴) |
| Polynomial | O(n^k) |
| Exponential | O(2^n) |
| Factorial | O(n!) |
| Two-variable | O(n+m), O(n*m) |
| Unbounded | O(∞) |
| Unknown | O(?) |
Optimization Detectors
OptiLang ships with ten detectors across three categories:
Static (AST only)
unused_vars— variables defined but never readdead_code— unreachable statements afterreturn/break/continueconstant_folding— expressions that can be evaluated at parse timeearly_return— functions that can exit earlier to reduce nesting
Hybrid (AST + profiling)
loop_invariant— computations inside loops that don't change per iterationstring_concat_loop— string+=inside loops (use list + join instead)nested_loops— deeply nested loop structures with high complexity
Dynamic (profiling-driven)
hot_loop— loops that account for a disproportionate share of execution timerepeated_computation— identical sub-expressions evaluated multiple timesexpensive_calls— function calls with high per-call cost
Scoring
calculate_score(...) returns a ScoreReport with:
- Final score from
0to100 - Grade label:
Excellent,Good,Fair, orPoor - Complexity class string
- Five-dimension breakdown:
| Dimension | Max Points | What it measures |
|---|---|---|
| Correctness | 35 | Error density |
| Efficiency | 15 | Optimizer pattern findings |
| Complexity | 15 | Big-O class |
| Quality | 20 | Runtime anti-patterns |
| Maintainability | 15 | Style and structure |
- Beginner-friendly narrative summary
Project Layout
optilang/ # Package root
__init__.py # Public API and backward-compat lazy loading
core/ # Language frontend
lexer.py # Tokenizer
parser.py # AST builder
ast_nodes.py # AST node definitions
token.py # Token types
analysis/ # Static analysis
complexity.py # Big-O complexity analyzer
optimizer.py # Optimization pattern detectors
scoring.py # Five-dimension scorer
semantic_analyzer.py # Semantic checks and annotation
runtime/ # Execution layer
executor.py # AST tree-walking interpreter
profiler.py # Line/function profiling
types/ # Shared data types
models.py # ExecutionResult, ScoreReport, etc.
constants.py # Complexity labels and scoring points
utils/ # Utilities
errors.py # Error types
patterns/ # Pattern classification constants
tests/ # Test suite (pytest)
test_complexity.py
test_errors.py
test_executor.py
test_integration.py
test_lexer.py
test_models.py
test_optimizer.py
test_parser.py
test_profiler.py
test_scoring.py
test_semantic_analyzer.py
docs/ # Documentation
README.md
USER_GUIDE.md
API_REFERENCE.md
Backward compatibility: The old flat import paths (
from optilang.lexer import tokenize,from optilang.optimizer import ..., etc.) continue to work via lazy module aliasing in__init__.py.
Documentation
Development
pip install -e ".[dev]"
python3 -m pytest
black optilang tests
mypy optilang
flake8 optilang
Contributing
See CONTRIBUTING.md for workflow, quality checks, and documentation expectations.
License
This project is licensed under the MIT License. See LICENSE for details.
Team
Contact
- Email: shresthamanik1820@gmail.com
- Issues: GitHub Issues
Metadata
Release files for optilang 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| optilang-1.0.3.tar.gz | 149.1 kB | Without provenance | Details for optilang-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| optilang-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for optilang-1.0.3-py3-none-any.whl |
Total release size: 235.5 kB
Release files / optilang-1.0.3.tar.gz
| Download URL | optilang-1.0.3.tar.gz |
|---|---|
| Size | 149.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c45e442f8e0739e942f48730040f94b22a11d96e3937bb606902f711649eb95d
|
|
BLAKE2b-256 checksum How to use checksums |
0e7edeadbad028f088312c335903eb068aaa3acdfa0e4b77ab81feada40a0c27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / optilang-1.0.3-py3-none-any.whl
| Download URL | optilang-1.0.3-py3-none-any.whl |
|---|---|
| Size | 86.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
186aea21d9d9a53add08c6202bd2b071eaca1887d97b7c52a782bd05bc784603
|
|
BLAKE2b-256 checksum How to use checksums |
e98b41565cad117c7dce2e551f817aabead06e26c8bcb200ebf487df4eba8214
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Security for optilang 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.