1use crate::symbols::{Symbol, SymbolKey, SymbolKind, Visibility};
4use crisp_ast::Span;
5use std::collections::BTreeMap;
6
7#[derive(Debug, Clone, Copy)]
8pub struct StdFn {
9 pub module: &'static str,
10 pub name: &'static str,
11 pub rust_path: &'static str,
12}
13
14pub fn std_functions() -> &'static [StdFn] {
16 &[
17 StdFn {
18 module: "std.vec",
19 name: "new",
20 rust_path: "Vec::new",
21 },
22 StdFn {
23 module: "std.vec",
24 name: "push",
25 rust_path: "Vec::push",
26 },
27 StdFn {
28 module: "std.vec",
29 name: "len",
30 rust_path: "Vec::len",
31 },
32 StdFn {
33 module: "std.fs",
34 name: "read_to_string",
35 rust_path: "std::fs::read_to_string",
36 },
37 StdFn {
38 module: "std.io",
39 name: "stdin_line",
40 rust_path: "std::io::stdin",
41 },
42 StdFn {
43 module: "std.sync",
44 name: "sleep_ms",
45 rust_path: "tokio::time::sleep",
46 },
47 StdFn {
48 module: "std.atomic",
49 name: "new_int",
50 rust_path: "std::sync::atomic::AtomicI64::new",
51 },
52 StdFn {
53 module: "std.net",
54 name: "parse_ip",
55 rust_path: "std::net::parse_ip",
56 },
57 StdFn {
58 module: "std.math",
59 name: "exp",
60 rust_path: "f64::exp",
61 },
62 StdFn {
63 module: "std.math",
64 name: "sin",
65 rust_path: "f64::sin",
66 },
67 StdFn {
68 module: "std.math",
69 name: "cos",
70 rust_path: "f64::cos",
71 },
72 StdFn {
73 module: "std.math",
74 name: "tanh",
75 rust_path: "f64::tanh",
76 },
77 StdFn {
78 module: "std.math",
79 name: "sqrt",
80 rust_path: "f64::sqrt",
81 },
82 ]
83}
84
85pub fn stdlib_symbols() -> Vec<Symbol> {
86 let span = Span::default();
87 let mut out = Vec::new();
88 for f in std_functions() {
89 out.push(Symbol {
90 key: SymbolKey {
91 module: f.module.to_string(),
92 name: f.name.to_string(),
93 },
94 kind: SymbolKind::PreludeFn,
95 visibility: Visibility::Public,
96 span,
97 from_prelude: true,
98 });
99 }
100 for (module, types) in [
101 ("std.option", ["Option"]),
102 ("std.result", ["Result"]),
103 ("std.string", ["String"]),
104 ("std.vec", ["Vec"]),
105 ("std.map", ["HashMap"]),
106 ("std.set", ["HashSet"]),
107 ] {
108 for name in types {
109 out.push(Symbol {
110 key: SymbolKey {
111 module: module.to_string(),
112 name: name.to_string(),
113 },
114 kind: SymbolKind::PreludeType,
115 visibility: Visibility::Public,
116 span,
117 from_prelude: true,
118 });
119 }
120 }
121 out
122}
123
124pub fn stdlib_fn_modules() -> BTreeMap<String, String> {
125 let mut map = BTreeMap::new();
126 for f in std_functions() {
127 map.insert(f.name.to_string(), f.module.to_string());
128 }
129 map
130}
131
132pub fn std_rust_path(module: &str, name: &str) -> Option<&'static str> {
133 std_functions()
134 .iter()
135 .find(|f| f.module == module && f.name == name)
136 .map(|f| f.rust_path)
137}