got all the load script stuff done.
added script args to main shell also added userlib tests to ci
This commit is contained in:
parent
381852b3bd
commit
3f75157fac
7 changed files with 122 additions and 49 deletions
140
src/run.rs
Normal file
140
src/run.rs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
/* 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::path::Path;
|
||||
use std::fs;
|
||||
use std::iter::FromIterator;
|
||||
use std::env::{vars, var, current_dir};
|
||||
use std::rc::Rc;
|
||||
|
||||
fn prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result<Ctr, String> {
|
||||
Ok(Ctr::Symbol("λ".to_string()))
|
||||
}
|
||||
|
||||
fn get_paths() -> Vec<String> {
|
||||
Vec::from_iter(var("PATH")
|
||||
.unwrap_or("".to_string().into())
|
||||
.split(':')
|
||||
.map(String::from))
|
||||
}
|
||||
|
||||
pub fn load_defaults(syms: &mut SymTable) {
|
||||
syms.insert(
|
||||
"CFG_RELISH_POSIX".to_string(),
|
||||
Symbol {
|
||||
name: String::from("CFG_RELISH_POSIX"),
|
||||
args: Args::None,
|
||||
conditional_branches: false,
|
||||
docs: "variable holding whether or not POSIX job control functions are to be loaded.
|
||||
checked at shell startup by configuration daemon. not used afterwards.
|
||||
|
||||
default value: false".to_string(),
|
||||
value: ValueType::VarForm(Box::new(Ctr::Bool(false))),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"CFG_RELISH_ENV".to_string(),
|
||||
Symbol {
|
||||
name: String::from("CFG_RELISH_ENV"),
|
||||
args: Args::None,
|
||||
conditional_branches: false,
|
||||
docs: "variable holding whether or not vars and other symbols should be linked to process environment variables.
|
||||
If set/defined all calls to def will result in additions or subtractions from user environment variables.
|
||||
checked at shell startup by configuration daemon. not used afterwards.
|
||||
|
||||
default value: 1 (set)
|
||||
".to_string(),
|
||||
value: ValueType::VarForm(Box::new(Ctr::Bool(true))),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"CFG_RELISH_PROMPT".to_string(),
|
||||
Symbol {
|
||||
name: String::from("default relish prompt"),
|
||||
args: Args::None,
|
||||
conditional_branches: false,
|
||||
docs: "function called to output prompt. this function is called with no arguments.
|
||||
default value (<lambda>)"
|
||||
.to_string(),
|
||||
value: ValueType::Internal(Rc::new(prompt_default_callback)),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn load_environment(syms: &mut SymTable) {
|
||||
for (key, value) in vars() {
|
||||
syms.insert(
|
||||
key.clone(),
|
||||
Symbol{
|
||||
name: key,
|
||||
args: Args::None,
|
||||
conditional_branches: false,
|
||||
docs: String::from("from env vars at time of load"),
|
||||
value: ValueType::VarForm(Box::new(Ctr::String(value))),
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(filename: String, syms: &mut SymTable) -> Result<(), String> {
|
||||
let script_read_res = fs::read_to_string(filename);
|
||||
if script_read_res.is_err() {
|
||||
Err(format!("Couldnt read script: {}", script_read_res.err().unwrap()))
|
||||
} else {
|
||||
let script_read = script_read_res.unwrap() + ")";
|
||||
let script = "(".to_string() + &script_read;
|
||||
eval(&*lex(&script)?, syms)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub const RUN_DOCSTRING: &str = "Takes one string argument.
|
||||
Attempts to find argument in PATH and attempts to call argument";
|
||||
|
||||
pub fn run_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
||||
let mut prefixes = get_paths();
|
||||
if let Ok(s) = current_dir() {
|
||||
prefixes.push(String::from(s.to_str().unwrap()));
|
||||
}
|
||||
if let Ctr::String(ref filename) = *ast.car {
|
||||
for prefix in prefixes {
|
||||
let candidate = Path::new(&prefix.clone()).join(filename);
|
||||
if candidate.exists() {
|
||||
if filename.ends_with(".rls") {
|
||||
return run(String::from(candidate.to_str().unwrap()), syms)
|
||||
.and(Ok(Ctr::None))
|
||||
} else {
|
||||
return Err("binary called, unimplemented!".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!("file {} not found", filename))
|
||||
} else {
|
||||
Err("impossible: not a string".to_string())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue