add append function
This commit is contained in:
parent
37eba3008f
commit
ee3b53bfb5
5 changed files with 79 additions and 3 deletions
68
src/append.rs
Normal file
68
src/append.rs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -98,7 +98,7 @@ fn main() {
|
||||||
return
|
return
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!("Prompt error: {:?}", err);
|
eprintln!("Prompt error: {:?}", err);
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ mod eval;
|
||||||
mod vars;
|
mod vars;
|
||||||
mod stl;
|
mod stl;
|
||||||
mod str;
|
mod str;
|
||||||
|
mod append;
|
||||||
|
|
||||||
pub mod ast {
|
pub mod ast {
|
||||||
pub use crate::segment::{Seg, Ctr, ast_to_string, Type, Ast, new_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 mod stdlib {
|
||||||
pub use crate::stl::{get_stdlib};
|
pub use crate::stl::{get_stdlib};
|
||||||
pub use crate::str::{get_echo};
|
pub use crate::str::{get_echo};
|
||||||
|
pub use crate::append::{get_append};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,9 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::str::get_echo;
|
use crate::str::get_echo;
|
||||||
|
use crate::append::get_append;
|
||||||
use crate::func::{FTable, func_declare};
|
use crate::func::{FTable, func_declare};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
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()))) {
|
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_echo()))) {
|
||||||
return Err(s)
|
return Err(s)
|
||||||
}
|
}
|
||||||
|
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_append()))) {
|
||||||
|
return Err(s)
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(ft)
|
return Ok(ft)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ use crate::segment::{Ctr, Ast, circuit, ast_as_string};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
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 {
|
pub fn get_echo() -> Function {
|
||||||
return Function{
|
return Function{
|
||||||
name: String::from("echo"),
|
name: String::from("echo"),
|
||||||
|
|
@ -44,8 +46,7 @@ pub fn get_echo() -> Function {
|
||||||
string.push_str("\n");
|
string.push_str("\n");
|
||||||
return true;
|
return true;
|
||||||
}) {
|
}) {
|
||||||
// TODO: use custom logger
|
eprintln!("circuit loop in echo should not have returned false")
|
||||||
println!("Echo failure!")
|
|
||||||
}
|
}
|
||||||
return Ctr::String(string);
|
return Ctr::String(string);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue