Skip to content

Commit

Permalink
Commands in repl for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaqqer committed Jan 5, 2024
1 parent 476ff58 commit cb93aad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions crates/saft-eval/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use saft_ast::{Block, Expr, Ident, Item, Module, Statement};
use saft_common::span::{Span, Spanned};
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt::Write;
use std::rc::Rc;

#[macro_export]
Expand Down Expand Up @@ -627,6 +628,29 @@ impl Interpreter {
}
})
}

pub fn print_env(&self) {
let mut lines = Vec::new();
for scope in self.env.scopes.iter() {
let mut line = Vec::<String>::new();

for (k, v) in scope.iter() {
line.push(format!("{}: {}", k, v.repr()));
}

lines.push(line);
}

println!(
"{}",
lines
.iter()
.enumerate()
.map(|(i, line)| format!("{}: {}", i, &line.join(", ")))
.collect::<Vec<_>>()
.join("\n")
);
}
}

impl Default for Interpreter {
Expand Down
1 change: 1 addition & 0 deletions crates/saft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.4.11", features = ["derive"] }
codespan-reporting = "0.11.1"
indoc = "2.0.4"
platform-dirs = "0.3.0"
rustyline = "13.0.0"
saft-ast = { version = "0.1.0", path = "../saft-ast" }
Expand Down
13 changes: 12 additions & 1 deletion crates/saft/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use codespan_reporting::{
termcolor::{ColorChoice, StandardStream},
},
};
use indoc::indoc;
use platform_dirs::AppDirs;
use rustyline::{error::ReadlineError, DefaultEditor};
use saft_eval::interpreter::Interpreter;
Expand Down Expand Up @@ -67,7 +68,17 @@ fn repl() {
match readline {
Ok(line) => {
rl.add_history_entry(&line).unwrap();
interpret_stmt(&mut interpreter, &line);
if line.starts_with(':') {
match line.as_str() {
":h" => println!(indoc! {"
:h - print help
:env - print env"}),
":env" => interpreter.print_env(),
_ => println!("Unknown command, :h for help"),
}
} else {
interpret_stmt(&mut interpreter, &line);
}
}
Err(ReadlineError::Interrupted | ReadlineError::Eof) => {
break;
Expand Down

0 comments on commit cb93aad

Please sign in to comment.