spec: gensym + symbol interning (OCaml + tests)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
gensym_counter ref + gensym/string->symbol/symbol->string/intern/symbol-interned? primitives in sx_primitives.ml. Fix ListRef case in seq_to_list on both sx_ref.ml and sx_primitives.ml. 19 new tests in test-gensym.sx. OCaml 4450/1080, JS 2205/2497, zero regressions. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -99,6 +99,8 @@ let rec to_string = function
|
||||
| RawHTML s -> s
|
||||
| v -> inspect v
|
||||
|
||||
let gensym_counter = ref 0
|
||||
|
||||
let () =
|
||||
(* === Arithmetic === *)
|
||||
register "+" (fun args ->
|
||||
@@ -2118,6 +2120,7 @@ let () =
|
||||
match v with
|
||||
| Nil -> List []
|
||||
| List _ -> v
|
||||
| ListRef { contents = items } -> List items
|
||||
| Vector arr -> List (Array.to_list arr)
|
||||
| String s ->
|
||||
let chars = ref [] in
|
||||
@@ -2197,4 +2200,29 @@ let () =
|
||||
if (step > 0 && i >= hi) || (step < 0 && i <= hi) then acc
|
||||
else build (i + step) (Integer i :: acc) in
|
||||
List (List.rev (build lo []))
|
||||
| _ -> raise (Eval_error "in-range: expected (end) or (start end) or (start end step)"))
|
||||
| _ -> raise (Eval_error "in-range: expected (end) or (start end) or (start end step)"));
|
||||
(* === gensym + symbol interning === *)
|
||||
register "gensym" (fun args ->
|
||||
let prefix = match args with
|
||||
| [] -> "g"
|
||||
| [String s] -> s
|
||||
| [Symbol s] -> s
|
||||
| _ -> raise (Eval_error "gensym: expected optional prefix string") in
|
||||
incr gensym_counter;
|
||||
Symbol (prefix ^ string_of_int !gensym_counter));
|
||||
register "string->symbol" (fun args ->
|
||||
match args with
|
||||
| [String s] -> Symbol s
|
||||
| _ -> raise (Eval_error "string->symbol: expected 1 string"));
|
||||
register "symbol->string" (fun args ->
|
||||
match args with
|
||||
| [Symbol s] -> String s
|
||||
| _ -> raise (Eval_error "symbol->string: expected 1 symbol"));
|
||||
register "intern" (fun args ->
|
||||
match args with
|
||||
| [String s] -> Symbol s
|
||||
| _ -> raise (Eval_error "intern: expected 1 string"));
|
||||
register "symbol-interned?" (fun args ->
|
||||
match args with
|
||||
| [Symbol _] -> Bool true
|
||||
| _ -> raise (Eval_error "symbol-interned?: expected 1 symbol"))
|
||||
|
||||
@@ -29,6 +29,7 @@ let seq_to_list v =
|
||||
match v with
|
||||
| Nil -> List []
|
||||
| List _ -> v
|
||||
| ListRef { contents = items } -> List items
|
||||
| Vector arr -> List (Array.to_list arr)
|
||||
| String s ->
|
||||
let chars = ref [] in
|
||||
|
||||
Reference in New Issue
Block a user