@@ -1,17 +1,23 @@
1use core::str;
1use core::str;
2use std::borrow::Borrow;
2use std::borrow::Borrow;
3−use std::borrow::BorrowMut;
4use std::collections::BTreeMap;
3use std::collections::BTreeMap;
4+use std::collections::HashMap;
5use std::rc::Rc;
5use std::rc::Rc;
6−use std::str::Utf8Error;
6+use std::sync::Mutex;
8use fplist::cons;
8use fplist::cons;
9use fplist::PersistentList;
9use fplist::PersistentList;
10+use lazy_static::lazy_static;
10use num_bigint::BigUint;
11use num_bigint::BigUint;
12use crate::jets::*;
13use crate::jets::*;
13use crate::noun::*;
14use crate::noun::*;
16+// #[cfg(feature = "count-jets")]
18+ pub static ref JET_COUNTER: Mutex<HashMap<Atom, u32>> = Mutex::new(HashMap::new());
15#[derive(Clone)]
21#[derive(Clone)]
16pub struct Nouns {
22pub struct Nouns {
17 pub y: Rc<Noun>,
23 pub y: Rc<Noun>,
@@ -26,6 +32,7 @@ pub struct Nouns {
26 pub memo: Rc<Noun>,
32 pub memo: Rc<Noun>,
27 pub mean: Rc<Noun>,
33 pub mean: Rc<Noun>,
28 pub slog: Rc<Noun>,
34 pub slog: Rc<Noun>,
31#[derive(Clone)]
38#[derive(Clone)]
@@ -44,6 +51,7 @@ pub struct InterpreterContext {
44 pub nouns: Nouns,
51 pub nouns: Nouns,
45 pub big_uints: BigUints,
52 pub big_uints: BigUints,
46 pub memo: BTreeMap<Hash, Rc<Noun>>,
53 pub memo: BTreeMap<Hash, Rc<Noun>>,
54+ pub slog: fn(&String) -> (),
49pub fn generate_interpreter_context() -> InterpreterContext {
57pub fn generate_interpreter_context() -> InterpreterContext {
@@ -66,6 +74,7 @@ pub fn generate_interpreter_context() -> InterpreterContext {
66 let memo = Rc::new(Noun::from_bytes(b"memo"));
74 let memo = Rc::new(Noun::from_bytes(b"memo"));
67 let mean = Rc::new(Noun::from_bytes(b"mean"));
75 let mean = Rc::new(Noun::from_bytes(b"mean"));
68 let slog = Rc::new(Noun::from_bytes(b"slog"));
76 let slog = Rc::new(Noun::from_bytes(b"slog"));
77+ let fast = Rc::new(Noun::from_bytes(b"fast"));
69 InterpreterContext {
78 InterpreterContext {
71 double_jets,
80 double_jets,
@@ -82,9 +91,11 @@ pub fn generate_interpreter_context() -> InterpreterContext {
86 big_uints,
96 big_uints,
87 memo: BTreeMap::new(),
97 memo: BTreeMap::new(),
98+ slog: |str: &String| eprintln!("{}", str),
@@ -193,7 +204,7 @@ fn hax(
194fn tar_u32<'a>(
205fn tar_u32<'a>(
195 ctx: &mut InterpreterContext,
206 ctx: &mut InterpreterContext,
196− subj: Rc<Noun>,
207+ subj: &'a Rc<Noun>,
198 formula: &'a Rc<Noun>,
209 formula: &'a Rc<Noun>,
199) -> Result<Rc<Noun>, TTanks> {
210) -> Result<Rc<Noun>, TTanks> {
@@ -216,16 +227,16 @@ fn tar_u32<'a>(
216 match ctx.jets.get(&hash) {
227 match ctx.jets.get(&hash) {
217 Some(f) => f(ctx, sample),
228 Some(f) => f(ctx, sample),
218 None => {
229 None => {
219− let foo = tar(ctx, subj.clone(), &b)?;
230+ let foo = tar(ctx, subj, &b)?;
220 let bar = tar(ctx, subj, &c)?;
231 let bar = tar(ctx, subj, &c)?;
221− tar(ctx, foo, &bar)
232+ tar(ctx, &foo, &bar)
226− let foo = tar(ctx, subj.clone(), &b)?;
237+ let foo = tar(ctx, subj, &b)?;
227 let bar = tar(ctx, subj, &c)?;
238 let bar = tar(ctx, subj, &c)?;
228− tar(ctx, foo, &bar)
239+ tar(ctx, &foo, &bar)
@@ -236,7 +247,7 @@ fn tar_u32<'a>(
236 4 => Ok(Rc::new(lus(&*tar(ctx, subj, &formula)?)?)),
247 4 => Ok(Rc::new(lus(&*tar(ctx, subj, &formula)?)?)),
238 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
249 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
239− let foo = tar(ctx, subj.clone(), &b)?;
250+ let foo = tar(ctx, subj, &b)?;
240 let bar = tar(ctx, subj, &c)?;
251 let bar = tar(ctx, subj, &c)?;
241 Ok(tis(ctx, &foo, &bar).clone())
252 Ok(tis(ctx, &foo, &bar).clone())
@@ -244,29 +255,29 @@ fn tar_u32<'a>(
244 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
255 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
245 let (c, d) = c.as_cell().ok_or(TTanks::new())?;
256 let (c, d) = c.as_cell().ok_or(TTanks::new())?;
247− let bar = tar_u32(ctx, subj.clone(), 4, &cell(&ctx.nouns.four.clone(), &b))?;
258+ let bar = tar_u32(ctx, subj, 4, &cell(&ctx.nouns.four.clone(), &b))?;
249− let foo = tar_u32(ctx, ctx.nouns.two_three.clone(), 0, &bar)?;
260+ let foo = tar_u32(ctx, &ctx.nouns.two_three.clone(), 0, &bar)?;
250− let foo = tar_u32(ctx, cell(&c, &d), 0, &foo)?;
261+ let foo = tar_u32(ctx, &cell(&c, &d), 0, &foo)?;
252 tar(ctx, subj, &foo)
263 tar(ctx, subj, &foo)
255 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
266 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
256 let foo = tar(ctx, subj, &b)?;
267 let foo = tar(ctx, subj, &b)?;
257− tar(ctx, foo, &c)
268+ tar(ctx, &foo, &c)
260 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
271 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
261− let foo = tar(ctx, subj.clone(), &b)?;
272+ let foo = tar(ctx, subj, &b)?;
262− tar(ctx, cell(&foo, &subj), &c)
273+ tar(ctx, &cell(&foo, &subj), &c)
265 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
276 let (b, c) = formula.as_cell().ok_or(TTanks::new())?;
266 let foo = tar(ctx, subj, &c)?;
277 let foo = tar(ctx, subj, &c)?;
271 &cell(&ctx.nouns.sig_one, &cell(&ctx.nouns.sig, &b)),
282 &cell(&ctx.nouns.sig_one, &cell(&ctx.nouns.sig, &b)),
@@ -276,7 +287,7 @@ fn tar_u32<'a>(
276 let (b, c) = b.as_cell().ok_or(TTanks::new())?;
287 let (b, c) = b.as_cell().ok_or(TTanks::new())?;
277 let b = b.as_atom().ok_or(TTanks::new())?;
288 let b = b.as_atom().ok_or(TTanks::new())?;
279− let foo = tar(ctx, subj.clone(), &c)?;
290+ let foo = tar(ctx, subj, &c)?;
280 let bar = tar(ctx, subj, &d)?;
291 let bar = tar(ctx, subj, &d)?;
281 hax(ctx, b, &foo, &bar)
292 hax(ctx, b, &foo, &bar)
@@ -285,7 +296,7 @@ fn tar_u32<'a>(
285 match b.borrow() {
296 match b.borrow() {
286 Noun::Atom(_) => tar(ctx, subj, &d),
297 Noun::Atom(_) => tar(ctx, subj, &d),
287 Noun::Cell { p, q, .. } => {
298 Noun::Cell { p, q, .. } => {
288− let eval = |ctx: &mut InterpreterContext| tar(ctx, subj.clone(), &d);
299+ let eval = |ctx: &mut InterpreterContext| tar(ctx, subj, &d);
289 if *p == ctx.nouns.memo {
300 if *p == ctx.nouns.memo {
290 let hash = cell(&subj, &d).hash();
301 let hash = cell(&subj, &d).hash();
291 match ctx.memo.get(&hash) {
302 match ctx.memo.get(&hash) {
@@ -299,10 +310,32 @@ fn tar_u32<'a>(
299 } else if *p == ctx.nouns.mean {
310 } else if *p == ctx.nouns.mean {
300 with_trace((subj.clone(), q.clone()), eval(ctx))
311 with_trace((subj.clone(), q.clone()), eval(ctx))
301 } else if *p == ctx.nouns.slog {
312 } else if *p == ctx.nouns.slog {
302− let noun = tar(ctx, subj.clone(), &q)?;
313+ let noun = tar(ctx, subj, &q)?;
303 let (_prio, tank) = noun.as_cell().ok_or(TTanks::new())?;
314 let (_prio, tank) = noun.as_cell().ok_or(TTanks::new())?;
305− eprintln!("{}", ram(ctx, tank.clone()).unwrap_or(String::new()));
316+ (ctx.slog)(&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);
306 eval(ctx)
339 eval(ctx)
308 eval(ctx)
341 eval(ctx)
@@ -323,24 +356,22 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
323 Noun::Cell { p, q, .. } => {
356 Noun::Cell { p, q, .. } => {
324 let tag = p.as_atom()?.to_bytes_le();
357 let tag = p.as_atom()?.to_bytes_le();
325 if tag == b"leaf" {
358 if tag == b"leaf" {
326− let mut bytes = crip(ctx, q.clone()).ok()?.as_atom()?.to_bytes_le();
359+ let mut bytes = crip(ctx, q.clone()).ok()?.as_bytes()?;
327 Some(String::from(str::from_utf8_mut(&mut bytes).ok()?))
360 Some(String::from(str::from_utf8_mut(&mut bytes).ok()?))
328 } else if tag == b"palm" {
361 } else if tag == b"palm" {
329− let (mid, foo) = q.as_cell()?;
362+ let (spacers, tanks) = q.as_cell()?;
330− let (open, foo) = foo.as_cell()?;
363+ let (mid, open, flat_open, close) = spacers.tuple_4()?;
331− let (flat_open, foo) = foo.as_cell()?;
332− let (close, tanks) = foo.as_cell()?;
334− let mut mid = mid.as_bytes()?;
365+ let mut mid = crip(ctx, mid.clone()).ok()?.as_bytes()?;
335 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
366 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
337− let mut open = open.as_bytes()?;
368+ let mut open = crip(ctx, open.clone()).ok()?.as_bytes()?;
338 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
369 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
340− let mut flat_open = flat_open.as_bytes()?;
371+ let mut flat_open = crip(ctx, flat_open.clone()).ok()?.as_bytes()?;
341 let flat_open = String::from(str::from_utf8_mut(&mut flat_open).ok()?);
372 let flat_open = String::from(str::from_utf8_mut(&mut flat_open).ok()?);
343− let mut close = close.as_bytes()?;
374+ let mut close = crip(ctx, close.clone()).ok()?.as_bytes()?;
344 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
375 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
346 let mut target = String::new();
377 let mut target = String::new();
@@ -363,17 +394,16 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
364 Some(target)
395 Some(target)
365 } else if tag == b"rose" {
396 } else if tag == b"rose" {
366− let (mid, foo) = q.as_cell()?;
397+ let (spacers, tanks) = q.as_cell()?;
367− let (open, foo) = foo.as_cell()?;
398+ let (mid, open, close) = spacers.tuple_3()?;
368− let (close, tanks) = foo.as_cell()?;
370− let mut mid = mid.as_bytes()?;
400+ let mut mid = crip(ctx, mid.clone()).ok()?.as_bytes()?;
371 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
401 let mid = String::from(str::from_utf8_mut(&mut mid).ok()?);
373− let mut open = open.as_bytes()?;
403+ let mut open = crip(ctx, open.clone()).ok()?.as_bytes()?;
374 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
404 let open = String::from(str::from_utf8_mut(&mut open).ok()?);
376− let mut close = close.as_bytes()?;
406+ let mut close = crip(ctx, close.clone()).ok()?.as_bytes()?;
377 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
407 let close = String::from(str::from_utf8_mut(&mut close).ok()?);
379 let mut target = String::new();
409 let mut target = String::new();
@@ -404,8 +434,15 @@ fn ram(ctx: &mut InterpreterContext, tank: Rc<Noun>) -> Option<String> {
404pub fn ram_ttanks(ctx: &mut InterpreterContext, tanks: TTanks) -> String {
434pub fn ram_ttanks(ctx: &mut InterpreterContext, tanks: TTanks) -> String {
405 let mut target = String::new();
435 let mut target = String::new();
437+ let mut first = true;
407− for tank in tanks.iter() {
438+ for tank in tanks.iter().collect::<Vec<_>>().iter().cloned().rev() {
408 target.push_str(&*ram_ttank(ctx, tank.clone()));
443 target.push_str(&*ram_ttank(ctx, tank.clone()));
@@ -414,17 +451,21 @@ pub fn ram_ttanks(ctx: &mut InterpreterContext, tanks: TTanks) -> String {
414pub fn ram_ttank(ctx: &mut InterpreterContext, ttank: TTank) -> String {
451pub fn ram_ttank(ctx: &mut InterpreterContext, ttank: TTank) -> String {
415 let (subj, q) = ttank;
452 let (subj, q) = ttank;
417− let tank = match tar(ctx, subj.clone(), &q) {
454+ let tank = match tar(ctx, &subj, &q) {
418 Ok(gate) => match eval_pulled_gate(ctx, gate) {
455 Ok(gate) => match eval_pulled_gate(ctx, gate) {
419 Ok(tank) => tank,
456 Ok(tank) => tank,
420− Err(_) => return String::new(),
458+ return String::new();
422− Err(_) => return String::new(),
462+ return String::new();
424 ram(ctx, tank).unwrap_or(String::new())
465 ram(ctx, tank).unwrap_or(String::new())
427−fn crip(ctx: &mut InterpreterContext, tape: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
468+pub fn crip(ctx: &mut InterpreterContext, tape: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
428 RAP(ctx, &cell(&Rc::new(Noun::from_u32(3)), &tape))
469 RAP(ctx, &cell(&Rc::new(Noun::from_u32(3)), &tape))
@@ -443,13 +484,13 @@ pub const EMPTY_TANKS: TTanks = TTanks::new();
444pub fn tar<'a>(
485pub fn tar<'a>(
445 ctx: &mut InterpreterContext,
486 ctx: &mut InterpreterContext,
446− subj: Rc<Noun>,
487+ subj: &'a Rc<Noun>,
447 formula: &'a Noun,
488 formula: &'a Noun,
448) -> Result<Rc<Noun>, TTanks> {
489) -> Result<Rc<Noun>, TTanks> {
449 let (op, formula) = formula.as_cell().ok_or(TTanks::new())?;
490 let (op, formula) = formula.as_cell().ok_or(TTanks::new())?;
450 match op.borrow() {
491 match op.borrow() {
451 Noun::Cell { .. } => {
492 Noun::Cell { .. } => {
452− let foo = tar(ctx, subj.clone(), &op)?;
493+ let foo = tar(ctx, subj, &op)?;
453 let bar = tar(ctx, subj, &formula)?;
494 let bar = tar(ctx, subj, &formula)?;
454 Ok(cell(&foo, &bar))
495 Ok(cell(&foo, &bar))
@@ -470,7 +511,7 @@ pub fn eval_gate(
470 ctx: &mut InterpreterContext,
511 ctx: &mut InterpreterContext,
471 gate_factory: &Rc<Noun>,
512 gate_factory: &Rc<Noun>,
472) -> Result<Rc<Noun>, TTanks> {
513) -> Result<Rc<Noun>, TTanks> {
473− let pulled_gate = tar(ctx, ctx.nouns.sig.clone(), &gate_factory)?;
514+ let pulled_gate = tar(ctx, &ctx.nouns.sig.clone(), &gate_factory)?;
474 eval_pulled_gate(ctx, pulled_gate)
515 eval_pulled_gate(ctx, pulled_gate)
@@ -479,7 +520,7 @@ pub fn slam(
479 gate_factory: &Rc<Noun>,
520 gate_factory: &Rc<Noun>,
480 sample: &Rc<Noun>,
521 sample: &Rc<Noun>,
481) -> Result<Rc<Noun>, TTanks> {
522) -> Result<Rc<Noun>, TTanks> {
482− let gate = tar(ctx, ctx.nouns.sig.clone(), &gate_factory)?;
523+ let gate = tar(ctx, &ctx.nouns.sig.clone(), &gate_factory)?;
484 eval_pulled_gate(ctx, replace_sample(&gate, sample)?)
525 eval_pulled_gate(ctx, replace_sample(&gate, sample)?)
@@ -489,7 +530,7 @@ pub fn slam(
489pub fn eval_pulled_gate(ctx: &mut InterpreterContext, gate: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
530pub fn eval_pulled_gate(ctx: &mut InterpreterContext, gate: Rc<Noun>) -> Result<Rc<Noun>, TTanks> {
494 &Rc::new(Noun::Atom(BigUint::new(vec![9]))),
535 &Rc::new(Noun::Atom(BigUint::new(vec![9]))),
495 &cell(&ctx.nouns.two, &cell(&ctx.nouns.sig, &ctx.nouns.one)),
536 &cell(&ctx.nouns.two, &cell(&ctx.nouns.sig, &ctx.nouns.one)),