@@ -1,18 +1,39 @@
11use core::panic;
2+use murmur3::murmur3_32;
23use num_integer::Integer;
34use std::{
4− cmp::Ordering,
5+ borrow::Borrow,
56 collections::BTreeMap,
67 iter::{once, repeat_n, zip},
78 rc::Rc,
89};
910
1011use crate::{
11− interpreter::{slam, InterpreterContext},
12− noun::{self, cell, Atom, Bite, Noun},
12+ interpreter::{slam_pulled_gate, InterpreterContext, TTanks, EMPTY_TANKS},
13+ noun::{self, cell, AsNoun, Atom, Edge, Hair, Nail, Noun},
1314};
1415
16+fn traverse_iter_result<E: Clone, T: Clone, I: Iterator<Item = Result<T, E>>>(
17+ xs: I,
18+) -> Result<Vec<T>, E> {
19+ let mut target: Vec<T> = Vec::new();
20+
21+ for x in xs {
22+ target.push(x.clone()?)
23+ }
24+
25+ Ok(target)
26+}
27+
1528pub type Jets = BTreeMap<noun::Hash, Jet>;
29+pub type DoubleJets = BTreeMap<noun::Hash, DoubleJet>;
30+
31+pub fn generate_double_jets() -> DoubleJets {
32+ BTreeMap::from([
33+ (210326504639957723220404138306543145797, JUST),
34+ // (255213039400865122956425872354360738044, STIR),
35+ ])
36+}
1637
1738pub fn generate_jets() -> Jets {
1839 BTreeMap::from([
@@ -54,7 +75,7 @@ pub fn generate_jets() -> Jets {
5475 (288515703007942177334798811660651439897, SNAG),
5576 (322006565952434408151902864454572452802, SNIP),
5677 (290837622185731774795798043954046795930, SNOC),
57− (255402796382159268271460433298209603428, SORT),
78+ // (255402796382159268271460433298209603428, SORT),
5879 (10111398404342329752608067595798409804, SPIN),
5980 (209194119090564757355985847956576215234, SPUN),
6081 (103985097318911737222583881947453560061, TURN),
@@ -78,43 +99,72 @@ pub fn generate_jets() -> Jets {
7899 (52890757765966568893620799383929397067, RUT),
79100 (189837220965666741450798472864087835815, SEW),
80101 (122271149828189266412416527758198690539, SWP),
102+ (315388312266248574424227833154020891850, XEB),
103+ (26880808488368366377851132561579207892, CON),
104+ (311027486926441687148059942599447850754, DIS),
105+ (330927609077697663248483044495963838437, MIX),
106+ (39579736615539990724554083585557839000, NOT),
107+ (3482046397915506566435874021936376320, LAST),
108+ (124976064672625230427889101290245200507, TRIP),
109+ (4078279288073906183633157465551067635, MUK),
110+ (261900159748071234702685239353558471892, MUG),
111+ (198191257620924788695943358402651905954, AOR),
112+ (299422828284010240628877806738195667652, DOR),
113+ (166326796552601297695313176538120818632, GOR),
114+ (7942398240152905363972500667649380756, MOR),
115+ (315687984998079865981003916817293521765, LOOK),
81116 ])
82117}
83118
84−pub type Jet = fn(ctx: &InterpreterContext, Rc<Noun>) -> Rc<Noun>;
119+pub type Jet = fn(ctx: &mut InterpreterContext, &Rc<Noun>) -> Result<Rc<Noun>, TTanks>;
120+pub type DoubleJet = fn(ctx: &mut InterpreterContext, &Rc<Noun>, &Rc<Noun>) -> Option<Rc<Noun>>;
85121
86−static BINARY_ATOM: fn(Rc<Noun>, fn(&Atom, &Atom) -> Atom) -> Rc<Noun> =
87− |n: Rc<Noun>, f: fn(&Atom, &Atom) -> Atom| {
88− let Noun::Cell { p, q, .. } = (*n).clone() else {
89− panic!()
90− };
91− Rc::new(Noun::Atom(f(p.as_atom().unwrap(), q.as_atom().unwrap())))
122+static BINARY_ATOM: fn(&Rc<Noun>, fn(&Atom, &Atom) -> Atom) -> Result<Rc<Noun>, TTanks> =
123+ |n: &Rc<Noun>, f: fn(&Atom, &Atom) -> Atom| {
124+ let (p, q) = n.as_cell().ok_or(TTanks::new())?;
125+ Ok(Rc::new(Noun::Atom(f(
126+ p.as_atom().ok_or(TTanks::new())?,
127+ q.as_atom().ok_or(TTanks::new())?,
128+ ))))
92129 };
93130
94−static BINARY_ATOM_LOOB: fn(&InterpreterContext, Rc<Noun>, fn(&Atom, &Atom) -> bool) -> Rc<Noun> =
95− |ctx: &InterpreterContext, n: Rc<Noun>, f: fn(&Atom, &Atom) -> bool| {
96− let Noun::Cell { p, q, .. } = (*n).clone() else {
97− panic!()
98− };
99−
100− if f(p.as_atom().unwrap(), q.as_atom().unwrap()) {
101− ctx.nouns.sig.clone()
131+static BINARY_ATOM_LOOB: fn(
132+ &InterpreterContext,
133+ &Rc<Noun>,
134+ fn(&Atom, &Atom) -> bool,
135+) -> Result<Rc<Noun>, TTanks> =
136+ |ctx: &InterpreterContext, n: &Rc<Noun>, f: fn(&Atom, &Atom) -> bool| {
137+ let (p, q) = n.as_cell().ok_or(TTanks::new())?;
138+
139+ if f(
140+ p.as_atom().ok_or(TTanks::new())?,
141+ q.as_atom().ok_or(TTanks::new())?,
142+ ) {
143+ Ok(ctx.nouns.sig.clone())
102144 } else {
103− ctx.nouns.one.clone()
145+ Ok(ctx.nouns.one.clone())
104146 }
105147 };
106148
107−static BINARY_ATOM_PAIR: fn(Rc<Noun>, fn(&Atom, &Atom) -> (Atom, Atom)) -> Rc<Noun> =
108− |n: Rc<Noun>, f: fn(&Atom, &Atom) -> (Atom, Atom)| {
109− let Noun::Cell { p, q, .. } = (*n).clone() else {
110− panic!()
111− };
112− let (a, b) = f(p.as_atom().unwrap(), q.as_atom().unwrap());
113− cell(Rc::new(Noun::Atom(a)), Rc::new(Noun::Atom(b)))
114− };
149+static BINARY_ATOM_PAIR: fn(
150+ &Rc<Noun>,
151+ fn(&Atom, &Atom) -> (Atom, Atom),
152+) -> Result<Rc<Noun>, TTanks> = |n: &Rc<Noun>, f: fn(&Atom, &Atom) -> (Atom, Atom)| {
153+ let (p, q) = n.as_cell().ok_or(TTanks::new())?;
154+
155+ let (a, b) = f(
156+ p.as_atom().ok_or(TTanks::new())?,
157+ q.as_atom().ok_or(TTanks::new())?,
158+ );
159+ Ok(cell(&Rc::new(Noun::Atom(a)), &Rc::new(Noun::Atom(b))))
160+};
115161
116162static ADD: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a + b);
117−static DEC: Jet = |_ctx, n| Rc::new(Noun::Atom(n.as_atom().unwrap() - 1u32));
163+static DEC: Jet = |_ctx, n| {
164+ Ok(Rc::new(Noun::Atom(
165+ n.as_atom().ok_or(TTanks::new())? - 1u32,
166+ )))
167+};
118168static DVR: Jet = |_ctx, n| BINARY_ATOM_PAIR(n, |a, b| a.div_rem(&b));
119169static DIV: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a / b);
120170static GTE: Jet = |ctx, n| BINARY_ATOM_LOOB(ctx, n, |a, b| a >= b);
@@ -128,40 +178,42 @@ static MUL: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a * b);
128178static SUB: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a - b);
129179
130180static CAP: Jet = |ctx, n| {
131− let Noun::Atom(n) = (*n).clone() else {
132− panic!()
133− };
134− CAP_ATOM(ctx, n)
181+ let n = n.as_atom().ok_or(TTanks::new())?;
182+ CAP_ATOM(ctx, n.clone())
135183};
136−static CAP_ATOM: fn(&InterpreterContext, Atom) -> Rc<Noun> = |ctx, n: Atom| {
184+
185+static CAP_ATOM: fn(&InterpreterContext, Atom) -> Result<Rc<Noun>, TTanks> = |ctx, n: Atom| {
137186 let mut n_iter = n.iter_u32_digits();
138187
139188 match n_iter.len() {
140− 0 => panic!(),
189+ 0 => Err(TTanks::new()),
141190 1 => CAP_U32(ctx, n_iter.next().unwrap()),
142191 _ => CAP_ATOM(ctx, n >> 1),
143192 }
144193};
145−static CAP_U32: fn(ctx: &InterpreterContext, u32) -> Rc<Noun> = |ctx, n| match n {
146− 0 | 1 => panic!(),
147− 2 => ctx.nouns.two.clone(),
148− 3 => ctx.nouns.three.clone(),
194+static CAP_U32: fn(ctx: &InterpreterContext, u32) -> Result<Rc<Noun>, TTanks> = |ctx, n| match n {
195+ 0 | 1 => Err(TTanks::new()),
196+ 2 => Ok(ctx.nouns.two.clone()),
197+ 3 => Ok(ctx.nouns.three.clone()),
149198 n => CAP_U32(ctx, n >> 1),
150199};
151200
152201static MAS: Jet = |ctx, n| {
153− let Noun::Atom(n) = (*n).clone() else {
154− panic!()
155− };
156− Rc::new(Noun::Atom(MAS_ATOM(ctx, n)))
202+ let n = n.as_atom().ok_or(TTanks::new())?;
203+ Ok(Rc::new(Noun::Atom(
204+ MAS_ATOM(ctx, n.clone()).ok_or(TTanks::new())?,
205+ )))
157206};
158−static MAS_ATOM: fn(&InterpreterContext, Atom) -> Atom = |ctx, n| {
207+static MAS_ATOM: fn(&InterpreterContext, Atom) -> Option<Atom> = |ctx, n| {
159208 let mut n_iter = n.iter_u32_digits();
160209
161210 match n_iter.len() {
162− 0 => panic!(),
163− 1 => Atom::new(vec![MAS_U32(n_iter.next().unwrap())]),
164− _ => (&n & &ctx.big_uints.one) + (MAS_ATOM(ctx, &n >> 1) << 1),
211+ 0 => None,
212+ 1 => match n_iter.next().unwrap() {
213+ 1 => None,
214+ x => Some(Atom::new(vec![MAS_U32(x)])),
215+ },
216+ _ => Some((&n & &ctx.big_uints.one) + (MAS_ATOM(ctx, &n >> 1)? << 1)),
165217 }
166218};
167219static MAS_U32: fn(u32) -> u32 = |n| match n {
@@ -171,26 +223,24 @@ static MAS_U32: fn(u32) -> u32 = |n| match n {
171223};
172224
173225static PEG: Jet = |ctx, n| {
174− let Noun::Cell { p: a, q: b, .. } = (*n).clone() else {
175− panic!()
176− };
226+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
177227
178− let a = a.as_atom().unwrap();
179− let b = b.as_atom().unwrap();
228+ let a = a.as_atom().ok_or(TTanks::new())?;
229+ let b = b.as_atom().ok_or(TTanks::new())?;
180230
181231 if *a == Atom::ZERO {
182− panic!()
232+ return Err(TTanks::new());
183233 };
184234
185− Rc::new(Noun::Atom(PEG_ATOM(ctx, a, b.clone())))
235+ Ok(Rc::new(Noun::Atom(PEG_ATOM(ctx, a, b.clone())?)))
186236};
187−static PEG_ATOM: fn(&InterpreterContext, &Atom, Atom) -> Atom = |ctx, a, b| {
237+static PEG_ATOM: fn(&InterpreterContext, &Atom, Atom) -> Result<Atom, TTanks> = |ctx, a, b| {
188238 let mut b_iter = b.iter_u32_digits();
189239
190240 match b_iter.len() {
191− 0 => panic!(),
192− 1 => PEG_U32(a, b_iter.next().unwrap()),
193− _ => (&b & &ctx.big_uints.one) + (PEG_ATOM(ctx, a, b >> 1) << 1),
241+ 0 => Err(TTanks::new()),
242+ 1 => Ok(PEG_U32(a, b_iter.next().unwrap())),
243+ _ => Ok((&b & &ctx.big_uints.one) + (PEG_ATOM(ctx, a, b >> 1)? << 1)),
194244 }
195245};
196246static PEG_U32: fn(&Atom, u32) -> Atom = |a, b| match b {
@@ -202,12 +252,12 @@ static PEG_U32: fn(&Atom, u32) -> Atom = |a, b| match b {
202252};
203253
204254static FAND: Jet = |ctx, n| {
205− let (nedl, hstk) = n.as_cell().unwrap();
255+ let (nedl, hstk) = n.as_cell().ok_or(TTanks::new())?;
206256 if nedl.is_sig() || hstk.is_sig() {
207− return ctx.nouns.sig.clone();
257+ return Ok(ctx.nouns.sig.clone());
208258 }
209259
210− let nedl: Vec<Rc<Noun>> = nedl.list_iter().collect();
260+ let nedl: Vec<&Rc<Noun>> = nedl.list_iter().collect();
211261
212262 let mut result: Vec<u32> = Vec::new();
213263
@@ -228,16 +278,16 @@ static FAND: Jet = |ctx, n| {
228278 inner_iter = iter.clone();
229279 }
230280
231− Noun::list(result.iter().map(|a| Noun::from_u32(*a)))
281+ Ok(Noun::list(result.iter().map(|a| Noun::from_u32(*a))))
232282};
233283
234284static FIND: Jet = |ctx, n| {
235− let (nedl, hstk) = n.as_cell().unwrap();
285+ let (nedl, hstk) = n.as_cell().ok_or(TTanks::new())?;
236286 if nedl.is_sig() || hstk.is_sig() {
237− return ctx.nouns.sig.clone();
287+ return Ok(ctx.nouns.sig.clone());
238288 }
239289
240− let nedl: Vec<Rc<Noun>> = nedl.list_iter().collect();
290+ let nedl: Vec<&Rc<Noun>> = nedl.list_iter().collect();
241291
242292 let mut iter = zip(0.., hstk.list_iter());
243293
@@ -250,28 +300,28 @@ static FIND: Jet = |ctx, n| {
250300 .collect::<Vec<_>>()
251301 == nedl
252302 {
253− return Rc::new(Noun::from_u32(i)).unit();
303+ return Ok(Rc::new(Noun::from_u32(i)).unit());
254304 }
255305
256306 inner_iter = iter.clone();
257307 }
258308
259− return ctx.nouns.sig.clone();
309+ return Ok(ctx.nouns.sig.clone());
260310};
261311
262312static FLOP: Jet = |ctx, n| {
263− n.list_iter()
264− .fold(ctx.nouns.sig.clone(), |tail, head| cell(head, tail))
313+ Ok(n.list_iter()
314+ .fold(ctx.nouns.sig.clone(), |tail, head| cell(&head, &tail)))
265315};
266316
267317static GULF: Jet = |_ctx, n| {
268− let (from, to) = n.as_cell().unwrap();
318+ let (from, to) = n.as_cell().ok_or(TTanks::new())?;
269319
270− let mut from = (*from.as_atom().unwrap()).clone();
271− let to = to.as_atom().unwrap().clone();
320+ let mut from = (*from.as_atom().ok_or(TTanks::new())?).clone();
321+ let to = to.as_atom().ok_or(TTanks::new())?.clone();
272322
273323 if from > to {
274− panic!()
324+ return Err(TTanks::new());
275325 }
276326
277327 let mut tmp = Vec::new();
@@ -281,384 +331,434 @@ static GULF: Jet = |_ctx, n| {
281331 from += 1u32;
282332 }
283333
284− Noun::list(tmp.iter().rev().map(|a| Noun::Atom(a.clone())))
334+ Ok(Noun::list(tmp.iter().rev().map(|a| Noun::Atom(a.clone()))))
285335};
286336
287337static INTO: Jet = |_ctx, n| {
288− let (a, b) = n.as_cell().unwrap();
289− let (b, c) = b.as_cell().unwrap();
338+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
339+ let (b, c) = b.as_cell().ok_or(TTanks::new())?;
290340
291− let mut b_iter = b.as_atom().unwrap().iter_u32_digits();
341+ let mut b_iter = b.as_atom().ok_or(TTanks::new())?.iter_u32_digits();
292342
293343 match b_iter.len() {
294− 0 => cell(c, a),
344+ 0 => Ok(cell(&c, &a)),
295345 1 => {
296346 let l = a.list_iter();
297347 let b = b_iter.next().unwrap() as usize;
298− Noun::list_refs(
348+ Ok(Noun::list_refs(
299349 l.clone()
300350 .take(b)
301351 .chain(once(c))
302352 .chain(l.skip(b))
303353 .collect::<Vec<_>>()
304354 .iter()
305− .map(|x| x.clone()),
306− )
355+ .cloned()
356+ .cloned(),
357+ ))
307358 }
308− _ => Noun::list_refs(
359+ _ => Ok(Noun::list_refs(
309360 a.list_iter()
310361 .chain(once(c))
311362 .collect::<Vec<_>>()
312363 .iter()
313− .map(|x| x.clone()),
314− ),
364+ .cloned()
365+ .cloned(),
366+ )),
315367 }
316368};
317369
318370static JOIN: Jet = |_ctx, n| {
319− let (sep, list) = n.as_cell().unwrap();
320− Noun::list_refs(
371+ let (sep, list) = n.as_cell().ok_or(TTanks::new())?;
372+ Ok(Noun::list_refs(
321373 list.list_iter()
322374 .intersperse(sep)
323375 .collect::<Vec<_>>()
324376 .iter()
325− .map(|x| x.clone()),
326− )
377+ .cloned()
378+ .cloned(),
379+ ))
327380};
328381
329−static LENT: Jet = |_ctx, n| Rc::new(Noun::from_u32(n.list_iter().fold(0u32, |l, _| l + 1)));
382+static LENT: Jet = |_ctx, n| {
383+ Ok(Rc::new(Noun::from_u32(
384+ n.list_iter().fold(0u32, |l, _| l + 1),
385+ )))
386+};
330387
331388static LEVY: Jet = |ctx, n| {
332− let (a, b) = n.as_cell().unwrap();
333− if a.list_iter()
334− .all(|n| slam(ctx, b.clone(), n) == ctx.nouns.y)
389+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
390+ if traverse_iter_result(a.list_iter().map(|n| slam_pulled_gate(ctx, b, n)))?
391+ .iter()
392+ .all(|x| x.is_y())
335393 {
336− ctx.nouns.y.clone()
394+ Ok(ctx.nouns.y.clone())
337395 } else {
338− ctx.nouns.n.clone()
396+ Ok(ctx.nouns.n.clone())
339397 }
340398};
341399
342400static LIEN: Jet = |ctx, n| {
343− let (a, b) = n.as_cell().unwrap();
344− if a.list_iter()
345− .any(|n| slam(ctx, b.clone(), n) == ctx.nouns.y)
401+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
402+ if traverse_iter_result(a.list_iter().map(|n| slam_pulled_gate(ctx, b, n)))?
403+ .iter()
404+ .any(|x| x.is_y())
346405 {
347− ctx.nouns.y.clone()
406+ Ok(ctx.nouns.y.clone())
348407 } else {
349− ctx.nouns.n.clone()
408+ Ok(ctx.nouns.n.clone())
350409 }
351410};
352411
353412static MURN: Jet = |ctx, n| {
354− let (a, b) = n.as_cell().unwrap();
413+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
355414
356− Noun::list_refs(
357− a.list_iter()
358− .filter_map(|n| slam(ctx, b.clone(), n).as_unit())
359− .collect::<Vec<_>>()
360− .iter()
361− .map(|x| x.clone()),
362− )
415+ Ok(Noun::list_refs(
416+ traverse_iter_result(
417+ a.list_iter()
418+ .map(|n| Ok(slam_pulled_gate(ctx, b, n)?.as_unit().cloned())),
419+ )?
420+ .iter()
421+ .filter_map(|x| x.clone()),
422+ ))
363423};
364424
365425static REAP: Jet = |_ctx, n| {
366− let (a, b) = n.as_cell().unwrap();
426+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
367427
368− Noun::list_refs(repeat_n(b, a.as_u32().unwrap() as usize))
428+ Ok(Noun::list_refs(repeat_n(
429+ b.clone(),
430+ a.as_u32().unwrap() as usize,
431+ )))
369432};
370433
371−static REAR: Jet = |_ctx, n| n.list_iter().last().unwrap();
434+static REAR: Jet = |_ctx, n| n.list_iter().last().ok_or(TTanks::new()).cloned();
372435
373436static REEL: Jet = |ctx, n| {
374− let (list, gate) = n.as_cell().unwrap();
437+ let (list, gate) = n.as_cell().ok_or(TTanks::new())?;
375438
376439 list.list_iter().collect::<Vec<_>>().iter().rfold(
377− gate.clone().gate_sample().unwrap().as_cell().unwrap().1,
378− |acc, next| slam(ctx, gate.clone(), cell(next.clone(), acc)),
440+ Ok(gate
441+ .gate_sample()
442+ .ok_or(TTanks::new())?
443+ .as_cell()
444+ .ok_or(TTanks::new())?
445+ .1
446+ .clone()),
447+ |acc, next| {
448+ let acc = acc?;
449+ slam_pulled_gate(ctx, gate, &cell(&next, &acc))
450+ },
379451 )
380452};
381453
382454static ROLL: Jet = |ctx, n| {
383− let (list, gate) = n.as_cell().unwrap();
455+ let (list, gate) = n.as_cell().ok_or(TTanks::new())?;
384456
385457 list.list_iter().fold(
386− gate.clone().gate_sample().unwrap().as_cell().unwrap().1,
387− |acc, next| slam(ctx, gate.clone(), cell(next.clone(), acc)),
458+ Ok(gate
459+ .clone()
460+ .gate_sample()
461+ .ok_or(TTanks::new())?
462+ .as_cell()
463+ .ok_or(TTanks::new())?
464+ .1
465+ .clone()),
466+ |acc, next| {
467+ let acc = acc?;
468+ slam_pulled_gate(ctx, gate, &cell(&next, &acc))
469+ },
388470 )
389471};
390472
391473static SCAG: Jet = |_ctx, n| {
392− let (a, b) = n.as_cell().unwrap();
474+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
393475
394− Noun::list_refs(
476+ Ok(Noun::list_refs(
395477 b.list_iter()
396478 .take(a.as_u32().unwrap() as usize)
397479 .collect::<Vec<_>>()
398480 .iter()
399− .map(|x| x.clone()),
400− )
481+ .cloned()
482+ .cloned(),
483+ ))
401484};
402485
403486static SKID: Jet = |ctx, n| {
404− let (list, gate) = n.as_cell().unwrap();
487+ let (list, gate) = n.as_cell().ok_or(TTanks::new())?;
405488
406− let (ys, ns): (Vec<Rc<Noun>>, _) = list
407− .list_iter()
408− .partition(|n| slam(ctx, gate.clone(), n.clone()).is_y());
489+ let slammed_n = traverse_iter_result(
490+ list.list_iter()
491+ .map(|n| Ok((slam_pulled_gate(ctx, gate, n)?, n))),
492+ )?;
493+ let (ys, ns): (Vec<_>, _) = slammed_n.iter().partition(|(l, _)| l.is_y());
409494
410− cell(
411− Noun::list_refs(ys.iter().cloned()),
412− Noun::list_refs(ns.iter().cloned()),
413− )
495+ Ok(cell(
496+ &Noun::list_refs(ys.iter().map(|(_, x)| (*x).clone())),
497+ &Noun::list_refs(ns.iter().map(|(_, x)| (*x).clone())),
498+ ))
414499};
415500
416501static SKIM: Jet = |ctx, n| {
417− let (list, gate) = n.as_cell().unwrap();
502+ let (list, gate) = n.as_cell().ok_or(TTanks::new())?;
418503
419− Noun::list_refs(
420− list.list_iter()
421− .filter(|n| slam(ctx, gate.clone(), n.clone()).is_y())
422− .collect::<Vec<_>>()
423− .iter()
424− .cloned(),
425− )
504+ Ok(Noun::list_refs(
505+ traverse_iter_result(
506+ list.list_iter()
507+ .map(|n| Ok((slam_pulled_gate(ctx, gate, n)?, n))),
508+ )?
509+ .iter()
510+ .filter(|(l, _)| l.is_y())
511+ .map(|(_, x)| (*x).clone()),
512+ ))
426513};
427514
428515static SKIP: Jet = |ctx, n| {
429− let (list, gate) = n.as_cell().unwrap();
516+ let (list, gate) = n.as_cell().ok_or(TTanks::new())?;
430517
431− Noun::list_refs(
432− list.list_iter()
433− .filter(|n| slam(ctx, gate.clone(), n.clone()).is_n())
434− .collect::<Vec<_>>()
435− .iter()
436− .cloned(),
437− )
518+ Ok(Noun::list_refs(
519+ traverse_iter_result(
520+ list.list_iter()
521+ .map(|n| Ok((slam_pulled_gate(ctx, gate, n)?, n))),
522+ )?
523+ .iter()
524+ .filter(|(l, _)| l.is_y())
525+ .map(|(_, x)| (*x).clone()),
526+ ))
438527};
439528
440529static SLAG: Jet = |ctx, n| {
441− let (a, list) = n.as_cell().unwrap();
530+ let (a, list) = n.as_cell().ok_or(TTanks::new())?;
442531
443− let mut a_iter = a.as_atom().unwrap().iter_u32_digits();
532+ let mut a_iter = a.as_atom().ok_or(TTanks::new())?.iter_u32_digits();
444533
445534 match a_iter.len() {
446− 0 => list,
447− 1 => Noun::list_refs(
535+ 0 => Ok(list.clone()),
536+ 1 => Ok(Noun::list_refs(
448537 list.list_iter()
449538 .skip(a_iter.next().unwrap() as usize)
450539 .collect::<Vec<_>>()
451540 .iter()
541+ .cloned()
452542 .cloned(),
453− ),
454− _ => ctx.nouns.sig.clone(),
543+ )),
544+ _ => Ok(ctx.nouns.sig.clone()),
455545 }
456546};
457547
458548static SNAG: Jet = |_ctx, n| {
459− let (a, list) = n.as_cell().unwrap();
549+ let (a, list) = n.as_cell().ok_or(TTanks::new())?;
460550
461− list.list_iter().nth(a.as_u32().unwrap() as usize).unwrap()
551+ list.list_iter()
552+ .nth(a.as_u32().unwrap() as usize)
553+ .cloned()
554+ .ok_or(TTanks::new())
462555};
463556
464557static SNIP: Jet = |_ctx, n| {
465− Noun::list_refs(
558+ Ok(Noun::list_refs(
466559 n.list_iter()
467560 .collect::<Vec<_>>()
468561 .iter()
469562 .rev()
470563 .skip(1)
471564 .rev()
565+ .cloned()
472566 .cloned(),
473− )
567+ ))
474568};
475569
476570static SNOC: Jet = |_ctx, n| {
477− let (list, b) = n.as_cell().unwrap();
571+ let (list, b) = n.as_cell().ok_or(TTanks::new())?;
478572
479− Noun::list_refs(
573+ Ok(Noun::list_refs(
480574 list.list_iter()
481575 .chain(once(b))
482576 .collect::<Vec<_>>()
483577 .iter()
578+ .cloned()
484579 .cloned(),
485− )
580+ ))
486581};
487582
488−static SORT: Jet = |ctx, n| {
489− let (list, lth) = n.as_cell().unwrap();
583+// static SORT: Jet = |ctx, n| {
584+// let (list, lth) = n.as_cell().unwrap();
490585
491− let mut v: Vec<_> = list.list_iter().collect();
586+// let mut v: Vec<_> = list.list_iter().collect();
492587
493− v.sort_by(|a, b| {
494− if slam(ctx, lth.clone(), cell(a.clone(), b.clone())).is_y() {
495− Ordering::Less
496− } else if slam(ctx, lth.clone(), cell(b.clone(), a.clone())).is_y() {
497− Ordering::Greater
498− } else {
499− Ordering::Equal
500− }
501− });
588+// v.sort_by(|a, b| {
589+// if slam(ctx, lth.clone(), cell(a.clone(), b.clone())) == ctx.nouns.some_y {
590+// Ordering::Less
591+// } else if slam(ctx, lth.clone(), cell(b.clone(), a.clone())) == ctx.nouns.some_y {
592+// Ordering::Greater
593+// } else {
594+// Ordering::Equal
595+// }
596+// });
502597
503− Noun::list_refs(v.iter().cloned())
504−};
598+// Some(Noun::list_refs(v.iter().cloned()))
599+// };
505600
506601static SPIN: Jet = |ctx, n| {
507− let (list, b) = n.as_cell().unwrap();
508− let (mut acc, gate) = b.as_cell().unwrap();
602+ let (list, b) = n.as_cell().ok_or(TTanks::new())?;
603+ let (acc, gate) = b.as_cell().ok_or(TTanks::new())?;
509604
510605 let mut res = Vec::new();
606+ let mut acc = acc.clone();
511607
512608 for n in list.list_iter() {
513− let (el, new_acc) = slam(ctx, gate.clone(), cell(n, acc)).as_cell().unwrap();
514− acc = new_acc;
515− res.push(el);
609+ let foo = slam_pulled_gate(ctx, gate, &cell(&n, &acc))?;
610+ let (el, new_acc) = foo.as_cell().ok_or(TTanks::new())?;
611+ acc = new_acc.clone();
612+ res.push(el.clone());
516613 }
517614
518− cell(Noun::list_refs(res.iter().cloned()), acc)
615+ Ok(cell(&Noun::list_refs(res.iter().cloned()), &acc))
519616};
520617
521618static SPUN: Jet = |ctx, n| {
522− let (list, gate) = n.as_cell().unwrap();
619+ let (list, gate) = n.as_cell().ok_or(TTanks::new())?;
523620
524621 let mut res = Vec::new();
525− let mut acc = gate.clone().gate_sample().unwrap().as_cell().unwrap().1;
622+ let mut acc = gate
623+ .gate_sample()
624+ .ok_or(TTanks::new())?
625+ .as_cell()
626+ .ok_or(TTanks::new())?
627+ .1
628+ .clone();
526629
527630 for n in list.list_iter() {
528− let (el, new_acc) = slam(ctx, gate.clone(), cell(n, acc)).as_cell().unwrap();
529− acc = new_acc;
530− res.push(el);
631+ let foo = slam_pulled_gate(ctx, gate, &cell(&n, &acc))?;
632+ let (el, new_acc) = foo.as_cell().ok_or(TTanks::new())?;
633+ acc = new_acc.clone();
634+ res.push(el.clone());
531635 }
532636
533− cell(Noun::list_refs(res.iter().cloned()), acc)
637+ Ok(cell(&Noun::list_refs(res.iter().cloned()), &acc))
534638};
535639
536640static TURN: Jet = |ctx, n| {
537− let (list, gate) = n.as_cell().unwrap();
538− Noun::list_refs(
539− list.list_iter()
540− .map(|x| slam(ctx, gate.clone(), x))
541− .collect::<Vec<_>>()
641+ let (list, gate) = n.as_cell().ok_or(TTanks::new())?;
642+ Ok(Noun::list_refs(
643+ traverse_iter_result(list.list_iter().map(|x| slam_pulled_gate(ctx, gate, x)))?
542644 .iter()
543645 .cloned(),
544− )
646+ ))
545647};
546648
547649static WELD: Jet = |_ctx, n| {
548− let (a, b) = n.as_cell().unwrap();
650+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
549651
550− Noun::list_refs(
652+ Ok(Noun::list_refs(
551653 a.list_iter()
552654 .chain(b.list_iter())
553655 .collect::<Vec<_>>()
554656 .iter()
657+ .cloned()
555658 .cloned(),
556− )
659+ ))
557660};
558661
559662static WELP: Jet = WELD;
560663
561664static ZING: Jet = |_ctx, n| {
562− Noun::list_refs(
665+ Ok(Noun::list_refs(
563666 n.list_iter()
564667 .map(|l| l.list_iter())
565668 .flatten()
566669 .collect::<Vec<_>>()
567670 .iter()
671+ .cloned()
568672 .cloned(),
569− )
673+ ))
570674};
571675
572−static BEX: Jet = |ctx, n| Rc::new(Noun::Atom(&ctx.big_uints.one << n.as_u32().unwrap()));
676+static BEX: Jet = |ctx, n| {
677+ Ok(Rc::new(Noun::Atom(
678+ &ctx.big_uints.one << n.as_u32().unwrap(),
679+ )))
680+};
573681
574682static END: Jet = |ctx, n| {
575− let (a, b) = n.as_cell().unwrap();
576− let bite = a.as_bite().unwrap();
683+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
684+ let bite = a.as_bite();
577685
578686 let bits = bite.bits();
579687
580688 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
581689
582− Rc::new(Noun::Atom(mask & b.as_atom().unwrap()))
690+ Ok(Rc::new(Noun::Atom(
691+ mask & b.as_atom().ok_or(TTanks::new())?,
692+ )))
583693};
584694
585695static CAN: Jet = |ctx, n| {
586− let (a, b) = n.as_cell().unwrap();
696+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
587697 let bloq = a.as_u32().unwrap();
588698 let bloq_ = 2u32.pow(bloq);
589699
590− let atom: Atom =
591− b.list_iter()
592− .collect::<Vec<_>>()
593− .iter()
594− .rfold(ctx.big_uints.zero.clone(), |acc, next| {
595− let (p, q) = next.as_cell().unwrap();
596− let step = p.as_u32().unwrap();
597− let bits = bloq_ * step;
598− let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
599−
600− (acc << bits) | (mask & q.as_atom().unwrap())
601− });
602− Rc::new(Noun::Atom(atom))
700+ let atom: Option<Atom> = b.list_iter().collect::<Vec<_>>().iter().rfold(
701+ Some(ctx.big_uints.zero.clone()),
702+ |acc, next| {
703+ let (p, q) = next.as_cell()?;
704+ let step = p.as_u32().unwrap();
705+ let bits = bloq_ * step;
706+ let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
707+
708+ Some((acc? << bits) | (mask & q.as_atom()?))
709+ },
710+ );
711+ Ok(Rc::new(Noun::Atom(atom.ok_or(TTanks::new())?)))
603712};
604713
605714static CAT: Jet = |_ctx, n| {
606− let (bloq, b) = n.as_cell().unwrap();
607− let (b, c) = b.as_cell().unwrap();
715+ let (bloq, b) = n.as_cell().ok_or(TTanks::new())?;
716+ let (b, c) = b.as_cell().ok_or(TTanks::new())?;
608717
609− let bloq = bloq.as_u32().unwrap();
610− let b = b.as_atom().unwrap();
611− let c = c.as_atom().unwrap();
718+ let bloq = bloq.as_u32().ok_or(TTanks::new())?;
719+ let b = b.as_atom().ok_or(TTanks::new())?;
720+ let c = c.as_atom().ok_or(TTanks::new())?;
612721
613− Rc::new(Noun::Atom(
614− (c << (met(bloq, b.clone()) * 2u32.pow(bloq))) | b,
615− ))
722+ Ok(Rc::new(Noun::Atom(
723+ (c << (met(bloq, &b) * 2u32.pow(bloq))) | b,
724+ )))
616725};
617726
618727static MET: Jet = |_ctx, n| {
619− let (bloq, b) = n.as_cell().unwrap();
620− let b: Atom = b.as_atom().unwrap().clone();
728+ let (bloq, b) = n.as_cell().ok_or(TTanks::new())?;
729+ let b: Atom = b.as_atom().ok_or(TTanks::new())?.clone();
621730 let bloq = bloq.as_u32().unwrap();
622731
623− Rc::new(Noun::from_u32(met(bloq, b)))
732+ Ok(Rc::new(Noun::from_u32(met(bloq, &b))))
624733};
625734
626−fn met(bloq: u32, mut a: Atom) -> u32 {
627− let bits = 2u32.pow(bloq);
628−
629− let mut c = 0;
630−
631− while a > Atom::ZERO {
632− a = a >> bits;
633− c += 1;
634− }
635−
636− c
735+fn met(bloq: u32, a: &Atom) -> u32 {
736+ (a.bits() as u32).div_ceil(2u32.pow(bloq))
637737}
638738
639739static CUT: Jet = |ctx, n| {
640− let (a, b) = n.as_cell().unwrap();
641− let (b, d) = b.as_cell().unwrap();
642− let (b, c) = b.as_cell().unwrap();
740+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
741+ let (b, d) = b.as_cell().ok_or(TTanks::new())?;
742+ let (b, c) = b.as_cell().ok_or(TTanks::new())?;
643743
644744 let bits = 2u32.pow(a.as_u32().unwrap());
645745
646746 let mask = (&ctx.big_uints.one << (bits * c.as_u32().unwrap())) - &ctx.big_uints.one;
647747
648− Rc::new(Noun::Atom(
649− (d.as_atom().unwrap() >> (bits * b.as_u32().unwrap())) & mask,
650− ))
748+ Ok(Rc::new(Noun::Atom(
749+ (d.as_atom().ok_or(TTanks::new())? >> (bits * b.as_u32().unwrap())) & mask,
750+ )))
651751};
652752
653753static FIL: Jet = |ctx, n| {
654− let (a, b) = n.as_cell().unwrap();
655− let (b, c) = b.as_cell().unwrap();
754+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
755+ let (b, c) = b.as_cell().ok_or(TTanks::new())?;
656756
657757 let bits = 2u32.pow(a.as_u32().unwrap());
658758
659759 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
660760
661− let src = c.as_atom().unwrap() & mask;
761+ let src = c.as_atom().ok_or(TTanks::new())? & mask;
662762
663763 let mut target = ctx.big_uints.zero.clone();
664764
@@ -666,45 +766,45 @@ static FIL: Jet = |ctx, n| {
666766 target = (target << bits) | &src;
667767 }
668768
669− Rc::new(Noun::Atom(target))
769+ Ok(Rc::new(Noun::Atom(target)))
670770};
671771
672772static LSH: Jet = |_ctx, n| {
673− let (a, b) = n.as_cell().unwrap();
674− let a = a.as_bite().unwrap();
675− let b = b.as_atom().unwrap();
773+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
774+ let a = a.as_bite();
775+ let b = b.as_atom().ok_or(TTanks::new())?;
676776
677− Rc::new(Noun::Atom(b << a.bits()))
777+ Ok(Rc::new(Noun::Atom(b << a.bits())))
678778};
679779
680780static RSH: Jet = |_ctx, n| {
681− let (a, b) = n.as_cell().unwrap();
682− let a = a.as_bite().unwrap();
683− let b = b.as_atom().unwrap();
781+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
782+ let a = a.as_bite();
783+ let b = b.as_atom().ok_or(TTanks::new())?;
684784
685− Rc::new(Noun::Atom(b >> a.bits()))
785+ Ok(Rc::new(Noun::Atom(b >> a.bits())))
686786};
687787
688−static RAP: Jet = |ctx, n| {
689− let (bloq, b) = n.as_cell().unwrap();
788+pub static RAP: Jet = |ctx, n| {
789+ let (bloq, b) = n.as_cell().ok_or(TTanks::new())?;
690790 let bloq = bloq.as_u32().unwrap();
691791 let bits = 2u32.pow(bloq);
692792
693793 let mut target = ctx.big_uints.zero.clone();
694794
695795 for el in b.list_iter().collect::<Vec<_>>().iter().rev() {
696− let el = el.as_atom().unwrap();
796+ let el = el.as_atom().ok_or(TTanks::new())?;
697797 if el != &ctx.big_uints.zero {
698− target = (target << (bits * met(bloq, el.clone()))) | el;
798+ target = (target << (bits * met(bloq, &el))) | el;
699799 }
700800 }
701801
702− Rc::new(Noun::Atom(target))
802+ Ok(Rc::new(Noun::Atom(target)))
703803};
704804
705805static REP: Jet = |ctx, n| {
706− let (bite, b) = n.as_cell().unwrap();
707− let bite = bite.as_bite().unwrap();
806+ let (bite, b) = n.as_cell().ok_or(TTanks::new())?;
807+ let bite = bite.as_bite();
708808
709809 let bits = bite.bits();
710810 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -712,18 +812,18 @@ static REP: Jet = |ctx, n| {
712812 let mut target = ctx.big_uints.zero.clone();
713813
714814 for el in b.list_iter().collect::<Vec<_>>().iter().rev() {
715− target = (target << bits) | (el.as_atom().unwrap() & &mask);
815+ target = (target << bits) | (el.as_atom().ok_or(TTanks::new())? & &mask);
716816 }
717817
718− Rc::new(Noun::Atom(target))
818+ Ok(Rc::new(Noun::Atom(target)))
719819};
720820
721821static REV: Jet = |ctx, n| {
722− let (bloq, b) = n.as_cell().unwrap();
723− let (len, dat) = b.as_cell().unwrap();
822+ let (bloq, b) = n.as_cell().ok_or(TTanks::new())?;
823+ let (len, dat) = b.as_cell().ok_or(TTanks::new())?;
724824 let bloq = bloq.as_u32().unwrap();
725825 let len = len.as_u32().unwrap();
726− let mut dat: Atom = dat.as_atom().unwrap().clone();
826+ let mut dat: Atom = dat.as_atom().ok_or(TTanks::new())?.clone();
727827
728828 let bits = 2u32.pow(bloq);
729829 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -735,13 +835,13 @@ static REV: Jet = |ctx, n| {
735835 dat = dat >> bits;
736836 }
737837
738− Rc::new(Noun::Atom(target))
838+ Ok(Rc::new(Noun::Atom(target)))
739839};
740840
741841static RIP: Jet = |ctx, n| {
742− let (bite, b) = n.as_cell().unwrap();
743− let bite = bite.as_bite().unwrap();
744− let mut b: Atom = b.as_atom().unwrap().clone();
842+ let (bite, b) = n.as_cell().ok_or(TTanks::new())?;
843+ let bite = bite.as_bite();
844+ let mut b: Atom = b.as_atom().ok_or(TTanks::new())?.clone();
745845
746846 let bits = bite.bits();
747847 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -753,15 +853,15 @@ static RIP: Jet = |ctx, n| {
753853 b = b >> bits;
754854 }
755855
756− Noun::list_refs(target.iter().cloned())
856+ Ok(Noun::list_refs(target.iter().cloned()))
757857};
758858
759859static RUN: Jet = |ctx, n| {
760− let (bite, b) = n.as_cell().unwrap();
761− let (b, gate) = b.as_cell().unwrap();
860+ let (bite, b) = n.as_cell().ok_or(TTanks::new())?;
861+ let (b, gate) = b.as_cell().ok_or(TTanks::new())?;
762862
763− let bite = bite.as_bite().unwrap();
764− let mut b: Atom = b.as_atom().unwrap().clone();
863+ let bite = bite.as_bite();
864+ let mut b: Atom = b.as_atom().ok_or(TTanks::new())?.clone();
765865
766866 let bits = bite.bits();
767867 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -770,22 +870,22 @@ static RUN: Jet = |ctx, n| {
770870
771871 while b > ctx.big_uints.zero {
772872 target = (target << bits)
773− | ((*slam(ctx, gate.clone(), Rc::new(Noun::Atom(&mask & &b))))
873+ | ((*slam_pulled_gate(ctx, gate, &Rc::new(Noun::Atom(&mask & &b)))?)
774874 .as_atom()
775− .unwrap()
875+ .ok_or(TTanks::new())?
776876 & &mask);
777877 b = b >> bits;
778878 }
779879
780− Rc::new(Noun::Atom(target))
880+ Ok(Rc::new(Noun::Atom(target)))
781881};
782882
783883static RUT: Jet = |ctx, n| {
784− let (bite, b) = n.as_cell().unwrap();
785− let (b, gate) = b.as_cell().unwrap();
884+ let (bite, b) = n.as_cell().ok_or(TTanks::new())?;
885+ let (b, gate) = b.as_cell().ok_or(TTanks::new())?;
786886
787− let bite = bite.as_bite().unwrap();
788− let mut b: Atom = b.as_atom().unwrap().clone();
887+ let bite = bite.as_bite();
888+ let mut b: Atom = b.as_atom().ok_or(TTanks::new())?.clone();
789889
790890 let bits = bite.bits();
791891 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
@@ -794,24 +894,24 @@ static RUT: Jet = |ctx, n| {
794894
795895 while b > ctx.big_uints.zero {
796896 target.push(
797− (*slam(ctx, gate.clone(), Rc::new(Noun::Atom(&mask & &b))))
897+ (*slam_pulled_gate(ctx, gate, &Rc::new(Noun::Atom(&mask & &b)))?)
798898 .as_atom()
799− .unwrap()
899+ .ok_or(TTanks::new())?
800900 & &mask,
801901 );
802902 b = b >> bits;
803903 }
804904
805− Noun::list(target.iter().cloned().map(Noun::Atom))
905+ Ok(Noun::list(target.iter().cloned().map(Noun::Atom)))
806906};
807907
808908static SEW: Jet = |ctx, n| {
809− let (a, b) = n.as_cell().unwrap();
810− let (b, recipient) = b.as_cell().unwrap();
811− let (sew_start, c) = b.as_cell().unwrap();
812− let (number_of_blocks, donor) = c.as_cell().unwrap();
813− let recipient = recipient.as_atom().unwrap();
814− let donor = donor.as_atom().unwrap();
909+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
910+ let (b, recipient) = b.as_cell().ok_or(TTanks::new())?;
911+ let (sew_start, c) = b.as_cell().ok_or(TTanks::new())?;
912+ let (number_of_blocks, donor) = c.as_cell().ok_or(TTanks::new())?;
913+ let recipient = recipient.as_atom().ok_or(TTanks::new())?;
914+ let donor = donor.as_atom().ok_or(TTanks::new())?;
815915
816916 let bloq = a.as_u32().unwrap();
817917 let bits = 2u32.pow(bloq);
@@ -823,16 +923,16 @@ static SEW: Jet = |ctx, n| {
823923
824924 let target = (recipient ^ apendix) | (donor & mask);
825925
826− Rc::new(Noun::Atom(target))
926+ Ok(Rc::new(Noun::Atom(target)))
827927};
828928
829929static SWP: Jet = |ctx, n| {
830− let (bloq, b) = n.as_cell().unwrap();
930+ let (bloq, b) = n.as_cell().ok_or(TTanks::new())?;
831931 let bloq = bloq.as_u32().unwrap();
832932 let bits = 2u32.pow(bloq);
833933 let mask = (&ctx.big_uints.one << bits) - &ctx.big_uints.one;
834934
835− let mut source = b.as_atom().unwrap().clone();
935+ let mut source = b.as_atom().ok_or(TTanks::new())?.clone();
836936 let mut target = ctx.big_uints.zero.clone();
837937
838938 while source > ctx.big_uints.zero {
@@ -840,5 +940,331 @@ static SWP: Jet = |ctx, n| {
840940 source = source >> bits;
841941 }
842942
843− Rc::new(Noun::Atom(target))
943+ Ok(Rc::new(Noun::Atom(target)))
944+};
945+
946+static XEB: Jet = |_ctx, n| {
947+ let mut a = n.as_atom().ok_or(TTanks::new())?.clone();
948+
949+ let mut c = 0;
950+
951+ while a > Atom::ZERO {
952+ a = a >> 1;
953+ c += 1;
954+ }
955+
956+ Ok(Rc::new(Noun::from_u32(c)))
957+};
958+
959+static CON: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a | b);
960+static DIS: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a & b);
961+static MIX: Jet = |_ctx, n| BINARY_ATOM(n, |a, b| a ^ b);
962+
963+static NOT: Jet = |ctx, n| {
964+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
965+ let (b, c) = b.as_cell().ok_or(TTanks::new())?;
966+
967+ let bloq = a.as_u32().unwrap();
968+ let bits = 2u32.pow(bloq);
969+ let mask = (&ctx.big_uints.one << (bits * b.as_u32().unwrap())) - &ctx.big_uints.one;
970+
971+ let target = c.as_atom().ok_or(TTanks::new())? ^ &mask;
972+
973+ Ok(Rc::new(Noun::Atom(target)))
974+};
975+
976+static LAST: Jet = |_ctx, n| {
977+ let (a, b) = n.as_cell().ok_or(TTanks::new())?;
978+ let a = a.as_hair().ok_or(TTanks::new())?;
979+ let b = b.as_hair().ok_or(TTanks::new())?;
980+ Ok(Noun::from_hair(last(&a, &b)))
981+};
982+
983+fn last<'a>(a: &'a Hair, b: &'a Hair) -> &'a Hair {
984+ if a.line == b.line {
985+ if a.column > b.column {
986+ a
987+ } else {
988+ b
989+ }
990+ } else {
991+ if a.line > b.line {
992+ a
993+ } else {
994+ b
995+ }
996+ }
997+}
998+
999+static TRIP: Jet = |_ctx, n| {
1000+ let a = n.as_atom().ok_or(TTanks::new())?;
1001+ Ok(Noun::list(
1002+ a.to_bytes_le().iter().map(|b| Noun::from_u32(*b as u32)),
1003+ ))
1004+};
1005+
1006+static MUK: Jet = |_ctx, n| {
1007+ let (seed, len) = n.as_cell().ok_or(TTanks::new())?;
1008+ let (len, key) = len.as_cell().ok_or(TTanks::new())?;
1009+ let seed = seed.as_u32().ok_or(TTanks::new())?;
1010+ let len = len.as_u32().ok_or(TTanks::new())?;
1011+ let key = key.as_atom().ok_or(TTanks::new())?;
1012+
1013+ Ok(Rc::new(Noun::from_u32(muk(seed, len, key))))
1014+};
1015+
1016+static AOR: Jet = |ctx, n| {
1017+ let (a, b) = n.as_cell().ok_or(EMPTY_TANKS)?;
1018+
1019+ if a == b {
1020+ Ok(ctx.nouns.y.clone())
1021+ } else {
1022+ match (a.borrow(), b.borrow()) {
1023+ (Noun::Cell { .. }, Noun::Atom(_)) => Ok(ctx.nouns.n.clone()),
1024+ (Noun::Cell { p: a_p, q: a_q, .. }, Noun::Cell { p: b_p, q: b_q, .. }) => {
1025+ if a_p == b_p {
1026+ AOR(ctx, &cell(a_q, b_q))
1027+ } else {
1028+ AOR(ctx, &cell(a_p, b_p))
1029+ }
1030+ }
1031+ (Noun::Atom(_), Noun::Cell { .. }) => Ok(ctx.nouns.y.clone()),
1032+ (Noun::Atom(a), Noun::Atom(b)) => {
1033+ let a = a.to_bytes_le();
1034+ let b = b.to_bytes_le();
1035+
1036+ let mut i = 0;
1037+ loop {
1038+ let c = a.get(i).unwrap_or(&0);
1039+ let d = b.get(i).unwrap_or(&0);
1040+
1041+ if a[0] == b[0] {
1042+ i += 1;
1043+ } else {
1044+ if c < d {
1045+ return Ok(ctx.nouns.y.clone());
1046+ } else {
1047+ return Ok(ctx.nouns.n.clone());
1048+ }
1049+ }
1050+ }
1051+ }
1052+ }
1053+ }
1054+};
1055+
1056+static DOR: Jet = |ctx, n| {
1057+ let (a, b) = n.as_cell().ok_or(EMPTY_TANKS)?;
1058+
1059+ if a == b {
1060+ Ok(ctx.nouns.y.clone())
1061+ } else {
1062+ match (a.borrow(), b.borrow()) {
1063+ (Noun::Cell { .. }, Noun::Atom(_)) => Ok(ctx.nouns.n.clone()),
1064+ (Noun::Cell { p: a_p, q: a_q, .. }, Noun::Cell { p: b_p, q: b_q, .. }) => {
1065+ if a_p == b_p {
1066+ DOR(ctx, &cell(a_q, b_q))
1067+ } else {
1068+ DOR(ctx, &cell(a_p, b_p))
1069+ }
1070+ }
1071+ (Noun::Atom(_), Noun::Cell { .. }) => Ok(ctx.nouns.y.clone()),
1072+ (Noun::Atom(a), Noun::Atom(b)) => {
1073+ if a < b {
1074+ Ok(ctx.nouns.y.clone())
1075+ } else {
1076+ Ok(ctx.nouns.n.clone())
1077+ }
1078+ }
1079+ }
1080+ }
8441081};
1082+
1083+static GOR: Jet = |ctx, n| {
1084+ let (a, b) = n.as_cell().ok_or(EMPTY_TANKS)?;
1085+ let c = a.mug();
1086+ let d = b.mug();
1087+
1088+ if c == d {
1089+ DOR(ctx, n)
1090+ } else {
1091+ if c < d {
1092+ Ok(ctx.nouns.y.clone())
1093+ } else {
1094+ Ok(ctx.nouns.n.clone())
1095+ }
1096+ }
1097+};
1098+
1099+static GOR_PAIR: fn(&mut InterpreterContext, &Rc<Noun>, &Rc<Noun>) -> Result<bool, TTanks> =
1100+ |ctx, a, b| {
1101+ let c = a.mug();
1102+ let d = b.mug();
1103+
1104+ if c == d {
1105+ Ok(DOR(ctx, &(a.clone(), b.clone()).as_noun())? == ctx.nouns.y)
1106+ } else {
1107+ Ok(c < d)
1108+ }
1109+ };
1110+
1111+static MOR: Jet = |ctx, n| {
1112+ let (a, b) = n.as_cell().ok_or(EMPTY_TANKS)?;
1113+ let c = Noun::from_u32(a.mug()).mug();
1114+ let d = Noun::from_u32(b.mug()).mug();
1115+
1116+ if c == d {
1117+ DOR(ctx, n)
1118+ } else {
1119+ if c < d {
1120+ Ok(ctx.nouns.y.clone())
1121+ } else {
1122+ Ok(ctx.nouns.n.clone())
1123+ }
1124+ }
1125+};
1126+
1127+static LOOK: Jet = |ctx, n| {
1128+ let (cog, dab) = n.as_cell().ok_or(EMPTY_TANKS)?;
1129+ LOOK_PAIR(ctx, Atom::new(vec![1]), cog, dab)
1130+};
1131+
1132+static LOOK_PAIR: fn(
1133+ &mut InterpreterContext,
1134+ Atom,
1135+ &Rc<Noun>,
1136+ &Rc<Noun>,
1137+) -> Result<Rc<Noun>, TTanks> = |ctx, axe, cog, dab| {
1138+ if dab.is_sig() {
1139+ Ok(ctx.nouns.sig.clone())
1140+ } else {
1141+ let (node, b, c) = dab.tuple_3().ok_or(EMPTY_TANKS)?;
1142+ let l = b;
1143+ let r = c;
1144+ let (p, q) = node.as_cell().ok_or(EMPTY_TANKS)?;
1145+ if b.is_sig() && c.is_sig() {
1146+ if cog == p {
1147+ Ok((ctx.nouns.sig.clone(), Noun::Atom(axe), q.clone()).as_noun())
1148+ } else {
1149+ Ok(ctx.nouns.sig.clone())
1150+ }
1151+ } else if b.is_sig() {
1152+ if cog == p {
1153+ Ok((ctx.nouns.sig.clone(), PEG_U32(&axe, 2), q.clone()).as_noun())
1154+ } else if GOR_PAIR(ctx, cog, p)? {
1155+ Ok(ctx.nouns.sig.clone())
1156+ } else {
1157+ LOOK_PAIR(ctx, PEG_U32(&axe, 3), cog, r)
1158+ }
1159+ } else if c.is_sig() {
1160+ if cog == p {
1161+ Ok((ctx.nouns.sig.clone(), PEG_U32(&axe, 2), q.clone()).as_noun())
1162+ } else if GOR_PAIR(ctx, cog, p)? {
1163+ LOOK_PAIR(ctx, PEG_U32(&axe, 3), cog, l)
1164+ } else {
1165+ Ok(ctx.nouns.sig.clone())
1166+ }
1167+ } else {
1168+ if cog == p {
1169+ Ok((ctx.nouns.sig.clone(), PEG_U32(&axe, 2), q.clone()).as_noun())
1170+ } else if GOR_PAIR(ctx, cog, p)? {
1171+ LOOK_PAIR(ctx, PEG_U32(&axe, 6), cog, l)
1172+ } else {
1173+ LOOK_PAIR(ctx, PEG_U32(&axe, 7), cog, r)
1174+ }
1175+ }
1176+ }
1177+};
1178+
1179+static JUST: DoubleJet = |ctx, n, m| {
1180+ let char = n.as_atom()?;
1181+ let nail = m.as_nail()?;
1182+ if *nail.rest == ctx.nouns.sig {
1183+ Some(fail(nail).as_noun())
1184+ } else {
1185+ let (i, _) = nail.rest.as_cell()?;
1186+ let i = i.as_atom()?;
1187+ if i == char {
1188+ Some(next(nail)?.as_noun())
1189+ } else {
1190+ Some(fail(nail).as_noun())
1191+ }
1192+ }
1193+};
1194+
1195+// static STIR: DoubleJet = |ctx, n, m| {
1196+// let (rud, raq) = n.as_cell()?;
1197+// let (raq, fel) = raq.as_cell()?;
1198+// let tub = m.as_nail()?;
1199+
1200+// let mut lef = Vec::new();
1201+// let mut vex = slam(ctx, fel, tub)?.as;
1202+// if vex.
1203+// };
1204+
1205+fn next<'a>(nail: Nail<'a>) -> Option<Edge<'a>> {
1206+ if **nail.rest == Noun::SIG {
1207+ Some(fail(nail))
1208+ } else {
1209+ let (i, t) = nail.rest.as_cell()?;
1210+ let zac = lust(i.as_atom()?.clone(), nail.hair);
1211+ // cell(zac, cell(Rc::new(Noun::SIG), cell(i, cell(zac, t))))
1212+ Some(Edge {
1213+ hair: zac.clone(),
1214+ result: Some((i, Nail { hair: zac, rest: t })),
1215+ })
1216+ }
1217+}
1218+
1219+fn lust(char: Atom, hair: Hair) -> Hair {
1220+ if char == Atom::from_slice(&vec![10]) {
1221+ Hair {
1222+ line: hair.line + 1,
1223+ column: 1,
1224+ }
1225+ } else {
1226+ Hair {
1227+ line: hair.line,
1228+ column: hair.column + 1,
1229+ }
1230+ }
1231+}
1232+
1233+fn fail(nail: Nail) -> Edge {
1234+ Edge {
1235+ hair: nail.hair,
1236+ result: None,
1237+ }
1238+}
1239+
1240+const MUG: Jet = |__ctx, n| Ok(Rc::new(Noun::from_u32(n.mug())));
1241+
1242+pub fn mum(syd: u32, fal: u32, key: &Atom) -> u32 {
1243+ let wyd = met(3, &key);
1244+ let mut i = 0;
1245+ loop {
1246+ if i == 8 {
1247+ break fal;
1248+ } else {
1249+ let haz = muk(syd, wyd, &key);
1250+ let ham = (haz >> 31) ^ (haz & !(1 << 31));
1251+ if ham == 0 {
1252+ i += 1;
1253+ continue;
1254+ } else {
1255+ break ham;
1256+ }
1257+ }
1258+ }
1259+}
1260+
1261+fn muk(seed: u32, len: u32, key: &Atom) -> u32 {
1262+ let key: Vec<_> = key
1263+ .to_bytes_le()
1264+ .iter()
1265+ .take(len as usize)
1266+ .cloned()
1267+ .collect();
1268+
1269+ murmur3_32(&mut &*key, seed).unwrap()
1270+}