Skip to content

Commit a3f2536

Browse files
committed
add more documentation for builtin functions
1 parent 522972f commit a3f2536

1 file changed

Lines changed: 113 additions & 24 deletions

File tree

src/completion/builtin.rs

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub fn get_builtin_functions() -> Vec<FunctionTemplate> {
5050
.collect()
5151
}
5252

53+
#[allow(warnings)]
5354
pub fn match_callname(call: CallName) -> Option<FunctionTemplate> {
5455
match call {
5556
CallName::UnwrapLeft(aliased_type) => {
@@ -60,25 +61,44 @@ pub fn match_callname(call: CallName) -> Option<FunctionTemplate> {
6061
str_vec![format!("{ty}")],
6162
str_vec![format!("Either<{ty}, U>")],
6263
ty,
63-
"Unwrap left side of `Either`",
64+
"Extracts the left variant of an `Either` value.
65+
66+
Returns the left-side value if it exists, otherwise panics.
67+
68+
```simplicityhl
69+
let x: Either<u8, u8> = Left(42);
70+
let y: u8 = unwrap_left::<u8>(x); // 42
71+
```",
6472
))
6573
}
6674
CallName::UnwrapRight(aliased_type) => {
6775
let ty = aliased_type.to_string();
6876
Some(FunctionTemplate::new(
6977
format!("unwrap_right::<{ty}>"),
70-
"unwrap_left",
78+
"unwrap_right",
7179
str_vec![format!("{ty}")],
7280
str_vec![format!("Either<T, {ty}>")],
7381
ty,
74-
"Unwrap right side of `Either`",
82+
"Extracts the right variant of an `Either` value.
83+
84+
Returns the right-side value if it exists, otherwise panics.
85+
86+
```simplicityhl
87+
let x: Either<u8, u8> = Right(128);
88+
let y: u8 = unwrap_right::<u8>(x); // 128
89+
```",
7590
))
7691
}
7792
CallName::Unwrap => Some(FunctionTemplate::simple(
7893
"unwrap",
7994
str_vec!["Option<T>"],
8095
"T",
81-
"Unwrap `Option` type",
96+
"Unwraps an `Option` value, panicking if it is `None`.
97+
98+
```simplicityhl
99+
let x: Option<u8> = Some(5);
100+
let y: u8 = unwrap(x); // 5
101+
```",
82102
)),
83103
CallName::IsNone(aliased_type) => {
84104
let ty = aliased_type.to_string();
@@ -88,50 +108,119 @@ pub fn match_callname(call: CallName) -> Option<FunctionTemplate> {
88108
str_vec![format!("{ty}")],
89109
str_vec![format!("Option<{ty}>").as_str()],
90110
"bool",
91-
"Check if `Option` is None",
111+
"Checks if an `Option` is `None`.
112+
113+
Returns `true` if the value is `None`, otherwise `false`.
114+
",
92115
))
93116
}
94117
CallName::Assert => Some(FunctionTemplate::simple(
95118
"assert!",
96-
str_vec!["bool"],
119+
str_vec!["condition: bool"],
97120
"()",
98-
"Fails program if argument is 'false'",
121+
"Panics when `condition` is false.",
99122
)),
100123
CallName::Panic => Some(FunctionTemplate::simple(
101124
"panic!",
102125
str_vec![],
103126
"()",
104-
"Fails program",
127+
"Unconditionally terminates program execution.",
105128
)),
106129
CallName::Debug => Some(FunctionTemplate::simple(
107130
"dbg!",
108131
str_vec!["T"],
109132
"T",
110-
"Print value and return it",
133+
"Prints a value if debugging symbols is enabled
134+
and returns it unchanged.
135+
136+
```simplicityhl
137+
let x: u32 = dbg!(42); // prints 42, returns 42
138+
```",
111139
)),
112140
CallName::Fold(_, _) => Some(FunctionTemplate::new(
113-
"fold::<F, B>",
141+
"fold::<f, N>",
114142
"fold",
115-
str_vec!["F", "B"],
116-
str_vec!["iter", "init"],
117-
"B",
118-
"Fold operation over an iterator",
143+
str_vec!["f", "N"],
144+
str_vec!["list: List<E,N>", "initial_accumulator: A"],
145+
"A",
146+
"
147+
Fold a list of bounded length by repeatedly applying a function.
148+
149+
- Signature: `fold::<f, N>(list: List<E, N>, initial_accumulator: A) -> A`
150+
- Fold step: `fn f(element: E, acc: A) -> A`
151+
- Note: `N` is a power of two; lists hold fewer than `N` elements.
152+
153+
Example: sum a list of 32-bit integers.
154+
155+
```simplicityhl
156+
fn sum(elt: u32, acc: u32) -> u32 {
157+
let (_, acc): (bool, u32) = jet::add_32(elt, acc);
158+
acc
159+
}
160+
161+
fn main() {
162+
let xs: List<u32, 8> = list![1, 2, 3];
163+
let s: u32 = fold::<sum, 8>(xs, 0);
164+
assert!(jet::eq_32(s, 6));
165+
}
166+
```
167+
",
119168
)),
120169
CallName::ArrayFold(_, _) => Some(FunctionTemplate::new(
121-
"array_fold::<F, N>",
170+
"array_fold::<f, N>",
122171
"array_fold",
123-
str_vec!["F", "N"],
124-
str_vec!["array", "init"],
125-
"B",
126-
"Fold operation over an array of size N",
172+
str_vec!["f", "N"],
173+
str_vec!["array: [E; N]", "initial_accumulator: A"],
174+
"A",
175+
"
176+
Fold a fixed-size array by repeatedly applying a function.
177+
178+
- Signature: `array_fold::<f, N>(array: [E; N], initial_accumulator: A) -> A`
179+
- Fold step: `fn f(element: E, acc: A) -> A`
180+
181+
Example: sum an array of 7 elements.
182+
183+
```simplicityhl
184+
fn sum(elt: u32, acc: u32) -> u32 {
185+
let (_, acc): (bool, u32) = jet::add_32(elt, acc);
186+
acc
187+
}
188+
189+
fn main() {
190+
let arr: [u32; 7] = [1, 2, 3, 4, 5, 6, 7];
191+
let sum: u32 = array_fold::<sum, 7>(arr, 0);
192+
assert!(jet::eq_32(sum, 28));
193+
}
194+
```",
127195
)),
128196
CallName::ForWhile(_) => Some(FunctionTemplate::new(
129-
"for_while::<F>",
197+
"for_while::<f>",
130198
"for_while",
131-
str_vec!["F"],
132-
str_vec!["condition", "body"],
133-
"()",
134-
"While loop with a function",
199+
str_vec!["f"],
200+
str_vec!["accumulator: A", "context: C"],
201+
"Either<B, A>",
202+
"
203+
Run a function `f` repeatedly with a bounded counter. The loop stops early when the function returns a successful value.
204+
205+
- Signature: `for_while::<f>(initial_accumulator: A, readonly_context: C) -> Either<B, A>`
206+
- Loop body: `fn f(acc: A, ctx: C, counter: uN) -> Either<B, A>` where `N ∈ {1, 2, 4, 8, 16}`
207+
208+
Example: stop when `counter == 10`.
209+
210+
```simplicityhl
211+
fn stop_at_10(acc: (), _: (), i: u8) -> Either<u8, ()> {
212+
match jet::eq_8(i, 10) {
213+
true => Left(i), // success → exit loop
214+
false => Right(acc), // continue with same accumulator
215+
}
216+
}
217+
218+
fn main() {
219+
let out: Either<u8, ()> = for_while::<stop_at_10>((), ());
220+
assert!(jet::eq_8(10, unwrap_left::<()>(out)));
221+
}
222+
```
223+
",
135224
)),
136225
// TODO: implement TypeCast definition
137226
CallName::Jet(_) | CallName::TypeCast(_) | CallName::Custom(_) => None,

0 commit comments

Comments
 (0)