@@ -1,23 +1,15 @@
11use core::str;
22use std::borrow::Borrow;
33use std::collections::BTreeMap;
4−use std::collections::HashMap;
54use std::rc::Rc;
6−use std::sync::Mutex;
75
86use fplist::cons;
97use fplist::PersistentList;
10−use lazy_static::lazy_static;
118use num_bigint::BigUint;
129
1310use crate::jets::*;
1411use crate::noun::*;
1512
16−// #[cfg(feature = "count-jets")]
17−lazy_static! {
18− pub static ref JET_COUNTER: Mutex<HashMap<Atom, u32>> = Mutex::new(HashMap::new());
19−}
20−
2113#[derive(Clone)]
2214pub struct Nouns {
2315 pub y: Rc<Noun>,
@@ -32,7 +24,6 @@ pub struct Nouns {
3224 pub memo: Rc<Noun>,
3325 pub mean: Rc<Noun>,
3426 pub slog: Rc<Noun>,
35− pub fast: Rc<Noun>,
3627}
3728
3829#[derive(Clone)]
@@ -51,7 +42,6 @@ pub struct InterpreterContext {
5142 pub nouns: Nouns,
5243 pub big_uints: BigUints,
5344 pub memo: BTreeMap<Hash, Rc<Noun>>,
54− pub slog: fn(&String) -> (),
5545}
5646
5747pub fn generate_interpreter_context() -> InterpreterContext {
@@ -74,7 +64,6 @@ pub fn generate_interpreter_context() -> InterpreterContext {
7464 let memo = Rc::new(Noun::from_bytes(b"memo"));
7565 let mean = Rc::new(Noun::from_bytes(b"mean"));
7666 let slog = Rc::new(Noun::from_bytes(b"slog"));
77− let fast = Rc::new(Noun::from_bytes(b"fast"));
7867 InterpreterContext {
7968 jets,
8069 double_jets,
@@ -91,11 +80,9 @@ pub fn generate_interpreter_context() -> InterpreterContext {
9180 memo,
9281 mean,
9382 slog,
94− fast,
9583 },
9684 big_uints,
9785 memo: BTreeMap::new(),
98− slog: |str: &String| eprintln!("{}", str),
9986 }
10087}
10188
@@ -204,7 +191,7 @@ fn hax(
204191
205192fn tar_u32<'a>(
206193 ctx: &mut InterpreterContext,
207− subj: &'a Rc<Noun>,
194+ subj: Rc<Noun>,
208195 op: u32,
209196 formula: &'a Rc<Noun>,
210197) -> Result<Rc<Noun>, TTanks> {
@@ -227,16 +214,16 @@ fn tar_u32<'a>(
227214 match ctx.jets.get(&hash) {
228215 Some(f) => f(ctx, sample),
229216 None => {
230− let foo = tar(ctx, subj, &b)?;
217+ let foo = tar(ctx, subj.clone(), &b)?;
231218 let bar = tar(ctx, subj, &c)?;
232− tar(ctx, &foo, &bar)
219+ tar(ctx, foo, &bar)
233220 }
234221 }
235222 }
236223 _ => {
237− let foo = tar(ctx, subj, &b)?;
224+ let foo = tar(ctx, subj.clone(), &b)?;
238225 let bar = tar(ctx, subj, &c)?;
239− tar(ctx, &foo, &bar)
226+ tar(ctx, foo, &bar)
240227 }
241228 }
242229 }
@@ -247,7 +234,7 @@ fn tar_u32<'a>(
247234 4 => Ok(Rc::new(lus(&*tar(ctx, subj, &formula)?)?)),
248235 5 => {
249236 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
250− let foo = tar(ctx, subj, &b)?;
237+ let foo = tar(ctx, subj.clone(), &b)?;
251238 let bar = tar(ctx, subj, &c)?;
252239 Ok(tis(ctx, &foo, &bar).clone())
253240 }
@@ -255,29 +242,29 @@ fn tar_u32<'a>(
255242 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
256243 let (c, d) = c.as_cell().ok_or(TTanks::new())?;
257244
258− let bar = tar_u32(ctx, subj, 4, &cell(&ctx.nouns.four.clone(), &b))?;
245+ let bar = tar_u32(ctx, subj.clone(), 4, &cell(&ctx.nouns.four.clone(), &b))?;
259246
260− let foo = tar_u32(ctx, &ctx.nouns.two_three.clone(), 0, &bar)?;
261− let foo = tar_u32(ctx, &cell(&c, &d), 0, &foo)?;
247+ let foo = tar_u32(ctx, ctx.nouns.two_three.clone(), 0, &bar)?;
248+ let foo = tar_u32(ctx, cell(&c, &d), 0, &foo)?;
262249
263250 tar(ctx, subj, &foo)
264251 }
265252 7 => {
266253 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
267254 let foo = tar(ctx, subj, &b)?;
268− tar(ctx, &foo, &c)
255+ tar(ctx, foo, &c)
269256 }
270257 8 => {
271258 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
272− let foo = tar(ctx, subj, &b)?;
273− tar(ctx, &cell(&foo, &subj), &c)
259+ let foo = tar(ctx, subj.clone(), &b)?;
260+ tar(ctx, cell(&foo, &subj), &c)
274261 }
275262 9 => {
276263 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
277264 let foo = tar(ctx, subj, &c)?;
278265 tar_u32(
279266 ctx,
280− &foo,
267+ foo,
281268 2,
282269 &cell(&ctx.nouns.sig_one, &cell(&ctx.nouns.sig, &b)),
283270 )
@@ -287,7 +274,7 @@ fn tar_u32<'a>(
287274 let (b, c) = b.as_cell().ok_or(TTanks::new())?;
288275 let b = b.as_atom().ok_or(TTanks::new())?;
289276
290− let foo = tar(ctx, subj, &c)?;
277+ let foo = tar(ctx, subj.clone(), &c)?;
291278 let bar = tar(ctx, subj, &d)?;
292279 hax(ctx, b, &foo, &bar)
293280 }
@@ -296,7 +283,7 @@ fn tar_u32<'a>(
296283 match b.borrow() {
297284 Noun::Atom(_) => tar(ctx, subj, &d),
298285 Noun::Cell { p, q, .. } => {
299− let eval = |ctx: &mut InterpreterContext| tar(ctx, subj, &d);
286+ let eval = |ctx: &mut InterpreterContext| tar(ctx, subj.clone(), &d);
300287 if *p == ctx.nouns.memo {
301288 let hash = cell(&subj, &d).hash();
302289 match ctx.memo.get(&hash) {
@@ -310,32 +297,10 @@ fn tar_u32<'a>(
310297 } else if *p == ctx.nouns.mean {
311298 with_trace((subj.clone(), q.clone()), eval(ctx))
312299 } else if *p == ctx.nouns.slog {
313− let noun = tar(ctx, subj, &q)?;
300+ let noun = tar(ctx, subj.clone(), &q)?;
314301 let (_prio, tank) = noun.as_cell().ok_or(TTanks::new())?;
315302
316− (ctx.slog)(&ram(ctx, tank.clone()).unwrap_or(String::new()));
317− eval(ctx)
318− } else if *p == ctx.nouns.fast {
319− if cfg!(feature = "count-jets") {
320− let tag = q.as_cell().unwrap().1.as_cell().unwrap().0.as_atom();
321− match tag {
322− None => (),
323− Some(tag) => {
324− let mut counter = JET_COUNTER.lock().unwrap();
325− match counter.get(tag) {
326− None => {
327− counter.insert(tag.clone(), 1);
328− ()
329− }
330− Some(v) => {
331− let v = *v;
332− counter.insert(tag.clone(), v + 1);
333− ()
334− }
335− }
336− }
337− }
338− }
303+ eprintln!("{}", ram(ctx, tank.clone()).unwrap_or(String::new()));
339304 eval(ctx)
340305 } else {
341306 eval(ctx)
@@ -356,22 +321,24 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
356321 Noun::Cell { p, q, .. } => {
357322 let tag = p.as_atom()?.to_bytes_le();
358323 if tag == b"leaf" {
359− let mut bytes = crip(ctx, q.clone()).ok()?.as_bytes()?;
324+ let mut bytes = crip(ctx, q.clone()).ok()?.as_atom()?.to_bytes_le();
360325 Some(String::from(str::from_utf8_mut(&mut bytes).ok()?))
361326 } else if tag == b"palm" {
362− let (spacers, tanks) = q.as_cell()?;
363− let (mid, open, flat_open, close) = spacers.tuple_4()?;
327+ let (mid, foo) = q.as_cell()?;
328+ let (open, foo) = foo.as_cell()?;
329+ let (flat_open, foo) = foo.as_cell()?;
330+ let (close, tanks) = foo.as_cell()?;
364331
365− let mut mid = crip(ctx, mid.clone()).ok()?.as_bytes()?;
332+ let mut mid = mid.as_bytes()?;
366333 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
367334
368− let mut open = crip(ctx, open.clone()).ok()?.as_bytes()?;
335+ let mut open = open.as_bytes()?;
369336 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
370337
371− let mut flat_open = crip(ctx, flat_open.clone()).ok()?.as_bytes()?;
338+ let mut flat_open = flat_open.as_bytes()?;
372339 let flat_open = String::from(str::from_utf8_mut(&mut flat_open).ok()?);
373340
374− let mut close = crip(ctx, close.clone()).ok()?.as_bytes()?;
341+ let mut close = close.as_bytes()?;
375342 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
376343
377344 let mut target = String::new();
@@ -394,16 +361,17 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
394361
395362 Some(target)
396363 } else if tag == b"rose" {
397− let (spacers, tanks) = q.as_cell()?;
398− let (mid, open, close) = spacers.tuple_3()?;
364+ let (mid, foo) = q.as_cell()?;
365+ let (open, foo) = foo.as_cell()?;
366+ let (close, tanks) = foo.as_cell()?;
399367
400− let mut mid = crip(ctx, mid.clone()).ok()?.as_bytes()?;
368+ let mut mid = mid.as_bytes()?;
401369 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
402370
403− let mut open = crip(ctx, open.clone()).ok()?.as_bytes()?;
371+ let mut open = open.as_bytes()?;
404372 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
405373
406− let mut close = crip(ctx, close.clone()).ok()?.as_bytes()?;
374+ let mut close = close.as_bytes()?;
407375 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
408376
409377 let mut target = String::new();
@@ -434,15 +402,8 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
434402pub fn ram_ttanks(ctx: &mut InterpreterContext, tanks: TTanks) -> String {
435403 let mut target = String::new();
436404
437− let mut first = true;
438− for tank in tanks.iter().collect::<Vec<_>>().iter().cloned().rev() {
439− if !first {
440− target.push('\n');
441− }
442−
405+ for tank in tanks.iter() {
443406 target.push_str(&*ram_ttank(ctx, tank.clone()));
444−
445− first = false;
446407 }
447408
448409 target
@@ -451,21 +412,17 @@ pub fn ram_ttanks(ctx: &mut InterpreterContext, tanks: TTanks) -> String {
451412pub fn ram_ttank(ctx: &mut InterpreterContext, ttank: TTank) -> String {
452413 let (subj, q) = ttank;
453414
454− let tank = match tar(ctx, &subj, &q) {
415+ let tank = match tar(ctx, subj.clone(), &q) {
455416 Ok(gate) => match eval_pulled_gate(ctx, gate) {
456417 Ok(tank) => tank,
457− Err(_) => {
458− return String::new();
459− }
418+ Err(_) => return String::new(),
460419 },
461− Err(_) => {
462− return String::new();
463− }
420+ Err(_) => return String::new(),
464421 };
465422 ram(ctx, tank).unwrap_or(String::new())
466423}
467424
468−pub fn crip(ctx: &mut InterpreterContext, tape: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
425+fn crip(ctx: &mut InterpreterContext, tape: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
469426 RAP(ctx, &cell(&Rc::new(Noun::from_u32(3)), &tape))
470427}
471428
@@ -484,13 +441,13 @@ pub const EMPTY_TANKS: TTanks = TTanks::new();
484441
485442pub fn tar<'a>(
486443 ctx: &mut InterpreterContext,
487− subj: &'a Rc<Noun>,
444+ subj: Rc<Noun>,
488445 formula: &'a Noun,
489446) -> Result<Rc<Noun>, TTanks> {
490447 let (op, formula) = formula.as_cell().ok_or(TTanks::new())?;
491448 match op.borrow() {
492449 Noun::Cell { .. } => {
493− let foo = tar(ctx, subj, &op)?;
450+ let foo = tar(ctx, subj.clone(), &op)?;
494451 let bar = tar(ctx, subj, &formula)?;
495452 Ok(cell(&foo, &bar))
496453 }
@@ -511,7 +468,7 @@ pub fn eval_gate(
511468 ctx: &mut InterpreterContext,
512469 gate_factory: &Rc<Noun>,
513470) -> Result<Rc<Noun>, TTanks> {
514− let pulled_gate = tar(ctx, &ctx.nouns.sig.clone(), &gate_factory)?;
471+ let pulled_gate = tar(ctx, ctx.nouns.sig.clone(), &gate_factory)?;
515472 eval_pulled_gate(ctx, pulled_gate)
516473}
517474
@@ -520,7 +477,7 @@ pub fn slam(
520477 gate_factory: &Rc<Noun>,
521478 sample: &Rc<Noun>,
522479) -> Result<Rc<Noun>, TTanks> {
523− let gate = tar(ctx, &ctx.nouns.sig.clone(), &gate_factory)?;
480+ let gate = tar(ctx, ctx.nouns.sig.clone(), &gate_factory)?;
524481
525482 eval_pulled_gate(ctx, replace_sample(&gate, sample)?)
526483}
@@ -530,7 +487,7 @@ pub fn slam(
530487pub fn eval_pulled_gate(ctx: &mut InterpreterContext, gate: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
531488 tar(
532489 ctx,
533− &gate,
490+ gate,
534491 &cell(
535492 &Rc::new(Noun::Atom(BigUint::new(vec![9]))),
536493 &cell(&ctx.nouns.two, &cell(&ctx.nouns.sig, &ctx.nouns.one)),