Browse Source

some repl improvements

Getty Ritter 2 years ago
parent
commit
6dff93d870
3 changed files with 34 additions and 5 deletions
  1. 1 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 32 5
      src/main.rs

+ 1 - 0
Cargo.lock

@@ -337,6 +337,7 @@ dependencies = [
 name = "matzo"
 version = "0.1.0"
 dependencies = [
+ "ansi_term",
  "lalrpop",
  "lalrpop-util",
  "logos",

+ 1 - 0
Cargo.toml

@@ -24,6 +24,7 @@ rand = "*"
 lalrpop-util = { version = "*", features = ["lexer"] }
 logos = "*"
 rustyline = "*"
+ansi_term = "*"
 
 [build-dependencies.lalrpop]
 version = "*"

+ 32 - 5
src/main.rs

@@ -17,8 +17,14 @@ fn run_repl() -> Result<(), Box<dyn std::error::Error>> {
     let mut rl = rustyline::Editor::<()>::new();
     let mut state = State::new();
     let parser = StmtsParser::new();
-    println!("matzo interpreter");
-    println!("(work-in-progress)");
+    println!(
+        "{}",
+        ansi_term::Colour::Blue.bold().paint("matzo interpreter"),
+    );
+    println!(
+        "{}",
+        ansi_term::Colour::Blue.paint("(work-in-progress)"),
+    );
 
     loop {
         let line = match rl.readline(">>> ") {
@@ -33,13 +39,34 @@ fn run_repl() -> Result<(), Box<dyn std::error::Error>> {
         let stmts = match parser.parse(lexed) {
             Ok(stmts) => stmts,
             Err(err) => {
-                eprintln!("{:?}", err);
-                continue;
+                // for the REPL specifically, let's try adding a
+                // `puts` to see if that works
+                let added_puts = format!("puts {}", line);
+                let lexed = tokens(&added_puts);
+                match parser.parse(lexed) {
+                    Ok(stmts) => stmts,
+                    Err(_) => {
+                        // that didn't fix it, so report the
+                        // _original_ parse error, not the new one
+                        eprintln!(
+                            "{}",
+                            ansi_term::Colour::Red.paint(
+                                format!("{:?}", err)
+                            ),
+                        );
+                        continue;
+                    }
+                }
             }
         };
+
         for stmt in stmts {
             if let Err(err) = state.execute(&stmt) {
-                eprintln!("error: {}", err);
+                eprintln!(
+                    "{} {}",
+                    ansi_term::Colour::Red.bold().paint("error:"),
+                    ansi_term::Colour::Red.paint(format!("{}", err)),
+                );
             }
         }
     }