|
|
@@ -2,11 +2,14 @@ mod jquery_terminal_formatter;
|
|
|
mod utils;
|
|
|
mod wasm_importer;
|
|
|
|
|
|
+use std::sync::{Arc, Mutex};
|
|
|
+
|
|
|
use jquery_terminal_formatter::{jt_format, JqueryTerminalFormatter};
|
|
|
+use numbat::markup as m;
|
|
|
use numbat::markup::Formatter;
|
|
|
use numbat::pretty_print::PrettyPrint;
|
|
|
use numbat::resolver::CodeSource;
|
|
|
-use numbat::{Context, InterpreterResult};
|
|
|
+use numbat::{Context, InterpreterResult, InterpreterSettings};
|
|
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
|
@@ -37,14 +40,39 @@ impl Numbat {
|
|
|
}
|
|
|
|
|
|
pub fn interpret(&mut self, code: &str) -> String {
|
|
|
+ let mut output = String::new();
|
|
|
+
|
|
|
let fmt = JqueryTerminalFormatter {};
|
|
|
|
|
|
- match self.ctx.interpret(&code, CodeSource::Text) {
|
|
|
- Ok((_, result)) => match result {
|
|
|
- InterpreterResult::Value(q) => fmt.format(&q.pretty_print(), true),
|
|
|
- InterpreterResult::Continue => "".into(),
|
|
|
- InterpreterResult::Exit(_) => jt_format("error", "Error!".into()),
|
|
|
- },
|
|
|
+ let to_be_printed: Arc<Mutex<Vec<m::Markup>>> = Arc::new(Mutex::new(vec![]));
|
|
|
+ let to_be_printed_c = to_be_printed.clone();
|
|
|
+ let mut settings = InterpreterSettings {
|
|
|
+ print_fn: Box::new(move |s: &m::Markup| {
|
|
|
+ to_be_printed_c.lock().unwrap().push(s.clone());
|
|
|
+ }),
|
|
|
+ };
|
|
|
+
|
|
|
+ match self
|
|
|
+ .ctx
|
|
|
+ .interpret_with_settings(&mut settings, &code, CodeSource::Text)
|
|
|
+ {
|
|
|
+ Ok((_, result)) => {
|
|
|
+ for content in to_be_printed.lock().unwrap().iter() {
|
|
|
+ output.push_str(&fmt.format(content, true));
|
|
|
+ }
|
|
|
+
|
|
|
+ match result {
|
|
|
+ InterpreterResult::Value(q) => {
|
|
|
+ output.push_str(&fmt.format(&q.pretty_print(), true))
|
|
|
+ }
|
|
|
+ InterpreterResult::Continue => {}
|
|
|
+ InterpreterResult::Exit(_) => {
|
|
|
+ output.push_str(&jt_format("error", "Error!".into()))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ output
|
|
|
+ }
|
|
|
Err(e) => format!("{:#}", e),
|
|
|
}
|
|
|
}
|