Skip to content

Commit

Permalink
πŸ’ polish the main loop
Browse files Browse the repository at this point in the history
  • Loading branch information
cratelyn committed Nov 23, 2023
1 parent c49b689 commit cb4a07a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ voice feels like, and why people do it.

#### 🐣 j

**πŸ” main loop**

above we looked at the Incunabulum's main event loop. here is j's equivalent entrypoint:

```rust
mod p;use{p::*,j::*,std::io::Write};
fn main()->R<()>{let mut st=ST::default(); // define symbol table
let prompt =| |{print!(" ");std::io::stdout().flush()?;ok!()}; // (callback) print whitespace
let read =|_ |{let mut l=S::new();stdin().read_line(&mut l)?;Ok(l)}; // (callback) read input
let mut eval=|s:S|{eval(&s,&mut st)}; // (callback) read and evaluate once
let print =|a:A|{println!("{a}")}; // (callback) print array
loop{prompt().and_then(read).and_then(&mut eval)?.map(print);}; /* !!! main event loop !!! */ }
```

**πŸƒ verbs**

the core of the four dyadic verbs `[`, `]`, `+`, and `*` is shown below. the definitions of the `A`
array type and the `D` dyadic verb enum are included for reference.

Expand Down
15 changes: 9 additions & 6 deletions src/x.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! a J interpreter fragment, implemented in Rust.
#![allow(dead_code,unused_variables,unreachable_code,unused_imports,unused_parens)]
mod p; use{p::*,j::*}; fn main()->R<()>{use std::io::Write;let(mut st)=ST::default();
let(pr)=||{print!(" ");std::io::stdout().flush()};
let(rl)=||{let(mut l)=S::new();stdin().read_line(&mut l)?;Ok::<_,E>(l)};let(mut ev)=|l:S|eval(&l,&mut st);
loop{pr()?;let(o)=rl().and_then(|l|ev(l))?;if let Some(o)=o{println!("{}",o)}}ur!();}
/**a J interpreter fragment, implemented in Rust.*/
mod p;use{p::*,j::*,std::io::Write};
/// main interpreter entrypoint and event-loop.
fn main()->R<()>{let mut st=ST::default(); // define symbol table
let prompt =| |{print!(" ");std::io::stdout().flush()?;ok!()}; // (callback) print whitespace
let read =|_ |{let mut l=S::new();stdin().read_line(&mut l)?;Ok(l)}; // (callback) read input
let mut eval=|s:S|{eval(&s,&mut st)}; // (callback) read and evaluate once
let print =|a:A|{println!("{a}")}; // (callback) print array
loop{prompt().and_then(read).and_then(&mut eval)?.map(print);}; /* !!! main event loop !!! */ }

0 comments on commit cb4a07a

Please sign in to comment.