diff --git a/Readme.org b/Readme.org index 3a9b890..a71b4f1 100644 --- a/Readme.org +++ b/Readme.org @@ -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 diff --git a/src/stl.rs b/src/stl.rs index 9890c23..e8acc37 100644 --- a/src/stl.rs +++ b/src/stl.rs @@ -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(()) } diff --git a/src/stl/strings.rs b/src/stl/strings.rs index 32dcb97..aed89b3 100644 --- a/src/stl/strings.rs +++ b/src/stl/strings.rs @@ -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 { 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 { + 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()) + } +} diff --git a/src/sym.rs b/src/sym.rs index c20266f..2bf9db5 100644 --- a/src/sym.rs +++ b/src/sym.rs @@ -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();