ocaml: phase 5.1 caesar.ml baseline (ROT13 + s.[i] + Char ops)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Side-quests required to land caesar.ml:
1. Top-level 'let r = expr in body' is now an expression decl, not a
broken decl-let. ocaml-parse-program's dispatch now checks
has-matching-in? at every top-level let; if matched, slices via
skip-let-rhs-boundary (which already opens depth on a leading let
with matching in) and ocaml-parse on the slice, wrapping as :expr.
2. runtime.sx: added String.make / String.init / String.map. Used by
caesar.ml's encode = String.init n (fun i -> shift_char s.[i] k).
3. baseline run.sh per-program timeout 240->480s (system load on the
shared host frequently exceeds 240s for large baselines).
caesar.ml exercises:
* the new top-level let-in expression dispatch
* s.[i] string indexing
* Char.code / Char.chr round-trip math
* String.init with a closure that captures k
Test value: Char.code r.[0] + Char.code r.[4] after ROT13(ROT13('hello')) = 104 + 111 = 215.
This commit is contained in:
14
lib/ocaml/baseline/caesar.ml
Normal file
14
lib/ocaml/baseline/caesar.ml
Normal file
@@ -0,0 +1,14 @@
|
||||
let shift_char c k =
|
||||
let n = Char.code c in
|
||||
if n >= 97 && n <= 122 then
|
||||
Char.chr (((n - 97 + k) mod 26 + 26) mod 26 + 97)
|
||||
else c
|
||||
|
||||
let encode s k =
|
||||
String.init (String.length s) (fun i -> shift_char s.[i] k)
|
||||
;;
|
||||
|
||||
(* ROT13 round-trip: encode (encode "hello" 13) 13 = "hello".
|
||||
Sum the codes of two chars to give a deterministic integer check. *)
|
||||
let r = encode (encode "hello" 13) 13 in
|
||||
Char.code r.[0] + Char.code r.[4]
|
||||
@@ -2,6 +2,7 @@
|
||||
"anagrams.ml": 3,
|
||||
"bfs.ml": 6,
|
||||
"btree.ml": 39,
|
||||
"caesar.ml": 215,
|
||||
"calc.ml": 13,
|
||||
"closures.ml": 315,
|
||||
"exception_handle.ml": 4,
|
||||
|
||||
@@ -36,7 +36,7 @@ for f in lib/ocaml/baseline/*.ml; do
|
||||
(eval "(ocaml-run-program (file-read \"$f\"))")
|
||||
EOF
|
||||
|
||||
output=$(timeout 240 "$SX_SERVER" < "$TMP" 2>/dev/null)
|
||||
output=$(timeout 480 "$SX_SERVER" < "$TMP" 2>/dev/null)
|
||||
rm -f "$TMP"
|
||||
|
||||
result=$(echo "$output" | awk '
|
||||
|
||||
@@ -1603,7 +1603,22 @@
|
||||
(cond
|
||||
((= (ocaml-tok-type (peek-tok)) "eof") nil)
|
||||
((at-kw? "let")
|
||||
(begin (append! decls (parse-decl-let)) (loop)))
|
||||
(cond
|
||||
;; `let r = expr in body` at the top level is an
|
||||
;; expression-let, not a decl. Detect by scanning
|
||||
;; for a matching `in` at this depth — has-matching-in?
|
||||
;; walks the same boundaries as the decl scanner.
|
||||
((has-matching-in?)
|
||||
(let ((expr-start (cur-pos)))
|
||||
(begin
|
||||
(skip-let-rhs-boundary!)
|
||||
(let ((expr-src (slice src expr-start (cur-pos))))
|
||||
(let ((expr (ocaml-parse expr-src)))
|
||||
(begin
|
||||
(append! decls (list :expr expr))
|
||||
(loop)))))))
|
||||
(else
|
||||
(begin (append! decls (parse-decl-let)) (loop)))))
|
||||
((at-kw? "module")
|
||||
(begin (append! decls (parse-decl-module)) (loop)))
|
||||
((at-kw? "open")
|
||||
|
||||
@@ -360,6 +360,20 @@
|
||||
let split_on_char c s = _string_split_on_char c s
|
||||
let replace_all s a b = _string_replace s a b
|
||||
let index_of s sub = _string_index_of s sub
|
||||
let make n c =
|
||||
let rec aux i acc = if i = 0 then acc else aux (i - 1) (acc ^ c) in
|
||||
aux n \"\"
|
||||
let init n f =
|
||||
let rec aux i acc =
|
||||
if i >= n then acc else aux (i + 1) (acc ^ f i)
|
||||
in
|
||||
aux 0 \"\"
|
||||
let map f s =
|
||||
let rec aux i acc =
|
||||
if i >= _string_length s then acc
|
||||
else aux (i + 1) (acc ^ f (_string_get s i))
|
||||
in
|
||||
aux 0 \"\"
|
||||
end ;;
|
||||
|
||||
module Bytes = struct
|
||||
|
||||
Reference in New Issue
Block a user