on master help sign in
more optionalization
commita1c62e3202217526d903c27fc26cf7e545c8ecbd authoriko <mail@iko.soy> dateTue 27 Aug 2024, 23:59 (2 years ago) parentbd361ab
3 files changed+161−185
nock/src/interpreter.rs+24−37 nock/src/jets.rs+129−140 nock/src/noun.rs+8−8
nock/src/interpreter.rs+24−37
@@ -90,7 +90,7 @@ fn fas(ctx: &InterpreterContext, addr: Atom, noun: Rc<Noun>) -> Option<Rc<Noun>>
9090 let mut addr_iter = addr.iter_u32_digits();
9191
9292 match addr_iter.len() {
93− 0 => panic!(),
93+ 0 => None,
9494 1 => Some(fas_u32(addr_iter.next()?, noun)?),
9595 _ => {
9696 let rest = fas(ctx, &addr >> 1, noun)?;
@@ -149,7 +149,7 @@ fn hax(
149149 let mut addr_iter = addr.iter_u32_digits();
150150
151151 match addr_iter.len() {
152− 0 => panic!(),
152+ 0 => None,
153153 1 => hax_u32(addr_iter.next().expect("invariant"), new_value, target),
154154 _ => hax(
155155 ctx,
@@ -183,9 +183,7 @@ fn tar_u32(
183183 }
184184 1 => Some(formula),
185185 2 => {
186− let Noun::Cell { p: b, q: c, .. } = (*formula).clone() else {
187− panic!()
188− };
186+ let (b, c) = formula.as_cell()?;
189187
190188 match (*c).clone() {
191189 Noun::Cell { p, q, .. }
@@ -210,9 +208,7 @@ fn tar_u32(
210208 3 => Some(wut(ctx, tar(ctx, subj, formula)?)),
211209 4 => Some(Rc::new(lus(tar(ctx, subj, formula)?)?)),
212210 5 => {
213− let Noun::Cell { p: b, q: c, .. } = (*formula).clone() else {
214− panic!()
215− };
211+ let (b, c) = formula.as_cell()?;
216212 Some(tis(ctx, tar(ctx, subj.clone(), b)?, tar(ctx, subj, c)?))
217213 }
218214 6 => {
@@ -236,21 +232,15 @@ fn tar_u32(
236232 )
237233 }
238234 7 => {
239− let Noun::Cell { p: b, q: c, .. } = (*formula).clone() else {
240− panic!()
241− };
235+ let (b, c) = formula.as_cell()?;
242236 tar(ctx, tar(ctx, subj, b)?, c)
243237 }
244238 8 => {
245− let Noun::Cell { p: b, q: c, .. } = (*formula).clone() else {
246− panic!()
247− };
239+ let (b, c) = formula.as_cell()?;
248240 tar(ctx, cell(tar(ctx, subj.clone(), b)?, subj), c)
249241 }
250242 9 => {
251− let Noun::Cell { p: b, q: c, .. } = (*formula).clone() else {
252− panic!()
253− };
243+ let (b, c) = formula.as_cell()?;
254244 tar_u32(
255245 ctx,
256246 tar(ctx, subj, c)?,
@@ -259,22 +249,19 @@ fn tar_u32(
259249 )
260250 }
261251 10 => {
262− let Noun::Cell { p: b, q: d, .. } = (*formula).clone() else {
263− panic!()
264− };
265− let Noun::Cell { p: b, q: c, .. } = (*b).clone() else {
266− panic!()
267− };
268− let Noun::Atom(b) = (*b).clone() else {
269− panic!()
270− };
252+ let (b, d) = formula.as_cell()?;
253+ let (b, c) = b.as_cell()?;
254+ let b = b.as_atom()?;
271255
272− hax(ctx, b, tar(ctx, subj.clone(), c)?, tar(ctx, subj, d)?)
256+ hax(
257+ ctx,
258+ b.clone(),
259+ tar(ctx, subj.clone(), c)?,
260+ tar(ctx, subj, d)?,
261+ )
273262 }
274263 11 => {
275− let Noun::Cell { p: b, q: d, .. } = (*formula).clone() else {
276− panic!()
277− };
264+ let (b, d) = formula.as_cell()?;
278265 match (*b).clone() {
279266 Noun::Atom(_) => tar(ctx, subj, d),
280267 Noun::Cell { q: c, .. } => tar_u32(
@@ -285,7 +272,7 @@ fn tar_u32(
285272 ),
286273 }
287274 }
288− _ => panic!(),
275+ _ => None,
289276 }
290277}
291278
@@ -296,11 +283,11 @@ pub fn tar(ctx: &InterpreterContext, subj: Rc<Noun>, formula: Rc<Noun>) -> Optio
296283 Noun::Atom(ref op) => {
297284 let mut op_iter = op.iter_u32_digits();
298285 let op = match op_iter.len() {
299− 0 => 0,
300− 1 => op_iter.next().expect("invariant"),
301− _ => panic!(),
286+ 0 => Some(0),
287+ 1 => Some(op_iter.next().unwrap()),
288+ _ => None,
302289 };
303− tar_u32(ctx, subj, op, formula)
290+ tar_u32(ctx, subj, op?, formula)
304291 }
305292 }
306293}
@@ -320,8 +307,8 @@ pub fn eval_gate(ctx: &InterpreterContext, gate: Rc<Noun>) -> Option<Rc<Noun>> {
320307}
321308
322309pub fn slam(ctx: &InterpreterContext, gate: Rc<Noun>, sample: Rc<Noun>) -> Option<Rc<Noun>> {
323− let (battery, payload) = gate.as_cell().unwrap();
324− let (_sample, context) = payload.as_cell().unwrap();
310+ let (battery, payload) = gate.as_cell()?;
311+ let (_sample, context) = payload.as_cell()?;
325312
326313 eval_gate(ctx, cell(battery, cell(sample, context)))
327314}
nock/src/jets.rs+129−140
@@ -1,7 +1,6 @@
11use core::panic;
22use num_integer::Integer;
33use std::{
4− cmp::Ordering,
54 collections::BTreeMap,
65 iter::{once, repeat_n, zip},
76 rc::Rc,
@@ -109,7 +108,7 @@ static BINARY_ATOM_LOOB: fn(
109108 Rc<Noun>,
110109 fn(&Atom, &Atom) -> bool,
111110) -> Option<Rc<Noun>> = |ctx: &InterpreterContext, n: Rc<Noun>, f: fn(&Atom, &Atom) -> bool| {
112− let (p, q) = n.as_cell().unwrap();
111+ let (p, q) = n.as_cell()?;
113112
114113 if f(p.as_atom()?, q.as_atom()?) {
115114 Some(ctx.nouns.sig.clone())
@@ -120,14 +119,14 @@ static BINARY_ATOM_LOOB: fn(
120119
121120static BINARY_ATOM_PAIR: fn(Rc<Noun>, fn(&Atom, &Atom) -> (Atom, Atom)) -> Option<Rc<Noun>> =
122121 |n: Rc<Noun>, f: fn(&Atom, &Atom) -> (Atom, Atom)| {
123− let (p, q) = n.as_cell().unwrap();
122+ let (p, q) = n.as_cell()?;
124123
125− let (a, b) = f(p.as_atom().unwrap(), q.as_atom().unwrap());
124+ let (a, b) = f(p.as_atom()?, q.as_atom()?);
126125 Some(cell(Rc::new(Noun::Atom(a)), Rc::new(Noun::Atom(b))))
127126 };
128127
129128static ADD: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a + b);
130−static DEC: Jet = |_ctx, n| Some(Rc::new(Noun::Atom(n.as_atom().unwrap() - 1u32)));
129+static DEC: Jet = |_ctx, n| Some(Rc::new(Noun::Atom(n.as_atom()? - 1u32)));
131130static DVR: Jet = |_ctx, n| BINARY_ATOM_PAIR(n, |a, b| a.div_rem(&b));
132131static DIV: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a / b);
133132static GTE: Jet = |ctx, n| BINARY_ATOM_LOOB(ctx, n, |a, b| a >= b);
@@ -141,40 +140,40 @@ static MUL: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a * b);
141140static SUB: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a - b);
142141
143142static CAP: Jet = |ctx, n| {
144− let Noun::Atom(n) = (*n).clone() else {
145− panic!()
146− };
147− CAP_ATOM(ctx, n)
143+ let n = n.as_atom()?;
144+ CAP_ATOM(ctx, n.clone())
148145};
146+
149147static CAP_ATOM: fn(&InterpreterContext, Atom) -> Option<Rc<Noun>> = |ctx, n: Atom| {
150148 let mut n_iter = n.iter_u32_digits();
151149
152150 match n_iter.len() {
153− 0 => panic!(),
151+ 0 => None,
154152 1 => CAP_U32(ctx, n_iter.next().unwrap()),
155153 _ => CAP_ATOM(ctx, n >> 1),
156154 }
157155};
158156static CAP_U32: fn(ctx: &InterpreterContext, u32) -> Option<Rc<Noun>> = |ctx, n| match n {
159− 0 | 1 => panic!(),
157+ 0 | 1 => None,
160158 2 => Some(ctx.nouns.two.clone()),
161159 3 => Some(ctx.nouns.three.clone()),
162160 n => CAP_U32(ctx, n >> 1),
163161};
164162
165163static MAS: Jet = |ctx, n| {
166− let Noun::Atom(n) = (*n).clone() else {
167− panic!()
168− };
169− Some(Rc::new(Noun::Atom(MAS_ATOM(ctx, n))))
164+ let n = n.as_atom()?;
165+ Some(Rc::new(Noun::Atom(MAS_ATOM(ctx, n.clone())?)))
170166};
171−static MAS_ATOM: fn(&InterpreterContext, Atom) -> Atom = |ctx, n| {
167+static MAS_ATOM: fn(&InterpreterContext, Atom) -> Option<Atom> = |ctx, n| {
172168 let mut n_iter = n.iter_u32_digits();
173169
174170 match n_iter.len() {
175− 0 => panic!(),
176− 1 => Atom::new(vec![MAS_U32(n_iter.next().unwrap())]),
177− _ => (&n & &ctx.big_uints.one) + (MAS_ATOM(ctx, &n >> 1) << 1),
171+ 0 => None,
172+ 1 => match n_iter.next().unwrap() {
173+ 1 => None,
174+ x => Some(Atom::new(vec![MAS_U32(x)])),
175+ },
176+ _ => Some((&n & &ctx.big_uints.one) + (MAS_ATOM(ctx, &n >> 1)? << 1)),
178177 }
179178};
180179static MAS_U32: fn(u32) -> u32 = |n| match n {
@@ -184,26 +183,24 @@ static MAS_U32: fn(u32) -> u32 = |n| match n {
184183};
185184
186185static PEG: Jet = |ctx, n| {
187− let Noun::Cell { p: a, q: b, .. } = (*n).clone() else {
188− panic!()
189− };
186+ let (a, b) = n.as_cell()?;
190187
191− let a = a.as_atom().unwrap();
192− let b = b.as_atom().unwrap();
188+ let a = a.as_atom()?;
189+ let b = b.as_atom()?;
193190
194191 if *a == Atom::ZERO {
195− panic!()
192+ return None;
196193 };
197194
198− Some(Rc::new(Noun::Atom(PEG_ATOM(ctx, a, b.clone()))))
195+ Some(Rc::new(Noun::Atom(PEG_ATOM(ctx, a, b.clone())?)))
199196};
200−static PEG_ATOM: fn(&InterpreterContext, &Atom, Atom) -> Atom = |ctx, a, b| {
197+static PEG_ATOM: fn(&InterpreterContext, &Atom, Atom) -> Option<Atom> = |ctx, a, b| {
201198 let mut b_iter = b.iter_u32_digits();
202199
203200 match b_iter.len() {
204− 0 => panic!(),
205− 1 => PEG_U32(a, b_iter.next().unwrap()),
206− _ => (&b & &ctx.big_uints.one) + (PEG_ATOM(ctx, a, b >> 1) << 1),
201+ 0 => None,
202+ 1 => Some(PEG_U32(a, b_iter.next().unwrap())),
203+ _ => Some((&b & &ctx.big_uints.one) + (PEG_ATOM(ctx, a, b >> 1)? << 1)),
207204 }
208205};
209206static PEG_U32: fn(&Atom, u32) -> Atom = |a, b| match b {
@@ -215,7 +212,7 @@ static PEG_U32: fn(&Atom, u32) -> Atom = |a, b| match b {
215212};
216213
217214static FAND: Jet = |ctx, n| {
218− let (nedl, hstk) = n.as_cell().unwrap();
215+ let (nedl, hstk) = n.as_cell()?;
219216 if nedl.is_sig() || hstk.is_sig() {
220217 return Some(ctx.nouns.sig.clone());
221218 }
@@ -245,7 +242,7 @@ static FAND: Jet = |ctx, n| {
245242};
246243
247244static FIND: Jet = |ctx, n| {
248− let (nedl, hstk) = n.as_cell().unwrap();
245+ let (nedl, hstk) = n.as_cell()?;
249246 if nedl.is_sig() || hstk.is_sig() {
250247 return Some(ctx.nouns.sig.clone());
251248 }
@@ -280,13 +277,13 @@ static FLOP: Jet = |ctx, n| {
280277};
281278
282279static GULF: Jet = |_ctx, n| {
283− let (from, to) = n.as_cell().unwrap();
280+ let (from, to) = n.as_cell()?;
284281
285− let mut from = (*from.as_atom().unwrap()).clone();
286− let to = to.as_atom().unwrap().clone();
282+ let mut from = (*from.as_atom()?).clone();
283+ let to = to.as_atom()?.clone();
287284
288285 if from > to {
289− panic!()
286+ return None;
290287 }
291288
292289 let mut tmp = Vec::new();
@@ -300,10 +297,10 @@ static GULF: Jet = |_ctx, n| {
300297};
301298
302299static INTO: Jet = |_ctx, n| {
303− let (a, b) = n.as_cell().unwrap();
304− let (b, c) = b.as_cell().unwrap();
300+ let (a, b) = n.as_cell()?;
301+ let (b, c) = b.as_cell()?;
305302
306− let mut b_iter = b.as_atom().unwrap().iter_u32_digits();
303+ let mut b_iter = b.as_atom()?.iter_u32_digits();
307304
308305 match b_iter.len() {
309306 0 => Some(cell(c, a)),
@@ -331,13 +328,13 @@ static INTO: Jet = |_ctx, n| {
331328};
332329
333330static JOIN: Jet = |_ctx, n| {
334− let (sep, list) = n.as_cell().unwrap();
331+ let (sep, list) = n.as_cell()?;
335332 Some(Noun::list_refs(
336333 list.list_iter()
337334 .intersperse(sep)
338335 .collect::<Vec<_>>()
339336 .iter()
340− .map(|x| x.clone()),
337+ .cloned(),
341338 ))
342339};
343340
@@ -348,7 +345,7 @@ static LENT: Jet = |_ctx, n| {
348345};
349346
350347static LEVY: Jet = |ctx, n| {
351− let (a, b) = n.as_cell().unwrap();
348+ let (a, b) = n.as_cell()?;
352349 if traverse_iter_option(a.list_iter().map(|n| slam(ctx, b.clone(), n)))?
353350 .iter()
354351 .all(|x| x.is_y())
@@ -360,7 +357,7 @@ static LEVY: Jet = |ctx, n| {
360357};
361358
362359static LIEN: Jet = |ctx, n| {
363− let (a, b) = n.as_cell().unwrap();
360+ let (a, b) = n.as_cell()?;
364361 if traverse_iter_option(a.list_iter().map(|n| slam(ctx, b.clone(), n)))?
365362 .iter()
366363 .any(|x| x.is_y())
@@ -372,7 +369,7 @@ static LIEN: Jet = |ctx, n| {
372369};
373370
374371static MURN: Jet = |ctx, n| {
375− let (a, b) = n.as_cell().unwrap();
372+ let (a, b) = n.as_cell()?;
376373
377374 Some(Noun::list_refs(
378375 a.list_iter()
@@ -384,7 +381,7 @@ static MURN: Jet = |ctx, n| {
384381};
385382
386383static REAP: Jet = |_ctx, n| {
387− let (a, b) = n.as_cell().unwrap();
384+ let (a, b) = n.as_cell()?;
388385
389386 Some(Noun::list_refs(repeat_n(b, a.as_u32().unwrap() as usize)))
390387};
@@ -392,7 +389,7 @@ static REAP: Jet = |_ctx, n| {
392389static REAR: Jet = |_ctx, n| n.list_iter().last();
393390
394391static REEL: Jet = |ctx, n| {
395− let (list, gate) = n.as_cell().unwrap();
392+ let (list, gate) = n.as_cell()?;
396393
397394 list.list_iter().collect::<Vec<_>>().iter().rfold(
398395 Some(gate.clone().gate_sample()?.as_cell()?.1),
@@ -401,7 +398,7 @@ static REEL: Jet = |ctx, n| {
401398};
402399
403400static ROLL: Jet = |ctx, n| {
404− let (list, gate) = n.as_cell().unwrap();
401+ let (list, gate) = n.as_cell()?;
405402
406403 list.list_iter().fold(
407404 Some(gate.clone().gate_sample()?.as_cell()?.1),
@@ -410,7 +407,7 @@ static ROLL: Jet = |ctx, n| {
410407};
411408
412409static SCAG: Jet = |_ctx, n| {
413− let (a, b) = n.as_cell().unwrap();
410+ let (a, b) = n.as_cell()?;
414411
415412 Some(Noun::list_refs(
416413 b.list_iter()
@@ -422,7 +419,7 @@ static SCAG: Jet = |_ctx, n| {
422419};
423420
424421static SKID: Jet = |ctx, n| {
425− let (list, gate) = n.as_cell().unwrap();
422+ let (list, gate) = n.as_cell()?;
426423
427424 let slammed_n = traverse_iter_option(
428425 list.list_iter()
@@ -437,7 +434,7 @@ static SKID: Jet = |ctx, n| {
437434};
438435
439436static SKIM: Jet = |ctx, n| {
440− let (list, gate) = n.as_cell().unwrap();
437+ let (list, gate) = n.as_cell()?;
441438
442439 Some(Noun::list_refs(
443440 traverse_iter_option(
@@ -452,7 +449,7 @@ static SKIM: Jet = |ctx, n| {
452449};
453450
454451static SKIP: Jet = |ctx, n| {
455− let (list, gate) = n.as_cell().unwrap();
452+ let (list, gate) = n.as_cell()?;
456453
457454 Some(Noun::list_refs(
458455 traverse_iter_option(
@@ -467,9 +464,9 @@ static SKIP: Jet = |ctx, n| {
467464};
468465
469466static SLAG: Jet = |ctx, n| {
470− let (a, list) = n.as_cell().unwrap();
467+ let (a, list) = n.as_cell()?;
471468
472− let mut a_iter = a.as_atom().unwrap().iter_u32_digits();
469+ let mut a_iter = a.as_atom()?.iter_u32_digits();
473470
474471 match a_iter.len() {
475472 0 => Some(list),
@@ -485,7 +482,7 @@ static SLAG: Jet = |ctx, n| {
485482};
486483
487484static SNAG: Jet = |_ctx, n| {
488− let (a, list) = n.as_cell().unwrap();
485+ let (a, list) = n.as_cell()?;
489486
490487 list.list_iter().nth(a.as_u32().unwrap() as usize)
491488};
@@ -503,7 +500,7 @@ static SNIP: Jet = |_ctx, n| {
503500};
504501
505502static SNOC: Jet = |_ctx, n| {
506− let (list, b) = n.as_cell().unwrap();
503+ let (list, b) = n.as_cell()?;
507504
508505 Some(Noun::list_refs(
509506 list.list_iter()
@@ -533,13 +530,13 @@ static SNOC: Jet = |_ctx, n| {
533530// };
534531
535532static SPIN: Jet = |ctx, n| {
536− let (list, b) = n.as_cell().unwrap();
537− let (mut acc, gate) = b.as_cell().unwrap();
533+ let (list, b) = n.as_cell()?;
534+ let (mut acc, gate) = b.as_cell()?;
538535
539536 let mut res = Vec::new();
540537
541538 for n in list.list_iter() {
542− let (el, new_acc) = slam(ctx, gate.clone(), cell(n, acc))?.as_cell().unwrap();
539+ let (el, new_acc) = slam(ctx, gate.clone(), cell(n, acc))?.as_cell()?;
543540 acc = new_acc;
544541 res.push(el);
545542 }
@@ -548,13 +545,13 @@ static SPIN: Jet = |ctx, n| {
548545};
549546
550547static SPUN: Jet = |ctx, n| {
551− let (list, gate) = n.as_cell().unwrap();
548+ let (list, gate) = n.as_cell()?;
552549
553550 let mut res = Vec::new();
554− let mut acc = gate.clone().gate_sample().unwrap().as_cell().unwrap().1;
551+ let mut acc = gate.clone().gate_sample()?.as_cell()?.1;
555552
556553 for n in list.list_iter() {
557− let (el, new_acc) = slam(ctx, gate.clone(), cell(n, acc))?.as_cell().unwrap();
554+ let (el, new_acc) = slam(ctx, gate.clone(), cell(n, acc))?.as_cell()?;
558555 acc = new_acc;
559556 res.push(el);
560557 }
@@ -563,7 +560,7 @@ static SPUN: Jet = |ctx, n| {
563560};
564561
565562static TURN: Jet = |ctx, n| {
566− let (list, gate) = n.as_cell().unwrap();
563+ let (list, gate) = n.as_cell()?;
567564 Some(Noun::list_refs(
568565 traverse_iter_option(list.list_iter().map(|x| slam(ctx, gate.clone(), x)))?
569566 .iter()
@@ -572,7 +569,7 @@ static TURN: Jet = |ctx, n| {
572569};
573570
574571static WELD: Jet = |_ctx, n| {
575− let (a, b) = n.as_cell().unwrap();
572+ let (a, b) = n.as_cell()?;
576573
577574 Some(Noun::list_refs(
578575 a.list_iter()
@@ -603,43 +600,42 @@ static BEX: Jet = |ctx, n| {
603600};
604601
605602static END: Jet = |ctx, n| {
606− let (a, b) = n.as_cell().unwrap();
607− let bite = a.as_bite().unwrap();
603+ let (a, b) = n.as_cell()?;
604+ let bite = a.as_bite();
608605
609606 let bits = bite.bits();
610607
611608 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
612609
613− Some(Rc::new(Noun::Atom(mask & b.as_atom().unwrap())))
610+ Some(Rc::new(Noun::Atom(mask & b.as_atom()?)))
614611};
615612
616613static CAN: Jet = |ctx, n| {
617− let (a, b) = n.as_cell().unwrap();
614+ let (a, b) = n.as_cell()?;
618615 let bloq = a.as_u32().unwrap();
619616 let bloq_ = 2u32.pow(bloq);
620617
621− let atom: Atom =
622− b.list_iter()
623− .collect::<Vec<_>>()
624− .iter()
625− .rfold(ctx.big_uints.zero.clone(), |acc, next| {
626− let (p, q) = next.as_cell().unwrap();
627− let step = p.as_u32().unwrap();
628− let bits = bloq_ * step;
629− let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
618+ let atom: Option<Atom> = b.list_iter().collect::<Vec<_>>().iter().rfold(
619+ Some(ctx.big_uints.zero.clone()),
620+ |acc, next| {
621+ let (p, q) = next.as_cell()?;
622+ let step = p.as_u32().unwrap();
623+ let bits = bloq_ * step;
624+ let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
630625
631− (acc << bits) | (mask & q.as_atom().unwrap())
632− });
633− Some(Rc::new(Noun::Atom(atom)))
626+ Some((acc? << bits) | (mask & q.as_atom()?))
627+ },
628+ );
629+ Some(Rc::new(Noun::Atom(atom?)))
634630};
635631
636632static CAT: Jet = |_ctx, n| {
637− let (bloq, b) = n.as_cell().unwrap();
638− let (b, c) = b.as_cell().unwrap();
633+ let (bloq, b) = n.as_cell()?;
634+ let (b, c) = b.as_cell()?;
639635
640− let bloq = bloq.as_u32().unwrap();
641− let b = b.as_atom().unwrap();
642− let c = c.as_atom().unwrap();
636+ let bloq = bloq.as_u32()?;
637+ let b = b.as_atom()?;
638+ let c = c.as_atom()?;
643639
644640 Some(Rc::new(Noun::Atom(
645641 (c << (met(bloq, b.clone()) * 2u32.pow(bloq))) | b,
@@ -647,8 +643,8 @@ static CAT: Jet = |_ctx, n| {
647643};
648644
649645static MET: Jet = |_ctx, n| {
650− let (bloq, b) = n.as_cell().unwrap();
651− let b: Atom = b.as_atom().unwrap().clone();
646+ let (bloq, b) = n.as_cell()?;
647+ let b: Atom = b.as_atom()?.clone();
652648 let bloq = bloq.as_u32().unwrap();
653649
654650 Some(Rc::new(Noun::from_u32(met(bloq, b))))
@@ -668,28 +664,28 @@ fn met(bloq: u32, mut a: Atom) -> u32 {
668664}
669665
670666static CUT: Jet = |ctx, n| {
671− let (a, b) = n.as_cell().unwrap();
672− let (b, d) = b.as_cell().unwrap();
673− let (b, c) = b.as_cell().unwrap();
667+ let (a, b) = n.as_cell()?;
668+ let (b, d) = b.as_cell()?;
669+ let (b, c) = b.as_cell()?;
674670
675671 let bits = 2u32.pow(a.as_u32().unwrap());
676672
677673 let mask = (&ctx.big_uints.one << (bits * c.as_u32().unwrap())) - &ctx.big_uints.one;
678674
679675 Some(Rc::new(Noun::Atom(
680− (d.as_atom().unwrap() >> (bits * b.as_u32().unwrap())) & mask,
676+ (d.as_atom()? >> (bits * b.as_u32().unwrap())) & mask,
681677 )))
682678};
683679
684680static FIL: Jet = |ctx, n| {
685− let (a, b) = n.as_cell().unwrap();
686− let (b, c) = b.as_cell().unwrap();
681+ let (a, b) = n.as_cell()?;
682+ let (b, c) = b.as_cell()?;
687683
688684 let bits = 2u32.pow(a.as_u32().unwrap());
689685
690686 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
691687
692− let src = c.as_atom().unwrap() & mask;
688+ let src = c.as_atom()? & mask;
693689
694690 let mut target = ctx.big_uints.zero.clone();
695691
@@ -701,30 +697,30 @@ static FIL: Jet = |ctx, n| {
701697};
702698
703699static LSH: Jet = |_ctx, n| {
704− let (a, b) = n.as_cell().unwrap();
705− let a = a.as_bite().unwrap();
706− let b = b.as_atom().unwrap();
700+ let (a, b) = n.as_cell()?;
701+ let a = a.as_bite();
702+ let b = b.as_atom()?;
707703
708704 Some(Rc::new(Noun::Atom(b << a.bits())))
709705};
710706
711707static RSH: Jet = |_ctx, n| {
712− let (a, b) = n.as_cell().unwrap();
713− let a = a.as_bite().unwrap();
714− let b = b.as_atom().unwrap();
708+ let (a, b) = n.as_cell()?;
709+ let a = a.as_bite();
710+ let b = b.as_atom()?;
715711
716712 Some(Rc::new(Noun::Atom(b >> a.bits())))
717713};
718714
719715static RAP: Jet = |ctx, n| {
720− let (bloq, b) = n.as_cell().unwrap();
716+ let (bloq, b) = n.as_cell()?;
721717 let bloq = bloq.as_u32().unwrap();
722718 let bits = 2u32.pow(bloq);
723719
724720 let mut target = ctx.big_uints.zero.clone();
725721
726722 for el in b.list_iter().collect::<Vec<_>>().iter().rev() {
727− let el = el.as_atom().unwrap();
723+ let el = el.as_atom()?;
728724 if el != &ctx.big_uints.zero {
729725 target = (target << (bits * met(bloq, el.clone()))) | el;
730726 }
@@ -734,8 +730,8 @@ static RAP: Jet = |ctx, n| {
734730};
735731
736732static REP: Jet = |ctx, n| {
737− let (bite, b) = n.as_cell().unwrap();
738− let bite = bite.as_bite().unwrap();
733+ let (bite, b) = n.as_cell()?;
734+ let bite = bite.as_bite();
739735
740736 let bits = bite.bits();
741737 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -743,18 +739,18 @@ static REP: Jet = |ctx, n| {
743739 let mut target = ctx.big_uints.zero.clone();
744740
745741 for el in b.list_iter().collect::<Vec<_>>().iter().rev() {
746− target = (target << bits) | (el.as_atom().unwrap() & &mask);
742+ target = (target << bits) | (el.as_atom()? & &mask);
747743 }
748744
749745 Some(Rc::new(Noun::Atom(target)))
750746};
751747
752748static REV: Jet = |ctx, n| {
753− let (bloq, b) = n.as_cell().unwrap();
754− let (len, dat) = b.as_cell().unwrap();
749+ let (bloq, b) = n.as_cell()?;
750+ let (len, dat) = b.as_cell()?;
755751 let bloq = bloq.as_u32().unwrap();
756752 let len = len.as_u32().unwrap();
757− let mut dat: Atom = dat.as_atom().unwrap().clone();
753+ let mut dat: Atom = dat.as_atom()?.clone();
758754
759755 let bits = 2u32.pow(bloq);
760756 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -770,9 +766,9 @@ static REV: Jet = |ctx, n| {
770766};
771767
772768static RIP: Jet = |ctx, n| {
773− let (bite, b) = n.as_cell().unwrap();
774− let bite = bite.as_bite().unwrap();
775− let mut b: Atom = b.as_atom().unwrap().clone();
769+ let (bite, b) = n.as_cell()?;
770+ let bite = bite.as_bite();
771+ let mut b: Atom = b.as_atom()?.clone();
776772
777773 let bits = bite.bits();
778774 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -788,11 +784,11 @@ static RIP: Jet = |ctx, n| {
788784};
789785
790786static RUN: Jet = |ctx, n| {
791− let (bite, b) = n.as_cell().unwrap();
792− let (b, gate) = b.as_cell().unwrap();
787+ let (bite, b) = n.as_cell()?;
788+ let (b, gate) = b.as_cell()?;
793789
794− let bite = bite.as_bite().unwrap();
795− let mut b: Atom = b.as_atom().unwrap().clone();
790+ let bite = bite.as_bite();
791+ let mut b: Atom = b.as_atom()?.clone();
796792
797793 let bits = bite.bits();
798794 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -801,10 +797,7 @@ static RUN: Jet = |ctx, n| {
801797
802798 while b > ctx.big_uints.zero {
803799 target = (target << bits)
804− | ((*slam(ctx, gate.clone(), Rc::new(Noun::Atom(&mask & &b)))?)
805− .as_atom()
806− .unwrap()
807− & &mask);
800+ | ((*slam(ctx, gate.clone(), Rc::new(Noun::Atom(&mask & &b)))?).as_atom()? & &mask);
808801 b = b >> bits;
809802 }
810803
@@ -812,11 +805,11 @@ static RUN: Jet = |ctx, n| {
812805};
813806
814807static RUT: Jet = |ctx, n| {
815− let (bite, b) = n.as_cell().unwrap();
816− let (b, gate) = b.as_cell().unwrap();
808+ let (bite, b) = n.as_cell()?;
809+ let (b, gate) = b.as_cell()?;
817810
818− let bite = bite.as_bite().unwrap();
819− let mut b: Atom = b.as_atom().unwrap().clone();
811+ let bite = bite.as_bite();
812+ let mut b: Atom = b.as_atom()?.clone();
820813
821814 let bits = bite.bits();
822815 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -824,12 +817,8 @@ static RUT: Jet = |ctx, n| {
824817 let mut target = Vec::new();
825818
826819 while b > ctx.big_uints.zero {
827− target.push(
828− (*slam(ctx, gate.clone(), Rc::new(Noun::Atom(&mask & &b)))?)
829− .as_atom()
830− .unwrap()
831− & &mask,
832− );
820+ target
821+ .push((*slam(ctx, gate.clone(), Rc::new(Noun::Atom(&mask & &b)))?).as_atom()? & &mask);
833822 b = b >> bits;
834823 }
835824
@@ -837,12 +826,12 @@ static RUT: Jet = |ctx, n| {
837826};
838827
839828static SEW: Jet = |ctx, n| {
840− let (a, b) = n.as_cell().unwrap();
841− let (b, recipient) = b.as_cell().unwrap();
842− let (sew_start, c) = b.as_cell().unwrap();
843− let (number_of_blocks, donor) = c.as_cell().unwrap();
844− let recipient = recipient.as_atom().unwrap();
845− let donor = donor.as_atom().unwrap();
829+ let (a, b) = n.as_cell()?;
830+ let (b, recipient) = b.as_cell()?;
831+ let (sew_start, c) = b.as_cell()?;
832+ let (number_of_blocks, donor) = c.as_cell()?;
833+ let recipient = recipient.as_atom()?;
834+ let donor = donor.as_atom()?;
846835
847836 let bloq = a.as_u32().unwrap();
848837 let bits = 2u32.pow(bloq);
@@ -858,12 +847,12 @@ static SEW: Jet = |ctx, n| {
858847};
859848
860849static SWP: Jet = |ctx, n| {
861− let (bloq, b) = n.as_cell().unwrap();
850+ let (bloq, b) = n.as_cell()?;
862851 let bloq = bloq.as_u32().unwrap();
863852 let bits = 2u32.pow(bloq);
864853 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
865854
866− let mut source = b.as_atom().unwrap().clone();
855+ let mut source = b.as_atom()?.clone();
867856 let mut target = ctx.big_uints.zero.clone();
868857
869858 while source > ctx.big_uints.zero {
@@ -875,7 +864,7 @@ static SWP: Jet = |ctx, n| {
875864};
876865
877866static XEB: Jet = |_ctx, n| {
878− let mut a = n.as_atom().unwrap().clone();
867+ let mut a = n.as_atom()?.clone();
879868
880869 let mut c = 0;
881870
@@ -892,14 +881,14 @@ static DIS: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a & b);
892881static MIX: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a ^ b);
893882
894883static NOT: Jet = |ctx, n| {
895− let (a, b) = n.as_cell().unwrap();
896− let (b, c) = b.as_cell().unwrap();
884+ let (a, b) = n.as_cell()?;
885+ let (b, c) = b.as_cell()?;
897886
898887 let bloq = a.as_u32().unwrap();
899888 let bits = 2u32.pow(bloq);
900889 let mask = (&ctx.big_uints.one << (bits * b.as_u32().unwrap())) - &ctx.big_uints.one;
901890
902− let target = c.as_atom().unwrap() ^ &mask;
891+ let target = c.as_atom()? ^ &mask;
903892
904893 Some(Rc::new(Noun::Atom(target)))
905894};
nock/src/noun.rs+8−8
@@ -146,19 +146,19 @@ impl Noun {
146146 (hash_pair((*battery).clone(), context), sample)
147147 }
148148
149− pub fn as_bite(self: &Self) -> Option<Bite> {
149+ pub fn as_bite(self: &Self) -> Bite {
150150 match self {
151− Noun::Atom(_) => Option::Some(Bite {
152− bloq: self.as_u32()?,
151+ Noun::Atom(_) => Bite {
152+ bloq: self.as_u32().unwrap(),
153153 step: 1,
154− }),
154+ },
155155
156156 Noun::Cell {
157157 p: bloq, q: step, ..
158− } => Option::Some(Bite {
159− bloq: bloq.as_u32()?,
160− step: step.as_u32()?,
161− }),
158+ } => Bite {
159+ bloq: bloq.as_u32().unwrap(),
160+ step: step.as_u32().unwrap(),
161+ },
162162 }
163163 }
164164}