2023-02-27 17:30:49 -08: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 crate::eval::eval;
|
|
|
|
|
use crate::lex::lex;
|
2023-03-01 11:38:02 -08:00
|
|
|
use crate::segment::{Ctr, Seg};
|
|
|
|
|
use crate::sym::{Args, SymTable, Symbol, ValueType};
|
2023-02-27 17:30:49 -08:00
|
|
|
use std::fs;
|
2023-03-01 11:14:42 -08:00
|
|
|
use std::io;
|
2023-02-27 17:30:49 -08:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
2023-03-01 11:14:42 -08:00
|
|
|
fn prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
|
2023-03-01 11:33:30 -08:00
|
|
|
Ok(Ctr::Symbol("λ ".to_string()))
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:14:42 -08:00
|
|
|
/* loads defaults, evaluates config script */
|
|
|
|
|
pub fn configure(filename: String, syms: &mut SymTable) -> Result<(), String> {
|
2023-03-01 11:38:02 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"CFG_RELISH_POSIX".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("CFG_RELISH_POSIX"),
|
|
|
|
|
args: Args::None,
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
value: ValueType::VarForm(Box::new(Ctr::String("0".to_string()))),
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-03-01 11:14:42 -08:00
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"CFG_RELISH_ENV".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("CFG_RELISH_ENV"),
|
|
|
|
|
args: Args::None,
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
value: ValueType::VarForm(Box::new(Ctr::String("1".to_string()))),
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-03-01 11:14:42 -08:00
|
|
|
|
2023-03-01 11:38:02 -08:00
|
|
|
syms.insert(
|
|
|
|
|
"CFG_RELISH_PROMPT".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("default relish prompt"),
|
|
|
|
|
args: Args::None,
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
value: ValueType::Internal(Rc::new(prompt_default_callback)),
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-03-01 11:14:42 -08:00
|
|
|
|
2023-03-01 12:20:43 -08:00
|
|
|
let mut config_document = fs::read_to_string(filename)
|
|
|
|
|
.unwrap_or_else(|err: io::Error| {
|
|
|
|
|
eprintln!("{}", err.to_string());
|
|
|
|
|
"".to_string()
|
|
|
|
|
}) + ")";
|
|
|
|
|
|
|
|
|
|
config_document = "(".to_string() + &config_document;
|
|
|
|
|
|
2023-03-01 11:14:42 -08:00
|
|
|
let config_tree = lex(&config_document)?;
|
|
|
|
|
let config_result = eval(&config_tree, syms)?;
|
|
|
|
|
|
|
|
|
|
println!("config result: {config_result}");
|
|
|
|
|
Ok(())
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|