Print debug data of reader and evaluator on the rep

This commit is contained in:
Sameer Rahmani 2020-11-01 20:15:07 +00:00
parent 124fd3e267
commit 9181292651
5 changed files with 27 additions and 14 deletions

View File

@ -10,3 +10,15 @@ test-bootstrap:
.PHONY: test .PHONY: test
test: test-bootstrap test: test-bootstrap
.PHONY: clean-bootstrap
clean-bootstrap:
cd $(THIS_DIR)/bootstrap && cargo clean
.PHONY: bootstrap-repl
bootstrap-repl:
cd $(THIS_DIR)/bootstrap && cargo run repl
.PHONY: clean
clean: clean-bootstrap

View File

@ -24,18 +24,25 @@ fn eval_expr(rt: &RT, scope: &Scope, expr: Expr) -> PossibleExpr {
Ok(expr) Ok(expr)
} }
pub fn eval(rt: &RT, scope: &Scope, exprs: Vec<Expr>) -> PossibleExpr { pub fn eval(rt: &RT, exprs: Vec<Expr>) -> PossibleExpr {
match exprs.last() { match exprs.last() {
Some(e) => Ok(e.clone()), Some(e) => Ok(e.clone()),
_ => Err(err("NotImplemented".to_string())), _ => Err(err("NotImplemented".to_string())),
} }
} }
pub fn rep(rt: &RT, scope: &Scope, input: &str) { pub fn read_eval_print(rt: &RT, input: &str) {
match read_string(input) { match read_string(input) {
Ok(exprs) => { Ok(exprs) => {
let result_expr = eval(rt, scope, exprs); if rt.is_debug() {
println!("<<"); println!("Read Result: \n{:?}\n", exprs);
}
let result_expr = eval(rt, exprs);
if rt.is_debug() {
println!("Eval Result: \n{:?}\n", result_expr);
}
} }
Err(e) => println!("Error: {}", e), Err(e) => println!("Error: {}", e),
} }

View File

@ -19,6 +19,7 @@ pub trait IError {
fn message(&self) -> &str; fn message(&self) -> &str;
} }
#[derive(Debug)]
pub struct Error { pub struct Error {
msg: String, msg: String,
} }

View File

@ -15,10 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use clap::{load_yaml, App, ArgMatches}; use clap::{load_yaml, App, ArgMatches};
use std::fs::File;
use std::io; use std::io;
use std::io::prelude::*;
use std::string::String;
pub mod ast; pub mod ast;
pub mod builtins; pub mod builtins;
@ -50,11 +47,6 @@ fn main() -> io::Result<()> {
let yaml = load_yaml!("cli.yml"); let yaml = load_yaml!("cli.yml");
let app = App::from(yaml); let app = App::from(yaml);
// let rt = runtime::RT::new();
// rt.create_ns("user".to_string(), None);
// rt.set_current_ns("user".to_string());
match app.get_matches().subcommand() { match app.get_matches().subcommand() {
Some(("repl", args)) => repl(args.clone()), Some(("repl", args)) => repl(args.clone()),
Some(("run", args)) => println!("repl, {:?}", args), Some(("run", args)) => println!("repl, {:?}", args),

View File

@ -13,7 +13,8 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use crate::core::read_eval_print;
use crate::runtime::RT; use crate::runtime::RT;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use rustyline::Editor; use rustyline::Editor;
@ -32,7 +33,7 @@ pub fn repl(rt: RT) {
match readline { match readline {
Ok(line) => { Ok(line) => {
rl.add_history_entry(line.as_str()); rl.add_history_entry(line.as_str());
println!("Line: {}", line); read_eval_print(&rt, &line);
} }
Err(ReadlineError::Interrupted) => { Err(ReadlineError::Interrupted) => {
println!("CTRL-C"); println!("CTRL-C");