1use crate::types::{Effect, SideEffect, StackType, Type};
12use std::collections::HashMap;
13use std::sync::LazyLock;
14
15macro_rules! ty {
17 (Int) => {
18 Type::Int
19 };
20 (Bool) => {
21 Type::Bool
22 };
23 (String) => {
24 Type::String
25 };
26 (Float) => {
27 Type::Float
28 };
29 (Symbol) => {
30 Type::Symbol
31 };
32 (Channel) => {
33 Type::Channel
34 };
35 (T) => {
37 Type::Var("T".to_string())
38 };
39 (U) => {
40 Type::Var("U".to_string())
41 };
42 (V) => {
43 Type::Var("V".to_string())
44 };
45 (W) => {
46 Type::Var("W".to_string())
47 };
48 (K) => {
49 Type::Var("K".to_string())
50 };
51 (M) => {
52 Type::Var("M".to_string())
53 };
54 (Q) => {
55 Type::Var("Q".to_string())
56 };
57 (T1) => {
59 Type::Var("T1".to_string())
60 };
61 (T2) => {
62 Type::Var("T2".to_string())
63 };
64 (T3) => {
65 Type::Var("T3".to_string())
66 };
67 (T4) => {
68 Type::Var("T4".to_string())
69 };
70 (V2) => {
71 Type::Var("V2".to_string())
72 };
73 (M2) => {
74 Type::Var("M2".to_string())
75 };
76 (Acc) => {
77 Type::Var("Acc".to_string())
78 };
79}
80
81macro_rules! stack {
83 (a) => {
85 StackType::RowVar("a".to_string())
86 };
87 (a $t1:tt) => {
89 StackType::RowVar("a".to_string()).push(ty!($t1))
90 };
91 (a $t1:tt $t2:tt) => {
93 StackType::RowVar("a".to_string())
94 .push(ty!($t1))
95 .push(ty!($t2))
96 };
97 (a $t1:tt $t2:tt $t3:tt) => {
99 StackType::RowVar("a".to_string())
100 .push(ty!($t1))
101 .push(ty!($t2))
102 .push(ty!($t3))
103 };
104 (a $t1:tt $t2:tt $t3:tt $t4:tt) => {
106 StackType::RowVar("a".to_string())
107 .push(ty!($t1))
108 .push(ty!($t2))
109 .push(ty!($t3))
110 .push(ty!($t4))
111 };
112 (a $t1:tt $t2:tt $t3:tt $t4:tt $t5:tt) => {
114 StackType::RowVar("a".to_string())
115 .push(ty!($t1))
116 .push(ty!($t2))
117 .push(ty!($t3))
118 .push(ty!($t4))
119 .push(ty!($t5))
120 };
121 (b) => {
123 StackType::RowVar("b".to_string())
124 };
125 (b $t1:tt) => {
126 StackType::RowVar("b".to_string()).push(ty!($t1))
127 };
128 (b $t1:tt $t2:tt) => {
129 StackType::RowVar("b".to_string())
130 .push(ty!($t1))
131 .push(ty!($t2))
132 };
133}
134
135macro_rules! builtin {
139 ($sigs:ident, $name:expr, (a -- a)) => {
141 $sigs.insert($name.to_string(), Effect::new(stack!(a), stack!(a)));
142 };
143 ($sigs:ident, $name:expr, (a -- a $o1:tt)) => {
145 $sigs.insert($name.to_string(), Effect::new(stack!(a), stack!(a $o1)));
146 };
147 ($sigs:ident, $name:expr, (a -- a $o1:tt $o2:tt)) => {
149 $sigs.insert($name.to_string(), Effect::new(stack!(a), stack!(a $o1 $o2)));
150 };
151 ($sigs:ident, $name:expr, (a $i1:tt -- a)) => {
153 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1), stack!(a)));
154 };
155 ($sigs:ident, $name:expr, (a $i1:tt -- a $o1:tt)) => {
157 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1), stack!(a $o1)));
158 };
159 ($sigs:ident, $name:expr, (a $i1:tt -- a $o1:tt $o2:tt)) => {
161 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1), stack!(a $o1 $o2)));
162 };
163 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt -- a)) => {
165 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2), stack!(a)));
166 };
167 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt -- a $o1:tt)) => {
169 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2), stack!(a $o1)));
170 };
171 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt -- a $o1:tt $o2:tt)) => {
173 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2), stack!(a $o1 $o2)));
174 };
175 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt -- a $o1:tt $o2:tt $o3:tt)) => {
177 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2), stack!(a $o1 $o2 $o3)));
178 };
179 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt -- a $o1:tt $o2:tt $o3:tt $o4:tt)) => {
181 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2), stack!(a $o1 $o2 $o3 $o4)));
182 };
183 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt $i3:tt -- a)) => {
185 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2 $i3), stack!(a)));
186 };
187 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt $i3:tt -- a $o1:tt)) => {
189 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2 $i3), stack!(a $o1)));
190 };
191 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt $i3:tt -- a $o1:tt $o2:tt)) => {
193 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2 $i3), stack!(a $o1 $o2)));
194 };
195 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt $i3:tt -- a $o1:tt $o2:tt $o3:tt)) => {
197 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2 $i3), stack!(a $o1 $o2 $o3)));
198 };
199 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt $i3:tt $i4:tt -- a $o1:tt)) => {
201 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2 $i3 $i4), stack!(a $o1)));
202 };
203 ($sigs:ident, $name:expr, (a $i1:tt $i2:tt $i3:tt $i4:tt $i5:tt -- a $o1:tt)) => {
205 $sigs.insert($name.to_string(), Effect::new(stack!(a $i1 $i2 $i3 $i4 $i5), stack!(a $o1)));
206 };
207}
208
209macro_rules! builtins_int_int_to_int {
212 ($sigs:ident, $($name:expr),+ $(,)?) => {
213 $(
214 builtin!($sigs, $name, (a Int Int -- a Int));
215 )+
216 };
217}
218
219macro_rules! builtins_int_int_to_bool {
220 ($sigs:ident, $($name:expr),+ $(,)?) => {
221 $(
222 builtin!($sigs, $name, (a Int Int -- a Bool));
223 )+
224 };
225}
226
227macro_rules! builtins_bool_bool_to_bool {
228 ($sigs:ident, $($name:expr),+ $(,)?) => {
229 $(
230 builtin!($sigs, $name, (a Bool Bool -- a Bool));
231 )+
232 };
233}
234
235macro_rules! builtins_int_to_int {
236 ($sigs:ident, $($name:expr),+ $(,)?) => {
237 $(
238 builtin!($sigs, $name, (a Int -- a Int));
239 )+
240 };
241}
242
243macro_rules! builtins_string_to_string {
244 ($sigs:ident, $($name:expr),+ $(,)?) => {
245 $(
246 builtin!($sigs, $name, (a String -- a String));
247 )+
248 };
249}
250
251macro_rules! builtins_float_float_to_float {
252 ($sigs:ident, $($name:expr),+ $(,)?) => {
253 $(
254 builtin!($sigs, $name, (a Float Float -- a Float));
255 )+
256 };
257}
258
259macro_rules! builtins_float_float_to_bool {
260 ($sigs:ident, $($name:expr),+ $(,)?) => {
261 $(
262 builtin!($sigs, $name, (a Float Float -- a Bool));
263 )+
264 };
265}
266
267pub fn builtin_signature(name: &str) -> Option<Effect> {
269 let signatures = builtin_signatures();
270 signatures.get(name).cloned()
271}
272
273pub fn builtin_signatures() -> HashMap<String, Effect> {
275 let mut sigs = HashMap::new();
276
277 builtin!(sigs, "io.write", (a String -- a)); builtin!(sigs, "io.write-line", (a String -- a));
283 builtin!(sigs, "io.read-line", (a -- a String Bool)); builtin!(sigs, "io.read-line+", (a -- a String Int)); builtin!(sigs, "io.read-n", (a Int -- a String Int)); builtin!(sigs, "args.count", (a -- a Int));
292 builtin!(sigs, "args.at", (a Int -- a String));
293
294 builtin!(sigs, "file.slurp", (a String -- a String Bool)); builtin!(sigs, "file.exists?", (a String -- a Bool));
300
301 sigs.insert(
303 "file.for-each-line+".to_string(),
304 Effect::new(
305 StackType::RowVar("a".to_string())
306 .push(Type::String)
307 .push(Type::Quotation(Box::new(Effect::new(
308 StackType::RowVar("a".to_string()).push(Type::String),
309 StackType::RowVar("a".to_string()),
310 )))),
311 StackType::RowVar("a".to_string())
312 .push(Type::String)
313 .push(Type::Bool),
314 ),
315 );
316
317 builtin!(sigs, "int->string", (a Int -- a String));
322 builtin!(sigs, "int->float", (a Int -- a Float));
323 builtin!(sigs, "float->int", (a Float -- a Int));
324 builtin!(sigs, "float->string", (a Float -- a String));
325 builtin!(sigs, "string->int", (a String -- a Int Bool)); builtin!(sigs, "string->float", (a String -- a Float Bool)); builtin!(sigs, "char->string", (a Int -- a String));
328 builtin!(sigs, "symbol->string", (a Symbol -- a String));
329 builtin!(sigs, "string->symbol", (a String -- a Symbol));
330
331 builtins_int_int_to_int!(
336 sigs,
337 "i.add",
338 "i.subtract",
339 "i.multiply",
340 "i.divide",
341 "i.modulo"
342 );
343 builtins_int_int_to_int!(sigs, "i.+", "i.-", "i.*", "i./", "i.%");
344
345 builtins_int_int_to_bool!(sigs, "i.=", "i.<", "i.>", "i.<=", "i.>=", "i.<>");
350 builtins_int_int_to_bool!(sigs, "i.eq", "i.lt", "i.gt", "i.lte", "i.gte", "i.neq");
351
352 builtins_bool_bool_to_bool!(sigs, "and", "or");
357 builtin!(sigs, "not", (a Bool -- a Bool));
358
359 builtins_int_int_to_int!(sigs, "band", "bor", "bxor", "shl", "shr");
364 builtins_int_to_int!(sigs, "bnot", "popcount", "clz", "ctz");
365 builtin!(sigs, "int-bits", (a -- a Int));
366
367 builtin!(sigs, "dup", (a T -- a T T));
372 builtin!(sigs, "drop", (a T -- a));
373 builtin!(sigs, "swap", (a T U -- a U T));
374 builtin!(sigs, "over", (a T U -- a T U T));
375 builtin!(sigs, "rot", (a T U V -- a U V T));
376 builtin!(sigs, "nip", (a T U -- a U));
377 builtin!(sigs, "tuck", (a T U -- a U T U));
378 builtin!(sigs, "2dup", (a T U -- a T U T U));
379 builtin!(sigs, "3drop", (a T U V -- a));
380
381 builtin!(sigs, "pick", (a T Int -- a T T));
384 builtin!(sigs, "roll", (a T Int -- a T));
386
387 builtin!(sigs, "chan.make", (a -- a Channel));
393 builtin!(sigs, "chan.send", (a T Channel -- a Bool)); builtin!(sigs, "chan.receive", (a Channel -- a T Bool)); builtin!(sigs, "chan.close", (a Channel -- a));
396 builtin!(sigs, "chan.yield", (a - -a));
397
398 sigs.insert(
405 "call".to_string(),
406 Effect::new(
407 StackType::RowVar("a".to_string()).push(Type::Var("Q".to_string())),
408 StackType::RowVar("b".to_string()),
409 ),
410 );
411
412 sigs.insert(
414 "cond".to_string(),
415 Effect::new(
416 StackType::RowVar("a".to_string()),
417 StackType::RowVar("b".to_string()),
418 ),
419 );
420
421 sigs.insert(
424 "strand.spawn".to_string(),
425 Effect::new(
426 StackType::RowVar("a".to_string()).push(Type::Quotation(Box::new(Effect::new(
427 StackType::RowVar("spawn_in".to_string()),
428 StackType::RowVar("spawn_out".to_string()),
429 )))),
430 StackType::RowVar("a".to_string()).push(Type::Int),
431 ),
432 );
433
434 sigs.insert(
438 "strand.weave".to_string(),
439 Effect::new(
440 StackType::RowVar("a".to_string()).push(Type::Quotation(Box::new(Effect::new(
441 StackType::RowVar("weave_in".to_string()),
442 StackType::RowVar("weave_out".to_string()),
443 )))),
444 StackType::RowVar("a".to_string()).push(Type::Var("handle".to_string())),
445 ),
446 );
447
448 sigs.insert(
451 "strand.resume".to_string(),
452 Effect::new(
453 StackType::RowVar("a".to_string())
454 .push(Type::Var("handle".to_string()))
455 .push(Type::Var("b".to_string())),
456 StackType::RowVar("a".to_string())
457 .push(Type::Var("handle".to_string()))
458 .push(Type::Var("b".to_string()))
459 .push(Type::Bool),
460 ),
461 );
462
463 sigs.insert(
467 "yield".to_string(),
468 Effect::with_effects(
469 StackType::RowVar("a".to_string())
470 .push(Type::Var("ctx".to_string()))
471 .push(Type::Var("b".to_string())),
472 StackType::RowVar("a".to_string())
473 .push(Type::Var("ctx".to_string()))
474 .push(Type::Var("b".to_string())),
475 vec![SideEffect::Yield(Box::new(Type::Var("b".to_string())))],
476 ),
477 );
478
479 sigs.insert(
483 "strand.weave-cancel".to_string(),
484 Effect::new(
485 StackType::RowVar("a".to_string()).push(Type::Var("handle".to_string())),
486 StackType::RowVar("a".to_string()),
487 ),
488 );
489
490 builtin!(sigs, "tcp.listen", (a Int -- a Int));
495 builtin!(sigs, "tcp.accept", (a Int -- a Int));
496 builtin!(sigs, "tcp.read", (a Int -- a String));
497 builtin!(sigs, "tcp.write", (a String Int -- a));
498 builtin!(sigs, "tcp.close", (a Int -- a));
499
500 builtin!(sigs, "os.getenv", (a String -- a String Bool));
505 builtin!(sigs, "os.home-dir", (a -- a String Bool));
506 builtin!(sigs, "os.current-dir", (a -- a String Bool));
507 builtin!(sigs, "os.path-exists", (a String -- a Bool));
508 builtin!(sigs, "os.path-is-file", (a String -- a Bool));
509 builtin!(sigs, "os.path-is-dir", (a String -- a Bool));
510 builtin!(sigs, "os.path-join", (a String String -- a String));
511 builtin!(sigs, "os.path-parent", (a String -- a String Bool));
512 builtin!(sigs, "os.path-filename", (a String -- a String Bool));
513 builtin!(sigs, "os.exit", (a Int -- a)); builtin!(sigs, "os.name", (a -- a String));
515 builtin!(sigs, "os.arch", (a -- a String));
516
517 builtin!(sigs, "signal.trap", (a Int -- a));
522 builtin!(sigs, "signal.received?", (a Int -- a Bool));
523 builtin!(sigs, "signal.pending?", (a Int -- a Bool));
524 builtin!(sigs, "signal.default", (a Int -- a));
525 builtin!(sigs, "signal.ignore", (a Int -- a));
526 builtin!(sigs, "signal.clear", (a Int -- a));
527 builtin!(sigs, "signal.SIGINT", (a -- a Int));
529 builtin!(sigs, "signal.SIGTERM", (a -- a Int));
530 builtin!(sigs, "signal.SIGHUP", (a -- a Int));
531 builtin!(sigs, "signal.SIGPIPE", (a -- a Int));
532 builtin!(sigs, "signal.SIGUSR1", (a -- a Int));
533 builtin!(sigs, "signal.SIGUSR2", (a -- a Int));
534 builtin!(sigs, "signal.SIGCHLD", (a -- a Int));
535 builtin!(sigs, "signal.SIGALRM", (a -- a Int));
536 builtin!(sigs, "signal.SIGCONT", (a -- a Int));
537
538 builtin!(sigs, "terminal.raw-mode", (a Bool -- a));
543 builtin!(sigs, "terminal.read-char", (a -- a Int));
544 builtin!(sigs, "terminal.read-char?", (a -- a Int));
545 builtin!(sigs, "terminal.width", (a -- a Int));
546 builtin!(sigs, "terminal.height", (a -- a Int));
547 builtin!(sigs, "terminal.flush", (a - -a));
548
549 builtin!(sigs, "string.concat", (a String String -- a String));
554 builtin!(sigs, "string.length", (a String -- a Int));
555 builtin!(sigs, "string.byte-length", (a String -- a Int));
556 builtin!(sigs, "string.char-at", (a String Int -- a Int));
557 builtin!(sigs, "string.substring", (a String Int Int -- a String));
558 builtin!(sigs, "string.find", (a String String -- a Int));
559 builtin!(sigs, "string.split", (a String String -- a V)); builtin!(sigs, "string.contains", (a String String -- a Bool));
561 builtin!(sigs, "string.starts-with", (a String String -- a Bool));
562 builtin!(sigs, "string.empty?", (a String -- a Bool));
563 builtin!(sigs, "string.equal?", (a String String -- a Bool));
564
565 builtin!(sigs, "symbol.=", (a Symbol Symbol -- a Bool));
567
568 builtins_string_to_string!(
570 sigs,
571 "string.trim",
572 "string.chomp",
573 "string.to-upper",
574 "string.to-lower",
575 "string.json-escape"
576 );
577
578 builtin!(sigs, "encoding.base64-encode", (a String -- a String));
583 builtin!(sigs, "encoding.base64-decode", (a String -- a String Bool));
584 builtin!(sigs, "encoding.base64url-encode", (a String -- a String));
585 builtin!(sigs, "encoding.base64url-decode", (a String -- a String Bool));
586 builtin!(sigs, "encoding.hex-encode", (a String -- a String));
587 builtin!(sigs, "encoding.hex-decode", (a String -- a String Bool));
588
589 builtin!(sigs, "crypto.sha256", (a String -- a String));
594 builtin!(sigs, "crypto.hmac-sha256", (a String String -- a String));
595 builtin!(sigs, "crypto.constant-time-eq", (a String String -- a Bool));
596 builtin!(sigs, "crypto.random-bytes", (a Int -- a String));
597 builtin!(sigs, "crypto.random-int", (a Int Int -- a Int));
598 builtin!(sigs, "crypto.uuid4", (a -- a String));
599 builtin!(sigs, "crypto.aes-gcm-encrypt", (a String String -- a String Bool));
600 builtin!(sigs, "crypto.aes-gcm-decrypt", (a String String -- a String Bool));
601 builtin!(sigs, "crypto.pbkdf2-sha256", (a String String Int -- a String Bool));
602 builtin!(sigs, "crypto.ed25519-keypair", (a -- a String String));
603 builtin!(sigs, "crypto.ed25519-sign", (a String String -- a String Bool));
604 builtin!(sigs, "crypto.ed25519-verify", (a String String String -- a Bool));
605
606 builtin!(sigs, "http.get", (a String -- a M));
611 builtin!(sigs, "http.post", (a String String String -- a M));
612 builtin!(sigs, "http.put", (a String String String -- a M));
613 builtin!(sigs, "http.delete", (a String -- a M));
614
615 builtin!(sigs, "regex.match?", (a String String -- a Bool));
620 builtin!(sigs, "regex.find", (a String String -- a String Bool));
621 builtin!(sigs, "regex.find-all", (a String String -- a V));
622 builtin!(sigs, "regex.replace", (a String String String -- a String));
623 builtin!(sigs, "regex.replace-all", (a String String String -- a String));
624 builtin!(sigs, "regex.captures", (a String String -- a V Bool));
625 builtin!(sigs, "regex.split", (a String String -- a V));
626 builtin!(sigs, "regex.valid?", (a String -- a Bool));
627
628 builtin!(sigs, "compress.gzip", (a String -- a String Bool));
633 builtin!(sigs, "compress.gzip-level", (a String Int -- a String Bool));
634 builtin!(sigs, "compress.gunzip", (a String -- a String Bool));
635 builtin!(sigs, "compress.zstd", (a String -- a String Bool));
636 builtin!(sigs, "compress.zstd-level", (a String Int -- a String Bool));
637 builtin!(sigs, "compress.unzstd", (a String -- a String Bool));
638
639 builtin!(sigs, "variant.field-count", (a V -- a Int));
644 builtin!(sigs, "variant.tag", (a V -- a Symbol));
645 builtin!(sigs, "variant.field-at", (a V Int -- a T));
646 builtin!(sigs, "variant.append", (a V T -- a V2));
647 builtin!(sigs, "variant.last", (a V -- a T));
648 builtin!(sigs, "variant.init", (a V -- a V2));
649
650 builtin!(sigs, "variant.make-0", (a Symbol -- a V));
652 builtin!(sigs, "variant.make-1", (a T1 Symbol -- a V));
653 builtin!(sigs, "variant.make-2", (a T1 T2 Symbol -- a V));
654 builtin!(sigs, "variant.make-3", (a T1 T2 T3 Symbol -- a V));
655 builtin!(sigs, "variant.make-4", (a T1 T2 T3 T4 Symbol -- a V));
656 for n in 5..=12 {
658 let mut input = StackType::RowVar("a".to_string());
659 for i in 1..=n {
660 input = input.push(Type::Var(format!("T{}", i)));
661 }
662 input = input.push(Type::Symbol);
663 let output = StackType::RowVar("a".to_string()).push(Type::Var("V".to_string()));
664 sigs.insert(format!("variant.make-{}", n), Effect::new(input, output));
665 }
666
667 builtin!(sigs, "wrap-0", (a Symbol -- a V));
669 builtin!(sigs, "wrap-1", (a T1 Symbol -- a V));
670 builtin!(sigs, "wrap-2", (a T1 T2 Symbol -- a V));
671 builtin!(sigs, "wrap-3", (a T1 T2 T3 Symbol -- a V));
672 builtin!(sigs, "wrap-4", (a T1 T2 T3 T4 Symbol -- a V));
673 for n in 5..=12 {
675 let mut input = StackType::RowVar("a".to_string());
676 for i in 1..=n {
677 input = input.push(Type::Var(format!("T{}", i)));
678 }
679 input = input.push(Type::Symbol);
680 let output = StackType::RowVar("a".to_string()).push(Type::Var("V".to_string()));
681 sigs.insert(format!("wrap-{}", n), Effect::new(input, output));
682 }
683
684 builtin!(sigs, "list.make", (a -- a V));
690 builtin!(sigs, "list.push", (a V T -- a V));
691 builtin!(sigs, "list.get", (a V Int -- a T Bool));
692 builtin!(sigs, "list.set", (a V Int T -- a V Bool));
693
694 builtin!(sigs, "list.length", (a V -- a Int));
695 builtin!(sigs, "list.empty?", (a V -- a Bool));
696
697 sigs.insert(
700 "list.map".to_string(),
701 Effect::new(
702 StackType::RowVar("a".to_string())
703 .push(Type::Var("V".to_string()))
704 .push(Type::Quotation(Box::new(Effect::new(
705 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
706 StackType::RowVar("b".to_string()).push(Type::Var("U".to_string())),
707 )))),
708 StackType::RowVar("a".to_string()).push(Type::Var("V2".to_string())),
709 ),
710 );
711
712 sigs.insert(
715 "list.filter".to_string(),
716 Effect::new(
717 StackType::RowVar("a".to_string())
718 .push(Type::Var("V".to_string()))
719 .push(Type::Quotation(Box::new(Effect::new(
720 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
721 StackType::RowVar("b".to_string()).push(Type::Bool),
722 )))),
723 StackType::RowVar("a".to_string()).push(Type::Var("V2".to_string())),
724 ),
725 );
726
727 sigs.insert(
730 "list.fold".to_string(),
731 Effect::new(
732 StackType::RowVar("a".to_string())
733 .push(Type::Var("V".to_string()))
734 .push(Type::Var("Acc".to_string()))
735 .push(Type::Quotation(Box::new(Effect::new(
736 StackType::RowVar("b".to_string())
737 .push(Type::Var("Acc".to_string()))
738 .push(Type::Var("T".to_string())),
739 StackType::RowVar("b".to_string()).push(Type::Var("Acc".to_string())),
740 )))),
741 StackType::RowVar("a".to_string()).push(Type::Var("Acc".to_string())),
742 ),
743 );
744
745 sigs.insert(
748 "list.each".to_string(),
749 Effect::new(
750 StackType::RowVar("a".to_string())
751 .push(Type::Var("V".to_string()))
752 .push(Type::Quotation(Box::new(Effect::new(
753 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
754 StackType::RowVar("b".to_string()),
755 )))),
756 StackType::RowVar("a".to_string()),
757 ),
758 );
759
760 builtin!(sigs, "map.make", (a -- a M));
765 builtin!(sigs, "map.get", (a M K -- a V Bool)); builtin!(sigs, "map.set", (a M K V -- a M2));
767 builtin!(sigs, "map.has?", (a M K -- a Bool));
768 builtin!(sigs, "map.remove", (a M K -- a M2));
769 builtin!(sigs, "map.keys", (a M -- a V));
770 builtin!(sigs, "map.values", (a M -- a V));
771 builtin!(sigs, "map.size", (a M -- a Int));
772 builtin!(sigs, "map.empty?", (a M -- a Bool));
773
774 builtins_float_float_to_float!(sigs, "f.add", "f.subtract", "f.multiply", "f.divide");
779 builtins_float_float_to_float!(sigs, "f.+", "f.-", "f.*", "f./");
780
781 builtins_float_float_to_bool!(sigs, "f.=", "f.<", "f.>", "f.<=", "f.>=", "f.<>");
786 builtins_float_float_to_bool!(sigs, "f.eq", "f.lt", "f.gt", "f.lte", "f.gte", "f.neq");
787
788 builtin!(sigs, "test.init", (a String -- a));
793 builtin!(sigs, "test.finish", (a - -a));
794 builtin!(sigs, "test.has-failures", (a -- a Bool));
795 builtin!(sigs, "test.assert", (a Bool -- a));
796 builtin!(sigs, "test.assert-not", (a Bool -- a));
797 builtin!(sigs, "test.assert-eq", (a Int Int -- a));
798 builtin!(sigs, "test.assert-eq-str", (a String String -- a));
799 builtin!(sigs, "test.fail", (a String -- a));
800 builtin!(sigs, "test.pass-count", (a -- a Int));
801 builtin!(sigs, "test.fail-count", (a -- a Int));
802
803 builtin!(sigs, "time.now", (a -- a Int));
805 builtin!(sigs, "time.nanos", (a -- a Int));
806 builtin!(sigs, "time.sleep-ms", (a Int -- a));
807
808 builtin!(sigs, "son.dump", (a T -- a String));
810 builtin!(sigs, "son.dump-pretty", (a T -- a String));
811
812 sigs.insert(
815 "stack.dump".to_string(),
816 Effect::new(
817 StackType::RowVar("a".to_string()), StackType::RowVar("b".to_string()), ),
820 );
821
822 sigs
823}
824
825pub fn builtin_doc(name: &str) -> Option<&'static str> {
827 BUILTIN_DOCS.get(name).copied()
828}
829
830pub fn builtin_docs() -> &'static HashMap<&'static str, &'static str> {
832 &BUILTIN_DOCS
833}
834
835static BUILTIN_DOCS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
837 let mut docs = HashMap::new();
838
839 docs.insert(
841 "io.write",
842 "Write a string to stdout without a trailing newline.",
843 );
844 docs.insert(
845 "io.write-line",
846 "Write a string to stdout followed by a newline.",
847 );
848 docs.insert(
849 "io.read-line",
850 "Read a line from stdin. Returns (line, success).",
851 );
852 docs.insert(
853 "io.read-line+",
854 "DEPRECATED: Use io.read-line instead. Read a line from stdin. Returns (line, status_code).",
855 );
856 docs.insert(
857 "io.read-n",
858 "Read N bytes from stdin. Returns (bytes, status_code).",
859 );
860
861 docs.insert("args.count", "Get the number of command-line arguments.");
863 docs.insert("args.at", "Get the command-line argument at index N.");
864
865 docs.insert(
867 "file.slurp",
868 "Read entire file contents. Returns (content, success).",
869 );
870 docs.insert("file.exists?", "Check if a file exists at the given path.");
871 docs.insert(
872 "file.for-each-line+",
873 "Execute a quotation for each line in a file.",
874 );
875
876 docs.insert(
878 "int->string",
879 "Convert an integer to its string representation.",
880 );
881 docs.insert(
882 "int->float",
883 "Convert an integer to a floating-point number.",
884 );
885 docs.insert("float->int", "Truncate a float to an integer.");
886 docs.insert(
887 "float->string",
888 "Convert a float to its string representation.",
889 );
890 docs.insert(
891 "string->int",
892 "Parse a string as an integer. Returns (value, success).",
893 );
894 docs.insert(
895 "string->float",
896 "Parse a string as a float. Returns (value, success).",
897 );
898 docs.insert(
899 "char->string",
900 "Convert a Unicode codepoint to a single-character string.",
901 );
902 docs.insert(
903 "symbol->string",
904 "Convert a symbol to its string representation.",
905 );
906 docs.insert("string->symbol", "Intern a string as a symbol.");
907
908 docs.insert("i.add", "Add two integers.");
910 docs.insert("i.subtract", "Subtract second integer from first.");
911 docs.insert("i.multiply", "Multiply two integers.");
912 docs.insert("i.divide", "Integer division (truncates toward zero).");
913 docs.insert("i.modulo", "Integer modulo (remainder after division).");
914 docs.insert("i.+", "Add two integers.");
915 docs.insert("i.-", "Subtract second integer from first.");
916 docs.insert("i.*", "Multiply two integers.");
917 docs.insert("i./", "Integer division (truncates toward zero).");
918 docs.insert("i.%", "Integer modulo (remainder after division).");
919
920 docs.insert("i.=", "Test if two integers are equal.");
922 docs.insert("i.<", "Test if first integer is less than second.");
923 docs.insert("i.>", "Test if first integer is greater than second.");
924 docs.insert(
925 "i.<=",
926 "Test if first integer is less than or equal to second.",
927 );
928 docs.insert(
929 "i.>=",
930 "Test if first integer is greater than or equal to second.",
931 );
932 docs.insert("i.<>", "Test if two integers are not equal.");
933 docs.insert("i.eq", "Test if two integers are equal.");
934 docs.insert("i.lt", "Test if first integer is less than second.");
935 docs.insert("i.gt", "Test if first integer is greater than second.");
936 docs.insert(
937 "i.lte",
938 "Test if first integer is less than or equal to second.",
939 );
940 docs.insert(
941 "i.gte",
942 "Test if first integer is greater than or equal to second.",
943 );
944 docs.insert("i.neq", "Test if two integers are not equal.");
945
946 docs.insert("and", "Logical AND of two booleans.");
948 docs.insert("or", "Logical OR of two booleans.");
949 docs.insert("not", "Logical NOT of a boolean.");
950
951 docs.insert("band", "Bitwise AND of two integers.");
953 docs.insert("bor", "Bitwise OR of two integers.");
954 docs.insert("bxor", "Bitwise XOR of two integers.");
955 docs.insert("bnot", "Bitwise NOT (complement) of an integer.");
956 docs.insert("shl", "Shift left by N bits.");
957 docs.insert("shr", "Shift right by N bits (arithmetic).");
958 docs.insert("popcount", "Count the number of set bits.");
959 docs.insert("clz", "Count leading zeros.");
960 docs.insert("ctz", "Count trailing zeros.");
961 docs.insert("int-bits", "Push the bit width of integers (64).");
962
963 docs.insert("dup", "Duplicate the top stack value.");
965 docs.insert("drop", "Remove the top stack value.");
966 docs.insert("swap", "Swap the top two stack values.");
967 docs.insert("over", "Copy the second value to the top.");
968 docs.insert("rot", "Rotate the top three values (third to top).");
969 docs.insert("nip", "Remove the second value from the stack.");
970 docs.insert("tuck", "Copy the top value below the second.");
971 docs.insert("2dup", "Duplicate the top two values.");
972 docs.insert("3drop", "Remove the top three values.");
973 docs.insert("pick", "Copy the value at depth N to the top.");
974 docs.insert("roll", "Rotate N+1 items, bringing depth N to top.");
975
976 docs.insert(
978 "chan.make",
979 "Create a new channel for inter-strand communication.",
980 );
981 docs.insert(
982 "chan.send",
983 "Send a value on a channel. Returns success flag.",
984 );
985 docs.insert(
986 "chan.receive",
987 "Receive a value from a channel. Returns (value, success).",
988 );
989 docs.insert("chan.close", "Close a channel.");
990 docs.insert("chan.yield", "Yield control to the scheduler.");
991
992 docs.insert("call", "Call a quotation or closure.");
994 docs.insert(
995 "cond",
996 "Multi-way conditional: test clauses until one succeeds.",
997 );
998
999 docs.insert(
1001 "strand.spawn",
1002 "Spawn a concurrent strand. Returns strand ID.",
1003 );
1004 docs.insert(
1005 "strand.weave",
1006 "Create a generator/coroutine. Returns handle.",
1007 );
1008 docs.insert(
1009 "strand.resume",
1010 "Resume a weave with a value. Returns (handle, value, has_more).",
1011 );
1012 docs.insert(
1013 "yield",
1014 "Yield a value from a weave and receive resume value.",
1015 );
1016 docs.insert(
1017 "strand.weave-cancel",
1018 "Cancel a weave and release its resources.",
1019 );
1020
1021 docs.insert(
1023 "tcp.listen",
1024 "Start listening on a port. Returns socket ID.",
1025 );
1026 docs.insert(
1027 "tcp.accept",
1028 "Accept a connection. Returns client socket ID.",
1029 );
1030 docs.insert("tcp.read", "Read data from a socket. Returns string.");
1031 docs.insert("tcp.write", "Write data to a socket.");
1032 docs.insert("tcp.close", "Close a socket.");
1033
1034 docs.insert(
1036 "os.getenv",
1037 "Get environment variable. Returns (value, exists).",
1038 );
1039 docs.insert(
1040 "os.home-dir",
1041 "Get user's home directory. Returns (path, success).",
1042 );
1043 docs.insert(
1044 "os.current-dir",
1045 "Get current working directory. Returns (path, success).",
1046 );
1047 docs.insert("os.path-exists", "Check if a path exists.");
1048 docs.insert("os.path-is-file", "Check if path is a regular file.");
1049 docs.insert("os.path-is-dir", "Check if path is a directory.");
1050 docs.insert("os.path-join", "Join two path components.");
1051 docs.insert(
1052 "os.path-parent",
1053 "Get parent directory. Returns (path, success).",
1054 );
1055 docs.insert(
1056 "os.path-filename",
1057 "Get filename component. Returns (name, success).",
1058 );
1059 docs.insert("os.exit", "Exit the program with a status code.");
1060 docs.insert(
1061 "os.name",
1062 "Get the operating system name (e.g., \"macos\", \"linux\").",
1063 );
1064 docs.insert(
1065 "os.arch",
1066 "Get the CPU architecture (e.g., \"aarch64\", \"x86_64\").",
1067 );
1068
1069 docs.insert(
1071 "signal.trap",
1072 "Trap a signal: set internal flag on receipt instead of default action.",
1073 );
1074 docs.insert(
1075 "signal.received?",
1076 "Check if signal was received and clear the flag. Returns Bool.",
1077 );
1078 docs.insert(
1079 "signal.pending?",
1080 "Check if signal is pending without clearing the flag. Returns Bool.",
1081 );
1082 docs.insert(
1083 "signal.default",
1084 "Restore the default handler for a signal.",
1085 );
1086 docs.insert(
1087 "signal.ignore",
1088 "Ignore a signal entirely (useful for SIGPIPE in servers).",
1089 );
1090 docs.insert(
1091 "signal.clear",
1092 "Clear the pending flag for a signal without checking it.",
1093 );
1094 docs.insert("signal.SIGINT", "SIGINT constant (Ctrl+C interrupt).");
1095 docs.insert("signal.SIGTERM", "SIGTERM constant (termination request).");
1096 docs.insert("signal.SIGHUP", "SIGHUP constant (hangup detected).");
1097 docs.insert("signal.SIGPIPE", "SIGPIPE constant (broken pipe).");
1098 docs.insert(
1099 "signal.SIGUSR1",
1100 "SIGUSR1 constant (user-defined signal 1).",
1101 );
1102 docs.insert(
1103 "signal.SIGUSR2",
1104 "SIGUSR2 constant (user-defined signal 2).",
1105 );
1106 docs.insert("signal.SIGCHLD", "SIGCHLD constant (child status changed).");
1107 docs.insert("signal.SIGALRM", "SIGALRM constant (alarm clock).");
1108 docs.insert("signal.SIGCONT", "SIGCONT constant (continue if stopped).");
1109
1110 docs.insert(
1112 "terminal.raw-mode",
1113 "Enable/disable raw terminal mode. In raw mode: no line buffering, no echo, Ctrl+C read as byte 3.",
1114 );
1115 docs.insert(
1116 "terminal.read-char",
1117 "Read a single byte from stdin (blocking). Returns 0-255 on success, -1 on EOF/error.",
1118 );
1119 docs.insert(
1120 "terminal.read-char?",
1121 "Read a single byte from stdin (non-blocking). Returns 0-255 if available, -1 otherwise.",
1122 );
1123 docs.insert(
1124 "terminal.width",
1125 "Get terminal width in columns. Returns 80 if unknown.",
1126 );
1127 docs.insert(
1128 "terminal.height",
1129 "Get terminal height in rows. Returns 24 if unknown.",
1130 );
1131 docs.insert(
1132 "terminal.flush",
1133 "Flush stdout. Use after writing escape sequences or partial lines.",
1134 );
1135
1136 docs.insert("string.concat", "Concatenate two strings.");
1138 docs.insert("string.length", "Get the character length of a string.");
1139 docs.insert("string.byte-length", "Get the byte length of a string.");
1140 docs.insert(
1141 "string.char-at",
1142 "Get Unicode codepoint at character index.",
1143 );
1144 docs.insert(
1145 "string.substring",
1146 "Extract substring from start index with length.",
1147 );
1148 docs.insert(
1149 "string.find",
1150 "Find substring. Returns index or -1 if not found.",
1151 );
1152 docs.insert("string.split", "Split string by delimiter. Returns a list.");
1153 docs.insert("string.contains", "Check if string contains a substring.");
1154 docs.insert(
1155 "string.starts-with",
1156 "Check if string starts with a prefix.",
1157 );
1158 docs.insert("string.empty?", "Check if string is empty.");
1159 docs.insert("string.equal?", "Check if two strings are equal.");
1160 docs.insert("string.trim", "Remove leading and trailing whitespace.");
1161 docs.insert("string.chomp", "Remove trailing newline.");
1162 docs.insert("string.to-upper", "Convert to uppercase.");
1163 docs.insert("string.to-lower", "Convert to lowercase.");
1164 docs.insert("string.json-escape", "Escape special characters for JSON.");
1165 docs.insert("symbol.=", "Check if two symbols are equal.");
1166
1167 docs.insert(
1169 "encoding.base64-encode",
1170 "Encode a string to Base64 (standard alphabet with padding).",
1171 );
1172 docs.insert(
1173 "encoding.base64-decode",
1174 "Decode a Base64 string. Returns (decoded, success).",
1175 );
1176 docs.insert(
1177 "encoding.base64url-encode",
1178 "Encode to URL-safe Base64 (no padding). Suitable for JWTs and URLs.",
1179 );
1180 docs.insert(
1181 "encoding.base64url-decode",
1182 "Decode URL-safe Base64. Returns (decoded, success).",
1183 );
1184 docs.insert(
1185 "encoding.hex-encode",
1186 "Encode a string to lowercase hexadecimal.",
1187 );
1188 docs.insert(
1189 "encoding.hex-decode",
1190 "Decode a hexadecimal string. Returns (decoded, success).",
1191 );
1192
1193 docs.insert(
1195 "crypto.sha256",
1196 "Compute SHA-256 hash of a string. Returns 64-char hex digest.",
1197 );
1198 docs.insert(
1199 "crypto.hmac-sha256",
1200 "Compute HMAC-SHA256 signature. ( message key -- signature )",
1201 );
1202 docs.insert(
1203 "crypto.constant-time-eq",
1204 "Timing-safe string comparison. Use for comparing signatures/tokens.",
1205 );
1206 docs.insert(
1207 "crypto.random-bytes",
1208 "Generate N cryptographically secure random bytes as hex string.",
1209 );
1210 docs.insert(
1211 "crypto.random-int",
1212 "Generate uniform random integer in [min, max). ( min max -- Int ) Uses rejection sampling to avoid modulo bias.",
1213 );
1214 docs.insert("crypto.uuid4", "Generate a random UUID v4 string.");
1215 docs.insert(
1216 "crypto.aes-gcm-encrypt",
1217 "Encrypt with AES-256-GCM. ( plaintext hex-key -- ciphertext success )",
1218 );
1219 docs.insert(
1220 "crypto.aes-gcm-decrypt",
1221 "Decrypt AES-256-GCM ciphertext. ( ciphertext hex-key -- plaintext success )",
1222 );
1223 docs.insert(
1224 "crypto.pbkdf2-sha256",
1225 "Derive key from password. ( password salt iterations -- hex-key success ) Min 1000 iterations, 100000+ recommended.",
1226 );
1227 docs.insert(
1228 "crypto.ed25519-keypair",
1229 "Generate Ed25519 keypair. ( -- public-key private-key ) Both as 64-char hex strings.",
1230 );
1231 docs.insert(
1232 "crypto.ed25519-sign",
1233 "Sign message with Ed25519 private key. ( message private-key -- signature success ) Signature is 128-char hex.",
1234 );
1235 docs.insert(
1236 "crypto.ed25519-verify",
1237 "Verify Ed25519 signature. ( message signature public-key -- valid )",
1238 );
1239
1240 docs.insert(
1242 "http.get",
1243 "HTTP GET request. ( url -- response-map ) Map has status, body, ok, error.",
1244 );
1245 docs.insert(
1246 "http.post",
1247 "HTTP POST request. ( url body content-type -- response-map )",
1248 );
1249 docs.insert(
1250 "http.put",
1251 "HTTP PUT request. ( url body content-type -- response-map )",
1252 );
1253 docs.insert(
1254 "http.delete",
1255 "HTTP DELETE request. ( url -- response-map )",
1256 );
1257
1258 docs.insert(
1260 "regex.match?",
1261 "Check if pattern matches anywhere in string. ( text pattern -- bool )",
1262 );
1263 docs.insert(
1264 "regex.find",
1265 "Find first match. ( text pattern -- matched success )",
1266 );
1267 docs.insert(
1268 "regex.find-all",
1269 "Find all matches. ( text pattern -- list )",
1270 );
1271 docs.insert(
1272 "regex.replace",
1273 "Replace first match. ( text pattern replacement -- result )",
1274 );
1275 docs.insert(
1276 "regex.replace-all",
1277 "Replace all matches. ( text pattern replacement -- result )",
1278 );
1279 docs.insert(
1280 "regex.captures",
1281 "Extract capture groups. ( text pattern -- groups success )",
1282 );
1283 docs.insert(
1284 "regex.split",
1285 "Split string by pattern. ( text pattern -- list )",
1286 );
1287 docs.insert(
1288 "regex.valid?",
1289 "Check if pattern is valid regex. ( pattern -- bool )",
1290 );
1291
1292 docs.insert(
1294 "compress.gzip",
1295 "Compress string with gzip. Returns base64-encoded data. ( data -- compressed success )",
1296 );
1297 docs.insert(
1298 "compress.gzip-level",
1299 "Compress with gzip at level 1-9. ( data level -- compressed success )",
1300 );
1301 docs.insert(
1302 "compress.gunzip",
1303 "Decompress gzip data. ( base64-data -- decompressed success )",
1304 );
1305 docs.insert(
1306 "compress.zstd",
1307 "Compress string with zstd. Returns base64-encoded data. ( data -- compressed success )",
1308 );
1309 docs.insert(
1310 "compress.zstd-level",
1311 "Compress with zstd at level 1-22. ( data level -- compressed success )",
1312 );
1313 docs.insert(
1314 "compress.unzstd",
1315 "Decompress zstd data. ( base64-data -- decompressed success )",
1316 );
1317
1318 docs.insert(
1320 "variant.field-count",
1321 "Get the number of fields in a variant.",
1322 );
1323 docs.insert(
1324 "variant.tag",
1325 "Get the tag (constructor name) of a variant.",
1326 );
1327 docs.insert("variant.field-at", "Get the field at index N.");
1328 docs.insert(
1329 "variant.append",
1330 "Append a value to a variant (creates new).",
1331 );
1332 docs.insert("variant.last", "Get the last field of a variant.");
1333 docs.insert("variant.init", "Get all fields except the last.");
1334 docs.insert("variant.make-0", "Create a variant with 0 fields.");
1335 docs.insert("variant.make-1", "Create a variant with 1 field.");
1336 docs.insert("variant.make-2", "Create a variant with 2 fields.");
1337 docs.insert("variant.make-3", "Create a variant with 3 fields.");
1338 docs.insert("variant.make-4", "Create a variant with 4 fields.");
1339 docs.insert("variant.make-5", "Create a variant with 5 fields.");
1340 docs.insert("variant.make-6", "Create a variant with 6 fields.");
1341 docs.insert("variant.make-7", "Create a variant with 7 fields.");
1342 docs.insert("variant.make-8", "Create a variant with 8 fields.");
1343 docs.insert("variant.make-9", "Create a variant with 9 fields.");
1344 docs.insert("variant.make-10", "Create a variant with 10 fields.");
1345 docs.insert("variant.make-11", "Create a variant with 11 fields.");
1346 docs.insert("variant.make-12", "Create a variant with 12 fields.");
1347 docs.insert("wrap-0", "Create a variant with 0 fields (alias).");
1348 docs.insert("wrap-1", "Create a variant with 1 field (alias).");
1349 docs.insert("wrap-2", "Create a variant with 2 fields (alias).");
1350 docs.insert("wrap-3", "Create a variant with 3 fields (alias).");
1351 docs.insert("wrap-4", "Create a variant with 4 fields (alias).");
1352 docs.insert("wrap-5", "Create a variant with 5 fields (alias).");
1353 docs.insert("wrap-6", "Create a variant with 6 fields (alias).");
1354 docs.insert("wrap-7", "Create a variant with 7 fields (alias).");
1355 docs.insert("wrap-8", "Create a variant with 8 fields (alias).");
1356 docs.insert("wrap-9", "Create a variant with 9 fields (alias).");
1357 docs.insert("wrap-10", "Create a variant with 10 fields (alias).");
1358 docs.insert("wrap-11", "Create a variant with 11 fields (alias).");
1359 docs.insert("wrap-12", "Create a variant with 12 fields (alias).");
1360
1361 docs.insert("list.make", "Create an empty list.");
1363 docs.insert("list.push", "Push a value onto a list. Returns new list.");
1364 docs.insert("list.get", "Get value at index. Returns (value, success).");
1365 docs.insert("list.set", "Set value at index. Returns (list, success).");
1366 docs.insert("list.length", "Get the number of elements in a list.");
1367 docs.insert("list.empty?", "Check if a list is empty.");
1368 docs.insert(
1369 "list.map",
1370 "Apply quotation to each element. Returns new list.",
1371 );
1372 docs.insert("list.filter", "Keep elements where quotation returns true.");
1373 docs.insert("list.fold", "Reduce list with accumulator and quotation.");
1374 docs.insert(
1375 "list.each",
1376 "Execute quotation for each element (side effects).",
1377 );
1378
1379 docs.insert("map.make", "Create an empty map.");
1381 docs.insert("map.get", "Get value for key. Returns (value, success).");
1382 docs.insert("map.set", "Set key to value. Returns new map.");
1383 docs.insert("map.has?", "Check if map contains a key.");
1384 docs.insert("map.remove", "Remove a key. Returns new map.");
1385 docs.insert("map.keys", "Get all keys as a list.");
1386 docs.insert("map.values", "Get all values as a list.");
1387 docs.insert("map.size", "Get the number of key-value pairs.");
1388 docs.insert("map.empty?", "Check if map is empty.");
1389
1390 docs.insert("f.add", "Add two floats.");
1392 docs.insert("f.subtract", "Subtract second float from first.");
1393 docs.insert("f.multiply", "Multiply two floats.");
1394 docs.insert("f.divide", "Divide first float by second.");
1395 docs.insert("f.+", "Add two floats.");
1396 docs.insert("f.-", "Subtract second float from first.");
1397 docs.insert("f.*", "Multiply two floats.");
1398 docs.insert("f./", "Divide first float by second.");
1399
1400 docs.insert("f.=", "Test if two floats are equal.");
1402 docs.insert("f.<", "Test if first float is less than second.");
1403 docs.insert("f.>", "Test if first float is greater than second.");
1404 docs.insert("f.<=", "Test if first float is less than or equal.");
1405 docs.insert("f.>=", "Test if first float is greater than or equal.");
1406 docs.insert("f.<>", "Test if two floats are not equal.");
1407 docs.insert("f.eq", "Test if two floats are equal.");
1408 docs.insert("f.lt", "Test if first float is less than second.");
1409 docs.insert("f.gt", "Test if first float is greater than second.");
1410 docs.insert("f.lte", "Test if first float is less than or equal.");
1411 docs.insert("f.gte", "Test if first float is greater than or equal.");
1412 docs.insert("f.neq", "Test if two floats are not equal.");
1413
1414 docs.insert(
1416 "test.init",
1417 "Initialize the test framework with a test name.",
1418 );
1419 docs.insert("test.finish", "Finish testing and print results.");
1420 docs.insert("test.has-failures", "Check if any tests have failed.");
1421 docs.insert("test.assert", "Assert that a boolean is true.");
1422 docs.insert("test.assert-not", "Assert that a boolean is false.");
1423 docs.insert("test.assert-eq", "Assert that two integers are equal.");
1424 docs.insert("test.assert-eq-str", "Assert that two strings are equal.");
1425 docs.insert("test.fail", "Mark a test as failed with a message.");
1426 docs.insert("test.pass-count", "Get the number of passed assertions.");
1427 docs.insert("test.fail-count", "Get the number of failed assertions.");
1428
1429 docs.insert("time.now", "Get current Unix timestamp in seconds.");
1431 docs.insert(
1432 "time.nanos",
1433 "Get high-resolution monotonic time in nanoseconds.",
1434 );
1435 docs.insert("time.sleep-ms", "Sleep for N milliseconds.");
1436
1437 docs.insert("son.dump", "Serialize any value to SON format (compact).");
1439 docs.insert(
1440 "son.dump-pretty",
1441 "Serialize any value to SON format (pretty-printed).",
1442 );
1443
1444 docs.insert(
1446 "stack.dump",
1447 "Print all stack values and clear the stack (REPL).",
1448 );
1449
1450 docs
1451});
1452
1453#[cfg(test)]
1454mod tests {
1455 use super::*;
1456
1457 #[test]
1458 fn test_builtin_signature_write_line() {
1459 let sig = builtin_signature("io.write-line").unwrap();
1460 let (rest, top) = sig.inputs.clone().pop().unwrap();
1462 assert_eq!(top, Type::String);
1463 assert_eq!(rest, StackType::RowVar("a".to_string()));
1464 assert_eq!(sig.outputs, StackType::RowVar("a".to_string()));
1465 }
1466
1467 #[test]
1468 fn test_builtin_signature_i_add() {
1469 let sig = builtin_signature("i.add").unwrap();
1470 let (rest, top) = sig.inputs.clone().pop().unwrap();
1472 assert_eq!(top, Type::Int);
1473 let (rest2, top2) = rest.pop().unwrap();
1474 assert_eq!(top2, Type::Int);
1475 assert_eq!(rest2, StackType::RowVar("a".to_string()));
1476
1477 let (rest3, top3) = sig.outputs.clone().pop().unwrap();
1478 assert_eq!(top3, Type::Int);
1479 assert_eq!(rest3, StackType::RowVar("a".to_string()));
1480 }
1481
1482 #[test]
1483 fn test_builtin_signature_dup() {
1484 let sig = builtin_signature("dup").unwrap();
1485 assert_eq!(
1487 sig.inputs,
1488 StackType::Cons {
1489 rest: Box::new(StackType::RowVar("a".to_string())),
1490 top: Type::Var("T".to_string())
1491 }
1492 );
1493 let (rest, top) = sig.outputs.clone().pop().unwrap();
1495 assert_eq!(top, Type::Var("T".to_string()));
1496 let (rest2, top2) = rest.pop().unwrap();
1497 assert_eq!(top2, Type::Var("T".to_string()));
1498 assert_eq!(rest2, StackType::RowVar("a".to_string()));
1499 }
1500
1501 #[test]
1502 fn test_all_builtins_have_signatures() {
1503 let sigs = builtin_signatures();
1504
1505 assert!(sigs.contains_key("io.write-line"));
1507 assert!(sigs.contains_key("io.read-line"));
1508 assert!(sigs.contains_key("int->string"));
1509 assert!(sigs.contains_key("i.add"));
1510 assert!(sigs.contains_key("dup"));
1511 assert!(sigs.contains_key("swap"));
1512 assert!(sigs.contains_key("chan.make"));
1513 assert!(sigs.contains_key("chan.send"));
1514 assert!(sigs.contains_key("chan.receive"));
1515 assert!(
1516 sigs.contains_key("string->float"),
1517 "string->float should be a builtin"
1518 );
1519 assert!(
1520 sigs.contains_key("signal.trap"),
1521 "signal.trap should be a builtin"
1522 );
1523 }
1524
1525 #[test]
1526 fn test_all_docs_have_signatures() {
1527 let sigs = builtin_signatures();
1528 let docs = builtin_docs();
1529
1530 for name in docs.keys() {
1531 assert!(
1532 sigs.contains_key(*name),
1533 "Builtin '{}' has documentation but no signature",
1534 name
1535 );
1536 }
1537 }
1538
1539 #[test]
1540 fn test_all_signatures_have_docs() {
1541 let sigs = builtin_signatures();
1542 let docs = builtin_docs();
1543
1544 for name in sigs.keys() {
1545 assert!(
1546 docs.contains_key(name.as_str()),
1547 "Builtin '{}' has signature but no documentation",
1548 name
1549 );
1550 }
1551 }
1552}