@@ -1,23 +1,15 @@
1use core::str;
1use core::str;
2use std::borrow::Borrow;
2use std::borrow::Borrow;
3use std::collections::BTreeMap;
3use std::collections::BTreeMap;
4−use std::collections::HashMap;
5use std::rc::Rc;
4use std::rc::Rc;
8use fplist::cons;
6use fplist::cons;
9use fplist::PersistentList;
7use fplist::PersistentList;
10−use lazy_static::lazy_static;
11use num_bigint::BigUint;
8use num_bigint::BigUint;
13use crate::jets::*;
10use crate::jets::*;
14use crate::noun::*;
11use crate::noun::*;
16−// #[cfg(feature = "count-jets")]
18− pub static ref JET_COUNTER: Mutex<HashMap<Atom, u32>> = Mutex::new(HashMap::new());
21#[derive(Clone)]
13#[derive(Clone)]
22pub struct Nouns {
14pub struct Nouns {
23 pub y: Rc<Noun>,
15 pub y: Rc<Noun>,
@@ -32,7 +24,6 @@ pub struct Nouns {
32 pub memo: Rc<Noun>,
24 pub memo: Rc<Noun>,
33 pub mean: Rc<Noun>,
25 pub mean: Rc<Noun>,
34 pub slog: Rc<Noun>,
26 pub slog: Rc<Noun>,
38#[derive(Clone)]
29#[derive(Clone)]
@@ -51,7 +42,6 @@ pub struct InterpreterContext {
51 pub nouns: Nouns,
42 pub nouns: Nouns,
52 pub big_uints: BigUints,
43 pub big_uints: BigUints,
53 pub memo: BTreeMap<Hash, Rc<Noun>>,
44 pub memo: BTreeMap<Hash, Rc<Noun>>,
54− pub slog: fn(&String) -> (),
57pub fn generate_interpreter_context() -> InterpreterContext {
47pub fn generate_interpreter_context() -> InterpreterContext {
@@ -74,7 +64,6 @@ pub fn generate_interpreter_context() -> InterpreterContext {
74 let memo = Rc::new(Noun::from_bytes(b"memo"));
64 let memo = Rc::new(Noun::from_bytes(b"memo"));
75 let mean = Rc::new(Noun::from_bytes(b"mean"));
65 let mean = Rc::new(Noun::from_bytes(b"mean"));
76 let slog = Rc::new(Noun::from_bytes(b"slog"));
66 let slog = Rc::new(Noun::from_bytes(b"slog"));
77− let fast = Rc::new(Noun::from_bytes(b"fast"));
78 InterpreterContext {
67 InterpreterContext {
80 double_jets,
69 double_jets,
@@ -91,11 +80,9 @@ pub fn generate_interpreter_context() -> InterpreterContext {
96 big_uints,
84 big_uints,
97 memo: BTreeMap::new(),
85 memo: BTreeMap::new(),
98− slog: |str: &String| eprintln!("{}", str),
@@ -204,7 +191,7 @@ fn hax(
205fn tar_u32<'a>(
192fn tar_u32<'a>(
206 ctx: &mut InterpreterContext,
193 ctx: &mut InterpreterContext,
207− subj: &'a Rc<Noun>,
194+ subj: Rc<Noun>,
209 formula: &'a Rc<Noun>,
196 formula: &'a Rc<Noun>,
210) -> Result<Rc<Noun>, TTanks> {
197) -> Result<Rc<Noun>, TTanks> {
@@ -227,16 +214,16 @@ fn tar_u32<'a>(
227 match ctx.jets.get(&hash) {
214 match ctx.jets.get(&hash) {
228 Some(f) => f(ctx, sample),
215 Some(f) => f(ctx, sample),
229 None => {
216 None => {
230− let foo = tar(ctx, subj, &b)?;
217+ let foo = tar(ctx, subj.clone(), &b)?;
231 let bar = tar(ctx, subj, &c)?;
218 let bar = tar(ctx, subj, &c)?;
232− tar(ctx, &foo, &bar)
219+ tar(ctx, foo, &bar)
237− let foo = tar(ctx, subj, &b)?;
224+ let foo = tar(ctx, subj.clone(), &b)?;
238 let bar = tar(ctx, subj, &c)?;
225 let bar = tar(ctx, subj, &c)?;
239− tar(ctx, &foo, &bar)
226+ tar(ctx, foo, &bar)
@@ -247,7 +234,7 @@ fn tar_u32<'a>(
247 4 => Ok(Rc::new(lus(&*tar(ctx, subj, &formula)?)?)),
234 4 => Ok(Rc::new(lus(&*tar(ctx, subj, &formula)?)?)),
249 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
236 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)?;
251 let bar = tar(ctx, subj, &c)?;
238 let bar = tar(ctx, subj, &c)?;
252 Ok(tis(ctx, &foo, &bar).clone())
239 Ok(tis(ctx, &foo, &bar).clone())
@@ -255,29 +242,29 @@ fn tar_u32<'a>(
255 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
242 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
256 let (c, d) = c.as_cell().ok_or(TTanks::new())?;
243 let (c, d) = c.as_cell().ok_or(TTanks::new())?;
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))?;
260− let foo = tar_u32(ctx, &ctx.nouns.two_three.clone(), 0, &bar)?;
247+ let foo = tar_u32(ctx, ctx.nouns.two_three.clone(), 0, &bar)?;
261− let foo = tar_u32(ctx, &cell(&c, &d), 0, &foo)?;
248+ let foo = tar_u32(ctx, cell(&c, &d), 0, &foo)?;
263 tar(ctx, subj, &foo)
250 tar(ctx, subj, &foo)
266 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
253 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
267 let foo = tar(ctx, subj, &b)?;
254 let foo = tar(ctx, subj, &b)?;
268− tar(ctx, &foo, &c)
255+ tar(ctx, foo, &c)
271 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
258 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
272− let foo = tar(ctx, subj, &b)?;
259+ let foo = tar(ctx, subj.clone(), &b)?;
273− tar(ctx, &cell(&foo, &subj), &c)
260+ tar(ctx, cell(&foo, &subj), &c)
276 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
263 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
277 let foo = tar(ctx, subj, &c)?;
264 let foo = tar(ctx, subj, &c)?;
282 &cell(&ctx.nouns.sig_one, &cell(&ctx.nouns.sig, &b)),
269 &cell(&ctx.nouns.sig_one, &cell(&ctx.nouns.sig, &b)),
@@ -287,7 +274,7 @@ fn tar_u32<'a>(
287 let (b, c) = b.as_cell().ok_or(TTanks::new())?;
274 let (b, c) = b.as_cell().ok_or(TTanks::new())?;
288 let b = b.as_atom().ok_or(TTanks::new())?;
275 let b = b.as_atom().ok_or(TTanks::new())?;
290− let foo = tar(ctx, subj, &c)?;
277+ let foo = tar(ctx, subj.clone(), &c)?;
291 let bar = tar(ctx, subj, &d)?;
278 let bar = tar(ctx, subj, &d)?;
292 hax(ctx, b, &foo, &bar)
279 hax(ctx, b, &foo, &bar)
@@ -296,7 +283,7 @@ fn tar_u32<'a>(
296 match b.borrow() {
283 match b.borrow() {
297 Noun::Atom(_) => tar(ctx, subj, &d),
284 Noun::Atom(_) => tar(ctx, subj, &d),
298 Noun::Cell { p, q, .. } => {
285 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);
300 if *p == ctx.nouns.memo {
287 if *p == ctx.nouns.memo {
301 let hash = cell(&subj, &d).hash();
288 let hash = cell(&subj, &d).hash();
302 match ctx.memo.get(&hash) {
289 match ctx.memo.get(&hash) {
@@ -310,32 +297,10 @@ fn tar_u32<'a>(
310 } else if *p == ctx.nouns.mean {
297 } else if *p == ctx.nouns.mean {
311 with_trace((subj.clone(), q.clone()), eval(ctx))
298 with_trace((subj.clone(), q.clone()), eval(ctx))
312 } else if *p == ctx.nouns.slog {
299 } else if *p == ctx.nouns.slog {
313− let noun = tar(ctx, subj, &q)?;
300+ let noun = tar(ctx, subj.clone(), &q)?;
314 let (_prio, tank) = noun.as_cell().ok_or(TTanks::new())?;
301 let (_prio, tank) = noun.as_cell().ok_or(TTanks::new())?;
316− (ctx.slog)(&ram(ctx, tank.clone()).unwrap_or(String::new()));
303+ eprintln!("{}", ram(ctx, tank.clone()).unwrap_or(String::new()));
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();
324− let mut counter = JET_COUNTER.lock().unwrap();
325− match counter.get(tag) {
327− counter.insert(tag.clone(), 1);
332− counter.insert(tag.clone(), v + 1);
339 eval(ctx)
304 eval(ctx)
341 eval(ctx)
306 eval(ctx)
@@ -356,22 +321,24 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
356 Noun::Cell { p, q, .. } => {
321 Noun::Cell { p, q, .. } => {
357 let tag = p.as_atom()?.to_bytes_le();
322 let tag = p.as_atom()?.to_bytes_le();
358 if tag == b"leaf" {
323 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();
360 Some(String::from(str::from_utf8_mut(&mut bytes).ok()?))
325 Some(String::from(str::from_utf8_mut(&mut bytes).ok()?))
361 } else if tag == b"palm" {
326 } else if tag == b"palm" {
362− let (spacers, tanks) = q.as_cell()?;
327+ let (mid, foo) = q.as_cell()?;
363− let (mid, open, flat_open, close) = spacers.tuple_4()?;
328+ let (open, foo) = foo.as_cell()?;
329+ let (flat_open, foo) = foo.as_cell()?;
330+ let (close, tanks) = foo.as_cell()?;
365− let mut mid = crip(ctx, mid.clone()).ok()?.as_bytes()?;
332+ let mut mid = mid.as_bytes()?;
366 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
333 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
368− let mut open = crip(ctx, open.clone()).ok()?.as_bytes()?;
335+ let mut open = open.as_bytes()?;
369 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
336 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
371− let mut flat_open = crip(ctx, flat_open.clone()).ok()?.as_bytes()?;
338+ let mut flat_open = flat_open.as_bytes()?;
372 let flat_open = String::from(str::from_utf8_mut(&mut flat_open).ok()?);
339 let flat_open = String::from(str::from_utf8_mut(&mut flat_open).ok()?);
374− let mut close = crip(ctx, close.clone()).ok()?.as_bytes()?;
341+ let mut close = close.as_bytes()?;
375 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
342 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
377 let mut target = String::new();
344 let mut target = String::new();
@@ -394,16 +361,17 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
395 Some(target)
362 Some(target)
396 } else if tag == b"rose" {
363 } else if tag == b"rose" {
397− let (spacers, tanks) = q.as_cell()?;
364+ let (mid, foo) = q.as_cell()?;
398− let (mid, open, close) = spacers.tuple_3()?;
365+ let (open, foo) = foo.as_cell()?;
366+ let (close, tanks) = foo.as_cell()?;
400− let mut mid = crip(ctx, mid.clone()).ok()?.as_bytes()?;
368+ let mut mid = mid.as_bytes()?;
401 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
369 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
403− let mut open = crip(ctx, open.clone()).ok()?.as_bytes()?;
371+ let mut open = open.as_bytes()?;
404 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
372 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
406− let mut close = crip(ctx, close.clone()).ok()?.as_bytes()?;
374+ let mut close = close.as_bytes()?;
407 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
375 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
409 let mut target = String::new();
377 let mut target = String::new();
@@ -434,15 +402,8 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
434pub fn ram_ttanks(ctx: &mut InterpreterContext, tanks: TTanks) -> String {
402pub fn ram_ttanks(ctx: &mut InterpreterContext, tanks: TTanks) -> String {
435 let mut target = String::new();
403 let mut target = String::new();
437− let mut first = true;
438− for tank in tanks.iter().collect::<Vec<_>>().iter().cloned().rev() {
405+ for tank in tanks.iter() {
443 target.push_str(&*ram_ttank(ctx, tank.clone()));
406 target.push_str(&*ram_ttank(ctx, tank.clone()));
@@ -451,21 +412,17 @@ pub fn ram_ttanks(ctx: &mut InterpreterContext, tanks: TTanks) -> String {
451pub fn ram_ttank(ctx: &mut InterpreterContext, ttank: TTank) -> String {
412pub fn ram_ttank(ctx: &mut InterpreterContext, ttank: TTank) -> String {
452 let (subj, q) = ttank;
413 let (subj, q) = ttank;
454− let tank = match tar(ctx, &subj, &q) {
415+ let tank = match tar(ctx, subj.clone(), &q) {
455 Ok(gate) => match eval_pulled_gate(ctx, gate) {
416 Ok(gate) => match eval_pulled_gate(ctx, gate) {
456 Ok(tank) => tank,
417 Ok(tank) => tank,
458− return String::new();
418+ Err(_) => return String::new(),
462− return String::new();
420+ Err(_) => return String::new(),
465 ram(ctx, tank).unwrap_or(String::new())
422 ram(ctx, tank).unwrap_or(String::new())
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> {
469 RAP(ctx, &cell(&Rc::new(Noun::from_u32(3)), &tape))
426 RAP(ctx, &cell(&Rc::new(Noun::from_u32(3)), &tape))
@@ -484,13 +441,13 @@ pub const EMPTY_TANKS: TTanks = TTanks::new();
485pub fn tar<'a>(
442pub fn tar<'a>(
486 ctx: &mut InterpreterContext,
443 ctx: &mut InterpreterContext,
487− subj: &'a Rc<Noun>,
444+ subj: Rc<Noun>,
488 formula: &'a Noun,
445 formula: &'a Noun,
489) -> Result<Rc<Noun>, TTanks> {
446) -> Result<Rc<Noun>, TTanks> {
490 let (op, formula) = formula.as_cell().ok_or(TTanks::new())?;
447 let (op, formula) = formula.as_cell().ok_or(TTanks::new())?;
491 match op.borrow() {
448 match op.borrow() {
492 Noun::Cell { .. } => {
449 Noun::Cell { .. } => {
493− let foo = tar(ctx, subj, &op)?;
450+ let foo = tar(ctx, subj.clone(), &op)?;
494 let bar = tar(ctx, subj, &formula)?;
451 let bar = tar(ctx, subj, &formula)?;
495 Ok(cell(&foo, &bar))
452 Ok(cell(&foo, &bar))
@@ -511,7 +468,7 @@ pub fn eval_gate(
511 ctx: &mut InterpreterContext,
468 ctx: &mut InterpreterContext,
512 gate_factory: &Rc<Noun>,
469 gate_factory: &Rc<Noun>,
513) -> Result<Rc<Noun>, TTanks> {
470) -> 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)?;
515 eval_pulled_gate(ctx, pulled_gate)
472 eval_pulled_gate(ctx, pulled_gate)
@@ -520,7 +477,7 @@ pub fn slam(
520 gate_factory: &Rc<Noun>,
477 gate_factory: &Rc<Noun>,
521 sample: &Rc<Noun>,
478 sample: &Rc<Noun>,
522) -> Result<Rc<Noun>, TTanks> {
479) -> 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)?;
525 eval_pulled_gate(ctx, replace_sample(&gate, sample)?)
482 eval_pulled_gate(ctx, replace_sample(&gate, sample)?)
@@ -530,7 +487,7 @@ pub fn slam(
530pub fn eval_pulled_gate(ctx: &mut InterpreterContext, gate: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
487pub fn eval_pulled_gate(ctx: &mut InterpreterContext, gate: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
535 &Rc::new(Noun::Atom(BigUint::new(vec![9]))),
492 &Rc::new(Noun::Atom(BigUint::new(vec![9]))),
536 &cell(&ctx.nouns.two, &cell(&ctx.nouns.sig, &ctx.nouns.one)),
493 &cell(&ctx.nouns.two, &cell(&ctx.nouns.sig, &ctx.nouns.one)),