Skip to content

Commit 3937465

Browse files
committed
Add _global_linear_idx, a new global C-order linear index of the current element
1 parent 31f18ba commit 3937465

6 files changed

Lines changed: 215 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ miniexpr includes a DSL (Domain-Specific Language) for writing multi-statement c
270270
- **Conditionals**: `where(cond, then, else)` for element-wise selection
271271
- **Loops**: `for var in range(limit)` iteration
272272
- **Control flow**: `break` and `continue` statements
273-
- **Index access**: Built-in `_i0``_i7` (position) and `_n0``_n7` (shape) variables
273+
- **Index access**: Built-in `_i0``_i7` (position), `_n0``_n7` (shape), and `_global_linear_idx` variables
274274
- **Function-style kernels**: `def name(args): ... return expr`
275275

276276
### DSL Example

doc/dsl-syntax.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ The following cannot be used as user variable/function names in DSL context:
156156
- `print`, `int`, `float`, `bool`, `def`, `return`
157157
- `_ndim`
158158
- `_i<d>` and `_n<d>` (reserved ND symbols)
159+
- `_global_linear_idx`
159160

160161
## ND reserved symbols
161162

@@ -164,6 +165,7 @@ Supported reserved ND symbols when referenced in expressions:
164165
- `_i0`, `_i1`, ... (index per dimension)
165166
- `_n0`, `_n1`, ... (shape per dimension)
166167
- `_ndim`
168+
- `_global_linear_idx` (global C-order linear index)
167169

168170
These are synthesized by DSL compiler/runtime when used.
169171

@@ -213,4 +215,3 @@ When parser/compiler/tests change DSL behavior:
213215
1. Update this document in the same PR.
214216
2. Update or add tests in `tests/test_dsl_syntax.c` / `tests/test_nd.c`.
215217
3. Keep frontend validators (e.g. python-blosc2) aligned with this spec.
216-

doc/dsl-usage.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The DSL extends single expressions to full programs with:
1313
- **Element-wise conditionals** - Using `where(cond, true_val, false_val)`
1414
- **Conditional blocks** - `if/elif/else` with scalar or element-wise conditions
1515
- **Loop constructs** - `for`/`while` loops with `break` and `continue`
16-
- **Index access** - Reference element positions via `_i0`, `_i1`, etc.
16+
- **Index access** - Reference element positions via `_i0`, `_i1`, etc. and `_global_linear_idx`
1717

1818
## Basic Syntax
1919

@@ -157,6 +157,7 @@ Access element positions using special variables:
157157
| `_i0`, `_i1`, ..., `_i7` | Current index in each dimension |
158158
| `_n0`, `_n1`, ..., `_n7` | Array shape in each dimension |
159159
| `_ndim` | Number of dimensions |
160+
| `_global_linear_idx` | Global C-order linear index of the current element |
160161
161162
Example - position-dependent computation:
162163
```
@@ -419,7 +420,7 @@ Rules:
419420
- Function/closure entries must use `ME_FUNCTION*` or `ME_CLOSURE*` in `type`.
420421
- The return dtype must be explicit (not `ME_AUTO`).
421422
- Names cannot shadow built-ins (`sin`, `sum`, etc.) or reserved identifiers
422-
(`def`, `return`, `print`, `int`, `float`, `bool`, `_i0`, `_n0`, `_ndim`).
423+
(`def`, `return`, `print`, `int`, `float`, `bool`, `_i0`, `_n0`, `_ndim`, `_global_linear_idx`).
423424
- String return types are not supported.
424425
425426
Example (pure function):

0 commit comments

Comments
 (0)