ocaml: phase 1 type annotations on let / (e : T) (+4 tests, 473 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s

let NAME [PARAMS] : T = expr and (expr : T) parse and skip the type
source. Runtime no-op since SX is dynamic. Works in inline let,
top-level let, and parenthesised expressions:

  let x : int = 5 ;; x + 1                        -> 6
  let f (x : int) : int = x + 1 in f 41           -> 42
  (5 : int)                                       -> 5
  ((1 + 2) : int) * 3                             -> 9
This commit is contained in:
2026-05-08 20:58:50 +00:00
parent ce75bd6848
commit a4ef9a8ec9
3 changed files with 60 additions and 1 deletions

View File

@@ -422,7 +422,21 @@
(else
(let
((e (parse-expr)))
(begin (consume! "op" ")") e))))))
(begin
;; Optional type annotation `(e : T)` — skip
;; the type source before `)`.
(when (at-op? ":")
(begin
(advance-tok!)
(define skip-pty
(fn ()
(cond
((>= idx tok-len) nil)
((= (ocaml-tok-type (peek-tok)) "eof") nil)
((at-op? ")") nil)
(else (begin (advance-tok!) (skip-pty))))))
(skip-pty)))
(consume! "op" ")") e))))))
((and (= tt "op") (= tv "["))
(begin
(advance-tok!)
@@ -683,6 +697,18 @@
(when (not (= p nil))
(begin (append! ps p) (collect-params))))))
(collect-params)
;; Optional type annotation: skip `: TYPE` before `=`.
(when (at-op? ":")
(begin
(advance-tok!)
(define skip-tann
(fn ()
(cond
((>= idx tok-len) nil)
((= (ocaml-tok-type (peek-tok)) "eof") nil)
((at-op? "=") nil)
(else (begin (advance-tok!) (skip-tann))))))
(skip-tann)))
(consume! "op" "=")
(let ((rhs (parse-expr)))
(append! bindings (list nm ps rhs)))))))
@@ -1101,6 +1127,18 @@
(collect-params)))
(else nil))))
(collect-params)
;; Optional type annotation: skip `: TYPE` before `=`.
(when (at-op? ":")
(begin
(advance-tok!)
(define skip-tann
(fn ()
(cond
((>= idx tok-len) nil)
((= (ocaml-tok-type (peek-tok)) "eof") nil)
((at-op? "=") nil)
(else (begin (advance-tok!) (skip-tann))))))
(skip-tann)))
(consume! "op" "=")
(let ((expr-start (cur-pos)))
(begin

View File

@@ -1162,6 +1162,16 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 4601)
(eval "(ocaml-run-program \"type t = int;; 42\")")
;; ── Type annotations: let x : T = e and (e : T) ──────────────
(epoch 4700)
(eval "(ocaml-run-program \"let x : int = 5;; x + 1\")")
(epoch 4701)
(eval "(ocaml-run \"let f (x : int) : int = x + 1 in f 41\")")
(epoch 4702)
(eval "(ocaml-run \"(5 : int)\")")
(epoch 4703)
(eval "(ocaml-run \"((1 + 2) : int) * 3\")")
EPOCHS
OUTPUT=$(timeout 180 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -1841,6 +1851,12 @@ check 4502 "try when fall through" '1005'
check 4600 "type t = int parses" '("type-alias" "t" ())'
check 4601 "type alias decl + use" '42'
# ── Type annotations ───────────────────────────────────────────
check 4700 "let x : int = 5" '6'
check 4701 "let f (x : int) : int" '42'
check 4702 "(5 : int)" '5'
check 4703 "((1+2) : int) * 3" '9'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"