rsaeb 0.1.0

A no_std + alloc interpreter for A=B ordered rewrite programs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# A=B Interpreter Library

A small Rust 2024 `no_std + alloc` library for ordered `lhs=rhs` rewrite
programs.

## Unofficial Project Notice

This project is an unofficial, independently developed interpreter library. It
is not affiliated with, endorsed by, or maintained by Artless Games or the
original A=B author.

A=B's tiny `lhs=rhs` rule system is an unusually elegant programming puzzle
idea, and this project exists because that design is worth studying, testing,
and reimplementing. Thank you to the original A=B author and Artless Games for
creating and publishing the game.

If this interpreter interests you, please support the original game:

- Steam store page: <https://store.steampowered.com/app/1720850/AB/>

The important split is deliberately boring and strict: program code and runtime
input are different domains. Program code is compact ASCII syntax. Runtime input
is ASCII data. The interpreter preserves input bytes that the program syntax
cannot write, such as spaces and reserved characters.

## `no_std` Library Boundary

`src/lib.rs` is `#![no_std]` and uses `alloc` for owned buffers such as
`Vec<u8>`, boxed per-run rule state, `RunResult`, trace events, and step-limit
error state. This means the interpreter core does not depend on `std`, files,
processes, host I/O streams, environment variables, or OS error types. It still
requires an allocator; this is `no_std + alloc`, not a fixed-capacity heapless
interpreter.

For embedded, WASM-core, kernel, or other non-`std` consumers, depend on this
library package or build only the library target:

```sh
cargo check -p rsaeb --lib
```

A downstream `std` application can use the library exactly the same way. A
`no_std` downstream must provide an allocator before calling APIs that allocate.

## Library Usage

This crate exposes the parser, runtime, tracing data, and structured error
types. External I/O and presentation formatting are outside the library
boundary. The library does not expose `std::io` errors, because the interpreter
does not read files.

Basic one-shot execution:

```rust
use rsaeb::{run, RunOptions};

fn main() -> Result<(), rsaeb::AebError> {
    let result = run("a=b", b"a", RunOptions::default())?;
    assert_eq!(result.output(), b"b");
    Ok(())
}
```

Reusable parsed program:

```rust
use rsaeb::{Program, RunOptions};

fn main() -> Result<(), rsaeb::AebError> {
    let program = Program::parse("(once)a=b\na=c")?;

    let first = program.run(b"aa", RunOptions::new(10_000))?;
    let second = program.run(b"aa", RunOptions::new(10_000))?;

    assert_eq!(first.output(), b"bc");
    assert_eq!(second.output(), b"bc");
    Ok(())
}
```

`(once)` consumption is runtime-local. Reusing `Program` is safe because parsed
programs are immutable; each run owns its own rule state.

The parser is byte-oriented. Comments may contain non-ASCII or even non-UTF-8
bytes because the library ignores bytes after `#` before validating executable
code:

```rust
use rsaeb::Program;

fn main() -> Result<(), rsaeb::ParseError> {
    let program = Program::parse(b"a=b#\xff\xfe\n")?;
    assert_eq!(program.rule_count(), 1);
    Ok(())
}
```

Trace output is library-owned data, not hard-coded side effects:

```rust
use rsaeb::{Program, RunOptions, TraceEvent};

fn main() -> Result<(), rsaeb::AebError> {
    let program = Program::parse("a=b\nb=(return)ok")?;
    let mut events = Vec::new();
    let result = program.run_with_trace(
        b"a",
        RunOptions::new(10_000),
        |event: TraceEvent| events.push(event),
    )?;

    assert_eq!(result.output(), b"ok");
    assert!(result.returned());
    assert_eq!(events.len(), 3);
    Ok(())
}
```

Trace step events carry a `RuleId`, not a cloned display string. Human-readable
rule text is metadata on `Program`:

```rust
use rsaeb::{Program, RunOptions, TraceEvent};

fn main() -> Result<(), rsaeb::AebError> {
    let program = Program::parse("a = b # comment")?;
    let mut applied_rule = None;

    program.run_with_trace(b"a", RunOptions::new(10_000), |event| {
        if let TraceEvent::Step { rule, .. } = event {
            applied_rule = Some(rule);
        }
    })?;

    let rule_id = applied_rule.expect("trace should apply a rule");
    let rule = program.rule(rule_id).expect("rule metadata should exist");
    assert_eq!(rule.line_number(), 1);
    assert_eq!(rule.compact_source(), b"a=b");
    Ok(())
}
```

## Public API Surface

Constants:

- `DEFAULT_MAX_STEPS`: default rewrite step limit.

Program construction and execution:

- `Program`
- `Program::parse(source)`: parse reusable program bytes.
- `Program::parse_bytes(source)`: explicit byte parser.
- `Program::parse_str(source)`: explicit UTF-8 string parser.
- `Program::rule_count()`: count executable rules.
- `Program::run(input, options)`: execute without tracing.
- `Program::run_with_trace(input, options, callback)`: execute and receive
  trace events.
- `run(source, input, options)`: one-shot parse and execute helper.

Rule metadata:

- `RuleId`: stable parsed-rule identifier within one `Program`.
- `RuleId::index()`: zero-based rule index in parse order.
- `RuleInfo`: read-only parsed-rule metadata.
- `RuleInfo::id()`: return the rule identifier.
- `RuleInfo::line_number()`: return the one-based source line number.
- `RuleInfo::compact_source()`: return whitespace-stripped executable code.
- `Program::rule(rule_id)`: read parsed rule metadata for tracing/debug UIs.
- `Program::rules()`: iterate parsed rule metadata in execution order.

Runtime configuration and result:

- `RunOptions`: currently holds the step limit.
- `RunOptions::new(max_steps)`: create options with an explicit step limit.
- `RunResult`: owns output bytes plus `steps` and `returned` metadata.
- `RunResult::output()`: borrow final output bytes.
- `RunResult::into_output()`: consume the result and return final output bytes.
- `RunResult::steps()`: return the number of applied rewrite steps.
- `RunResult::returned()`: report whether execution stopped by `(return)`.

Tracing:

- `TraceEvent`: `Initial` state and `Step` events emitted by tracing runs.
- `TraceEvent::bytes()`: borrow the bytes carried by a trace event.

Errors:

- `AebError`: one-shot `run` union of `ParseError` and `RunError`.
- `ParseError`: structured source parse failure.
- `ParseError::line()`: one-based source line.
- `ParseError::column()`: one-based source column when available.
- `ParseError::kind()`: structured parse error kind.
- `ParseErrorKind`: concrete parse failure category.
- `PayloadKind`: identifies which payload rejected a reserved byte.
- `RunError`: structured runtime failure.
- `InputError`: runtime input validation failure.
- `InputError::column()`: one-based input column.
- `InputError::byte()`: rejected input byte.
- `StepLimitError`: step-limit failure with preserved runtime state.
- `StepLimitError::max_steps()`: configured limit.
- `StepLimitError::state()`: borrow state bytes at the limit.
- `StepLimitError::into_state()`: consume the error and return state bytes.

## Execution Semantics

Execution is ordered and single-step.

On each step, the runtime scans rules from top to bottom and applies the first
rule that matches the current state. For an unanchored non-empty left side, the
leftmost match in the current state is used. After one rewrite step, scanning
restarts from the first rule.

Example:

```text
program:
aa=x
a=y

input:
aaaa

output:
xx
```

The first rule is preferred over the second rule, and each application rewrites
the leftmost matching `aa`.

## Step Limit Semantics

`RunOptions::max_steps` is the maximum number of rewrite steps that may be
applied successfully. A run that becomes stable exactly at the limit succeeds.
The limit is an error only when another matching rule would need to be applied
after that many steps.

Examples:

```rust
use rsaeb::{Program, RunError, RunOptions};

fn main() -> Result<(), rsaeb::AebError> {
    let exact = Program::parse("a=b")?.run(b"a", RunOptions::new(1))?;
    assert_eq!(exact.output(), b"b");
    assert_eq!(exact.steps(), 1);

    let no_match = Program::parse("a=b")?.run(b"x", RunOptions::new(0))?;
    assert_eq!(no_match.output(), b"x");
    assert_eq!(no_match.steps(), 0);

    let would_apply = Program::parse("a=b")?.run(b"a", RunOptions::new(0));
    assert!(matches!(would_apply, Err(RunError::StepLimit(_))));
    Ok(())
}
```

## Program Format

A program source is a byte sequence containing one rewrite rule per non-empty
code line:

```text
lhs=rhs
```

Each line is parsed in this order:

1. `#` starts a comment. Everything from `#` to the end of the line is ignored.
2. Non-ASCII bytes are rejected in the remaining code part.
3. ASCII whitespace in the code part is removed completely.
4. Empty compact code is ignored.
5. Non-empty compact code must contain exactly one `=`.
6. The left side and right side are parsed as compact rule syntax.

Internally, the parser keeps these phases separate instead of passing a naked
`Vec<u8>` through every stage:

```text
raw line bytes
  -> CodeLine          # comment removed, code ASCII validated
  -> CompactCodeLine   # whitespace removed, original source columns retained
  -> Rule              # modifiers, anchors, payloads, and actions parsed
```

This keeps diagnostics tied to the original source columns even after whitespace
compaction. For example, `a = b = c` reports the second `=` at its original
source position, not at whatever column it happened to occupy after compaction.

Examples:

```text
a=b# this is parsed as a=b
#a=b  this whole line is a comment
a b = b b  # this is parsed as ab=bb
```

Non-ASCII text is allowed only in comments:

```text
a=b# 日本語コメントは許可
```

This is invalid because the non-ASCII byte is in code:

```text
a=あ
```

## Reserved Characters

The following characters are reserved in program code:

```text
= # ( )
```

Their meanings are fixed:

- `=` separates the left side from the right side.
- `#` starts a comment.
- `(` and `)` are only allowed as part of supported modifier/action tokens.

A second `=` in compact code is a parse error:

```text
a=b=c
```

A second `=` inside a comment is ignored:

```text
a=b#=c
```

Unsupported parenthesis usage is always a parse error:

```text
a=b(
a=b)
a=b()
a=()
a=b(start)
a=(once)b
a(once)=b
```

Because whitespace is removed from program code, spaces cannot be represented as
rule data. Because `=`, `#`, `(`, and `)` are reserved, they also cannot be
represented as rule data.

The input is different. Input bytes are runtime data, not program code. Input
must be ASCII, but it may contain whitespace and reserved characters. Rules
cannot match, create, or delete those bytes directly. The bytes themselves remain
runtime data, although nearby editable bytes may be inserted, removed, or moved
by ordinary rewrites.

Example:

```text
program: a=b
input:   a=()#c
output:  b=()#c
```

Rules also cannot match across preserved runtime-only bytes:

```text
program: ab=bb
input:   a bc
output:  a bc
```

## Left-Side Modifiers

The left side may start with one repeat modifier and one anchor modifier:

- `(once)`: the rule may be used at most once per runtime execution.
- `(start)`: the rule only matches at the start of the current state.
- `(end)`: the rule only matches at the end of the current state.

Supported modifier order is `(once)` first, then an optional anchor. Duplicated
or unsupported left-side modifier order is a parse error.

Examples:

```text
a=b
(once)a=b
(start)a=b
(end)a=b
(once)(start)a=b
```

Because code whitespace is ignored, this is also valid and equivalent to
`(once)(start)a=b`:

```text
( once ) ( start ) a = b
```

## Right-Side Actions

The right side selects the action for a matching rule:

- `text`: replace the matched left side with `text`.
- `(start)text`: remove the match and insert `text` at the start of the state.
- `(end)text`: remove the match and append `text` to the end of the state.
- `(return)text`: stop execution immediately and output `text`.

The action payload is still program data, so it cannot contain whitespace,
reserved characters, or non-ASCII bytes.

Examples:

```text
a=b
x=(start)y
x=(end)y
x=(return)y
```

## Empty Sides

The left side and right side may be empty.

An empty right side deletes the matched left side:

```text
a=
```

An empty left side matches an empty byte sequence. For unanchored rules and
`(start)` rules, it matches at the start of the current state:

```text
(once)=x
```

With input `ab`, this inserts `x` at the start and produces `xab`.

For `(end)` rules, an empty left side matches at the end of the current state:

```text
(once)(end)=x
```

With input `ab`, this inserts `x` at the end and produces `abx`.

An unanchored empty-left rule without `(once)`, `(return)`, or some later rule
that makes execution stop can rewrite forever until the step limit is reached.
That is legal syntax; execution remains governed by the configured step limit.

## Error Model

The library error model is intentionally split:

```rust
use rsaeb::{Program, RunError};

fn main() -> Result<(), rsaeb::AebError> {
    match Program::parse("a=b=c") {
        Err(parse_error) => assert_eq!(parse_error.line(), 1),
        Ok(_) => panic!("expected parse error"),
    }

    let run_error = Program::parse("a=b")?.run("aあ".as_bytes(), Default::default());

    if let Err(RunError::Input(input_error)) = run_error {
        assert_eq!(input_error.column(), 2);
    }

    Ok(())
}
```

Filesystem failures are not part of the library error model. External I/O must
be handled before bytes enter `Program::parse` or `run`.