From 097c7f45908a0c841cb312d649e706acd28fac43 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 12:24:48 +0000 Subject: [PATCH] ocaml: phase 5.1 bf_full.ml baseline (full Brainfuck with [] loops, +++[.-] = 6) Extends the iter-92 brainfuck.ml subset interpreter with bracket matching: '[' if mem[ptr] = 0, jump past matching ']' (forward depth-counting scan: '[' increments depth, ']' decrements) ']' if mem[ptr] <> 0, jump back to matching '[' (backward depth-counting scan) Test program '+++[.-]': +++ set cell 0 = 3 [ enter loop (cell != 0) . acc += cell - cell -= 1 ] loop while cell != 0 result: acc = 3 + 2 + 1 = 6 Tests deeply nested while loops, mutable pc / ptr / acc, multi-arm if/else if dispatch on chars + nested begin/end blocks for loop body conditionals. 58 baseline programs total. --- lib/ocaml/baseline/bf_full.ml | 42 ++++++++++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 7 ++++++ 3 files changed, 50 insertions(+) create mode 100644 lib/ocaml/baseline/bf_full.ml diff --git a/lib/ocaml/baseline/bf_full.ml b/lib/ocaml/baseline/bf_full.ml new file mode 100644 index 00000000..65b3c8a0 --- /dev/null +++ b/lib/ocaml/baseline/bf_full.ml @@ -0,0 +1,42 @@ +let interpret prog = + let mem = Array.make 256 0 in + let ptr = ref 0 in + let pc = ref 0 in + let n = String.length prog in + let acc = ref 0 in + while !pc < n do + let c = prog.[!pc] in + (if c = '>' then ptr := !ptr + 1 + else if c = '<' then ptr := !ptr - 1 + else if c = '+' then mem.(!ptr) <- mem.(!ptr) + 1 + else if c = '-' then mem.(!ptr) <- mem.(!ptr) - 1 + else if c = '.' then acc := !acc + mem.(!ptr) + else if c = '[' then begin + if mem.(!ptr) = 0 then begin + let depth = ref 1 in + while !depth > 0 do + pc := !pc + 1; + let c = prog.[!pc] in + if c = '[' then depth := !depth + 1 + else if c = ']' then depth := !depth - 1 + done + end + end + else if c = ']' then begin + if mem.(!ptr) <> 0 then begin + let depth = ref 1 in + while !depth > 0 do + pc := !pc - 1; + let c = prog.[!pc] in + if c = ']' then depth := !depth + 1 + else if c = '[' then depth := !depth - 1 + done + end + end); + pc := !pc + 1 + done; + !acc + +;; + +interpret "+++[.-]" diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index e9ad3ee5..ccdfe1bb 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -3,6 +3,7 @@ "anagram_check.ml": 2, "anagrams.ml": 3, "bag.ml": 3, + "bf_full.ml": 6, "bigint_add.ml": 28, "bits.ml": 21, "balance.ml": 3, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 3a86cdb2..b6486d70 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — bf_full.ml baseline (Brainfuck interpreter + with `[`/`]` loops, `+++[.-]` → 3+2+1 = 6). Extends the iter-92 + brainfuck.ml subset with bracket matching: `[` jumps past matching + `]` if cell is zero (forward depth-counting scan); `]` jumps back + to matching `[` if cell is non-zero (backward depth-counting scan). + Tests deeply nested while loops, mutable pc + ptr + acc, multi-arm + if-else if dispatch on chars. 58 baseline programs total. - 2026-05-09 Phase 5.1 — anagram_check.ml baseline (char-frequency array, 2/4 pairs are anagrams). to_counts builds a 256-slot int array of character frequencies. same_counts compares two arrays