add echo function
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
914bf1303f
commit
eed16964e6
3 changed files with 30 additions and 10 deletions
18
src/lex.rs
18
src/lex.rs
|
|
@ -28,14 +28,8 @@ pub fn lex(document: &String) -> Result<Box<Seg>, 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<Box<Seg>, 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<Box<Seg>, 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));
|
||||
|
|
|
|||
20
src/stl.rs
20
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<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> {
|
||||
let is_var = ast.len() == 2;
|
||||
if let Ctr::Symbol(ref identifier) = *ast.car {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue