| 71 | // Use the `help` command at any time to see a list of available commands. |
| 72 | |
| 73 | pub fn start_repl(functions_file: Option<String>, stack_string: Option<String>) { |
| 74 | let mut program_lines: Vec<String> = Vec::new(); |
| 75 | let mut functions_miden = "".to_string(); |
| 76 | if let Some(functions_file) = functions_file { |
| 77 | let path = Path::new(&functions_file); |
| 78 | functions_miden = fs::read_to_string(path).expect("Something went wrong reading the file"); |
| 79 | } |
| 80 | if let Some(stack_string) = stack_string { |
| 81 | program_lines.push( |
| 82 | stack_string |
| 83 | .split(',') |
| 84 | .map(|s| format!("push.{}", s)) |
| 85 | .collect::<Vec<_>>() |
| 86 | .into_iter() |
| 87 | .rev() |
| 88 | .collect::<Vec<_>>() |
| 89 | .join(" "), |
| 90 | ); |
| 91 | } |
| 92 | let mut rl = Editor::<()>::new(); |
| 93 | loop { |
| 94 | let program = format!( |
| 95 | "begin\n{}\nend", |
| 96 | program_lines |
| 97 | .iter() |
| 98 | .map(|l| format!(" {}", l)) |
| 99 | .collect::<Vec<_>>() |
| 100 | .join("\n") |
| 101 | ); |
| 102 | let program_with_procs = format!( |
| 103 | "{}\n{}\n{}", |
| 104 | functions_miden, |
| 105 | load_all_procs(), |
| 106 | program.clone() |
| 107 | ); |
| 108 | let result = execute(program_with_procs, vec![]); |
| 109 | |
| 110 | let mut result_string = "".to_string(); |
| 111 | if !program_lines.is_empty() { |
| 112 | match result { |
| 113 | Ok(execution_value) => { |
| 114 | let stack = execution_value.last_stack_state(); |
| 115 | result_string = format!( |
| 116 | "\n{}", |
| 117 | stack |
| 118 | .iter() |
| 119 | .map(|f| format!("{}", f)) |
| 120 | .collect::<Vec<_>>() |
| 121 | .join(" "), |
| 122 | ); |
| 123 | } |
| 124 | Err(e) => { |
| 125 | result_string = format!("Error running program: {:?}", e); |
| 126 | println!("{}", result_string); |
| 127 | program_lines.pop(); |
| 128 | } |
| 129 | } |
| 130 | } |