rustfmt previous commit

This commit is contained in:
Ava Hahn 2023-03-05 22:21:18 -08:00
parent dc6342bc74
commit ca9f755d50
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
10 changed files with 144 additions and 123 deletions

View file

@ -62,7 +62,7 @@ fn main() {
match user_doc {
Ok(line) => {
rl.add_history_entry(line.as_str());
let l = line.as_str().to_owned();
let l = line.as_str().to_owned();
match lex(&l) {
Ok(a) => match eval(&a, &mut syms) {

View file

@ -30,18 +30,18 @@ fn prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result<Ctr, 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,
docs: "variable holding whether or not POSIX job control functions are to be loaded.
checked at shell startup by configuration daemon. not used afterwards.
"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: not set".to_string(),
value: ValueType::VarForm(Box::new(Ctr::String("0".to_string()))),
},
);*/
default value: not set".to_string(),
value: ValueType::VarForm(Box::new(Ctr::String("0".to_string()))),
},
);*/
syms.insert(
"CFG_RELISH_ENV".to_string(),
@ -66,7 +66,8 @@ default value: 1 (set)
args: Args::None,
conditional_branches: false,
docs: "function called to output prompt. this function is called with no arguments.
default value (<lambda>)".to_string(),
default value (<lambda>)"
.to_string(),
value: ValueType::Internal(Rc::new(prompt_default_callback)),
},
);

View file

@ -89,7 +89,8 @@ example: (let ((step1 'hello')
In this example step1, step2, and step3 are created sequentially.
Then, the echo form is evaluated, printing 'hello-world'.
Finally, the some-func form is evaluated.
Since the call to some-func is the final form, its value is returned.".to_string(),
Since the call to some-func is the final form, its value is returned."
.to_string(),
value: ValueType::Internal(Rc::new(control::let_callback)),
},
);
@ -126,7 +127,8 @@ example: (circuit (eq? (do-operation) myresult)
false
(do-another-operation))
in this example, do-another-operation will not be called".to_string(),
in this example, do-another-operation will not be called"
.to_string(),
value: ValueType::Internal(Rc::new(control::circuit_callback)),
},
);
@ -139,7 +141,8 @@ in this example, do-another-operation will not be called".to_string(),
conditional_branches: false,
docs: "traverses a list of N arguments, all of which are expected to be boolean.
starts with arg1 AND arg2, and then calculates prev_result AND next_arg.
returns final result.".to_string(),
returns final result."
.to_string(),
value: ValueType::Internal(Rc::new(boolean::bool_and_callback)),
},
);
@ -152,7 +155,8 @@ returns final result.".to_string(),
conditional_branches: false,
docs: "traverses a list of N arguments, all of which are expected to be boolean.
starts with arg1 OR arg2, and then calculates prev_result OR next_arg.
returns final result.".to_string(),
returns final result."
.to_string(),
value: ValueType::Internal(Rc::new(boolean::bool_or_callback)),
},
);
@ -164,7 +168,8 @@ returns final result.".to_string(),
args: Args::Strict(vec![Type::Bool]),
conditional_branches: false,
docs: "takes a single argument (expects a boolean).
returns false if arg is true or true if arg is false.".to_string(),
returns false if arg is true or true if arg is false."
.to_string(),
value: ValueType::Internal(Rc::new(boolean::bool_not_callback)),
},
);
@ -177,7 +182,8 @@ returns false if arg is true or true if arg is false.".to_string(),
conditional_branches: false,
docs: "traverses a list of N arguments.
returns true if all arguments hold the same value.
NOTE: 1 and 1.0 are the same, but '1' 'one' or one (symbol) aren't".to_string(),
NOTE: 1 and 1.0 are the same, but '1' 'one' or one (symbol) aren't"
.to_string(),
value: ValueType::Internal(Rc::new(boolean::bool_iseq_callback)),
},
);
@ -190,7 +196,8 @@ NOTE: 1 and 1.0 are the same, but '1' 'one' or one (symbol) aren't".to_string(),
conditional_branches: true,
docs: "switches a boolean symbol between true or false.
Takes a single argument (a symbol). Looks it up in the variable table.
Either sets the symbol to true if it is currently false, or vice versa.".to_string(),
Either sets the symbol to true if it is currently false, or vice versa."
.to_string(),
value: ValueType::Internal(Rc::new(boolean::bool_toggle_callback)),
},
);
@ -237,7 +244,8 @@ pub fn dynamic_stdlib(syms: &mut SymTable) -> Result<(), String> {
(def myfunc 'does a thing' (myarg1 myarg2) (dothing myarg1 myarg2) (add myarg1 myarg2))
3. symbol un-definition:
Takes just a name. Removes variable from table.
(def useless-var)".to_string(),
(def useless-var)"
.to_string(),
value: ValueType::Internal(Rc::new(
move |ast: &Seg, syms: &mut SymTable| -> Result<Ctr, String> {
_store_callback(ast, syms, env_cfg_user_form)
@ -261,7 +269,7 @@ fn _echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
fn _help_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
if ast.len() != 1 {
return Err("help only takes a single argument".to_string())
return Err("help only takes a single argument".to_string());
}
if let Ctr::Symbol(ref symbol) = *ast.car {
if let Some(ref sym) = syms.get(symbol) {
@ -271,17 +279,20 @@ fn _help_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
} else {
args_str = sym.args.to_string();
}
println!("NAME: {0}\n
println!(
"NAME: {0}\n
ARGS: {1}\n
DOCUMENTATION:\n
{2}\n
CURRENT VALUE AND/OR BODY:
(TODO)", sym.name, args_str, sym.docs);
(TODO)",
sym.name, args_str, sym.docs
);
} else {
return Err("undefined symbol".to_string())
return Err("undefined symbol".to_string());
}
} else {
return Err("help should only be called on a symbol".to_string())
return Err("help should only be called on a symbol".to_string());
}
Ok(Ctr::None)
@ -293,7 +304,7 @@ fn _store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr,
match &*ast.cdr {
// define a symbol
Ctr::Seg(doc_tree) => {
if let Ctr::String(ref doc) = *doc_tree.car {
if let Ctr::String(ref doc) = *doc_tree.car {
match &*doc_tree.cdr {
// define a variable
Ctr::Seg(data_tree) if is_var => match eval(&Box::new(data_tree), syms) {
@ -336,7 +347,8 @@ fn _store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr,
}
}) {
return Err(
"all arguments defined for function must be of type symbol".to_string()
"all arguments defined for function must be of type symbol"
.to_string(),
);
};
@ -361,17 +373,19 @@ fn _store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr,
);
}
} else {
return Err("expected list of arguments in function definition".to_string());
return Err(
"expected list of arguments in function definition".to_string()
);
}
},
}
// theres a name and a doc string but nothing else
_ => return Err("have name and doc string, but no body.".to_string())
_ => return Err("have name and doc string, but no body.".to_string()),
}
} else {
return Err("doc string is a required argument".to_string())
return Err("doc string is a required argument".to_string());
}
},
}
// undefine a symbol
Ctr::None => {
@ -379,12 +393,12 @@ fn _store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr,
if env_cfg {
env::remove_var(identifier);
}
},
}
_ => return Err("arguments not in standard form".to_string()),
}
} else {
return Err("first argument to export must be a symbol".to_string())
return Err("first argument to export must be a symbol".to_string());
}
Ok(Ctr::None)
}

View file

@ -18,8 +18,8 @@
use crate::eval::eval;
use crate::segment::{Ctr, Seg, Type};
use std::collections::HashMap;
use std::rc::Rc;
use std::fmt;
use std::rc::Rc;
#[derive(Clone)]
pub struct SymTable(HashMap<String, Symbol>);

View file

@ -7,7 +7,7 @@ mod eval_tests {
let test_doc = "(1 2)".to_string();
let mut syms = SymTable::new();
let doc_tree = lex(&test_doc).unwrap();
let reduced = *eval(&doc_tree, &mut syms).unwrap();
let reduced = *eval(&doc_tree, &mut syms).unwrap();
assert_eq!(reduced.to_string(), test_doc);
}
@ -16,9 +16,8 @@ mod eval_tests {
let test_doc = "(1 (1 2 3 4 5) 5)".to_string();
let mut syms = SymTable::new();
let doc_tree = lex(&test_doc).unwrap();
let reduced = *eval(&doc_tree, &mut syms).unwrap();
let reduced = *eval(&doc_tree, &mut syms).unwrap();
assert_eq!(reduced.to_string(), test_doc);
}
#[test]
@ -43,7 +42,7 @@ mod eval_tests {
syms.insert(String::from("echo"), test_external_func);
let doc_tree = lex(&test_doc).unwrap();
let reduced = *eval(&doc_tree, &mut syms).unwrap();
let reduced = *eval(&doc_tree, &mut syms).unwrap();
assert_eq!(reduced.to_string(), output);
}
@ -69,7 +68,7 @@ mod eval_tests {
syms.insert(String::from("echo"), test_external_func);
let doc_tree = lex(&test_doc).unwrap();
let reduced = *eval(&doc_tree, &mut syms).unwrap();
let reduced = *eval(&doc_tree, &mut syms).unwrap();
assert_eq!(reduced.to_string(), output);
}

View file

@ -25,7 +25,10 @@ mod func_tests {
};
let args = Seg::from(Box::new(Ctr::Bool(true)), Box::new(Ctr::None));
syms.insert(String::from("test_func_in"), test_internal_func);
if let Ctr::Bool(b) = *syms.call_symbol(&"test_func_in".to_string(), &args, true).unwrap() {
if let Ctr::Bool(b) = *syms
.call_symbol(&"test_func_in".to_string(), &args, true)
.unwrap()
{
assert!(b)
}
}
@ -51,7 +54,10 @@ mod func_tests {
);
syms.insert(String::from("test_func_in"), test_external_func);
if let Ctr::Bool(b) = *syms.call_symbol(&"test_func_in".to_string(), &args, true).unwrap() {
if let Ctr::Bool(b) = *syms
.call_symbol(&"test_func_in".to_string(), &args, true)
.unwrap()
{
assert!(b)
}
}
@ -77,10 +83,13 @@ mod func_tests {
);
syms.insert(String::from("echo_2"), test_external_func);
assert_eq!(*syms.call_symbol(&"echo_2".to_string(), &args, true)
.unwrap()
.to_string(),
"'test'".to_string());
assert_eq!(
*syms
.call_symbol(&"echo_2".to_string(), &args, true)
.unwrap()
.to_string(),
"'test'".to_string()
);
}
#[test]
@ -122,10 +131,12 @@ mod func_tests {
let args = Seg::from(Box::new(Ctr::Bool(true)), Box::new(Ctr::None));
syms.insert(String::from("test_inner"), inner_func);
syms.insert(String::from("test_outer"), outer_func);
assert_eq!(syms.call_symbol(&"test_outer".to_string(), &args, true)
.unwrap()
.to_string(),
"'test'".to_string());
assert_eq!(
syms.call_symbol(&"test_outer".to_string(), &args, true)
.unwrap()
.to_string(),
"'test'".to_string()
);
}
#[test]
@ -241,8 +252,7 @@ mod func_tests {
syms.call_symbol(&"test_func_in".to_string(), &args, true)
.err()
.unwrap(),
"error in call to undefined-symbol: undefined symbol: undefined-symbol"
.to_string(),
"error in call to undefined-symbol: undefined symbol: undefined-symbol".to_string(),
);
}
}

View file

@ -4,86 +4,59 @@ mod lex_tests {
#[test]
fn test_lex_basic_pair() {
let document = String::from("(hello 'world')");
assert_eq!(
lex(&document).unwrap().to_string(),
document
);
assert_eq!(lex(&document).unwrap().to_string(), document);
}
#[test]
fn test_lex_basic_list() {
let document = String::from("(hello 'world' 1 2 3)");
assert_eq!(
lex(&document).unwrap().to_string(),
document
);
assert_eq!(lex(&document).unwrap().to_string(), document);
}
#[test]
fn test_lex_complex_list() {
let document = String::from("(hello 'world' (1 2 (1 2 3)) 1 2 3)");
assert_eq!(
lex(&document).unwrap().to_string(),
document
);
assert_eq!(lex(&document).unwrap().to_string(), document);
}
#[test]
fn test_bad_symbol() {
let document = String::from("(as;dd)");
let output: &str = "Problem lexing document: \"Unparsable token: as;dd\"";
assert_eq!(
lex(&document).err().unwrap(),
output.to_string(),
);
assert_eq!(lex(&document).err().unwrap(), output.to_string(),);
}
#[test]
fn test_list_delim_in_str() {
let document = String::from("('(')");
assert_eq!(
lex(&document).unwrap().to_string(),
document
);
assert_eq!(lex(&document).unwrap().to_string(), document);
}
#[test]
fn test_empty_string() {
let document = String::from("('')");
assert_eq!(
lex(&document).unwrap().to_string(),
document
);
assert_eq!(lex(&document).unwrap().to_string(), document);
}
#[test]
fn test_unmatched_list_delim_flat() {
let document = String::from("(one two");
let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\"";
assert_eq!(
lex(&document).err().unwrap(),
output.to_string(),
);
assert_eq!(lex(&document).err().unwrap(), output.to_string(),);
}
#[test]
fn test_unmatched_list_delim_complex() {
let document = String::from("(one two (three)");
let output: &str = "Problem lexing document: \"Unmatched list delimiter in input\"";
assert_eq!(
lex(&document).err().unwrap(),
output.to_string(),
);
assert_eq!(lex(&document).err().unwrap(), output.to_string(),);
}
#[test]
fn test_comment() {
let document = String::from("#!/bin/relish\n(one two)");
let output: &str = "(one two)";
assert_eq!(
lex(&document).unwrap().to_string(),
output.to_string(),
);
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
}
#[test]
@ -91,30 +64,20 @@ mod lex_tests {
let document =
String::from("#!/bin/relish\n((one two)# another doc comment\n(three four))");
let output: &str = "((one two) (three four))";
assert_eq!(
lex(&document).unwrap().to_string(),
output.to_string(),
);
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
}
#[test]
fn test_inline_comment() {
let document = String::from("#!/bin/relish\n((one two)\n# another doc comment\nthree)");
let output: &str = "((one two) three)";
assert_eq!(
lex(&document).unwrap().to_string(),
output.to_string(),
);
assert_eq!(lex(&document).unwrap().to_string(), output.to_string(),);
}
#[test]
fn test_bad_token_list() {
let document = String::from("(one t(wo)");
let output: &str = "Problem lexing document: \"list started in middle of another token\"";
assert_eq!(
lex(&document).err().unwrap(),
output.to_string(),
);
assert_eq!(lex(&document).err().unwrap(), output.to_string(),);
}
}

View file

@ -11,7 +11,9 @@ mod append_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -26,7 +28,9 @@ mod append_lib_tests {
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -41,7 +45,9 @@ mod append_lib_tests {
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -56,7 +62,9 @@ mod append_lib_tests {
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -71,7 +79,9 @@ mod append_lib_tests {
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}

View file

@ -10,7 +10,9 @@ mod bool_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -23,7 +25,9 @@ mod bool_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -36,7 +40,9 @@ mod bool_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -49,10 +55,11 @@ mod bool_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
@ -63,10 +70,11 @@ mod bool_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
@ -77,7 +85,9 @@ mod bool_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -90,7 +100,9 @@ mod bool_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}

View file

@ -10,7 +10,9 @@ mod control_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -23,7 +25,9 @@ mod control_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -35,8 +39,10 @@ mod control_lib_tests {
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -52,7 +58,9 @@ mod control_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -65,7 +73,9 @@ mod control_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
@ -83,7 +93,9 @@ mod control_lib_tests {
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap().to_string(),
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}