flesh/src/bin/main.rs

147 lines
5 KiB
Rust
Raw Normal View History

2021-09-18 15:26:16 -07:00
/* relish: versatile lisp shell
* Copyright (C) 2021 Aidan Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use rustyline::error::ReadlineError;
use rustyline::Editor;
use dirs::home_dir;
use std::rc::Rc;
use std::env;
2021-09-18 15:26:16 -07:00
use std::cell::RefCell;
use relish::ast::{VTable, FTable, Ctr, lex, eval, ast_to_string, new_ast, func_call};
use relish::aux::configure;
2021-09-18 15:26:16 -07:00
use relish::stdlib::{get_stdlib};
fn main() {
let mut rl = Editor::<()>::new();
const HIST_FILE: &str = "/.relish_hist";
const CONFIG_FILE_DEFAULT: &str = "/.relishrc";
2021-09-18 15:26:16 -07:00
let mut hist: String = "".to_owned();
let mut cfg: String = "".to_owned();
2021-09-18 15:26:16 -07:00
if let Some(home) = home_dir() {
if let Some(h) = home.to_str() {
hist = h.to_owned() + HIST_FILE;
cfg = h.to_owned() + CONFIG_FILE_DEFAULT;
2021-09-18 15:26:16 -07:00
}
}
if hist != "" {
// ignore result. it loads or it doesnt.
let _ = rl.load_history(&hist);
}
2021-10-17 23:11:31 -07:00
let vt = Rc::new(RefCell::new(VTable::new()));
2021-09-18 15:26:16 -07:00
// if we fail to get stdlib we can just use this one
let mut ft = Rc::new(RefCell::new(FTable::new()));
match env::var("RELISH_CFG_FILE") {
Ok(s) => configure(s, vt.clone(), ft.clone()),
Err(_) => configure(cfg, vt.clone(), ft.clone())
}
match get_stdlib(vt.clone()) {
2021-09-18 15:26:16 -07:00
Ok(f) => ft = f,
Err(s) => println!("{}", s)
}
loop {
let readline: Result<String, ReadlineError>;
// Rust is pain
let tmp_ft_clone = ft.clone();
// this is not okay
let t_ft_c_b = tmp_ft_clone.borrow();
let pfunc = t_ft_c_b.get("CFG_RELISH_PROMPT");
if let Some(fnc) = pfunc {
match func_call(fnc.clone(), new_ast(Ctr::None, Ctr::None), vt.clone(), ft.clone()) {
Err(s) => {
eprintln!("Couldnt generate prompt: {}", s);
readline = rl.readline("");
},
Ok(c) => {
match c {
Ctr::Symbol(s) => readline = rl.readline(&s.to_owned()),
Ctr::String(s) => readline = rl.readline(&s),
Ctr::Integer(i) => readline = rl.readline(&format!("{}", i)),
Ctr::Float(f) => readline = rl.readline(&format!("{}", f)),
Ctr::Bool(b) => readline = rl.readline(&format!("{}", b)),
Ctr::Seg(c) => readline = rl.readline(&ast_to_string(c.clone())),
Ctr::None => readline = rl.readline("")
}
}
}
} else {
readline = rl.readline("");
}
2021-09-18 15:26:16 -07:00
match readline {
Ok(line) => {
rl.add_history_entry(line.as_str());
2021-09-18 17:02:40 -07:00
let mut l = line.as_str().to_owned();
if !l.starts_with("(") {
l = "(".to_owned() + &l;
}
if !l.ends_with(")") {
l = l + ")";
}
match lex(l) {
2021-09-18 15:26:16 -07:00
Ok(a) => {
match eval(a.clone(), vt.clone(), ft.clone(), false) {
Ok(a) => {
match a {
Ctr::Symbol(s) => println!("{}", s),
Ctr::String(s) => println!("{}", s),
Ctr::Integer(i) => println!("{}", i),
Ctr::Float(f) => println!("{}", f),
Ctr::Bool(b) => println!("{}", b),
Ctr::Seg(c) => println!("{}", ast_to_string(c.clone())),
Ctr::None => ()
}
},
Err(s) => {
println!("{}", s);
}
}
},
Err(s) => {
println!("{}", s);
}
}
},
Err(ReadlineError::Interrupted) => {
break
},
Err(ReadlineError::Eof) => {
return
},
Err(err) => {
2021-11-02 19:05:35 -07:00
eprintln!("Prompt error: {:?}", err);
2021-09-18 15:26:16 -07:00
break
}
}
}
if hist != "" {
rl.save_history(&hist).unwrap();
}
}