This MR finishes up all remaining Pre V1 goals
* add a posix exit() builtin * improve separation of concerns regarding standard library structure
This commit is contained in:
parent
b3c0b80ee6
commit
3bbea6bea0
9 changed files with 753 additions and 752 deletions
|
|
@ -15,16 +15,16 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::segment::{Ctr, Seg};
|
||||
use crate::sym::SymTable;
|
||||
use crate::segment::{Ctr, Seg, Type};
|
||||
use crate::sym::{SymTable, Symbol, ValueType, Args};
|
||||
use crate::error::{Traceback, start_trace};
|
||||
use std::io::Write;
|
||||
use std::io;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub const ECHO_DOCSTRING: &str =
|
||||
const ECHO_DOCSTRING: &str =
|
||||
"traverses any number of arguments. Prints their evaluated values on a new line for each.";
|
||||
|
||||
pub fn echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
fn echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
ast.circuit(&mut |arg: &Ctr| match arg {
|
||||
Ctr::String(s) => print!("{}", s) == (),
|
||||
_ => print!("{}", arg) == (),
|
||||
|
|
@ -33,11 +33,10 @@ pub fn echo_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback>
|
|||
Ok(Ctr::None)
|
||||
}
|
||||
|
||||
pub const CONCAT_DOCSTRING: &str = "Iterates over N args of any type other than SYMBOL.
|
||||
const CONCAT_DOCSTRING: &str = "Iterates over N args of any type other than SYMBOL.
|
||||
converts each argument to a string. Combines all strings.
|
||||
Returns final (combined) string.";
|
||||
|
||||
pub fn concat_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
fn concat_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
let mut string = String::from("");
|
||||
if !ast.circuit(&mut |arg: &Ctr| {
|
||||
match arg {
|
||||
|
|
@ -60,11 +59,10 @@ pub fn concat_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback
|
|||
return Ok(Ctr::String(string));
|
||||
}
|
||||
|
||||
pub const STRLEN_DOCSTRING: &str = "Takes a single arg of any type.
|
||||
const STRLEN_DOCSTRING: &str = "Takes a single arg of any type.
|
||||
Arg is converted to a string if not already a string.
|
||||
Returns string length of arg.";
|
||||
|
||||
pub fn strlen_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
fn strlen_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
match &*ast.car {
|
||||
Ctr::Symbol(s) => Ok(Ctr::Integer(s.len() as i128)),
|
||||
Ctr::String(s) => Ok(Ctr::Integer(s.len() as i128)),
|
||||
|
|
@ -78,10 +76,9 @@ pub fn strlen_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback
|
|||
}
|
||||
}
|
||||
|
||||
pub const STRCAST_DOCSTRING: &str = "Takes a single arg of any type.
|
||||
const STRCAST_DOCSTRING: &str = "Takes a single arg of any type.
|
||||
Arg is converted to a string and returned.";
|
||||
|
||||
pub fn strcast_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
fn strcast_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
match &*ast.car {
|
||||
Ctr::Symbol(s) => Ok(Ctr::String(s.clone())),
|
||||
Ctr::String(_) => Ok(*ast.car.clone()),
|
||||
|
|
@ -95,10 +92,9 @@ pub fn strcast_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Tracebac
|
|||
}
|
||||
}
|
||||
|
||||
pub const SUBSTR_DOCSTRING: &str =
|
||||
const SUBSTR_DOCSTRING: &str =
|
||||
"Takes two strings. Returns true if string1 contains at least one instance of string2";
|
||||
|
||||
pub fn substr_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
fn substr_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
let parent_str: String;
|
||||
if let Ctr::String(ref s) = *ast.car {
|
||||
parent_str = s.to_string();
|
||||
|
|
@ -129,10 +125,9 @@ pub fn substr_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback
|
|||
Ok(Ctr::Bool(parent_str.contains(&child_str)))
|
||||
}
|
||||
|
||||
pub const SPLIT_DOCSTRING: &str = "Takes two strings. String 1 is a source string and string 2 is a delimiter.
|
||||
const SPLIT_DOCSTRING: &str = "Takes two strings. String 1 is a source string and string 2 is a delimiter.
|
||||
Returns a list of substrings from string 1 that were found delimited by string 2.";
|
||||
|
||||
pub fn split_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
fn split_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
let parent_str: String;
|
||||
if let Ctr::String(ref s) = *ast.car {
|
||||
parent_str = s.to_string();
|
||||
|
|
@ -168,11 +163,10 @@ pub fn split_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback>
|
|||
Ok(Ctr::Seg(ret))
|
||||
}
|
||||
|
||||
pub const INPUT_DOCSTRING: &str = "Takes one argument (string) and prints it.
|
||||
const INPUT_DOCSTRING: &str = "Takes one argument (string) and prints it.
|
||||
Then prompts for user input.
|
||||
User input is returned as a string";
|
||||
|
||||
pub fn input_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
fn input_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
||||
if let Ctr::String(ref s) = *ast.car {
|
||||
print!("{}", s);
|
||||
let _= io::stdout().flush();
|
||||
|
|
@ -185,3 +179,89 @@ pub fn input_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback>
|
|||
.into()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_string_lib(syms: &mut SymTable) {
|
||||
syms.insert(
|
||||
"echo".to_string(),
|
||||
Symbol {
|
||||
name: String::from("echo"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: false,
|
||||
docs: ECHO_DOCSTRING.to_string(),
|
||||
value: ValueType::Internal(Rc::new(echo_callback)),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"concat".to_string(),
|
||||
Symbol {
|
||||
name: String::from("concat"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: false,
|
||||
docs: CONCAT_DOCSTRING.to_string(),
|
||||
value: ValueType::Internal(Rc::new(concat_callback)),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"substr?".to_string(),
|
||||
Symbol {
|
||||
name: String::from("substr?"),
|
||||
args: Args::Strict(vec![Type::String, Type::String]),
|
||||
conditional_branches: false,
|
||||
docs: SUBSTR_DOCSTRING.to_string(),
|
||||
value: ValueType::Internal(Rc::new(substr_callback)),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"split".to_string(),
|
||||
Symbol {
|
||||
name: String::from("split"),
|
||||
args: Args::Strict(vec![Type::String, Type::String]),
|
||||
conditional_branches: false,
|
||||
docs: SPLIT_DOCSTRING.to_string(),
|
||||
value: ValueType::Internal(Rc::new(split_callback)),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"strlen".to_string(),
|
||||
Symbol {
|
||||
name: String::from("strlen"),
|
||||
args: Args::Lazy(1),
|
||||
conditional_branches: false,
|
||||
docs: STRLEN_DOCSTRING.to_string(),
|
||||
value: ValueType::Internal(Rc::new(strlen_callback)),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"string".to_string(),
|
||||
Symbol {
|
||||
name: String::from("string"),
|
||||
args: Args::Lazy(1),
|
||||
conditional_branches: false,
|
||||
docs: STRCAST_DOCSTRING.to_string(),
|
||||
value: ValueType::Internal(Rc::new(strcast_callback)),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"input".to_string(),
|
||||
Symbol {
|
||||
name: String::from("input"),
|
||||
args: Args::Strict(vec![Type::String]),
|
||||
conditional_branches: false,
|
||||
docs: INPUT_DOCSTRING.to_string(),
|
||||
value: ValueType::Internal(Rc::new(input_callback)),
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue