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!(sigs, "i.add", "i.subtract", "i.multiply");
336 builtins_int_int_to_int!(sigs, "i.+", "i.-", "i.*");
337
338 builtin!(sigs, "i.divide", (a Int Int -- a Int Bool));
340 builtin!(sigs, "i.modulo", (a Int Int -- a Int Bool));
341 builtin!(sigs, "i./", (a Int Int -- a Int Bool));
342 builtin!(sigs, "i.%", (a Int Int -- a Int Bool));
343
344 builtins_int_int_to_bool!(sigs, "i.=", "i.<", "i.>", "i.<=", "i.>=", "i.<>");
349 builtins_int_int_to_bool!(sigs, "i.eq", "i.lt", "i.gt", "i.lte", "i.gte", "i.neq");
350
351 builtins_bool_bool_to_bool!(sigs, "and", "or");
356 builtin!(sigs, "not", (a Bool -- a Bool));
357
358 builtins_int_int_to_int!(sigs, "band", "bor", "bxor", "shl", "shr");
363 builtins_int_to_int!(sigs, "bnot", "popcount", "clz", "ctz");
364 builtin!(sigs, "int-bits", (a -- a Int));
365
366 builtin!(sigs, "dup", (a T -- a T T));
371 builtin!(sigs, "drop", (a T -- a));
372 builtin!(sigs, "swap", (a T U -- a U T));
373 builtin!(sigs, "over", (a T U -- a T U T));
374 builtin!(sigs, "rot", (a T U V -- a U V T));
375 builtin!(sigs, "nip", (a T U -- a U));
376 builtin!(sigs, "tuck", (a T U -- a U T U));
377 builtin!(sigs, "2dup", (a T U -- a T U T U));
378 builtin!(sigs, "3drop", (a T U V -- a));
379
380 builtin!(sigs, "pick", (a T Int -- a T T));
383 builtin!(sigs, "roll", (a T Int -- a T));
385
386 builtin!(sigs, "chan.make", (a -- a Channel));
392 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));
395 builtin!(sigs, "chan.yield", (a - -a));
396
397 sigs.insert(
404 "call".to_string(),
405 Effect::new(
406 StackType::RowVar("a".to_string()).push(Type::Var("Q".to_string())),
407 StackType::RowVar("b".to_string()),
408 ),
409 );
410
411 sigs.insert(
413 "cond".to_string(),
414 Effect::new(
415 StackType::RowVar("a".to_string()),
416 StackType::RowVar("b".to_string()),
417 ),
418 );
419
420 sigs.insert(
423 "strand.spawn".to_string(),
424 Effect::new(
425 StackType::RowVar("a".to_string()).push(Type::Quotation(Box::new(Effect::new(
426 StackType::RowVar("spawn_in".to_string()),
427 StackType::RowVar("spawn_out".to_string()),
428 )))),
429 StackType::RowVar("a".to_string()).push(Type::Int),
430 ),
431 );
432
433 sigs.insert(
437 "strand.weave".to_string(),
438 Effect::new(
439 StackType::RowVar("a".to_string()).push(Type::Quotation(Box::new(Effect::new(
440 StackType::RowVar("weave_in".to_string()),
441 StackType::RowVar("weave_out".to_string()),
442 )))),
443 StackType::RowVar("a".to_string()).push(Type::Var("handle".to_string())),
444 ),
445 );
446
447 sigs.insert(
450 "strand.resume".to_string(),
451 Effect::new(
452 StackType::RowVar("a".to_string())
453 .push(Type::Var("handle".to_string()))
454 .push(Type::Var("b".to_string())),
455 StackType::RowVar("a".to_string())
456 .push(Type::Var("handle".to_string()))
457 .push(Type::Var("b".to_string()))
458 .push(Type::Bool),
459 ),
460 );
461
462 sigs.insert(
466 "yield".to_string(),
467 Effect::with_effects(
468 StackType::RowVar("a".to_string())
469 .push(Type::Var("ctx".to_string()))
470 .push(Type::Var("b".to_string())),
471 StackType::RowVar("a".to_string())
472 .push(Type::Var("ctx".to_string()))
473 .push(Type::Var("b".to_string())),
474 vec![SideEffect::Yield(Box::new(Type::Var("b".to_string())))],
475 ),
476 );
477
478 sigs.insert(
482 "strand.weave-cancel".to_string(),
483 Effect::new(
484 StackType::RowVar("a".to_string()).push(Type::Var("handle".to_string())),
485 StackType::RowVar("a".to_string()),
486 ),
487 );
488
489 builtin!(sigs, "tcp.listen", (a Int -- a Int Bool));
495 builtin!(sigs, "tcp.accept", (a Int -- a Int Bool));
496 builtin!(sigs, "tcp.read", (a Int -- a String Bool));
497 builtin!(sigs, "tcp.write", (a String Int -- a Bool));
498 builtin!(sigs, "tcp.close", (a Int -- a Bool));
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));
621 builtin!(sigs, "regex.find", (a String String -- a String Bool));
622 builtin!(sigs, "regex.find-all", (a String String -- a V Bool));
623 builtin!(sigs, "regex.replace", (a String String String -- a String Bool));
624 builtin!(sigs, "regex.replace-all", (a String String String -- a String Bool));
625 builtin!(sigs, "regex.captures", (a String String -- a V Bool));
626 builtin!(sigs, "regex.split", (a String String -- a V Bool));
627 builtin!(sigs, "regex.valid?", (a String -- a Bool));
628
629 builtin!(sigs, "compress.gzip", (a String -- a String Bool));
634 builtin!(sigs, "compress.gzip-level", (a String Int -- a String Bool));
635 builtin!(sigs, "compress.gunzip", (a String -- a String Bool));
636 builtin!(sigs, "compress.zstd", (a String -- a String Bool));
637 builtin!(sigs, "compress.zstd-level", (a String Int -- a String Bool));
638 builtin!(sigs, "compress.unzstd", (a String -- a String Bool));
639
640 builtin!(sigs, "variant.field-count", (a V -- a Int));
645 builtin!(sigs, "variant.tag", (a V -- a Symbol));
646 builtin!(sigs, "variant.field-at", (a V Int -- a T));
647 builtin!(sigs, "variant.append", (a V T -- a V2));
648 builtin!(sigs, "variant.last", (a V -- a T));
649 builtin!(sigs, "variant.init", (a V -- a V2));
650
651 builtin!(sigs, "variant.make-0", (a Symbol -- a V));
653 builtin!(sigs, "variant.make-1", (a T1 Symbol -- a V));
654 builtin!(sigs, "variant.make-2", (a T1 T2 Symbol -- a V));
655 builtin!(sigs, "variant.make-3", (a T1 T2 T3 Symbol -- a V));
656 builtin!(sigs, "variant.make-4", (a T1 T2 T3 T4 Symbol -- a V));
657 for n in 5..=12 {
659 let mut input = StackType::RowVar("a".to_string());
660 for i in 1..=n {
661 input = input.push(Type::Var(format!("T{}", i)));
662 }
663 input = input.push(Type::Symbol);
664 let output = StackType::RowVar("a".to_string()).push(Type::Var("V".to_string()));
665 sigs.insert(format!("variant.make-{}", n), Effect::new(input, output));
666 }
667
668 builtin!(sigs, "wrap-0", (a Symbol -- a V));
670 builtin!(sigs, "wrap-1", (a T1 Symbol -- a V));
671 builtin!(sigs, "wrap-2", (a T1 T2 Symbol -- a V));
672 builtin!(sigs, "wrap-3", (a T1 T2 T3 Symbol -- a V));
673 builtin!(sigs, "wrap-4", (a T1 T2 T3 T4 Symbol -- a V));
674 for n in 5..=12 {
676 let mut input = StackType::RowVar("a".to_string());
677 for i in 1..=n {
678 input = input.push(Type::Var(format!("T{}", i)));
679 }
680 input = input.push(Type::Symbol);
681 let output = StackType::RowVar("a".to_string()).push(Type::Var("V".to_string()));
682 sigs.insert(format!("wrap-{}", n), Effect::new(input, output));
683 }
684
685 builtin!(sigs, "list.make", (a -- a V));
691 builtin!(sigs, "list.push", (a V T -- a V));
692 builtin!(sigs, "list.get", (a V Int -- a T Bool));
693 builtin!(sigs, "list.set", (a V Int T -- a V Bool));
694
695 builtin!(sigs, "list.length", (a V -- a Int));
696 builtin!(sigs, "list.empty?", (a V -- a Bool));
697
698 sigs.insert(
701 "list.map".to_string(),
702 Effect::new(
703 StackType::RowVar("a".to_string())
704 .push(Type::Var("V".to_string()))
705 .push(Type::Quotation(Box::new(Effect::new(
706 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
707 StackType::RowVar("b".to_string()).push(Type::Var("U".to_string())),
708 )))),
709 StackType::RowVar("a".to_string()).push(Type::Var("V2".to_string())),
710 ),
711 );
712
713 sigs.insert(
716 "list.filter".to_string(),
717 Effect::new(
718 StackType::RowVar("a".to_string())
719 .push(Type::Var("V".to_string()))
720 .push(Type::Quotation(Box::new(Effect::new(
721 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
722 StackType::RowVar("b".to_string()).push(Type::Bool),
723 )))),
724 StackType::RowVar("a".to_string()).push(Type::Var("V2".to_string())),
725 ),
726 );
727
728 sigs.insert(
731 "list.fold".to_string(),
732 Effect::new(
733 StackType::RowVar("a".to_string())
734 .push(Type::Var("V".to_string()))
735 .push(Type::Var("Acc".to_string()))
736 .push(Type::Quotation(Box::new(Effect::new(
737 StackType::RowVar("b".to_string())
738 .push(Type::Var("Acc".to_string()))
739 .push(Type::Var("T".to_string())),
740 StackType::RowVar("b".to_string()).push(Type::Var("Acc".to_string())),
741 )))),
742 StackType::RowVar("a".to_string()).push(Type::Var("Acc".to_string())),
743 ),
744 );
745
746 sigs.insert(
749 "list.each".to_string(),
750 Effect::new(
751 StackType::RowVar("a".to_string())
752 .push(Type::Var("V".to_string()))
753 .push(Type::Quotation(Box::new(Effect::new(
754 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
755 StackType::RowVar("b".to_string()),
756 )))),
757 StackType::RowVar("a".to_string()),
758 ),
759 );
760
761 builtin!(sigs, "map.make", (a -- a M));
766 builtin!(sigs, "map.get", (a M K -- a V Bool)); builtin!(sigs, "map.set", (a M K V -- a M2));
768 builtin!(sigs, "map.has?", (a M K -- a Bool));
769 builtin!(sigs, "map.remove", (a M K -- a M2));
770 builtin!(sigs, "map.keys", (a M -- a V));
771 builtin!(sigs, "map.values", (a M -- a V));
772 builtin!(sigs, "map.size", (a M -- a Int));
773 builtin!(sigs, "map.empty?", (a M -- a Bool));
774
775 builtins_float_float_to_float!(sigs, "f.add", "f.subtract", "f.multiply", "f.divide");
780 builtins_float_float_to_float!(sigs, "f.+", "f.-", "f.*", "f./");
781
782 builtins_float_float_to_bool!(sigs, "f.=", "f.<", "f.>", "f.<=", "f.>=", "f.<>");
787 builtins_float_float_to_bool!(sigs, "f.eq", "f.lt", "f.gt", "f.lte", "f.gte", "f.neq");
788
789 builtin!(sigs, "test.init", (a String -- a));
794 builtin!(sigs, "test.finish", (a - -a));
795 builtin!(sigs, "test.has-failures", (a -- a Bool));
796 builtin!(sigs, "test.assert", (a Bool -- a));
797 builtin!(sigs, "test.assert-not", (a Bool -- a));
798 builtin!(sigs, "test.assert-eq", (a Int Int -- a));
799 builtin!(sigs, "test.assert-eq-str", (a String String -- a));
800 builtin!(sigs, "test.fail", (a String -- a));
801 builtin!(sigs, "test.pass-count", (a -- a Int));
802 builtin!(sigs, "test.fail-count", (a -- a Int));
803
804 builtin!(sigs, "time.now", (a -- a Int));
806 builtin!(sigs, "time.nanos", (a -- a Int));
807 builtin!(sigs, "time.sleep-ms", (a Int -- a));
808
809 builtin!(sigs, "son.dump", (a T -- a String));
811 builtin!(sigs, "son.dump-pretty", (a T -- a String));
812
813 sigs.insert(
816 "stack.dump".to_string(),
817 Effect::new(
818 StackType::RowVar("a".to_string()), StackType::RowVar("b".to_string()), ),
821 );
822
823 sigs
824}
825
826pub fn builtin_doc(name: &str) -> Option<&'static str> {
828 BUILTIN_DOCS.get(name).copied()
829}
830
831pub fn builtin_docs() -> &'static HashMap<&'static str, &'static str> {
833 &BUILTIN_DOCS
834}
835
836static BUILTIN_DOCS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
838 let mut docs = HashMap::new();
839
840 docs.insert(
842 "io.write",
843 "Write a string to stdout without a trailing newline.",
844 );
845 docs.insert(
846 "io.write-line",
847 "Write a string to stdout followed by a newline.",
848 );
849 docs.insert(
850 "io.read-line",
851 "Read a line from stdin. Returns (line, success).",
852 );
853 docs.insert(
854 "io.read-line+",
855 "DEPRECATED: Use io.read-line instead. Read a line from stdin. Returns (line, status_code).",
856 );
857 docs.insert(
858 "io.read-n",
859 "Read N bytes from stdin. Returns (bytes, status_code).",
860 );
861
862 docs.insert("args.count", "Get the number of command-line arguments.");
864 docs.insert("args.at", "Get the command-line argument at index N.");
865
866 docs.insert(
868 "file.slurp",
869 "Read entire file contents. Returns (content, success).",
870 );
871 docs.insert("file.exists?", "Check if a file exists at the given path.");
872 docs.insert(
873 "file.for-each-line+",
874 "Execute a quotation for each line in a file.",
875 );
876
877 docs.insert(
879 "int->string",
880 "Convert an integer to its string representation.",
881 );
882 docs.insert(
883 "int->float",
884 "Convert an integer to a floating-point number.",
885 );
886 docs.insert("float->int", "Truncate a float to an integer.");
887 docs.insert(
888 "float->string",
889 "Convert a float to its string representation.",
890 );
891 docs.insert(
892 "string->int",
893 "Parse a string as an integer. Returns (value, success).",
894 );
895 docs.insert(
896 "string->float",
897 "Parse a string as a float. Returns (value, success).",
898 );
899 docs.insert(
900 "char->string",
901 "Convert a Unicode codepoint to a single-character string.",
902 );
903 docs.insert(
904 "symbol->string",
905 "Convert a symbol to its string representation.",
906 );
907 docs.insert("string->symbol", "Intern a string as a symbol.");
908
909 docs.insert("i.add", "Add two integers.");
911 docs.insert("i.subtract", "Subtract second integer from first.");
912 docs.insert("i.multiply", "Multiply two integers.");
913 docs.insert("i.divide", "Integer division (truncates toward zero).");
914 docs.insert("i.modulo", "Integer modulo (remainder after division).");
915 docs.insert("i.+", "Add two integers.");
916 docs.insert("i.-", "Subtract second integer from first.");
917 docs.insert("i.*", "Multiply two integers.");
918 docs.insert("i./", "Integer division (truncates toward zero).");
919 docs.insert("i.%", "Integer modulo (remainder after division).");
920
921 docs.insert("i.=", "Test if two integers are equal.");
923 docs.insert("i.<", "Test if first integer is less than second.");
924 docs.insert("i.>", "Test if first integer is greater than second.");
925 docs.insert(
926 "i.<=",
927 "Test if first integer is less than or equal to second.",
928 );
929 docs.insert(
930 "i.>=",
931 "Test if first integer is greater than or equal to second.",
932 );
933 docs.insert("i.<>", "Test if two integers are not equal.");
934 docs.insert("i.eq", "Test if two integers are equal.");
935 docs.insert("i.lt", "Test if first integer is less than second.");
936 docs.insert("i.gt", "Test if first integer is greater than second.");
937 docs.insert(
938 "i.lte",
939 "Test if first integer is less than or equal to second.",
940 );
941 docs.insert(
942 "i.gte",
943 "Test if first integer is greater than or equal to second.",
944 );
945 docs.insert("i.neq", "Test if two integers are not equal.");
946
947 docs.insert("and", "Logical AND of two booleans.");
949 docs.insert("or", "Logical OR of two booleans.");
950 docs.insert("not", "Logical NOT of a boolean.");
951
952 docs.insert("band", "Bitwise AND of two integers.");
954 docs.insert("bor", "Bitwise OR of two integers.");
955 docs.insert("bxor", "Bitwise XOR of two integers.");
956 docs.insert("bnot", "Bitwise NOT (complement) of an integer.");
957 docs.insert("shl", "Shift left by N bits.");
958 docs.insert("shr", "Shift right by N bits (arithmetic).");
959 docs.insert("popcount", "Count the number of set bits.");
960 docs.insert("clz", "Count leading zeros.");
961 docs.insert("ctz", "Count trailing zeros.");
962 docs.insert("int-bits", "Push the bit width of integers (64).");
963
964 docs.insert("dup", "Duplicate the top stack value.");
966 docs.insert("drop", "Remove the top stack value.");
967 docs.insert("swap", "Swap the top two stack values.");
968 docs.insert("over", "Copy the second value to the top.");
969 docs.insert("rot", "Rotate the top three values (third to top).");
970 docs.insert("nip", "Remove the second value from the stack.");
971 docs.insert("tuck", "Copy the top value below the second.");
972 docs.insert("2dup", "Duplicate the top two values.");
973 docs.insert("3drop", "Remove the top three values.");
974 docs.insert("pick", "Copy the value at depth N to the top.");
975 docs.insert("roll", "Rotate N+1 items, bringing depth N to top.");
976
977 docs.insert(
979 "chan.make",
980 "Create a new channel for inter-strand communication.",
981 );
982 docs.insert(
983 "chan.send",
984 "Send a value on a channel. Returns success flag.",
985 );
986 docs.insert(
987 "chan.receive",
988 "Receive a value from a channel. Returns (value, success).",
989 );
990 docs.insert("chan.close", "Close a channel.");
991 docs.insert("chan.yield", "Yield control to the scheduler.");
992
993 docs.insert("call", "Call a quotation or closure.");
995 docs.insert(
996 "cond",
997 "Multi-way conditional: test clauses until one succeeds.",
998 );
999
1000 docs.insert(
1002 "strand.spawn",
1003 "Spawn a concurrent strand. Returns strand ID.",
1004 );
1005 docs.insert(
1006 "strand.weave",
1007 "Create a generator/coroutine. Returns handle.",
1008 );
1009 docs.insert(
1010 "strand.resume",
1011 "Resume a weave with a value. Returns (handle, value, has_more).",
1012 );
1013 docs.insert(
1014 "yield",
1015 "Yield a value from a weave and receive resume value.",
1016 );
1017 docs.insert(
1018 "strand.weave-cancel",
1019 "Cancel a weave and release its resources.",
1020 );
1021
1022 docs.insert(
1024 "tcp.listen",
1025 "Start listening on a port. Returns (socket_id, success).",
1026 );
1027 docs.insert(
1028 "tcp.accept",
1029 "Accept a connection. Returns (client_id, success).",
1030 );
1031 docs.insert(
1032 "tcp.read",
1033 "Read data from a socket. Returns (string, success).",
1034 );
1035 docs.insert("tcp.write", "Write data to a socket. Returns success.");
1036 docs.insert("tcp.close", "Close a socket. Returns success.");
1037
1038 docs.insert(
1040 "os.getenv",
1041 "Get environment variable. Returns (value, exists).",
1042 );
1043 docs.insert(
1044 "os.home-dir",
1045 "Get user's home directory. Returns (path, success).",
1046 );
1047 docs.insert(
1048 "os.current-dir",
1049 "Get current working directory. Returns (path, success).",
1050 );
1051 docs.insert("os.path-exists", "Check if a path exists.");
1052 docs.insert("os.path-is-file", "Check if path is a regular file.");
1053 docs.insert("os.path-is-dir", "Check if path is a directory.");
1054 docs.insert("os.path-join", "Join two path components.");
1055 docs.insert(
1056 "os.path-parent",
1057 "Get parent directory. Returns (path, success).",
1058 );
1059 docs.insert(
1060 "os.path-filename",
1061 "Get filename component. Returns (name, success).",
1062 );
1063 docs.insert("os.exit", "Exit the program with a status code.");
1064 docs.insert(
1065 "os.name",
1066 "Get the operating system name (e.g., \"macos\", \"linux\").",
1067 );
1068 docs.insert(
1069 "os.arch",
1070 "Get the CPU architecture (e.g., \"aarch64\", \"x86_64\").",
1071 );
1072
1073 docs.insert(
1075 "signal.trap",
1076 "Trap a signal: set internal flag on receipt instead of default action.",
1077 );
1078 docs.insert(
1079 "signal.received?",
1080 "Check if signal was received and clear the flag. Returns Bool.",
1081 );
1082 docs.insert(
1083 "signal.pending?",
1084 "Check if signal is pending without clearing the flag. Returns Bool.",
1085 );
1086 docs.insert(
1087 "signal.default",
1088 "Restore the default handler for a signal.",
1089 );
1090 docs.insert(
1091 "signal.ignore",
1092 "Ignore a signal entirely (useful for SIGPIPE in servers).",
1093 );
1094 docs.insert(
1095 "signal.clear",
1096 "Clear the pending flag for a signal without checking it.",
1097 );
1098 docs.insert("signal.SIGINT", "SIGINT constant (Ctrl+C interrupt).");
1099 docs.insert("signal.SIGTERM", "SIGTERM constant (termination request).");
1100 docs.insert("signal.SIGHUP", "SIGHUP constant (hangup detected).");
1101 docs.insert("signal.SIGPIPE", "SIGPIPE constant (broken pipe).");
1102 docs.insert(
1103 "signal.SIGUSR1",
1104 "SIGUSR1 constant (user-defined signal 1).",
1105 );
1106 docs.insert(
1107 "signal.SIGUSR2",
1108 "SIGUSR2 constant (user-defined signal 2).",
1109 );
1110 docs.insert("signal.SIGCHLD", "SIGCHLD constant (child status changed).");
1111 docs.insert("signal.SIGALRM", "SIGALRM constant (alarm clock).");
1112 docs.insert("signal.SIGCONT", "SIGCONT constant (continue if stopped).");
1113
1114 docs.insert(
1116 "terminal.raw-mode",
1117 "Enable/disable raw terminal mode. In raw mode: no line buffering, no echo, Ctrl+C read as byte 3.",
1118 );
1119 docs.insert(
1120 "terminal.read-char",
1121 "Read a single byte from stdin (blocking). Returns 0-255 on success, -1 on EOF/error.",
1122 );
1123 docs.insert(
1124 "terminal.read-char?",
1125 "Read a single byte from stdin (non-blocking). Returns 0-255 if available, -1 otherwise.",
1126 );
1127 docs.insert(
1128 "terminal.width",
1129 "Get terminal width in columns. Returns 80 if unknown.",
1130 );
1131 docs.insert(
1132 "terminal.height",
1133 "Get terminal height in rows. Returns 24 if unknown.",
1134 );
1135 docs.insert(
1136 "terminal.flush",
1137 "Flush stdout. Use after writing escape sequences or partial lines.",
1138 );
1139
1140 docs.insert("string.concat", "Concatenate two strings.");
1142 docs.insert("string.length", "Get the character length of a string.");
1143 docs.insert("string.byte-length", "Get the byte length of a string.");
1144 docs.insert(
1145 "string.char-at",
1146 "Get Unicode codepoint at character index.",
1147 );
1148 docs.insert(
1149 "string.substring",
1150 "Extract substring from start index with length.",
1151 );
1152 docs.insert(
1153 "string.find",
1154 "Find substring. Returns index or -1 if not found.",
1155 );
1156 docs.insert("string.split", "Split string by delimiter. Returns a list.");
1157 docs.insert("string.contains", "Check if string contains a substring.");
1158 docs.insert(
1159 "string.starts-with",
1160 "Check if string starts with a prefix.",
1161 );
1162 docs.insert("string.empty?", "Check if string is empty.");
1163 docs.insert("string.equal?", "Check if two strings are equal.");
1164 docs.insert("string.trim", "Remove leading and trailing whitespace.");
1165 docs.insert("string.chomp", "Remove trailing newline.");
1166 docs.insert("string.to-upper", "Convert to uppercase.");
1167 docs.insert("string.to-lower", "Convert to lowercase.");
1168 docs.insert("string.json-escape", "Escape special characters for JSON.");
1169 docs.insert("symbol.=", "Check if two symbols are equal.");
1170
1171 docs.insert(
1173 "encoding.base64-encode",
1174 "Encode a string to Base64 (standard alphabet with padding).",
1175 );
1176 docs.insert(
1177 "encoding.base64-decode",
1178 "Decode a Base64 string. Returns (decoded, success).",
1179 );
1180 docs.insert(
1181 "encoding.base64url-encode",
1182 "Encode to URL-safe Base64 (no padding). Suitable for JWTs and URLs.",
1183 );
1184 docs.insert(
1185 "encoding.base64url-decode",
1186 "Decode URL-safe Base64. Returns (decoded, success).",
1187 );
1188 docs.insert(
1189 "encoding.hex-encode",
1190 "Encode a string to lowercase hexadecimal.",
1191 );
1192 docs.insert(
1193 "encoding.hex-decode",
1194 "Decode a hexadecimal string. Returns (decoded, success).",
1195 );
1196
1197 docs.insert(
1199 "crypto.sha256",
1200 "Compute SHA-256 hash of a string. Returns 64-char hex digest.",
1201 );
1202 docs.insert(
1203 "crypto.hmac-sha256",
1204 "Compute HMAC-SHA256 signature. ( message key -- signature )",
1205 );
1206 docs.insert(
1207 "crypto.constant-time-eq",
1208 "Timing-safe string comparison. Use for comparing signatures/tokens.",
1209 );
1210 docs.insert(
1211 "crypto.random-bytes",
1212 "Generate N cryptographically secure random bytes as hex string.",
1213 );
1214 docs.insert(
1215 "crypto.random-int",
1216 "Generate uniform random integer in [min, max). ( min max -- Int ) Uses rejection sampling to avoid modulo bias.",
1217 );
1218 docs.insert("crypto.uuid4", "Generate a random UUID v4 string.");
1219 docs.insert(
1220 "crypto.aes-gcm-encrypt",
1221 "Encrypt with AES-256-GCM. ( plaintext hex-key -- ciphertext success )",
1222 );
1223 docs.insert(
1224 "crypto.aes-gcm-decrypt",
1225 "Decrypt AES-256-GCM ciphertext. ( ciphertext hex-key -- plaintext success )",
1226 );
1227 docs.insert(
1228 "crypto.pbkdf2-sha256",
1229 "Derive key from password. ( password salt iterations -- hex-key success ) Min 1000 iterations, 100000+ recommended.",
1230 );
1231 docs.insert(
1232 "crypto.ed25519-keypair",
1233 "Generate Ed25519 keypair. ( -- public-key private-key ) Both as 64-char hex strings.",
1234 );
1235 docs.insert(
1236 "crypto.ed25519-sign",
1237 "Sign message with Ed25519 private key. ( message private-key -- signature success ) Signature is 128-char hex.",
1238 );
1239 docs.insert(
1240 "crypto.ed25519-verify",
1241 "Verify Ed25519 signature. ( message signature public-key -- valid )",
1242 );
1243
1244 docs.insert(
1246 "http.get",
1247 "HTTP GET request. ( url -- response-map ) Map has status, body, ok, error.",
1248 );
1249 docs.insert(
1250 "http.post",
1251 "HTTP POST request. ( url body content-type -- response-map )",
1252 );
1253 docs.insert(
1254 "http.put",
1255 "HTTP PUT request. ( url body content-type -- response-map )",
1256 );
1257 docs.insert(
1258 "http.delete",
1259 "HTTP DELETE request. ( url -- response-map )",
1260 );
1261
1262 docs.insert(
1264 "regex.match?",
1265 "Check if pattern matches anywhere in string. ( text pattern -- bool )",
1266 );
1267 docs.insert(
1268 "regex.find",
1269 "Find first match. ( text pattern -- matched success )",
1270 );
1271 docs.insert(
1272 "regex.find-all",
1273 "Find all matches. ( text pattern -- list success )",
1274 );
1275 docs.insert(
1276 "regex.replace",
1277 "Replace first match. ( text pattern replacement -- result success )",
1278 );
1279 docs.insert(
1280 "regex.replace-all",
1281 "Replace all matches. ( text pattern replacement -- result success )",
1282 );
1283 docs.insert(
1284 "regex.captures",
1285 "Extract capture groups. ( text pattern -- groups success )",
1286 );
1287 docs.insert(
1288 "regex.split",
1289 "Split string by pattern. ( text pattern -- list success )",
1290 );
1291 docs.insert(
1292 "regex.valid?",
1293 "Check if pattern is valid regex. ( pattern -- bool )",
1294 );
1295
1296 docs.insert(
1298 "compress.gzip",
1299 "Compress string with gzip. Returns base64-encoded data. ( data -- compressed success )",
1300 );
1301 docs.insert(
1302 "compress.gzip-level",
1303 "Compress with gzip at level 1-9. ( data level -- compressed success )",
1304 );
1305 docs.insert(
1306 "compress.gunzip",
1307 "Decompress gzip data. ( base64-data -- decompressed success )",
1308 );
1309 docs.insert(
1310 "compress.zstd",
1311 "Compress string with zstd. Returns base64-encoded data. ( data -- compressed success )",
1312 );
1313 docs.insert(
1314 "compress.zstd-level",
1315 "Compress with zstd at level 1-22. ( data level -- compressed success )",
1316 );
1317 docs.insert(
1318 "compress.unzstd",
1319 "Decompress zstd data. ( base64-data -- decompressed success )",
1320 );
1321
1322 docs.insert(
1324 "variant.field-count",
1325 "Get the number of fields in a variant.",
1326 );
1327 docs.insert(
1328 "variant.tag",
1329 "Get the tag (constructor name) of a variant.",
1330 );
1331 docs.insert("variant.field-at", "Get the field at index N.");
1332 docs.insert(
1333 "variant.append",
1334 "Append a value to a variant (creates new).",
1335 );
1336 docs.insert("variant.last", "Get the last field of a variant.");
1337 docs.insert("variant.init", "Get all fields except the last.");
1338 docs.insert("variant.make-0", "Create a variant with 0 fields.");
1339 docs.insert("variant.make-1", "Create a variant with 1 field.");
1340 docs.insert("variant.make-2", "Create a variant with 2 fields.");
1341 docs.insert("variant.make-3", "Create a variant with 3 fields.");
1342 docs.insert("variant.make-4", "Create a variant with 4 fields.");
1343 docs.insert("variant.make-5", "Create a variant with 5 fields.");
1344 docs.insert("variant.make-6", "Create a variant with 6 fields.");
1345 docs.insert("variant.make-7", "Create a variant with 7 fields.");
1346 docs.insert("variant.make-8", "Create a variant with 8 fields.");
1347 docs.insert("variant.make-9", "Create a variant with 9 fields.");
1348 docs.insert("variant.make-10", "Create a variant with 10 fields.");
1349 docs.insert("variant.make-11", "Create a variant with 11 fields.");
1350 docs.insert("variant.make-12", "Create a variant with 12 fields.");
1351 docs.insert("wrap-0", "Create a variant with 0 fields (alias).");
1352 docs.insert("wrap-1", "Create a variant with 1 field (alias).");
1353 docs.insert("wrap-2", "Create a variant with 2 fields (alias).");
1354 docs.insert("wrap-3", "Create a variant with 3 fields (alias).");
1355 docs.insert("wrap-4", "Create a variant with 4 fields (alias).");
1356 docs.insert("wrap-5", "Create a variant with 5 fields (alias).");
1357 docs.insert("wrap-6", "Create a variant with 6 fields (alias).");
1358 docs.insert("wrap-7", "Create a variant with 7 fields (alias).");
1359 docs.insert("wrap-8", "Create a variant with 8 fields (alias).");
1360 docs.insert("wrap-9", "Create a variant with 9 fields (alias).");
1361 docs.insert("wrap-10", "Create a variant with 10 fields (alias).");
1362 docs.insert("wrap-11", "Create a variant with 11 fields (alias).");
1363 docs.insert("wrap-12", "Create a variant with 12 fields (alias).");
1364
1365 docs.insert("list.make", "Create an empty list.");
1367 docs.insert("list.push", "Push a value onto a list. Returns new list.");
1368 docs.insert("list.get", "Get value at index. Returns (value, success).");
1369 docs.insert("list.set", "Set value at index. Returns (list, success).");
1370 docs.insert("list.length", "Get the number of elements in a list.");
1371 docs.insert("list.empty?", "Check if a list is empty.");
1372 docs.insert(
1373 "list.map",
1374 "Apply quotation to each element. Returns new list.",
1375 );
1376 docs.insert("list.filter", "Keep elements where quotation returns true.");
1377 docs.insert("list.fold", "Reduce list with accumulator and quotation.");
1378 docs.insert(
1379 "list.each",
1380 "Execute quotation for each element (side effects).",
1381 );
1382
1383 docs.insert("map.make", "Create an empty map.");
1385 docs.insert("map.get", "Get value for key. Returns (value, success).");
1386 docs.insert("map.set", "Set key to value. Returns new map.");
1387 docs.insert("map.has?", "Check if map contains a key.");
1388 docs.insert("map.remove", "Remove a key. Returns new map.");
1389 docs.insert("map.keys", "Get all keys as a list.");
1390 docs.insert("map.values", "Get all values as a list.");
1391 docs.insert("map.size", "Get the number of key-value pairs.");
1392 docs.insert("map.empty?", "Check if map is empty.");
1393
1394 docs.insert("f.add", "Add two floats.");
1396 docs.insert("f.subtract", "Subtract second float from first.");
1397 docs.insert("f.multiply", "Multiply two floats.");
1398 docs.insert("f.divide", "Divide first float by second.");
1399 docs.insert("f.+", "Add two floats.");
1400 docs.insert("f.-", "Subtract second float from first.");
1401 docs.insert("f.*", "Multiply two floats.");
1402 docs.insert("f./", "Divide first float by second.");
1403
1404 docs.insert("f.=", "Test if two floats are equal.");
1406 docs.insert("f.<", "Test if first float is less than second.");
1407 docs.insert("f.>", "Test if first float is greater than second.");
1408 docs.insert("f.<=", "Test if first float is less than or equal.");
1409 docs.insert("f.>=", "Test if first float is greater than or equal.");
1410 docs.insert("f.<>", "Test if two floats are not equal.");
1411 docs.insert("f.eq", "Test if two floats are equal.");
1412 docs.insert("f.lt", "Test if first float is less than second.");
1413 docs.insert("f.gt", "Test if first float is greater than second.");
1414 docs.insert("f.lte", "Test if first float is less than or equal.");
1415 docs.insert("f.gte", "Test if first float is greater than or equal.");
1416 docs.insert("f.neq", "Test if two floats are not equal.");
1417
1418 docs.insert(
1420 "test.init",
1421 "Initialize the test framework with a test name.",
1422 );
1423 docs.insert("test.finish", "Finish testing and print results.");
1424 docs.insert("test.has-failures", "Check if any tests have failed.");
1425 docs.insert("test.assert", "Assert that a boolean is true.");
1426 docs.insert("test.assert-not", "Assert that a boolean is false.");
1427 docs.insert("test.assert-eq", "Assert that two integers are equal.");
1428 docs.insert("test.assert-eq-str", "Assert that two strings are equal.");
1429 docs.insert("test.fail", "Mark a test as failed with a message.");
1430 docs.insert("test.pass-count", "Get the number of passed assertions.");
1431 docs.insert("test.fail-count", "Get the number of failed assertions.");
1432
1433 docs.insert("time.now", "Get current Unix timestamp in seconds.");
1435 docs.insert(
1436 "time.nanos",
1437 "Get high-resolution monotonic time in nanoseconds.",
1438 );
1439 docs.insert("time.sleep-ms", "Sleep for N milliseconds.");
1440
1441 docs.insert("son.dump", "Serialize any value to SON format (compact).");
1443 docs.insert(
1444 "son.dump-pretty",
1445 "Serialize any value to SON format (pretty-printed).",
1446 );
1447
1448 docs.insert(
1450 "stack.dump",
1451 "Print all stack values and clear the stack (REPL).",
1452 );
1453
1454 docs
1455});
1456
1457#[cfg(test)]
1458mod tests {
1459 use super::*;
1460
1461 #[test]
1462 fn test_builtin_signature_write_line() {
1463 let sig = builtin_signature("io.write-line").unwrap();
1464 let (rest, top) = sig.inputs.clone().pop().unwrap();
1466 assert_eq!(top, Type::String);
1467 assert_eq!(rest, StackType::RowVar("a".to_string()));
1468 assert_eq!(sig.outputs, StackType::RowVar("a".to_string()));
1469 }
1470
1471 #[test]
1472 fn test_builtin_signature_i_add() {
1473 let sig = builtin_signature("i.add").unwrap();
1474 let (rest, top) = sig.inputs.clone().pop().unwrap();
1476 assert_eq!(top, Type::Int);
1477 let (rest2, top2) = rest.pop().unwrap();
1478 assert_eq!(top2, Type::Int);
1479 assert_eq!(rest2, StackType::RowVar("a".to_string()));
1480
1481 let (rest3, top3) = sig.outputs.clone().pop().unwrap();
1482 assert_eq!(top3, Type::Int);
1483 assert_eq!(rest3, StackType::RowVar("a".to_string()));
1484 }
1485
1486 #[test]
1487 fn test_builtin_signature_dup() {
1488 let sig = builtin_signature("dup").unwrap();
1489 assert_eq!(
1491 sig.inputs,
1492 StackType::Cons {
1493 rest: Box::new(StackType::RowVar("a".to_string())),
1494 top: Type::Var("T".to_string())
1495 }
1496 );
1497 let (rest, top) = sig.outputs.clone().pop().unwrap();
1499 assert_eq!(top, Type::Var("T".to_string()));
1500 let (rest2, top2) = rest.pop().unwrap();
1501 assert_eq!(top2, Type::Var("T".to_string()));
1502 assert_eq!(rest2, StackType::RowVar("a".to_string()));
1503 }
1504
1505 #[test]
1506 fn test_all_builtins_have_signatures() {
1507 let sigs = builtin_signatures();
1508
1509 assert!(sigs.contains_key("io.write-line"));
1511 assert!(sigs.contains_key("io.read-line"));
1512 assert!(sigs.contains_key("int->string"));
1513 assert!(sigs.contains_key("i.add"));
1514 assert!(sigs.contains_key("dup"));
1515 assert!(sigs.contains_key("swap"));
1516 assert!(sigs.contains_key("chan.make"));
1517 assert!(sigs.contains_key("chan.send"));
1518 assert!(sigs.contains_key("chan.receive"));
1519 assert!(
1520 sigs.contains_key("string->float"),
1521 "string->float should be a builtin"
1522 );
1523 assert!(
1524 sigs.contains_key("signal.trap"),
1525 "signal.trap should be a builtin"
1526 );
1527 }
1528
1529 #[test]
1530 fn test_all_docs_have_signatures() {
1531 let sigs = builtin_signatures();
1532 let docs = builtin_docs();
1533
1534 for name in docs.keys() {
1535 assert!(
1536 sigs.contains_key(*name),
1537 "Builtin '{}' has documentation but no signature",
1538 name
1539 );
1540 }
1541 }
1542
1543 #[test]
1544 fn test_all_signatures_have_docs() {
1545 let sigs = builtin_signatures();
1546 let docs = builtin_docs();
1547
1548 for name in sigs.keys() {
1549 assert!(
1550 docs.contains_key(name.as_str()),
1551 "Builtin '{}' has signature but no documentation",
1552 name
1553 );
1554 }
1555 }
1556}