ocaml: phase 6 Array module + (op) operator sections (+6 tests, 512 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Array module (runtime.sx, OCaml syntax):
Backed by a 'ref of list'. make/length/get/init build the cell;
set rewrites the underlying list with one cell changed (O(n) but
works for short arrays in baseline programs). Includes
iter/iteri/map/mapi/fold_left/to_list/of_list/copy/blit/fill.
(op) operator sections (parser.sx, parse-atom):
When the token after '(' is a binop (any op with non-zero
precedence in the binop table) and the next token is ')', emit
(:fun ('a' 'b') (:op OP a b)) — i.e. (+) becomes fun a b -> a + b.
Recognises every binop including 'mod', 'land', '^', '@', '::',
etc.
Lets us write:
List.fold_left (+) 0 [1;2;3;4;5] = 15
let f = ( * ) in f 6 7 = 42
List.map ((-) 10) [1;2;3] = [9;8;7]
let a = Array.make 5 7 in
Array.set a 2 99;
Array.fold_left (+) 0 a = 127
This commit is contained in:
@@ -1262,6 +1262,22 @@ cat > "$TMPFILE" << 'EPOCHS'
|
||||
(epoch 5002)
|
||||
(eval "(ocaml-run \"List.map (fun x -> match x with `On -> 1 | `Off -> 0) [`On; `Off; `On]\")")
|
||||
|
||||
;; ── Array module ─────────────────────────────────────────────
|
||||
(epoch 5010)
|
||||
(eval "(ocaml-run \"let a = Array.make 5 7 in Array.set a 2 99; Array.fold_left (+) 0 a\")")
|
||||
(epoch 5011)
|
||||
(eval "(ocaml-run \"let a = Array.init 4 (fun i -> i * i) in Array.fold_left (+) 0 a\")")
|
||||
(epoch 5012)
|
||||
(eval "(ocaml-run \"let a = Array.make 3 0 in for i = 0 to 2 do Array.set a i (i + 1) done; Array.length a + Array.get a 0 + Array.get a 1 + Array.get a 2\")")
|
||||
|
||||
;; ── (op) operator sections ───────────────────────────────────
|
||||
(epoch 5020)
|
||||
(eval "(ocaml-run \"List.fold_left (+) 0 [1;2;3;4;5]\")")
|
||||
(epoch 5021)
|
||||
(eval "(ocaml-run \"let f = ( * ) in f 6 7\")")
|
||||
(epoch 5022)
|
||||
(eval "(ocaml-run \"List.map ((-) 10) [1;2;3]\")")
|
||||
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
@@ -2004,6 +2020,16 @@ check 5000 'match polyvar Foo / Bar' '1'
|
||||
check 5001 'match polyvar Pair (a, b)' '12'
|
||||
check 5002 'List.map polyvar On / Off' '(1 0 1)'
|
||||
|
||||
# ── Array module ────────────────────────────────────────────────
|
||||
check 5010 "Array.make + set 2 99" '127'
|
||||
check 5011 "Array.init 4 i*i" '14'
|
||||
check 5012 "Array.make 3 + for + length" '9'
|
||||
|
||||
# ── (op) operator sections ──────────────────────────────────────
|
||||
check 5020 "fold_left (+) sum" '15'
|
||||
check 5021 "let f = (*) in f 6 7" '42'
|
||||
check 5022 "List.map ((-) 10) [1;2;3]" '(9 8 7)'
|
||||
|
||||
TOTAL=$((PASS + FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
||||
|
||||
Reference in New Issue
Block a user