add append function

This commit is contained in:
Aidan Hahn 2021-11-02 19:05:35 -07:00
parent 37eba3008f
commit ee3b53bfb5
No known key found for this signature in database
GPG key ID: 327711E983899316
5 changed files with 79 additions and 3 deletions

68
src/append.rs Normal file
View file

@ -0,0 +1,68 @@
/* 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::func::{FTable, Function, Args, Operation};
use crate::vars::{VTable};
use crate::segment::{Ctr, Ast, circuit, list_idx, list_append, new_ast};
use std::rc::Rc;
use std::cell::RefCell;
pub fn get_append() -> Function {
return Function{
name: String::from("append"),
loose_syms: false,
eval_lazy: false,
args: Args::Lazy(-1),
function: Operation::Internal(
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
let ptr = list_idx(a.clone(), 0);
match ptr {
Ctr::Seg(ref c) => {
let acpy = a.clone();
match acpy.borrow().cdr.clone() {
Ctr::Seg(cn) => {
// append to list case
if !circuit(cn, &mut |arg: &Ctr| -> bool {
list_append(c.clone(), arg.clone());
return true;
}) {
eprintln!("get_append circuit loop should not have returned false");
}
},
_ => {
eprintln!("get_append args somehow not in standard form");
}
}
return ptr;
},
_ => {
let n = new_ast(Ctr::None, Ctr::None);
if !circuit(a, &mut |arg: &Ctr| -> bool {
list_append(n.clone(), arg.clone());
return true;
}) {
eprintln!("get_append circuit loop should not have returned false");
}
return Ctr::Seg(n);
}
}
}
)
};
}

View file

@ -98,7 +98,7 @@ fn main() {
return
},
Err(err) => {
println!("Prompt error: {:?}", err);
eprintln!("Prompt error: {:?}", err);
break
}
}

View file

@ -22,6 +22,7 @@ mod eval;
mod vars;
mod stl;
mod str;
mod append;
pub mod ast {
pub use crate::segment::{Seg, Ctr, ast_to_string, Type, Ast, new_ast};
@ -36,4 +37,5 @@ pub mod ast {
pub mod stdlib {
pub use crate::stl::{get_stdlib};
pub use crate::str::{get_echo};
pub use crate::append::{get_append};
}

View file

@ -14,7 +14,9 @@
* 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::str::get_echo;
use crate::append::get_append;
use crate::func::{FTable, func_declare};
use std::rc::Rc;
use std::cell::RefCell;
@ -24,6 +26,9 @@ pub fn get_stdlib() -> Result<Rc<RefCell<FTable>>, String> {
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_echo()))) {
return Err(s)
}
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_append()))) {
return Err(s)
}
return Ok(ft)
}

View file

@ -21,6 +21,8 @@ use crate::segment::{Ctr, Ast, circuit, ast_as_string};
use std::rc::Rc;
use std::cell::RefCell;
// Current primitive is to use a get_NNNNN function defined for each library function which returns a not-previously-owned
// copy of the Function struct.
pub fn get_echo() -> Function {
return Function{
name: String::from("echo"),
@ -44,8 +46,7 @@ pub fn get_echo() -> Function {
string.push_str("\n");
return true;
}) {
// TODO: use custom logger
println!("Echo failure!")
eprintln!("circuit loop in echo should not have returned false")
}
return Ctr::String(string);
}