# statsim-compiler

Compiler for the [StatSim](https://statsim.com) modeling language. Parses `.sm` models and compiles them to multiple probabilistic programming backends.

## Install

```bash
pip install statsim-compiler
```

## Usage

```python
from statsim_compiler import parse, compile, model

# Parse and compile to PyMC code
blocks = parse('p ~ Beta(1, 1)\nx ~ Bin(10, p) | 7\nsample(5000)')
result = compile(blocks, 'pymc')
print(result['code'])        # PyMC source code
print(result['param_names']) # ['p']

# High-level: parse + compile + get a runner
runner = model('p ~ Beta(1, 1)\nx ~ Bin(10, p) | 7\nsample(5000)')
print(runner.code)           # generated PyMC code
trace = runner.run()         # run MCMC inference (requires pymc)
```

## Targets

| Target | Output |
|--------|--------|
| `pymc` | PyMC Python code |
| `stan` | Stan code |
| `tfpjs` | TensorFlow Probability (JavaScript) |
| `z3` | Z3 constraint solver (Python) |

## API

- `parse(source)` — parse `.sm` source, returns a `BlockList`
- `compile(blocks, target)` — compile to target, returns `dict` with `code`, `data_names`, `param_names`, `inference_mode`
- `compile_script(blocks, target)` — compile to raw code string
- `model(source, target)` — parse + compile + return a runner (PyMC runner for `target='pymc'`)

## Runner API (PyMC)

```python
runner = model(source, 'pymc')
runner.code                    # generated source
runner.run(data, **kwargs)     # run inference, returns InferenceData
runner.build(data, **kwargs)   # get pm.Model for extension
```

## Language

One line, one concept. Math notation:

```
p ~ Beta(1, 1)                    # prior
x ~ Bin(10, p) | 7                # observation
delta = p_B - p_A                 # deterministic
y[i] ~ N(mu, sigma), i=1:N       # plated
sample(5000)                      # directive
```

See the [language reference](https://statsim.com/docs) for the full spec.

## License

MIT
