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
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:
@@ -422,7 +422,21 @@
|
|||||||
(else
|
(else
|
||||||
(let
|
(let
|
||||||
((e (parse-expr)))
|
((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 "["))
|
((and (= tt "op") (= tv "["))
|
||||||
(begin
|
(begin
|
||||||
(advance-tok!)
|
(advance-tok!)
|
||||||
@@ -683,6 +697,18 @@
|
|||||||
(when (not (= p nil))
|
(when (not (= p nil))
|
||||||
(begin (append! ps p) (collect-params))))))
|
(begin (append! ps p) (collect-params))))))
|
||||||
(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" "=")
|
(consume! "op" "=")
|
||||||
(let ((rhs (parse-expr)))
|
(let ((rhs (parse-expr)))
|
||||||
(append! bindings (list nm ps rhs)))))))
|
(append! bindings (list nm ps rhs)))))))
|
||||||
@@ -1101,6 +1127,18 @@
|
|||||||
(collect-params)))
|
(collect-params)))
|
||||||
(else nil))))
|
(else nil))))
|
||||||
(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" "=")
|
(consume! "op" "=")
|
||||||
(let ((expr-start (cur-pos)))
|
(let ((expr-start (cur-pos)))
|
||||||
(begin
|
(begin
|
||||||
|
|||||||
@@ -1162,6 +1162,16 @@ cat > "$TMPFILE" << 'EPOCHS'
|
|||||||
(epoch 4601)
|
(epoch 4601)
|
||||||
(eval "(ocaml-run-program \"type t = int;; 42\")")
|
(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
|
EPOCHS
|
||||||
|
|
||||||
OUTPUT=$(timeout 180 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
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 4600 "type t = int parses" '("type-alias" "t" ())'
|
||||||
check 4601 "type alias decl + use" '42'
|
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))
|
TOTAL=$((PASS + FAIL))
|
||||||
if [ $FAIL -eq 0 ]; then
|
if [ $FAIL -eq 0 ]; then
|
||||||
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
||||||
|
|||||||
@@ -407,6 +407,11 @@ _Newest first._
|
|||||||
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
||||||
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
||||||
recursive match, List.append, List.fold_left.
|
recursive match, List.append, List.fold_left.
|
||||||
|
- 2026-05-08 Phase 1 — type annotations on let-bindings and parens
|
||||||
|
expressions (+4 tests, 473 total). `let NAME [PARAMS] : T = expr`
|
||||||
|
and `(expr : T)` parse and skip the type source. Runtime no-op
|
||||||
|
(dynamic). Works in inline let, top-level let, and parenthesised
|
||||||
|
expressions: `let f (x : int) : int = x + 1 in f 41`.
|
||||||
- 2026-05-08 Phase 1+5.1 — type aliases + poly_stack baseline (+3
|
- 2026-05-08 Phase 1+5.1 — type aliases + poly_stack baseline (+3
|
||||||
tests, 469 total + 19 baseline). Parser dispatch on the post-`=`
|
tests, 469 total + 19 baseline). Parser dispatch on the post-`=`
|
||||||
token: `|` or `Ctor` → sum, `{` → record, otherwise → alias (skip
|
token: `|` or `Ctor` → sum, `{` → record, otherwise → alias (skip
|
||||||
|
|||||||
Reference in New Issue
Block a user