# Expression reference

`core/expressions.py`'s grammar: a restricted-Python AST allow-list, never `eval`/`exec`.

## Grammar

| Category | Allowed |
|---|---|
| Constants | int, float, str, bool, None |
| Names | `entity`, `TNOW` / `now`, `true` / `false`, submodel parameters, model variables |
| Attributes | `entity.<name>`; `self.<name>` in an agent type's expressions (its attributes, `self.state`, `self.id`); `AGENT("pop", i).<name>`; no leading `_`. `payload` in a message transition's guard and actions |
| Operators | `+ - * / // % **`, unary `+ - not`, `and`/`or`, comparisons, `in`/`not in` against a tuple or list literal, `a if c else b`, tuple and list literals |
| Calls | the functions below, positional arguments only |

## Names and case

Function names are case sensitive. The functions borrowed from Python are lower case: `abs`, `ceil`, `exp`, `float`, `floor`, `int`, `log`, `log10`, `max`, `min`, `round` and `sqrt`. Every other function is upper case, such as `IF`, `TABLE` or `EXPO`. `max(1, 2)` works, while `MAX(1, 2)` stops with an error that says the function is unknown. The one exception is `SQRT`, which exists in both spellings.

## Functions

One page for each group; every function has its call, what it does and an example with the value it gives.

| Group | Functions |
|---|---|
| [Math](math.md) | `abs`, `ceil`, `exp`, `float`, `floor`, `IF`, `int`, `log`, `log10`, `max`, `min`, `MODULO`, `round`, `sqrt`, `SQRT`, `XIDZ`, `ZIDZ` |
| [Trigonometry](trigonometry.md) | `ACOS`, `ASIN`, `ATAN`, `ATAN2`, `COS`, `COSH`, `DEG`, `PI`, `RAD`, `SIN`, `SINH`, `TAN`, `TANH` |
| [Random draws](random.md) | `DISC`, `EXPO`, `NORM`, `RAND`, `TRIA`, `UNIF` |
| [System dynamics](system-dynamics.md) | `ACTIVE_INITIAL`, `DELAY1`, `DELAY3`, `DELAYN`, `DELAYP`, `FORECAST`, `INITIAL`, `NOISE_NORMAL`, `NPV`, `PULSE`, `RAMP`, `SAMPLE_IF_TRUE`, `SMOOTH`, `SMOOTH3`, `STEP`, `TREND` |
| [Model state and lookups](model-state.md) | `AUX`, `CONV`, `DELAY`, `DISTANCE`, `FLOW`, `FLOWSUM`, `FRACTION`, `LEVEL`, `POWER`, `TABLE`, `TABLE_EXTRAPOLATE`, `TS`, `VAR` |
| [Resources and stores](resources.md) | `AVAILABLE`, `IDLE`, `MR`, `NQ`, `NR`, `NS` |
| [Arrays](arrays.md) | `MEAN`, `PROD`, `SUM`, `VMAX`, `VMIN` |
| [Agents and links](agents.md) | `AGENT`, `ALIVE`, `COUNT`, `IS_LINKED`, `LINK_MEAN`, `LINK_SUM`, `LINKED` |
| [Agents in space](agent-space.md) | `CELL_COUNT`, `KNN_MEAN`, `KNN_SUM`, `NEAR`, `NEAR_HEADING`, `NEAR_MAX`, `NEAR_MEAN`, `NEAR_MIN`, `NEAR_SUM`, `NEAREST`, `NEIGHBOUR_MEAN`, `NEIGHBOUR_SUM`, `NEIGHBOURS`, `PATCH` |
