ocaml: numeric tower — Integer/Number distinction + float contagion
Add `Integer of int` to sx_types.ml alongside `Number of float`. Parser produces Integer for whole-number literals. Arithmetic primitives apply float contagion (int op int → Integer, int op float → Number). Division always returns Number. Rounding (floor/truncate/round) returns Integer. Predicates: integer?, float?, exact?, inexact?, exact->inexact, inexact->exact. run_tests.ml updated for json_of_value, value_of_json, identical?, random-int mock, DOM accessors, and parser pattern matches. New spec/tests/test-numeric-tower.sx — 92 tests, all pass (394 unchanged). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -37,7 +37,10 @@ let rec deep_equal a b =
|
||||
match a, b with
|
||||
| Nil, Nil -> true
|
||||
| Bool a, Bool b -> a = b
|
||||
| Integer a, Integer b -> a = b
|
||||
| Number a, Number b -> a = b
|
||||
| Integer a, Number b -> float_of_int a = b
|
||||
| Number a, Integer b -> a = float_of_int b
|
||||
| String a, String b -> a = b
|
||||
| Symbol a, Symbol b -> a = b
|
||||
| Keyword a, Keyword b -> a = b
|
||||
@@ -226,7 +229,7 @@ let make_test_env () =
|
||||
| [String s] ->
|
||||
let parsed = Sx_parser.parse_all s in
|
||||
(match parsed with
|
||||
| [List (Symbol "sxbc" :: Number _ :: payload :: _)] -> payload
|
||||
| [List (Symbol "sxbc" :: (Number _ | Integer _) :: payload :: _)] -> payload
|
||||
| _ -> raise (Eval_error "bytecode-deserialize: invalid sxbc format"))
|
||||
| _ -> raise (Eval_error "bytecode-deserialize: expected string"));
|
||||
|
||||
@@ -240,7 +243,7 @@ let make_test_env () =
|
||||
| [String s] ->
|
||||
let parsed = Sx_parser.parse_all s in
|
||||
(match parsed with
|
||||
| [List (Symbol "cek-state" :: Number _ :: payload :: _)] -> payload
|
||||
| [List (Symbol "cek-state" :: (Number _ | Integer _) :: payload :: _)] -> payload
|
||||
| _ -> raise (Eval_error "cek-deserialize: invalid cek-state format"))
|
||||
| _ -> raise (Eval_error "cek-deserialize: expected string"));
|
||||
|
||||
@@ -320,7 +323,10 @@ let make_test_env () =
|
||||
bind "identical?" (fun args ->
|
||||
match args with
|
||||
| [a; b] -> Bool (match a, b with
|
||||
| Integer x, Integer y -> x = y
|
||||
| Number x, Number y -> x = y
|
||||
| Integer x, Number y -> float_of_int x = y
|
||||
| Number x, Integer y -> x = float_of_int y
|
||||
| String x, String y -> x = y
|
||||
| Bool x, Bool y -> x = y
|
||||
| Nil, Nil -> true
|
||||
@@ -366,11 +372,15 @@ let make_test_env () =
|
||||
|
||||
bind "append!" (fun args ->
|
||||
match args with
|
||||
| [ListRef r; v; Number n] when int_of_float n = 0 ->
|
||||
| [ListRef r; v; (Number n)] when int_of_float n = 0 ->
|
||||
r := v :: !r; ListRef r (* prepend *)
|
||||
| [ListRef r; v; (Integer 0)] ->
|
||||
r := v :: !r; ListRef r (* prepend Integer index *)
|
||||
| [ListRef r; v] -> r := !r @ [v]; ListRef r (* append in place *)
|
||||
| [List items; v; Number n] when int_of_float n = 0 ->
|
||||
| [List items; v; (Number n)] when int_of_float n = 0 ->
|
||||
List (v :: items) (* immutable prepend *)
|
||||
| [List items; v; (Integer 0)] ->
|
||||
List (v :: items) (* immutable prepend Integer index *)
|
||||
| [List items; v] -> List (items @ [v]) (* immutable fallback *)
|
||||
| _ -> raise (Eval_error "append!: expected list and value"));
|
||||
|
||||
@@ -546,7 +556,10 @@ let make_test_env () =
|
||||
bind "batch-begin!" (fun _args -> Sx_ref.batch_begin_b ());
|
||||
bind "batch-end!" (fun _args -> Sx_ref.batch_end_b ());
|
||||
bind "now-ms" (fun _args -> Number 1000.0);
|
||||
bind "random-int" (fun args -> match args with [Number lo; _] -> Number lo | _ -> Number 0.0);
|
||||
bind "random-int" (fun args -> match args with
|
||||
| [Number lo; _] -> Number lo
|
||||
| [Integer lo; _] -> Integer lo
|
||||
| _ -> Integer 0);
|
||||
bind "try-rerender-page" (fun _args -> Nil);
|
||||
bind "collect!" (fun args ->
|
||||
match args with
|
||||
@@ -1142,18 +1155,20 @@ let run_foundation_tests () =
|
||||
in
|
||||
|
||||
Printf.printf "Suite: parser\n";
|
||||
assert_eq "number" (Number 42.0) (List.hd (parse_all "42"));
|
||||
assert_eq "number" (Integer 42) (List.hd (parse_all "42"));
|
||||
assert_eq "string" (String "hello") (List.hd (parse_all "\"hello\""));
|
||||
assert_eq "bool true" (Bool true) (List.hd (parse_all "true"));
|
||||
assert_eq "nil" Nil (List.hd (parse_all "nil"));
|
||||
assert_eq "keyword" (Keyword "class") (List.hd (parse_all ":class"));
|
||||
assert_eq "symbol" (Symbol "foo") (List.hd (parse_all "foo"));
|
||||
assert_eq "list" (List [Symbol "+"; Number 1.0; Number 2.0]) (List.hd (parse_all "(+ 1 2)"));
|
||||
assert_eq "list" (List [Symbol "+"; Integer 1; Integer 2]) (List.hd (parse_all "(+ 1 2)"));
|
||||
(match List.hd (parse_all "(div :class \"card\" (p \"hi\"))") with
|
||||
| List [Symbol "div"; Keyword "class"; String "card"; List [Symbol "p"; String "hi"]] ->
|
||||
incr pass_count; Printf.printf " PASS: nested list\n"
|
||||
| v -> incr fail_count; Printf.printf " FAIL: nested list — got %s\n" (Sx_types.inspect v));
|
||||
(match List.hd (parse_all "'(1 2 3)") with
|
||||
| List [Symbol "quote"; List [Integer 1; Integer 2; Integer 3]] ->
|
||||
incr pass_count; Printf.printf " PASS: quote sugar\n"
|
||||
| List [Symbol "quote"; List [Number 1.0; Number 2.0; Number 3.0]] ->
|
||||
incr pass_count; Printf.printf " PASS: quote sugar\n"
|
||||
| v -> incr fail_count; Printf.printf " FAIL: quote sugar — got %s\n" (Sx_types.inspect v));
|
||||
@@ -1161,7 +1176,7 @@ let run_foundation_tests () =
|
||||
| Dict d when dict_has d "a" && dict_has d "b" ->
|
||||
incr pass_count; Printf.printf " PASS: dict literal\n"
|
||||
| v -> incr fail_count; Printf.printf " FAIL: dict literal — got %s\n" (Sx_types.inspect v));
|
||||
assert_eq "comment" (Number 42.0) (List.hd (parse_all ";; comment\n42"));
|
||||
assert_eq "comment" (Integer 42) (List.hd (parse_all ";; comment\n42"));
|
||||
assert_eq "string escape" (String "hello\nworld") (List.hd (parse_all "\"hello\\nworld\""));
|
||||
assert_eq "multiple exprs" (Number 2.0) (Number (float_of_int (List.length (parse_all "(1 2 3) (4 5)"))));
|
||||
|
||||
@@ -1978,6 +1993,10 @@ let run_spec_tests env test_files =
|
||||
(match Hashtbl.find_opt d "children" with
|
||||
| Some (List l) when i >= 0 && i < List.length l -> List.nth l i
|
||||
| _ -> (match Hashtbl.find_opt d (string_of_int i) with Some v -> v | None -> Nil))
|
||||
| [Dict d; Integer n] ->
|
||||
(match Hashtbl.find_opt d "children" with
|
||||
| Some (List l) when n >= 0 && n < List.length l -> List.nth l n
|
||||
| _ -> (match Hashtbl.find_opt d (string_of_int n) with Some v -> v | None -> Nil))
|
||||
| _ -> Nil);
|
||||
|
||||
(* Stringify a value for DOM string properties *)
|
||||
@@ -2052,8 +2071,8 @@ let run_spec_tests env test_files =
|
||||
Hashtbl.replace d "childNodes" (List [])
|
||||
| _ -> ());
|
||||
stored
|
||||
| [ListRef r; Number n; value] ->
|
||||
let idx = int_of_float n in
|
||||
| [ListRef r; idx_v; value] when (match idx_v with Number _ | Integer _ -> true | _ -> false) ->
|
||||
let idx = match idx_v with Number n -> int_of_float n | Integer n -> n | _ -> 0 in
|
||||
let lst = !r in
|
||||
if idx >= 0 && idx < List.length lst then
|
||||
r := List.mapi (fun i v -> if i = idx then value else v) lst
|
||||
@@ -2190,7 +2209,7 @@ let run_spec_tests env test_files =
|
||||
| [String name; value] ->
|
||||
let attrs = match Hashtbl.find_opt d "attributes" with Some (Dict a) -> a | _ ->
|
||||
let a = Hashtbl.create 4 in Hashtbl.replace d "attributes" (Dict a); a in
|
||||
let sv = match value with String s -> s | Number n ->
|
||||
let sv = match value with String s -> s | Integer n -> string_of_int n | Number n ->
|
||||
let i = int_of_float n in if float_of_int i = n then string_of_int i
|
||||
else string_of_float n | _ -> Sx_types.inspect value in
|
||||
Hashtbl.replace attrs name (String sv);
|
||||
@@ -2632,6 +2651,7 @@ let run_spec_tests env test_files =
|
||||
let rec json_of_value = function
|
||||
| Nil -> `Null
|
||||
| Bool b -> `Bool b
|
||||
| Integer n -> `Int n
|
||||
| Number n ->
|
||||
if Float.is_integer n && Float.abs n < 1e16
|
||||
then `Int (int_of_float n) else `Float n
|
||||
@@ -2647,8 +2667,8 @@ let run_spec_tests env test_files =
|
||||
let rec value_of_json = function
|
||||
| `Null -> Nil
|
||||
| `Bool b -> Bool b
|
||||
| `Int i -> Number (float_of_int i)
|
||||
| `Intlit s -> (try Number (float_of_string s) with _ -> String s)
|
||||
| `Int i -> Integer i
|
||||
| `Intlit s -> (try Integer (int_of_string s) with _ -> try Number (float_of_string s) with _ -> String s)
|
||||
| `Float f -> Number f
|
||||
| `String s -> String s
|
||||
| `List xs -> List (List.map value_of_json xs)
|
||||
|
||||
Reference in New Issue
Block a user