1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::io::{stdin, stdout, Write};

use forth3::{
    leakbox::{LBForth, LBForthParams},
    Forth,
};

fn main() {
    let params = LBForthParams {
        data_stack_elems: 1024,
        return_stack_elems: 1024,
        control_stack_elems: 64,
        input_buf_elems: 1024,
        output_buf_elems: 4096,
        dict_buf_elems: 16 * 1024,
    };
    let mut lbf = LBForth::from_params(params, (), Forth::FULL_BUILTINS);
    let forth = &mut lbf.forth;

    let mut inp = String::new();
    loop {
        print!("> ");
        stdout().flush().unwrap();
        stdin().read_line(&mut inp).unwrap();
        forth.input.fill(&inp).unwrap();
        match forth.process_line() {
            Ok(()) => {
                print!("{}", forth.output.as_str());
            }
            Err(e) => {
                println!();
                println!("Input failed. Error: {:?}", e);
                println!("Unprocessed tokens:");
                while let Some(tok) = forth.input.cur_word() {
                    print!("'{}', ", tok);
                    forth.input.advance();
                }
                println!();
            }
        }

        inp.clear();
        forth.output.clear();
    }
}