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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
//! usage: rlwrap cargo run --release --example repl
//!
//! Example Session:
//!
//! github.com/fasteval$ rlwrap cargo run --release --example repl
//! Finished release [optimized] target(s) in 0.01s
//! Running `target/release/examples/repl`
//! >>> print("Hello fasteval", 1, 2, 3)
//! Hello fasteval 1 2 3
//! 3
//! >>> _ + 1
//! 4
//! >>> _ + 1
//! 5
//! >>> _ * 2
//! 10
//! >>> _ ^ 0.5
//! 3.1622776601683795
//! >>> let a = 1
//! 1
//! >>> let b = a + 1
//! 2
//! >>> let c = a + b * 3
//! 7
//! >>> a + b + c
//! 10
//! >>> push
//! Entered scope[1]
//! >>> let b = b + 10
//! 12
//! >>> a + b + c
//! 20
//! >>> pop
//! Exited scope[1]
//! >>> a + b + c
//! 10
//! >>> 1+2*3/4^5%6 + log(100K) + log(e(),100) + [3*(3-3)/3] + (2<3) && 1.23
//! 1.23
//! >>> 1+2*3/4^5%6 + print("log(100K) =",log(100K)) + log(e(),100) + [3*(3-3)/3] + (2<3) && 1.23
//! log(100K) = 5
//! 1.23
use fasteval::Evaler; // Import this trait for '.eval()' functionality.
use fasteval::{Parser, Slab};
use std::collections::BTreeMap;
use std::io::{self, BufRead, Write};
fn main() {
repl();
}
fn repl() {
let parser = Parser::new();
let mut slab = Slab::new();
let mut ns_stack = vec![BTreeMap::new()];
let stdin = io::stdin();
let mut lines = stdin.lock().lines();
loop {
eprint!(">>> "); io::stderr().flush().unwrap();
let mut ans_key = "_".to_string();
let line = match lines.next() {
Some(res) => res.unwrap(),
None => break,
};
let mut line = line.trim().to_string();
if line == "" { continue; }
let pieces : Vec<&str> = line.split_whitespace().collect();
if pieces[0] == "let" {
if pieces.len()<4 || pieces[2]!="=" {
eprintln!("incorrect 'let' syntax. Should be: let x = ...");
continue;
}
ans_key = pieces[1].to_string();
line = pieces[3..].join(" ");
} else if pieces[0] == "push" {
ns_stack.push(BTreeMap::new());
eprintln!("Entered scope[{}]", ns_stack.len()-1);
continue;
} else if pieces[0] == "pop" {
let mut return_value = std::f64::NAN; let mut has_return_value = false;
if let Some(v) = ns_stack.last().unwrap().get(&ans_key) {
return_value = *v;
has_return_value = true;
}
ns_stack.pop();
eprintln!("Exited scope[{}]", ns_stack.len());
if ns_stack.is_empty() { ns_stack.push(BTreeMap::new()); } // All scopes have been removed. Add a new one.
if has_return_value {
ns_stack.last_mut().unwrap().insert(ans_key, return_value);
}
continue;
}
let expr_ref = match parser.parse(&line, &mut slab.ps) {
Ok(expr_i) => slab.ps.get_expr(expr_i),
Err(err) => {
eprintln!("parse error: {}", err);
continue;
}
};
let ans = match expr_ref.eval(&slab, &mut ns_stack) {
Ok(val) => val,
Err(err) => {
eprintln!("eval error: {}", err);
continue;
}
};
println!("{}", ans);
ns_stack.last_mut().unwrap().insert(ans_key, ans);
}
println!();
}
|