add echo function

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-03-01 12:27:45 -08:00
parent 914bf1303f
commit eed16964e6
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
3 changed files with 30 additions and 10 deletions

View file

@ -0,0 +1,2 @@
(def CFG_RELISH_POSIX "1")
(echo "Welcome back pupper")

View file

@ -28,14 +28,8 @@ pub fn lex(document: &String) -> Result<Box<Seg>, String> {
return Err("document may only contain ascii characters".to_string()); return Err("document may only contain ascii characters".to_string());
} }
let mut document_normal = document.clone(); // finish a singlet token, or do nothing
if !document_normal.ends_with(')') { let document_normal = document.clone() + " ";
document_normal = document_normal + ")";
}
if !document_normal.starts_with('(') {
document_normal = "(".to_string() + &document_normal;
}
let tree = process(&document_normal); let tree = process(&document_normal);
// TODO: Make multiple forms of Ok() // TODO: Make multiple forms of Ok()
@ -159,7 +153,11 @@ fn process(document: &String) -> Result<Box<Seg>, String> {
return Err("Empty token".to_string()); return Err("Empty token".to_string());
} }
let mut current_seg = ref_stack.pop().unwrap(); let mut return_singlet = false;
let mut current_seg = ref_stack.pop().unwrap_or_else(|| {
return_singlet = true;
Seg::new()
});
let obj; let obj;
if is_str { if is_str {
obj = Box::from(Ctr::String(token)); obj = Box::from(Ctr::String(token));
@ -185,7 +183,7 @@ fn process(document: &String) -> Result<Box<Seg>, String> {
current_seg.append(obj.clone()); current_seg.append(obj.clone());
} }
if alloc_list { if alloc_list || return_singlet {
// return if we have finished the document // return if we have finished the document
if ref_stack.is_empty() { if ref_stack.is_empty() {
return Ok(Box::new(current_seg)); return Ok(Box::new(current_seg));

View file

@ -49,6 +49,16 @@ pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
}, },
); );
syms.insert(
"echo".to_string(),
Symbol {
name: String::from("echo"),
args: Args::Infinite,
conditional_branches: false,
value: ValueType::Internal(Rc::new(_echo_callback)),
},
);
syms.insert( syms.insert(
"if".to_string(), "if".to_string(),
Symbol { Symbol {
@ -90,6 +100,16 @@ pub fn dynamic_stdlib(syms: &mut SymTable) -> Result<(), String> {
Ok(()) Ok(())
} }
fn _echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
if ast.len() == 1 {
println!("{}", ast.car);
} else {
ast.circuit(&mut |arg: &Ctr| print!("{}", arg) == ());
}
Ok(Ctr::None)
}
fn _store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> { fn _store_callback(ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Result<Ctr, String> {
let is_var = ast.len() == 2; let is_var = ast.len() == 2;
if let Ctr::Symbol(ref identifier) = *ast.car { if let Ctr::Symbol(ref identifier) = *ast.car {