From eed16964e6b24d34c0ee056f0c6058bd6d631e82 Mon Sep 17 00:00:00 2001 From: Ava Hahn Date: Wed, 1 Mar 2023 12:27:45 -0800 Subject: [PATCH] add echo function Signed-off-by: Ava Hahn --- snippets/basic_minimal_configuration.rls | 2 ++ src/lex.rs | 18 ++++++++---------- src/stl.rs | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 snippets/basic_minimal_configuration.rls diff --git a/snippets/basic_minimal_configuration.rls b/snippets/basic_minimal_configuration.rls new file mode 100644 index 0000000..c83be79 --- /dev/null +++ b/snippets/basic_minimal_configuration.rls @@ -0,0 +1,2 @@ +(def CFG_RELISH_POSIX "1") +(echo "Welcome back pupper") diff --git a/src/lex.rs b/src/lex.rs index eba3943..ca93293 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -28,14 +28,8 @@ pub fn lex(document: &String) -> Result, String> { return Err("document may only contain ascii characters".to_string()); } - let mut document_normal = document.clone(); - if !document_normal.ends_with(')') { - document_normal = document_normal + ")"; - } - if !document_normal.starts_with('(') { - document_normal = "(".to_string() + &document_normal; - } - + // finish a singlet token, or do nothing + let document_normal = document.clone() + " "; let tree = process(&document_normal); // TODO: Make multiple forms of Ok() @@ -159,7 +153,11 @@ fn process(document: &String) -> Result, 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; if is_str { obj = Box::from(Ctr::String(token)); @@ -185,7 +183,7 @@ fn process(document: &String) -> Result, String> { current_seg.append(obj.clone()); } - if alloc_list { + if alloc_list || return_singlet { // return if we have finished the document if ref_stack.is_empty() { return Ok(Box::new(current_seg)); diff --git a/src/stl.rs b/src/stl.rs index babae62..976ac0b 100644 --- a/src/stl.rs +++ b/src/stl.rs @@ -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( "if".to_string(), Symbol { @@ -90,6 +100,16 @@ pub fn dynamic_stdlib(syms: &mut SymTable) -> Result<(), String> { Ok(()) } +fn _echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result { + 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 { let is_var = ast.len() == 2; if let Ctr::Symbol(ref identifier) = *ast.car {