2021-09-18 15:26:16 -07:00
|
|
|
/* relish: versatile lisp shell
|
2024-02-06 22:39:08 +00:00
|
|
|
* Copyright (C) 2021 Ava Affine
|
2021-09-18 15:26:16 -07:00
|
|
|
*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-16 22:02:40 -08:00
|
|
|
use dirs::home_dir;
|
|
|
|
|
use relish::ast::{ast_to_string, eval, func_call, lex, new_ast, Ctr, FTable, VTable};
|
|
|
|
|
use relish::aux::configure;
|
2021-09-18 15:26:16 -07:00
|
|
|
use rustyline::error::ReadlineError;
|
|
|
|
|
use rustyline::Editor;
|
|
|
|
|
use std::cell::RefCell;
|
2022-01-16 22:02:40 -08:00
|
|
|
use std::env;
|
|
|
|
|
use std::rc::Rc;
|
2021-09-18 15:26:16 -07:00
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let mut rl = Editor::<()>::new();
|
|
|
|
|
|
2021-11-08 00:59:14 -08:00
|
|
|
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();
|
2021-11-08 00:59:14 -08:00
|
|
|
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;
|
2021-11-08 00:59:14 -08:00
|
|
|
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()));
|
2023-01-21 16:12:21 -08:00
|
|
|
let conf_file;
|
2023-01-22 19:36:37 -08:00
|
|
|
let ft;
|
2021-11-07 22:04:57 -08:00
|
|
|
match env::var("RELISH_CFG_FILE") {
|
2023-01-21 16:12:21 -08:00
|
|
|
Ok(s) => {
|
|
|
|
|
conf_file = s
|
|
|
|
|
},
|
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("{}", e);
|
|
|
|
|
conf_file = cfg;
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match configure(conf_file, vt.clone()) {
|
|
|
|
|
Ok(f) => ft = f,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
ft = Rc::new(RefCell::new(FTable::new()));
|
|
|
|
|
eprintln!("{}", e);
|
|
|
|
|
},
|
2021-11-07 22:04:57 -08:00
|
|
|
}
|
|
|
|
|
|
2021-09-18 15:26:16 -07:00
|
|
|
loop {
|
2021-11-07 22:04:57 -08:00
|
|
|
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 {
|
2022-01-16 22:02:40 -08:00
|
|
|
match func_call(
|
|
|
|
|
fnc.clone(),
|
|
|
|
|
new_ast(Ctr::None, Ctr::None),
|
|
|
|
|
vt.clone(),
|
|
|
|
|
ft.clone(),
|
|
|
|
|
) {
|
2021-11-07 22:04:57 -08:00
|
|
|
Err(s) => {
|
|
|
|
|
eprintln!("Couldnt generate prompt: {}", s);
|
|
|
|
|
readline = rl.readline("");
|
|
|
|
|
}
|
2022-01-16 22:02:40 -08:00
|
|
|
|
|
|
|
|
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(""),
|
|
|
|
|
},
|
2021-11-07 22:04:57 -08:00
|
|
|
}
|
|
|
|
|
} 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) {
|
2022-01-16 22:02:40 -08: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);
|
2021-09-18 15:26:16 -07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(s) => {
|
|
|
|
|
println!("{}", s);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-16 22:02:40 -08:00
|
|
|
}
|
2021-09-18 15:26:16 -07:00
|
|
|
|
2022-01-16 22:02:40 -08:00
|
|
|
Err(ReadlineError::Interrupted) => break,
|
2021-09-18 15:26:16 -07:00
|
|
|
|
2022-01-16 22:02:40 -08:00
|
|
|
Err(ReadlineError::Eof) => return,
|
2021-09-18 15:26:16 -07:00
|
|
|
Err(err) => {
|
2021-11-02 19:05:35 -07:00
|
|
|
eprintln!("Prompt error: {:?}", err);
|
2022-01-16 22:02:40 -08:00
|
|
|
break;
|
2021-09-18 15:26:16 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if hist != "" {
|
|
|
|
|
rl.save_history(&hist).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|