flesh/src/config.rs

70 lines
2.2 KiB
Rust
Raw Normal View History

/* 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;
use crate::segment::{Ctr, Seg};
use crate::sym::{Args, SymTable, Symbol, ValueType};
use std::fs;
use std::io;
use std::rc::Rc;
fn prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
2023-03-01 11:33:30 -08:00
Ok(Ctr::Symbol("λ ".to_string()))
}
/* loads defaults, evaluates config script */
pub fn configure(filename: String, syms: &mut SymTable) -> Result<(), String> {
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()))),
},
);
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()))),
},
);
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)),
},
);
let config_document =
fs::read_to_string(filename).unwrap_or_else(|err: io::Error| err.to_string());
let config_tree = lex(&config_document)?;
let config_result = eval(&config_tree, syms)?;
println!("config result: {config_result}");
Ok(())
}