Skip to content

Commit cb4a07a

Browse files
committed
πŸ’ polish the main loop
1 parent c49b689 commit cb4a07a

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

β€ŽREADME.md

+16
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,22 @@ voice feels like, and why people do it.
139139

140140
#### 🐣 j
141141

142+
**πŸ” main loop**
143+
144+
above we looked at the Incunabulum's main event loop. here is j's equivalent entrypoint:
145+
146+
```rust
147+
mod p;use{p::*,j::*,std::io::Write};
148+
fn main()->R<()>{let mut st=ST::default(); // define symbol table
149+
let prompt =| |{print!(" ");std::io::stdout().flush()?;ok!()}; // (callback) print whitespace
150+
let read =|_ |{let mut l=S::new();stdin().read_line(&mut l)?;Ok(l)}; // (callback) read input
151+
let mut eval=|s:S|{eval(&s,&mut st)}; // (callback) read and evaluate once
152+
let print =|a:A|{println!("{a}")}; // (callback) print array
153+
loop{prompt().and_then(read).and_then(&mut eval)?.map(print);}; /* !!! main event loop !!! */ }
154+
```
155+
156+
**πŸƒ verbs**
157+
142158
the core of the four dyadic verbs `[`, `]`, `+`, and `*` is shown below. the definitions of the `A`
143159
array type and the `D` dyadic verb enum are included for reference.
144160

β€Žsrc/x.rs

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

0 commit comments

Comments
Β (0)