Control printing eval steps with flags

This commit is contained in:
greg 2017-01-09 04:26:42 -08:00
parent d23e5bff35
commit 9b74527618
2 changed files with 28 additions and 8 deletions

View File

@ -23,15 +23,22 @@ pub struct Evaluator<'a> {
functions: HashMap<String, Function>, functions: HashMap<String, Function>,
variables: HashMap<String, Expression>, variables: HashMap<String, Expression>,
stdout: BufWriter<Stdout>, stdout: BufWriter<Stdout>,
pub trace_evaluation: bool,
} }
impl<'a> Evaluator<'a> { impl<'a> Evaluator<'a> {
pub fn new_with_opts(parent: Option<&'a Evaluator>, trace_evaluation: bool) -> Evaluator<'a> {
let mut e = Evaluator::new(parent);
e.trace_evaluation = trace_evaluation;
e
}
pub fn new(parent: Option<&'a Evaluator>) -> Evaluator<'a> { pub fn new(parent: Option<&'a Evaluator>) -> Evaluator<'a> {
Evaluator { Evaluator {
functions: HashMap::new(), functions: HashMap::new(),
variables: HashMap::new(), variables: HashMap::new(),
parent: parent, parent: parent,
stdout: BufWriter::new(::std::io::stdout()), stdout: BufWriter::new(::std::io::stdout()),
trace_evaluation: parent.map_or(false, |e| e.trace_evaluation),
} }
} }
@ -126,7 +133,9 @@ impl<'a> Evaluator<'a> {
} }
fn step(&mut self, node: Statement) -> Statement { fn step(&mut self, node: Statement) -> Statement {
if self.trace_evaluation {
println!("Step: {:?}", node); println!("Step: {:?}", node);
}
let (new_node, side_effect) = self.reduce_astnode(node); let (new_node, side_effect) = self.reduce_astnode(node);
if let Some(s) = side_effect { if let Some(s) = side_effect {
self.perform_side_effect(s); self.perform_side_effect(s);
@ -280,7 +289,6 @@ impl<'a> Evaluator<'a> {
} }
fn reduce_binop(&mut self, op: Rc<String>, left: Expression, right: Expression) -> Expression { fn reduce_binop(&mut self, op: Rc<String>, left: Expression, right: Expression) -> Expression {
println!("Got op {:?}, l: {:?}, r: {:?}", op, left, right);
let truthy = Number(1.0); let truthy = Number(1.0);
let falsy = Null; let falsy = Null;
match (&op[..], left, right) { match (&op[..], left, right) {

View File

@ -24,12 +24,13 @@ mod llvm_wrap;
fn main() { fn main() {
let option_matches = let option_matches =
program_options().parse(std::env::args()).expect("Could not parse options"); program_options().parse(std::env::args()).expect("Could not parse options");
let trace = option_matches.opt_present("t");
match option_matches.free[..] { match option_matches.free[..] {
[] | [_] => { [] | [_] => {
run_repl(); run_repl(trace);
} }
[_, ref filename, _..] => { [_, ref filename, _..] => {
run_noninteractive(filename, !option_matches.opt_present("i")); run_noninteractive(filename, !option_matches.opt_present("i"), trace);
} }
}; };
} }
@ -39,10 +40,13 @@ fn program_options() -> getopts::Options {
options.optflag("i", options.optflag("i",
"interpret", "interpret",
"Interpret source file instead of compiling"); "Interpret source file instead of compiling");
options.optflag("t",
"trace-evaluation",
"Print out trace of evaluation");
options options
} }
fn run_noninteractive(filename: &str, compile: bool) { fn run_noninteractive(filename: &str, compile: bool, trace_evaluation: bool) {
let mut source_file = File::open(&Path::new(filename)).unwrap(); let mut source_file = File::open(&Path::new(filename)).unwrap();
let mut buffer = String::new(); let mut buffer = String::new();
source_file.read_to_string(&mut buffer).unwrap(); source_file.read_to_string(&mut buffer).unwrap();
@ -67,7 +71,7 @@ fn run_noninteractive(filename: &str, compile: bool) {
if compile { if compile {
compilation_sequence(ast, filename); compilation_sequence(ast, filename);
} else { } else {
let mut evaluator = Evaluator::new(None); let mut evaluator = Evaluator::new_with_opts(None, trace_evaluation);
let results = evaluator.run(ast); let results = evaluator.run(ast);
for result in results.iter() { for result in results.iter() {
println!("{}", result); println!("{}", result);
@ -75,12 +79,13 @@ fn run_noninteractive(filename: &str, compile: bool) {
} }
} }
fn run_repl() { fn run_repl(trace_evaluation: bool) {
println!("Schala v 0.02"); println!("Schala v 0.02");
let initial_state = InterpreterState { let initial_state = InterpreterState {
show_tokens: false, show_tokens: false,
show_parse: false, show_parse: false,
evaluator: Evaluator::new(None), show_eval: trace_evaluation,
evaluator: Evaluator::new_with_opts(None, trace_evaluation),
}; };
REPL::with_prompt_and_state(Box::new(repl_handler), ">> ", initial_state).run(); REPL::with_prompt_and_state(Box::new(repl_handler), ">> ", initial_state).run();
} }
@ -88,6 +93,7 @@ fn run_repl() {
struct InterpreterState<'a> { struct InterpreterState<'a> {
show_tokens: bool, show_tokens: bool,
show_parse: bool, show_parse: bool,
show_eval: bool,
evaluator: Evaluator<'a>, evaluator: Evaluator<'a>,
} }
@ -106,6 +112,12 @@ impl<'a> ReplState for InterpreterState<'a> {
["set", "show", "parse", "false"] => { ["set", "show", "parse", "false"] => {
self.show_parse = false; self.show_parse = false;
} }
["set", "show", "eval", "true"] => {
self.evaluator.trace_evaluation = true;
}
["set", "show", "eval", "false"] => {
self.evaluator.trace_evaluation = false;
}
_ => (), _ => (),
} }
} }