spec: format — CL-style string formatting (~a ~s ~d ~x ~o ~b ~f ~% ~& ~~ ~t)
28 tests, passes on both JS and OCaml. - spec/stdlib.sx: pure SX format function - spec/primitives.sx: format primitive declaration - lib/r7rs.sx: fix number->string to support optional radix arg - hosts/ocaml: add format-decimal primitive, load stdlib.sx in test runner - hosts/javascript: load stdlib.sx in test runner Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -344,7 +344,7 @@ if (fs.existsSync(swapPath)) {
|
||||
}
|
||||
|
||||
// Load spec library files (define-library modules imported by tests)
|
||||
for (const libFile of ["signals.sx", "coroutines.sx"]) {
|
||||
for (const libFile of ["stdlib.sx", "signals.sx", "coroutines.sx"]) {
|
||||
const libPath = path.join(projectDir, "spec", libFile);
|
||||
if (fs.existsSync(libPath)) {
|
||||
const libSrc = fs.readFileSync(libPath, "utf8");
|
||||
|
||||
@@ -2872,6 +2872,7 @@ let run_spec_tests env test_files =
|
||||
match sx_vm_execute with
|
||||
| Some fn -> Sx_ref.cek_call fn (List args)
|
||||
| None -> Nil)));
|
||||
load_module "stdlib.sx" spec_dir; (* pure SX stdlib: format etc. *)
|
||||
load_module "signals.sx" spec_dir; (* core reactive primitives *)
|
||||
load_module "signals.sx" web_dir; (* web extensions *)
|
||||
load_module "freeze.sx" lib_dir;
|
||||
|
||||
@@ -2806,6 +2806,13 @@ let () =
|
||||
match args with
|
||||
| [v] -> String (sx_display_val v)
|
||||
| _ -> raise (Eval_error "display-to-string: 1 arg"));
|
||||
register "format-decimal" (fun args ->
|
||||
match args with
|
||||
| [Integer n; Integer prec] -> String (Printf.sprintf "%.*f" prec (float_of_int n))
|
||||
| [Number n; Integer prec] -> String (Printf.sprintf "%.*f" prec n)
|
||||
| [Integer n; _] -> String (Printf.sprintf "%.6f" (float_of_int n))
|
||||
| [Number n; _] -> String (Printf.sprintf "%.6f" n)
|
||||
| _ -> raise (Eval_error "format-decimal: expected number precision"));
|
||||
register "current-input-port" (fun _ -> Nil);
|
||||
register "current-output-port" (fun _ -> Nil);
|
||||
register "current-error-port" (fun _ -> Nil);
|
||||
|
||||
Reference in New Issue
Block a user