input function

This commit is contained in:
Ava Apples Affine 2023-03-22 22:53:29 -07:00
parent df6b5b5f06
commit 1ce5fd3454
Signed by: affine
GPG key ID: 3A4645B8CF806069
4 changed files with 35 additions and 5 deletions

View file

@ -478,10 +478,6 @@ Note: this section only tracks the state of incomplete TODO items. Having everyt
** TODO Pre-alpha tasks
- Shell prompt is fully configurable (History, L, R, and Delim)
- Input function
- Lex function
- Read function (Input + Lex)
- get type function
- Shell module
- Only loadable via POSIX config var
- Overload Load function to hook into this lib
@ -499,6 +495,9 @@ Note: this section only tracks the state of incomplete TODO items. Having everyt
- Rename to Flesh
- master branch -> main branch
- Create a dedicated community channel on matrix.sunnypup.io
- get type function
- Lex function
- Read function (Input + Lex)
** TODO post-alpha tasks
- Post to relevant channels

View file

@ -595,6 +595,18 @@ pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
}
);
syms.insert(
"input".to_string(),
Symbol {
name: String::from("input"),
args: Args::Strict(vec![Type::String]),
conditional_branches: false,
docs: strings::INPUT_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(strings::input_callback)),
..Default::default()
}
);
Ok(())
}

View file

@ -17,6 +17,8 @@
use crate::segment::{Ctr, Seg};
use crate::sym::SymTable;
use std::io::Write;
use std::io;
pub const ECHO_DOCSTRING: &str =
"traverses any number of arguments. Prints their evaluated values on a new line for each.";
@ -150,3 +152,19 @@ pub fn split_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
Ok(Ctr::Seg(ret))
}
pub 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, String> {
if let Ctr::String(ref s) = *ast.car {
print!("{}", s);
let _= io::stdout().flush();
let mut input = String::new();
io::stdin().read_line(&mut input).expect("couldnt read user input");
Ok(Ctr::String(input.trim().to_string()))
} else {
Err("impossible: arg not string".to_string())
}
}

View file

@ -101,10 +101,11 @@ impl SymTable {
* * sym is not in self
* * sym has a newer generation than the entry in self
*/
let tmp = self.1;
for i in other.iter() {
self.0.entry(i.0.to_string())
.and_modify(|inner: &mut Symbol| {
if inner.__generation < i.1.__generation {
if tmp < i.1.__generation {
inner.__generation = i.1.__generation;
inner.value = i.1.value.clone();
inner.args = i.1.args.clone();