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(
423 "times".to_string(),
424 Effect::new(
425 StackType::RowVar("a".to_string())
426 .push(Type::Quotation(Box::new(Effect::new(
427 StackType::RowVar("a".to_string()),
428 StackType::RowVar("a".to_string()),
429 ))))
430 .push(Type::Int),
431 StackType::RowVar("a".to_string()),
432 ),
433 );
434
435 sigs.insert(
438 "while".to_string(),
439 Effect::new(
440 StackType::RowVar("a".to_string())
441 .push(Type::Quotation(Box::new(Effect::new(
442 StackType::RowVar("a".to_string()),
443 StackType::RowVar("a".to_string()).push(Type::Bool),
444 ))))
445 .push(Type::Quotation(Box::new(Effect::new(
446 StackType::RowVar("a".to_string()),
447 StackType::RowVar("a".to_string()),
448 )))),
449 StackType::RowVar("a".to_string()),
450 ),
451 );
452
453 sigs.insert(
456 "until".to_string(),
457 Effect::new(
458 StackType::RowVar("a".to_string())
459 .push(Type::Quotation(Box::new(Effect::new(
460 StackType::RowVar("a".to_string()),
461 StackType::RowVar("a".to_string()),
462 ))))
463 .push(Type::Quotation(Box::new(Effect::new(
464 StackType::RowVar("a".to_string()),
465 StackType::RowVar("a".to_string()).push(Type::Bool),
466 )))),
467 StackType::RowVar("a".to_string()),
468 ),
469 );
470
471 sigs.insert(
474 "strand.spawn".to_string(),
475 Effect::new(
476 StackType::RowVar("a".to_string()).push(Type::Quotation(Box::new(Effect::new(
477 StackType::RowVar("spawn_in".to_string()),
478 StackType::RowVar("spawn_out".to_string()),
479 )))),
480 StackType::RowVar("a".to_string()).push(Type::Int),
481 ),
482 );
483
484 sigs.insert(
488 "strand.weave".to_string(),
489 Effect::new(
490 StackType::RowVar("a".to_string()).push(Type::Quotation(Box::new(Effect::new(
491 StackType::RowVar("weave_in".to_string()),
492 StackType::RowVar("weave_out".to_string()),
493 )))),
494 StackType::RowVar("a".to_string()).push(Type::Var("handle".to_string())),
495 ),
496 );
497
498 sigs.insert(
501 "strand.resume".to_string(),
502 Effect::new(
503 StackType::RowVar("a".to_string())
504 .push(Type::Var("handle".to_string()))
505 .push(Type::Var("b".to_string())),
506 StackType::RowVar("a".to_string())
507 .push(Type::Var("handle".to_string()))
508 .push(Type::Var("b".to_string()))
509 .push(Type::Bool),
510 ),
511 );
512
513 sigs.insert(
517 "yield".to_string(),
518 Effect::with_effects(
519 StackType::RowVar("a".to_string())
520 .push(Type::Var("ctx".to_string()))
521 .push(Type::Var("b".to_string())),
522 StackType::RowVar("a".to_string())
523 .push(Type::Var("ctx".to_string()))
524 .push(Type::Var("b".to_string())),
525 vec![SideEffect::Yield(Box::new(Type::Var("b".to_string())))],
526 ),
527 );
528
529 sigs.insert(
533 "strand.weave-cancel".to_string(),
534 Effect::new(
535 StackType::RowVar("a".to_string()).push(Type::Var("handle".to_string())),
536 StackType::RowVar("a".to_string()),
537 ),
538 );
539
540 builtin!(sigs, "tcp.listen", (a Int -- a Int));
545 builtin!(sigs, "tcp.accept", (a Int -- a Int));
546 builtin!(sigs, "tcp.read", (a Int -- a String));
547 builtin!(sigs, "tcp.write", (a String Int -- a));
548 builtin!(sigs, "tcp.close", (a Int -- a));
549
550 builtin!(sigs, "os.getenv", (a String -- a String Bool));
555 builtin!(sigs, "os.home-dir", (a -- a String Bool));
556 builtin!(sigs, "os.current-dir", (a -- a String Bool));
557 builtin!(sigs, "os.path-exists", (a String -- a Bool));
558 builtin!(sigs, "os.path-is-file", (a String -- a Bool));
559 builtin!(sigs, "os.path-is-dir", (a String -- a Bool));
560 builtin!(sigs, "os.path-join", (a String String -- a String));
561 builtin!(sigs, "os.path-parent", (a String -- a String Bool));
562 builtin!(sigs, "os.path-filename", (a String -- a String Bool));
563 builtin!(sigs, "os.exit", (a Int -- a)); builtin!(sigs, "os.name", (a -- a String));
565 builtin!(sigs, "os.arch", (a -- a String));
566
567 builtin!(sigs, "terminal.raw-mode", (a Bool -- a));
572 builtin!(sigs, "terminal.read-char", (a -- a Int));
573 builtin!(sigs, "terminal.read-char?", (a -- a Int));
574 builtin!(sigs, "terminal.width", (a -- a Int));
575 builtin!(sigs, "terminal.height", (a -- a Int));
576 builtin!(sigs, "terminal.flush", (a - -a));
577
578 builtin!(sigs, "string.concat", (a String String -- a String));
583 builtin!(sigs, "string.length", (a String -- a Int));
584 builtin!(sigs, "string.byte-length", (a String -- a Int));
585 builtin!(sigs, "string.char-at", (a String Int -- a Int));
586 builtin!(sigs, "string.substring", (a String Int Int -- a String));
587 builtin!(sigs, "string.find", (a String String -- a Int));
588 builtin!(sigs, "string.split", (a String String -- a V)); builtin!(sigs, "string.contains", (a String String -- a Bool));
590 builtin!(sigs, "string.starts-with", (a String String -- a Bool));
591 builtin!(sigs, "string.empty?", (a String -- a Bool));
592 builtin!(sigs, "string.equal?", (a String String -- a Bool));
593
594 builtin!(sigs, "symbol.=", (a Symbol Symbol -- a Bool));
596
597 builtins_string_to_string!(
599 sigs,
600 "string.trim",
601 "string.chomp",
602 "string.to-upper",
603 "string.to-lower",
604 "string.json-escape"
605 );
606
607 builtin!(sigs, "encoding.base64-encode", (a String -- a String));
612 builtin!(sigs, "encoding.base64-decode", (a String -- a String Bool));
613 builtin!(sigs, "encoding.base64url-encode", (a String -- a String));
614 builtin!(sigs, "encoding.base64url-decode", (a String -- a String Bool));
615 builtin!(sigs, "encoding.hex-encode", (a String -- a String));
616 builtin!(sigs, "encoding.hex-decode", (a String -- a String Bool));
617
618 builtin!(sigs, "crypto.sha256", (a String -- a String));
623 builtin!(sigs, "crypto.hmac-sha256", (a String String -- a String));
624 builtin!(sigs, "crypto.constant-time-eq", (a String String -- a Bool));
625 builtin!(sigs, "crypto.random-bytes", (a Int -- a String));
626 builtin!(sigs, "crypto.random-int", (a Int Int -- a Int));
627 builtin!(sigs, "crypto.uuid4", (a -- a String));
628 builtin!(sigs, "crypto.aes-gcm-encrypt", (a String String -- a String Bool));
629 builtin!(sigs, "crypto.aes-gcm-decrypt", (a String String -- a String Bool));
630 builtin!(sigs, "crypto.pbkdf2-sha256", (a String String Int -- a String Bool));
631 builtin!(sigs, "crypto.ed25519-keypair", (a -- a String String));
632 builtin!(sigs, "crypto.ed25519-sign", (a String String -- a String Bool));
633 builtin!(sigs, "crypto.ed25519-verify", (a String String String -- a Bool));
634
635 builtin!(sigs, "http.get", (a String -- a M));
640 builtin!(sigs, "http.post", (a String String String -- a M));
641 builtin!(sigs, "http.put", (a String String String -- a M));
642 builtin!(sigs, "http.delete", (a String -- a M));
643
644 builtin!(sigs, "regex.match?", (a String String -- a Bool));
649 builtin!(sigs, "regex.find", (a String String -- a String Bool));
650 builtin!(sigs, "regex.find-all", (a String String -- a V));
651 builtin!(sigs, "regex.replace", (a String String String -- a String));
652 builtin!(sigs, "regex.replace-all", (a String String String -- a String));
653 builtin!(sigs, "regex.captures", (a String String -- a V Bool));
654 builtin!(sigs, "regex.split", (a String String -- a V));
655 builtin!(sigs, "regex.valid?", (a String -- a Bool));
656
657 builtin!(sigs, "compress.gzip", (a String -- a String Bool));
662 builtin!(sigs, "compress.gzip-level", (a String Int -- a String Bool));
663 builtin!(sigs, "compress.gunzip", (a String -- a String Bool));
664 builtin!(sigs, "compress.zstd", (a String -- a String Bool));
665 builtin!(sigs, "compress.zstd-level", (a String Int -- a String Bool));
666 builtin!(sigs, "compress.unzstd", (a String -- a String Bool));
667
668 builtin!(sigs, "variant.field-count", (a V -- a Int));
673 builtin!(sigs, "variant.tag", (a V -- a Symbol));
674 builtin!(sigs, "variant.field-at", (a V Int -- a T));
675 builtin!(sigs, "variant.append", (a V T -- a V2));
676 builtin!(sigs, "variant.last", (a V -- a T));
677 builtin!(sigs, "variant.init", (a V -- a V2));
678
679 builtin!(sigs, "variant.make-0", (a Symbol -- a V));
681 builtin!(sigs, "variant.make-1", (a T1 Symbol -- a V));
682 builtin!(sigs, "variant.make-2", (a T1 T2 Symbol -- a V));
683 builtin!(sigs, "variant.make-3", (a T1 T2 T3 Symbol -- a V));
684 builtin!(sigs, "variant.make-4", (a T1 T2 T3 T4 Symbol -- a V));
685
686 builtin!(sigs, "wrap-0", (a Symbol -- a V));
688 builtin!(sigs, "wrap-1", (a T1 Symbol -- a V));
689 builtin!(sigs, "wrap-2", (a T1 T2 Symbol -- a V));
690 builtin!(sigs, "wrap-3", (a T1 T2 T3 Symbol -- a V));
691 builtin!(sigs, "wrap-4", (a T1 T2 T3 T4 Symbol -- a V));
692
693 builtin!(sigs, "list.make", (a -- a V));
699 builtin!(sigs, "list.push", (a V T -- a V));
700 builtin!(sigs, "list.get", (a V Int -- a T Bool));
701 builtin!(sigs, "list.set", (a V Int T -- a V Bool));
702
703 builtin!(sigs, "list.length", (a V -- a Int));
704 builtin!(sigs, "list.empty?", (a V -- a Bool));
705
706 sigs.insert(
709 "list.map".to_string(),
710 Effect::new(
711 StackType::RowVar("a".to_string())
712 .push(Type::Var("V".to_string()))
713 .push(Type::Quotation(Box::new(Effect::new(
714 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
715 StackType::RowVar("b".to_string()).push(Type::Var("U".to_string())),
716 )))),
717 StackType::RowVar("a".to_string()).push(Type::Var("V2".to_string())),
718 ),
719 );
720
721 sigs.insert(
724 "list.filter".to_string(),
725 Effect::new(
726 StackType::RowVar("a".to_string())
727 .push(Type::Var("V".to_string()))
728 .push(Type::Quotation(Box::new(Effect::new(
729 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
730 StackType::RowVar("b".to_string()).push(Type::Bool),
731 )))),
732 StackType::RowVar("a".to_string()).push(Type::Var("V2".to_string())),
733 ),
734 );
735
736 sigs.insert(
739 "list.fold".to_string(),
740 Effect::new(
741 StackType::RowVar("a".to_string())
742 .push(Type::Var("V".to_string()))
743 .push(Type::Var("Acc".to_string()))
744 .push(Type::Quotation(Box::new(Effect::new(
745 StackType::RowVar("b".to_string())
746 .push(Type::Var("Acc".to_string()))
747 .push(Type::Var("T".to_string())),
748 StackType::RowVar("b".to_string()).push(Type::Var("Acc".to_string())),
749 )))),
750 StackType::RowVar("a".to_string()).push(Type::Var("Acc".to_string())),
751 ),
752 );
753
754 sigs.insert(
757 "list.each".to_string(),
758 Effect::new(
759 StackType::RowVar("a".to_string())
760 .push(Type::Var("V".to_string()))
761 .push(Type::Quotation(Box::new(Effect::new(
762 StackType::RowVar("b".to_string()).push(Type::Var("T".to_string())),
763 StackType::RowVar("b".to_string()),
764 )))),
765 StackType::RowVar("a".to_string()),
766 ),
767 );
768
769 builtin!(sigs, "map.make", (a -- a M));
774 builtin!(sigs, "map.get", (a M K -- a V Bool)); builtin!(sigs, "map.set", (a M K V -- a M2));
776 builtin!(sigs, "map.has?", (a M K -- a Bool));
777 builtin!(sigs, "map.remove", (a M K -- a M2));
778 builtin!(sigs, "map.keys", (a M -- a V));
779 builtin!(sigs, "map.values", (a M -- a V));
780 builtin!(sigs, "map.size", (a M -- a Int));
781 builtin!(sigs, "map.empty?", (a M -- a Bool));
782
783 builtins_float_float_to_float!(sigs, "f.add", "f.subtract", "f.multiply", "f.divide");
788 builtins_float_float_to_float!(sigs, "f.+", "f.-", "f.*", "f./");
789
790 builtins_float_float_to_bool!(sigs, "f.=", "f.<", "f.>", "f.<=", "f.>=", "f.<>");
795 builtins_float_float_to_bool!(sigs, "f.eq", "f.lt", "f.gt", "f.lte", "f.gte", "f.neq");
796
797 builtin!(sigs, "test.init", (a String -- a));
802 builtin!(sigs, "test.finish", (a - -a));
803 builtin!(sigs, "test.has-failures", (a -- a Bool));
804 builtin!(sigs, "test.assert", (a Bool -- a));
805 builtin!(sigs, "test.assert-not", (a Bool -- a));
806 builtin!(sigs, "test.assert-eq", (a Int Int -- a));
807 builtin!(sigs, "test.assert-eq-str", (a String String -- a));
808 builtin!(sigs, "test.fail", (a String -- a));
809 builtin!(sigs, "test.pass-count", (a -- a Int));
810 builtin!(sigs, "test.fail-count", (a -- a Int));
811
812 builtin!(sigs, "time.now", (a -- a Int));
814 builtin!(sigs, "time.nanos", (a -- a Int));
815 builtin!(sigs, "time.sleep-ms", (a Int -- a));
816
817 builtin!(sigs, "son.dump", (a T -- a String));
819 builtin!(sigs, "son.dump-pretty", (a T -- a String));
820
821 sigs.insert(
824 "stack.dump".to_string(),
825 Effect::new(
826 StackType::RowVar("a".to_string()), StackType::RowVar("b".to_string()), ),
829 );
830
831 sigs
832}
833
834pub fn builtin_doc(name: &str) -> Option<&'static str> {
836 BUILTIN_DOCS.get(name).copied()
837}
838
839pub fn builtin_docs() -> &'static HashMap<&'static str, &'static str> {
841 &BUILTIN_DOCS
842}
843
844static BUILTIN_DOCS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
846 let mut docs = HashMap::new();
847
848 docs.insert(
850 "io.write",
851 "Write a string to stdout without a trailing newline.",
852 );
853 docs.insert(
854 "io.write-line",
855 "Write a string to stdout followed by a newline.",
856 );
857 docs.insert(
858 "io.read-line",
859 "Read a line from stdin. Returns (line, success).",
860 );
861 docs.insert(
862 "io.read-line+",
863 "Read a line from stdin. Returns (line, status_code).",
864 );
865 docs.insert(
866 "io.read-n",
867 "Read N bytes from stdin. Returns (bytes, status_code).",
868 );
869
870 docs.insert("args.count", "Get the number of command-line arguments.");
872 docs.insert("args.at", "Get the command-line argument at index N.");
873
874 docs.insert(
876 "file.slurp",
877 "Read entire file contents. Returns (content, success).",
878 );
879 docs.insert("file.exists?", "Check if a file exists at the given path.");
880 docs.insert(
881 "file.for-each-line+",
882 "Execute a quotation for each line in a file.",
883 );
884
885 docs.insert(
887 "int->string",
888 "Convert an integer to its string representation.",
889 );
890 docs.insert(
891 "int->float",
892 "Convert an integer to a floating-point number.",
893 );
894 docs.insert("float->int", "Truncate a float to an integer.");
895 docs.insert(
896 "float->string",
897 "Convert a float to its string representation.",
898 );
899 docs.insert(
900 "string->int",
901 "Parse a string as an integer. Returns (value, success).",
902 );
903 docs.insert(
904 "string->float",
905 "Parse a string as a float. Returns (value, success).",
906 );
907 docs.insert(
908 "char->string",
909 "Convert a Unicode codepoint to a single-character string.",
910 );
911 docs.insert(
912 "symbol->string",
913 "Convert a symbol to its string representation.",
914 );
915 docs.insert("string->symbol", "Intern a string as a symbol.");
916
917 docs.insert("i.add", "Add two integers.");
919 docs.insert("i.subtract", "Subtract second integer from first.");
920 docs.insert("i.multiply", "Multiply two integers.");
921 docs.insert("i.divide", "Integer division (truncates toward zero).");
922 docs.insert("i.modulo", "Integer modulo (remainder after division).");
923 docs.insert("i.+", "Add two integers.");
924 docs.insert("i.-", "Subtract second integer from first.");
925 docs.insert("i.*", "Multiply two integers.");
926 docs.insert("i./", "Integer division (truncates toward zero).");
927 docs.insert("i.%", "Integer modulo (remainder after division).");
928
929 docs.insert("i.=", "Test if two integers are equal.");
931 docs.insert("i.<", "Test if first integer is less than second.");
932 docs.insert("i.>", "Test if first integer is greater than second.");
933 docs.insert(
934 "i.<=",
935 "Test if first integer is less than or equal to second.",
936 );
937 docs.insert(
938 "i.>=",
939 "Test if first integer is greater than or equal to second.",
940 );
941 docs.insert("i.<>", "Test if two integers are not equal.");
942 docs.insert("i.eq", "Test if two integers are equal.");
943 docs.insert("i.lt", "Test if first integer is less than second.");
944 docs.insert("i.gt", "Test if first integer is greater than second.");
945 docs.insert(
946 "i.lte",
947 "Test if first integer is less than or equal to second.",
948 );
949 docs.insert(
950 "i.gte",
951 "Test if first integer is greater than or equal to second.",
952 );
953 docs.insert("i.neq", "Test if two integers are not equal.");
954
955 docs.insert("and", "Logical AND of two booleans.");
957 docs.insert("or", "Logical OR of two booleans.");
958 docs.insert("not", "Logical NOT of a boolean.");
959
960 docs.insert("band", "Bitwise AND of two integers.");
962 docs.insert("bor", "Bitwise OR of two integers.");
963 docs.insert("bxor", "Bitwise XOR of two integers.");
964 docs.insert("bnot", "Bitwise NOT (complement) of an integer.");
965 docs.insert("shl", "Shift left by N bits.");
966 docs.insert("shr", "Shift right by N bits (arithmetic).");
967 docs.insert("popcount", "Count the number of set bits.");
968 docs.insert("clz", "Count leading zeros.");
969 docs.insert("ctz", "Count trailing zeros.");
970 docs.insert("int-bits", "Push the bit width of integers (64).");
971
972 docs.insert("dup", "Duplicate the top stack value.");
974 docs.insert("drop", "Remove the top stack value.");
975 docs.insert("swap", "Swap the top two stack values.");
976 docs.insert("over", "Copy the second value to the top.");
977 docs.insert("rot", "Rotate the top three values (third to top).");
978 docs.insert("nip", "Remove the second value from the stack.");
979 docs.insert("tuck", "Copy the top value below the second.");
980 docs.insert("2dup", "Duplicate the top two values.");
981 docs.insert("3drop", "Remove the top three values.");
982 docs.insert("pick", "Copy the value at depth N to the top.");
983 docs.insert("roll", "Rotate N+1 items, bringing depth N to top.");
984
985 docs.insert(
987 "chan.make",
988 "Create a new channel for inter-strand communication.",
989 );
990 docs.insert(
991 "chan.send",
992 "Send a value on a channel. Returns success flag.",
993 );
994 docs.insert(
995 "chan.receive",
996 "Receive a value from a channel. Returns (value, success).",
997 );
998 docs.insert("chan.close", "Close a channel.");
999 docs.insert("chan.yield", "Yield control to the scheduler.");
1000
1001 docs.insert("call", "Call a quotation or closure.");
1003 docs.insert(
1004 "cond",
1005 "Multi-way conditional: test clauses until one succeeds.",
1006 );
1007 docs.insert("times", "Execute a quotation N times.");
1008 docs.insert("while", "Loop while condition is true: [cond] [body] while");
1009 docs.insert("until", "Loop until condition is true: [body] [cond] until");
1010
1011 docs.insert(
1013 "strand.spawn",
1014 "Spawn a concurrent strand. Returns strand ID.",
1015 );
1016 docs.insert(
1017 "strand.weave",
1018 "Create a generator/coroutine. Returns handle.",
1019 );
1020 docs.insert(
1021 "strand.resume",
1022 "Resume a weave with a value. Returns (handle, value, has_more).",
1023 );
1024 docs.insert(
1025 "yield",
1026 "Yield a value from a weave and receive resume value.",
1027 );
1028 docs.insert(
1029 "strand.weave-cancel",
1030 "Cancel a weave and release its resources.",
1031 );
1032
1033 docs.insert(
1035 "tcp.listen",
1036 "Start listening on a port. Returns socket ID.",
1037 );
1038 docs.insert(
1039 "tcp.accept",
1040 "Accept a connection. Returns client socket ID.",
1041 );
1042 docs.insert("tcp.read", "Read data from a socket. Returns string.");
1043 docs.insert("tcp.write", "Write data to a socket.");
1044 docs.insert("tcp.close", "Close a socket.");
1045
1046 docs.insert(
1048 "os.getenv",
1049 "Get environment variable. Returns (value, exists).",
1050 );
1051 docs.insert(
1052 "os.home-dir",
1053 "Get user's home directory. Returns (path, success).",
1054 );
1055 docs.insert(
1056 "os.current-dir",
1057 "Get current working directory. Returns (path, success).",
1058 );
1059 docs.insert("os.path-exists", "Check if a path exists.");
1060 docs.insert("os.path-is-file", "Check if path is a regular file.");
1061 docs.insert("os.path-is-dir", "Check if path is a directory.");
1062 docs.insert("os.path-join", "Join two path components.");
1063 docs.insert(
1064 "os.path-parent",
1065 "Get parent directory. Returns (path, success).",
1066 );
1067 docs.insert(
1068 "os.path-filename",
1069 "Get filename component. Returns (name, success).",
1070 );
1071 docs.insert("os.exit", "Exit the program with a status code.");
1072 docs.insert(
1073 "os.name",
1074 "Get the operating system name (e.g., \"macos\", \"linux\").",
1075 );
1076 docs.insert(
1077 "os.arch",
1078 "Get the CPU architecture (e.g., \"aarch64\", \"x86_64\").",
1079 );
1080
1081 docs.insert(
1083 "terminal.raw-mode",
1084 "Enable/disable raw terminal mode. In raw mode: no line buffering, no echo, Ctrl+C read as byte 3.",
1085 );
1086 docs.insert(
1087 "terminal.read-char",
1088 "Read a single byte from stdin (blocking). Returns 0-255 on success, -1 on EOF/error.",
1089 );
1090 docs.insert(
1091 "terminal.read-char?",
1092 "Read a single byte from stdin (non-blocking). Returns 0-255 if available, -1 otherwise.",
1093 );
1094 docs.insert(
1095 "terminal.width",
1096 "Get terminal width in columns. Returns 80 if unknown.",
1097 );
1098 docs.insert(
1099 "terminal.height",
1100 "Get terminal height in rows. Returns 24 if unknown.",
1101 );
1102 docs.insert(
1103 "terminal.flush",
1104 "Flush stdout. Use after writing escape sequences or partial lines.",
1105 );
1106
1107 docs.insert("string.concat", "Concatenate two strings.");
1109 docs.insert("string.length", "Get the character length of a string.");
1110 docs.insert("string.byte-length", "Get the byte length of a string.");
1111 docs.insert(
1112 "string.char-at",
1113 "Get Unicode codepoint at character index.",
1114 );
1115 docs.insert(
1116 "string.substring",
1117 "Extract substring from start index with length.",
1118 );
1119 docs.insert(
1120 "string.find",
1121 "Find substring. Returns index or -1 if not found.",
1122 );
1123 docs.insert("string.split", "Split string by delimiter. Returns a list.");
1124 docs.insert("string.contains", "Check if string contains a substring.");
1125 docs.insert(
1126 "string.starts-with",
1127 "Check if string starts with a prefix.",
1128 );
1129 docs.insert("string.empty?", "Check if string is empty.");
1130 docs.insert("string.equal?", "Check if two strings are equal.");
1131 docs.insert("string.trim", "Remove leading and trailing whitespace.");
1132 docs.insert("string.chomp", "Remove trailing newline.");
1133 docs.insert("string.to-upper", "Convert to uppercase.");
1134 docs.insert("string.to-lower", "Convert to lowercase.");
1135 docs.insert("string.json-escape", "Escape special characters for JSON.");
1136 docs.insert("symbol.=", "Check if two symbols are equal.");
1137
1138 docs.insert(
1140 "encoding.base64-encode",
1141 "Encode a string to Base64 (standard alphabet with padding).",
1142 );
1143 docs.insert(
1144 "encoding.base64-decode",
1145 "Decode a Base64 string. Returns (decoded, success).",
1146 );
1147 docs.insert(
1148 "encoding.base64url-encode",
1149 "Encode to URL-safe Base64 (no padding). Suitable for JWTs and URLs.",
1150 );
1151 docs.insert(
1152 "encoding.base64url-decode",
1153 "Decode URL-safe Base64. Returns (decoded, success).",
1154 );
1155 docs.insert(
1156 "encoding.hex-encode",
1157 "Encode a string to lowercase hexadecimal.",
1158 );
1159 docs.insert(
1160 "encoding.hex-decode",
1161 "Decode a hexadecimal string. Returns (decoded, success).",
1162 );
1163
1164 docs.insert(
1166 "crypto.sha256",
1167 "Compute SHA-256 hash of a string. Returns 64-char hex digest.",
1168 );
1169 docs.insert(
1170 "crypto.hmac-sha256",
1171 "Compute HMAC-SHA256 signature. ( message key -- signature )",
1172 );
1173 docs.insert(
1174 "crypto.constant-time-eq",
1175 "Timing-safe string comparison. Use for comparing signatures/tokens.",
1176 );
1177 docs.insert(
1178 "crypto.random-bytes",
1179 "Generate N cryptographically secure random bytes as hex string.",
1180 );
1181 docs.insert(
1182 "crypto.random-int",
1183 "Generate uniform random integer in [min, max). ( min max -- Int ) Uses rejection sampling to avoid modulo bias.",
1184 );
1185 docs.insert("crypto.uuid4", "Generate a random UUID v4 string.");
1186 docs.insert(
1187 "crypto.aes-gcm-encrypt",
1188 "Encrypt with AES-256-GCM. ( plaintext hex-key -- ciphertext success )",
1189 );
1190 docs.insert(
1191 "crypto.aes-gcm-decrypt",
1192 "Decrypt AES-256-GCM ciphertext. ( ciphertext hex-key -- plaintext success )",
1193 );
1194 docs.insert(
1195 "crypto.pbkdf2-sha256",
1196 "Derive key from password. ( password salt iterations -- hex-key success ) Min 1000 iterations, 100000+ recommended.",
1197 );
1198 docs.insert(
1199 "crypto.ed25519-keypair",
1200 "Generate Ed25519 keypair. ( -- public-key private-key ) Both as 64-char hex strings.",
1201 );
1202 docs.insert(
1203 "crypto.ed25519-sign",
1204 "Sign message with Ed25519 private key. ( message private-key -- signature success ) Signature is 128-char hex.",
1205 );
1206 docs.insert(
1207 "crypto.ed25519-verify",
1208 "Verify Ed25519 signature. ( message signature public-key -- valid )",
1209 );
1210
1211 docs.insert(
1213 "http.get",
1214 "HTTP GET request. ( url -- response-map ) Map has status, body, ok, error.",
1215 );
1216 docs.insert(
1217 "http.post",
1218 "HTTP POST request. ( url body content-type -- response-map )",
1219 );
1220 docs.insert(
1221 "http.put",
1222 "HTTP PUT request. ( url body content-type -- response-map )",
1223 );
1224 docs.insert(
1225 "http.delete",
1226 "HTTP DELETE request. ( url -- response-map )",
1227 );
1228
1229 docs.insert(
1231 "regex.match?",
1232 "Check if pattern matches anywhere in string. ( text pattern -- bool )",
1233 );
1234 docs.insert(
1235 "regex.find",
1236 "Find first match. ( text pattern -- matched success )",
1237 );
1238 docs.insert(
1239 "regex.find-all",
1240 "Find all matches. ( text pattern -- list )",
1241 );
1242 docs.insert(
1243 "regex.replace",
1244 "Replace first match. ( text pattern replacement -- result )",
1245 );
1246 docs.insert(
1247 "regex.replace-all",
1248 "Replace all matches. ( text pattern replacement -- result )",
1249 );
1250 docs.insert(
1251 "regex.captures",
1252 "Extract capture groups. ( text pattern -- groups success )",
1253 );
1254 docs.insert(
1255 "regex.split",
1256 "Split string by pattern. ( text pattern -- list )",
1257 );
1258 docs.insert(
1259 "regex.valid?",
1260 "Check if pattern is valid regex. ( pattern -- bool )",
1261 );
1262
1263 docs.insert(
1265 "compress.gzip",
1266 "Compress string with gzip. Returns base64-encoded data. ( data -- compressed success )",
1267 );
1268 docs.insert(
1269 "compress.gzip-level",
1270 "Compress with gzip at level 1-9. ( data level -- compressed success )",
1271 );
1272 docs.insert(
1273 "compress.gunzip",
1274 "Decompress gzip data. ( base64-data -- decompressed success )",
1275 );
1276 docs.insert(
1277 "compress.zstd",
1278 "Compress string with zstd. Returns base64-encoded data. ( data -- compressed success )",
1279 );
1280 docs.insert(
1281 "compress.zstd-level",
1282 "Compress with zstd at level 1-22. ( data level -- compressed success )",
1283 );
1284 docs.insert(
1285 "compress.unzstd",
1286 "Decompress zstd data. ( base64-data -- decompressed success )",
1287 );
1288
1289 docs.insert(
1291 "variant.field-count",
1292 "Get the number of fields in a variant.",
1293 );
1294 docs.insert(
1295 "variant.tag",
1296 "Get the tag (constructor name) of a variant.",
1297 );
1298 docs.insert("variant.field-at", "Get the field at index N.");
1299 docs.insert(
1300 "variant.append",
1301 "Append a value to a variant (creates new).",
1302 );
1303 docs.insert("variant.last", "Get the last field of a variant.");
1304 docs.insert("variant.init", "Get all fields except the last.");
1305 docs.insert("variant.make-0", "Create a variant with 0 fields.");
1306 docs.insert("variant.make-1", "Create a variant with 1 field.");
1307 docs.insert("variant.make-2", "Create a variant with 2 fields.");
1308 docs.insert("variant.make-3", "Create a variant with 3 fields.");
1309 docs.insert("variant.make-4", "Create a variant with 4 fields.");
1310 docs.insert("wrap-0", "Create a variant with 0 fields (alias).");
1311 docs.insert("wrap-1", "Create a variant with 1 field (alias).");
1312 docs.insert("wrap-2", "Create a variant with 2 fields (alias).");
1313 docs.insert("wrap-3", "Create a variant with 3 fields (alias).");
1314 docs.insert("wrap-4", "Create a variant with 4 fields (alias).");
1315
1316 docs.insert("list.make", "Create an empty list.");
1318 docs.insert("list.push", "Push a value onto a list. Returns new list.");
1319 docs.insert("list.get", "Get value at index. Returns (value, success).");
1320 docs.insert("list.set", "Set value at index. Returns (list, success).");
1321 docs.insert("list.length", "Get the number of elements in a list.");
1322 docs.insert("list.empty?", "Check if a list is empty.");
1323 docs.insert(
1324 "list.map",
1325 "Apply quotation to each element. Returns new list.",
1326 );
1327 docs.insert("list.filter", "Keep elements where quotation returns true.");
1328 docs.insert("list.fold", "Reduce list with accumulator and quotation.");
1329 docs.insert(
1330 "list.each",
1331 "Execute quotation for each element (side effects).",
1332 );
1333
1334 docs.insert("map.make", "Create an empty map.");
1336 docs.insert("map.get", "Get value for key. Returns (value, success).");
1337 docs.insert("map.set", "Set key to value. Returns new map.");
1338 docs.insert("map.has?", "Check if map contains a key.");
1339 docs.insert("map.remove", "Remove a key. Returns new map.");
1340 docs.insert("map.keys", "Get all keys as a list.");
1341 docs.insert("map.values", "Get all values as a list.");
1342 docs.insert("map.size", "Get the number of key-value pairs.");
1343 docs.insert("map.empty?", "Check if map is empty.");
1344
1345 docs.insert("f.add", "Add two floats.");
1347 docs.insert("f.subtract", "Subtract second float from first.");
1348 docs.insert("f.multiply", "Multiply two floats.");
1349 docs.insert("f.divide", "Divide first float by second.");
1350 docs.insert("f.+", "Add two floats.");
1351 docs.insert("f.-", "Subtract second float from first.");
1352 docs.insert("f.*", "Multiply two floats.");
1353 docs.insert("f./", "Divide first float by second.");
1354
1355 docs.insert("f.=", "Test if two floats are equal.");
1357 docs.insert("f.<", "Test if first float is less than second.");
1358 docs.insert("f.>", "Test if first float is greater than second.");
1359 docs.insert("f.<=", "Test if first float is less than or equal.");
1360 docs.insert("f.>=", "Test if first float is greater than or equal.");
1361 docs.insert("f.<>", "Test if two floats are not equal.");
1362 docs.insert("f.eq", "Test if two floats are equal.");
1363 docs.insert("f.lt", "Test if first float is less than second.");
1364 docs.insert("f.gt", "Test if first float is greater than second.");
1365 docs.insert("f.lte", "Test if first float is less than or equal.");
1366 docs.insert("f.gte", "Test if first float is greater than or equal.");
1367 docs.insert("f.neq", "Test if two floats are not equal.");
1368
1369 docs.insert(
1371 "test.init",
1372 "Initialize the test framework with a test name.",
1373 );
1374 docs.insert("test.finish", "Finish testing and print results.");
1375 docs.insert("test.has-failures", "Check if any tests have failed.");
1376 docs.insert("test.assert", "Assert that a boolean is true.");
1377 docs.insert("test.assert-not", "Assert that a boolean is false.");
1378 docs.insert("test.assert-eq", "Assert that two integers are equal.");
1379 docs.insert("test.assert-eq-str", "Assert that two strings are equal.");
1380 docs.insert("test.fail", "Mark a test as failed with a message.");
1381 docs.insert("test.pass-count", "Get the number of passed assertions.");
1382 docs.insert("test.fail-count", "Get the number of failed assertions.");
1383
1384 docs.insert("time.now", "Get current Unix timestamp in seconds.");
1386 docs.insert(
1387 "time.nanos",
1388 "Get high-resolution monotonic time in nanoseconds.",
1389 );
1390 docs.insert("time.sleep-ms", "Sleep for N milliseconds.");
1391
1392 docs.insert("son.dump", "Serialize any value to SON format (compact).");
1394 docs.insert(
1395 "son.dump-pretty",
1396 "Serialize any value to SON format (pretty-printed).",
1397 );
1398
1399 docs.insert(
1401 "stack.dump",
1402 "Print all stack values and clear the stack (REPL).",
1403 );
1404
1405 docs
1406});
1407
1408#[cfg(test)]
1409mod tests {
1410 use super::*;
1411
1412 #[test]
1413 fn test_builtin_signature_write_line() {
1414 let sig = builtin_signature("io.write-line").unwrap();
1415 let (rest, top) = sig.inputs.clone().pop().unwrap();
1417 assert_eq!(top, Type::String);
1418 assert_eq!(rest, StackType::RowVar("a".to_string()));
1419 assert_eq!(sig.outputs, StackType::RowVar("a".to_string()));
1420 }
1421
1422 #[test]
1423 fn test_builtin_signature_i_add() {
1424 let sig = builtin_signature("i.add").unwrap();
1425 let (rest, top) = sig.inputs.clone().pop().unwrap();
1427 assert_eq!(top, Type::Int);
1428 let (rest2, top2) = rest.pop().unwrap();
1429 assert_eq!(top2, Type::Int);
1430 assert_eq!(rest2, StackType::RowVar("a".to_string()));
1431
1432 let (rest3, top3) = sig.outputs.clone().pop().unwrap();
1433 assert_eq!(top3, Type::Int);
1434 assert_eq!(rest3, StackType::RowVar("a".to_string()));
1435 }
1436
1437 #[test]
1438 fn test_builtin_signature_dup() {
1439 let sig = builtin_signature("dup").unwrap();
1440 assert_eq!(
1442 sig.inputs,
1443 StackType::Cons {
1444 rest: Box::new(StackType::RowVar("a".to_string())),
1445 top: Type::Var("T".to_string())
1446 }
1447 );
1448 let (rest, top) = sig.outputs.clone().pop().unwrap();
1450 assert_eq!(top, Type::Var("T".to_string()));
1451 let (rest2, top2) = rest.pop().unwrap();
1452 assert_eq!(top2, Type::Var("T".to_string()));
1453 assert_eq!(rest2, StackType::RowVar("a".to_string()));
1454 }
1455
1456 #[test]
1457 fn test_all_builtins_have_signatures() {
1458 let sigs = builtin_signatures();
1459
1460 assert!(sigs.contains_key("io.write-line"));
1462 assert!(sigs.contains_key("io.read-line"));
1463 assert!(sigs.contains_key("int->string"));
1464 assert!(sigs.contains_key("i.add"));
1465 assert!(sigs.contains_key("dup"));
1466 assert!(sigs.contains_key("swap"));
1467 assert!(sigs.contains_key("chan.make"));
1468 assert!(sigs.contains_key("chan.send"));
1469 assert!(sigs.contains_key("chan.receive"));
1470 assert!(
1471 sigs.contains_key("string->float"),
1472 "string->float should be a builtin"
1473 );
1474 }
1475
1476 #[test]
1477 fn test_all_docs_have_signatures() {
1478 let sigs = builtin_signatures();
1479 let docs = builtin_docs();
1480
1481 for name in docs.keys() {
1482 assert!(
1483 sigs.contains_key(*name),
1484 "Builtin '{}' has documentation but no signature",
1485 name
1486 );
1487 }
1488 }
1489
1490 #[test]
1491 fn test_all_signatures_have_docs() {
1492 let sigs = builtin_signatures();
1493 let docs = builtin_docs();
1494
1495 for name in sigs.keys() {
1496 assert!(
1497 docs.contains_key(name.as_str()),
1498 "Builtin '{}' has signature but no documentation",
1499 name
1500 );
1501 }
1502 }
1503}