repl now complete
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
fbb85f2e3d
commit
2439a37aaa
9 changed files with 188 additions and 123 deletions
|
|
@ -16,78 +16,44 @@
|
|||
*/
|
||||
|
||||
use crate::eval::eval;
|
||||
use crate::func::{func_declare, Args, FTable, Function, Operation};
|
||||
use crate::lex::lex;
|
||||
use crate::segment::{Ast, Ctr};
|
||||
use crate::stl::get_stdlib;
|
||||
use crate::vars::{define, VTable};
|
||||
use std::cell::RefCell;
|
||||
use crate::segment::{Seg, Ctr};
|
||||
use crate::sym::{SymTable, ValueType, Args, Symbol};
|
||||
use std::fs;
|
||||
use std::io::{self, Write};
|
||||
use std::io;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn prompt_default_callback(_: Ast, _: Rc<RefCell<VTable>>, _: Rc<RefCell<FTable>>) -> Ctr {
|
||||
return Ctr::String("λ ".to_string());
|
||||
fn prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
|
||||
Ok(Ctr::String("λ ".to_string()))
|
||||
}
|
||||
|
||||
pub fn configure(filename: String, vars: Rc<RefCell<VTable>>) -> Result<Rc<RefCell<FTable>>, String> {
|
||||
let funcs;
|
||||
/* 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()))),
|
||||
});
|
||||
|
||||
define(
|
||||
vars.clone(),
|
||||
String::from("CFG_RELISH_POSIX"),
|
||||
Rc::new(Ctr::String(String::from("0"))),
|
||||
);
|
||||
define(
|
||||
vars.clone(),
|
||||
String::from("CFG_RELISH_ENV"),
|
||||
Rc::new(Ctr::String(String::from("1"))),
|
||||
);
|
||||
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()))),
|
||||
});
|
||||
|
||||
match get_stdlib(vars.clone()) {
|
||||
Ok(f) => funcs = f,
|
||||
Err(s) => {
|
||||
funcs = Rc::new(RefCell::new(FTable::new()));
|
||||
println!("Couldnt get stdlib: {}", s)
|
||||
},
|
||||
}
|
||||
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)),
|
||||
});
|
||||
|
||||
match func_declare(
|
||||
funcs.clone(),
|
||||
Rc::new(RefCell::new(Function {
|
||||
name: String::from("CFG_RELISH_PROMPT"),
|
||||
loose_syms: false,
|
||||
eval_lazy: false,
|
||||
args: Args::Lazy(0),
|
||||
function: Operation::Internal(Box::new(prompt_default_callback)),
|
||||
})),
|
||||
) {
|
||||
Some(e) => return Err(e),
|
||||
None => {},
|
||||
}
|
||||
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)?;
|
||||
|
||||
match fs::read_to_string(filename.clone()) {
|
||||
Err(s) => {
|
||||
return Err(format!("Couldnt open configuration file: {}", s));
|
||||
}
|
||||
|
||||
Ok(raw_config) => {
|
||||
let mut l = raw_config;
|
||||
l = "(".to_owned() + &l + ")";
|
||||
|
||||
match lex(l) {
|
||||
Err(s) => {
|
||||
return Err(format!("Error in configuration: {}", s));
|
||||
}
|
||||
|
||||
Ok(config) => {
|
||||
if let Err(errst) = eval(config, vars, funcs.clone(), false) {
|
||||
return Err(format!("Error loading {}: {}", filename.clone(), errst));
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
return Ok(funcs);
|
||||
println!("config result: {config_result}");
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue