cl-interpret, cl-repl:

Move IO builtins into the CLI, so get_line can use repline keybinds.
This commit is contained in:
2025-02-06 21:35:17 -06:00
parent 0e3ba342c4
commit af9c293907
5 changed files with 42 additions and 27 deletions

View File

@@ -14,11 +14,11 @@ use std::{
#[derive(Clone, Copy)]
pub struct Builtin {
/// An identifier to be used during registration
name: &'static str,
pub name: &'static str,
/// The signature, displayed when the builtin is printed
desc: &'static str,
pub desc: &'static str,
/// The function to be run when called
func: &'static dyn Fn(&mut Environment, &[ConValue]) -> IResult<ConValue>,
pub func: &'static dyn Fn(&mut Environment, &[ConValue]) -> IResult<ConValue>,
}
impl Builtin {
@@ -173,23 +173,10 @@ pub const Builtins: &[Builtin] = &builtins![
})
}
/// Gets a line of input from stdin
fn get_line() {
let mut line = String::new();
let _ = std::io::stdin().read_line(&mut line);
Ok(line)
}
/// Returns a shark
fn shark() {
Ok('\u{1f988}')
}
/// Clears the screen
fn clear() {
println!("\x1b[G");
Ok(())
}
];
pub const Math: &[Builtin] = &builtins![

View File

@@ -52,16 +52,14 @@ impl Display for Environment {
impl Default for Environment {
fn default() -> Self {
Self {
builtin: to_hashmap2(Builtins.iter().chain(Math.iter())),
builtin: to_hashmap(Builtins.iter().chain(Math.iter())),
global: vec![(HashMap::new(), "globals")],
frames: vec![],
}
}
}
// fn to_hashmap(from: &[&'static dyn BuiltIn]) -> HashMap<Sym, Option<ConValue>> {
// from.iter().map(|&v| (v.name(), Some(v.into()))).collect()
// }
fn to_hashmap2(from: impl IntoIterator<Item = &'static Builtin>) -> HashMap<Sym, Option<ConValue>> {
fn to_hashmap(from: impl IntoIterator<Item = &'static Builtin>) -> HashMap<Sym, Option<ConValue>> {
from.into_iter()
.map(|v| (v.name(), Some(v.into())))
.collect()
@@ -84,8 +82,15 @@ impl Environment {
&self.builtin
}
pub fn add_builtin(&mut self, builtin: &'static Builtin) {
pub fn add_builtin(&mut self, builtin: &'static Builtin) -> &mut Self {
self.builtin.insert(builtin.name(), Some(builtin.into()));
self
}
pub fn add_builtins(&mut self, builtins: &'static [Builtin]) {
for builtin in builtins {
self.add_builtin(builtin);
}
}
pub fn push_frame(&mut self, name: &'static str, frame: StackFrame) {