From e7dd0caa4a315a7807b0c900b884316ad42bd029 Mon Sep 17 00:00:00 2001 From: Ava Hahn Date: Sat, 21 Jan 2023 16:12:21 -0800 Subject: [PATCH] flesh out todo list, better configure code Signed-off-by: Ava Hahn --- Readme.org | 20 +++++++++++++++++++- src/bin/main.rs | 22 +++++++++++++++++----- src/config.rs | 47 +++++++++++++++++++++++++++-------------------- src/func.rs | 1 + 4 files changed, 64 insertions(+), 26 deletions(-) diff --git a/Readme.org b/Readme.org index b5913f9..0b883ad 100644 --- a/Readme.org +++ b/Readme.org @@ -11,6 +11,7 @@ https://gitlab.com/whom/shs ** To be as portable as possible ** To provide code and framework that can be embedded in other applications needing a user facing interpreter ** To be well tested code +** No unsafe code without extreme consideration and rigorous containment * Current Status *** DONE Core interpreter stuffs @@ -19,19 +20,36 @@ https://gitlab.com/whom/shs **** DONE Evaluation **** DONE Function table **** DONE Variable table +*** DONE Echo function +In string lib *** TODO Rudimentary Control Flow **** TODO if clause **** TODO loop clause **** TODO while clause **** TODO circuit clause *** TODO Configuration +**** DONE Function to load configuration into Variable and Function tables +**** DONE Configure in main shell +**** DONE manual verification of config settings +**** FAILING manual verification of config defaults +Cannot get default CFG_RELISH_PROMPT. Unclear if this is a bug in FTable or config +*** TODO Help function +*** TODO Env function *** TODO User variable declaration *** TODO User function declaration +*** TODO Load (load a script) function +Pull/Refactor the logic out of the configure functions. +Optionally return a list of new variables and/or functions? +Will need a concatenate function for func tables +*** TODO Main shell calls Load function on arg and exits *** TODO Shell module **** TODO Process launching with environment variables **** TODO Foreground process TTY **** TODO Background processes *** TODO Custom error printing +*** TODO Custom ast pretty print +*** TODO Implement Ctr, Ast to_string / Display trait +*** TODO get_stdlibphase1 -> configuration -> get_stdlibphase2 *** TODO STDLIB **** DONE append **** TODO string operations @@ -64,7 +82,7 @@ By default Relish will read from ~/.relishrc for configuration, but the default *** The configuration file The configuration file is a script containing arbitrary Relish code. -On start, any shell which leverages the configuration code in [the config module](src/config.rs) will create a clean seperate context, including default configuration values, within which the standard library will be initialized. +On start, any shell which leverages the configuration code in the config module (file:src/config.rs) will create a clean seperate context, including default configuration values, within which the standard library will be initialized. The configuration file is evaluated and run as a standalone script and may include arbitrary executable code. Afterwards, configuration values found in the variable map will be used to configure the standard library function mappings that the shell will use. Errors during configuration are non-terminal and will result in default values being returned. diff --git a/src/bin/main.rs b/src/bin/main.rs index 3901372..e9a0b04 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -47,12 +47,24 @@ fn main() { } let vt = Rc::new(RefCell::new(VTable::new())); - // if we fail to get stdlib we can just use this one - let mut ft = Rc::new(RefCell::new(FTable::new())); - + let conf_file; + let mut ft; match env::var("RELISH_CFG_FILE") { - Ok(s) => configure(s, vt.clone(), ft.clone()), - Err(_) => configure(cfg, vt.clone(), ft.clone()), + 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); + }, } match get_stdlib(vt.clone()) { diff --git a/src/config.rs b/src/config.rs index 34deaeb..21fb91a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -30,7 +30,9 @@ fn prompt_default_callback(_: Ast, _: Rc>, _: Rc return Ctr::None; } -pub fn configure(filename: String, vars: Rc>, mut funcs: Rc>) { +pub fn configure(filename: String, vars: Rc>) -> Result>, String> { + let funcs; + define( vars.clone(), String::from("CFG_RELISH_POSIX"), @@ -44,10 +46,13 @@ pub fn configure(filename: String, vars: Rc>, mut funcs: Rc funcs = f, - Err(s) => println!("{}", s), + Err(s) => { + funcs = Rc::new(RefCell::new(FTable::new())); + println!("Couldnt get stdlib: {}", s) + }, } - func_declare( + match func_declare( funcs.clone(), Rc::new(RefCell::new(Function { name: String::from("CFG_RELISH_PROMPT"), @@ -56,31 +61,33 @@ pub fn configure(filename: String, vars: Rc>, mut funcs: Rc return Err(e), + None => {}, + } - match fs::read_to_string(filename) { + match fs::read_to_string(filename.clone()) { Err(s) => { - eprintln!("Couldnt open configuration file: {}", s); - return; + return Err(format!("Couldnt open configuration file: {}", s)); } - Ok(raw_config) => match lex(raw_config) { - Err(s) => { - println!("Error in configuration: {}", s); - return; - } + Ok(raw_config) => { + let mut l = raw_config; + l = "(".to_owned() + &l + ")"; - Ok(config) => match eval(config, vars, funcs, false) { + match lex(l) { Err(s) => { - println!("Error in applying configuration: {}", s); - return; + return Err(format!("Error in configuration: {}", s)); } - Ok(ctr) => match ctr { - Ctr::String(s) => println!("{}", s), - _ => (), - }, - }, + Ok(config) => { + if let Err(errst) = eval(config, vars, funcs.clone(), false) { + return Err(format!("Error loading {}: {}", filename.clone(), errst)); + } + } + } }, } + + return Ok(funcs); } diff --git a/src/func.rs b/src/func.rs index 595362b..dd7310f 100644 --- a/src/func.rs +++ b/src/func.rs @@ -27,6 +27,7 @@ pub type FTable = HashMap>>; // Standardized function signature for stdlib functions //pub type InternalOperation = impl Fn(Ast, Rc>, Rc>) -> Ctr; +#[derive(Debug)] pub struct ExternalOperation { // Un-evaluated abstract syntax tree // TODO: Intermediate evaluation to simplify branches with no argument in them