From 9c7d64e512e0cd9884cfedad72a17548aeb7d7f8 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 5 Feb 2024 10:21:33 -0600 Subject: [PATCH] updating builtin definitions --- src/builtin.rs | 35 +++++++++++++++++++++++++++++++++++ src/builtin/echo.rs | 18 ++++++++++++++++++ src/builtin/printenv.rs | 26 ++++++++++++++++++++++++++ src/builtins.rs | 36 ------------------------------------ src/ext.rs | 4 ---- src/ext/echo.rs | 19 ------------------- src/ext/printenv.rs | 33 --------------------------------- src/interactive.rs | 2 +- src/main.rs | 2 +- src/run.rs | 8 ++++---- src/syntax.rs | 2 +- 11 files changed, 86 insertions(+), 99 deletions(-) create mode 100644 src/builtin.rs create mode 100644 src/builtin/echo.rs create mode 100644 src/builtin/printenv.rs delete mode 100644 src/builtins.rs delete mode 100644 src/ext/echo.rs delete mode 100644 src/ext/printenv.rs diff --git a/src/builtin.rs b/src/builtin.rs new file mode 100644 index 0000000..b1d8ef8 --- /dev/null +++ b/src/builtin.rs @@ -0,0 +1,35 @@ +mod echo; +mod printenv; + +use crate::{ + error::ExecError, + run::{Call, Context, Value}, +}; +use std::collections::HashMap; + +#[derive(Clone, Copy)] +pub enum Builtin { + Echo, + Printenv, +} + +impl Builtin { + fn resolve(&self) -> &dyn Call { + use Builtin::*; + match self { + Echo => &echo::Echo, + Printenv => &printenv::Printenv, + } + } +} + +impl Call for Builtin { + fn call(&self, ctx: &mut Context, args: &[Value]) -> Result { + self.resolve().call(ctx, args) + } +} + +pub fn all() -> HashMap<&'static str, Builtin> { + use Builtin::*; + HashMap::from([("echo", Echo), ("printenv", Printenv)]) +} diff --git a/src/builtin/echo.rs b/src/builtin/echo.rs new file mode 100644 index 0000000..33bd6da --- /dev/null +++ b/src/builtin/echo.rs @@ -0,0 +1,18 @@ +use crate::{ + error::ExecError, + run::{Call, Context, Value}, +}; +use std::io::Write; + +pub struct Echo; + +impl Call for Echo { + fn call(&self, ctx: &mut Context, args: &[Value]) -> Result { + let args = args + .into_iter() + .map(|v| v.try_as_str()) + .collect::, _>>()?; + _ = write!(ctx.stdout, "{}\n", args.join(" ")); + Ok(Value::None) + } +} diff --git a/src/builtin/printenv.rs b/src/builtin/printenv.rs new file mode 100644 index 0000000..5442066 --- /dev/null +++ b/src/builtin/printenv.rs @@ -0,0 +1,26 @@ +use crate::{ + error::ExecError, + run::{Call, Context, Value}, +}; +use std::{env, io::Write}; + +pub struct Printenv; + +impl Call for Printenv { + fn call(&self, ctx: &mut Context, args: &[Value]) -> Result { + if args.len() > 0 { + for arg in args { + let name = arg.try_as_str()?; + _ = match env::var(name) { + Ok(val) => writeln!(ctx.stdout, "{}", val), + Err(e) => writeln!(ctx.stderr, "ERROR {}", e), + }; + } + } else { + for (name, val) in env::vars() { + _ = writeln!(ctx.stdout, "{name}={val}"); + } + } + Ok(Value::None) + } +} diff --git a/src/builtins.rs b/src/builtins.rs deleted file mode 100644 index d102293..0000000 --- a/src/builtins.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::{ - error::ExecError, - run::{Call, Context, Value}, -}; -use std::{boxed::Box, collections::HashMap, io::Write}; - -#[derive(Clone, Copy)] -pub enum Builtin { - Echo, -} - -impl Call for Builtin { - fn call(&self, ctx: &mut Context, args: &[Value]) -> Result { - match self { - Builtin::Echo => Echo.call(ctx, args), - } - } -} - -pub struct Echo; - -impl Call for Echo { - fn call(&self, ctx: &mut Context, args: &[Value]) -> Result { - let args = args - .into_iter() - .map(|v| v.try_as_str()) - .collect::, _>>()?; - _ = write!(ctx.stdout, "{}\n", args.join(" ")); - Ok(Value::None) - } -} - -pub fn all() -> HashMap<&'static str, Builtin> { - use Builtin::*; - HashMap::from([("echo", Echo)]) -} diff --git a/src/ext.rs b/src/ext.rs index a2c8c51..e6ffbdf 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,14 +1,10 @@ mod command; mod tail; mod which; -mod printenv; -mod echo; pub use command::Command; -pub use echo::Echo; pub use tail::Tail; pub use which::Which; -pub use printenv::Printenv; /* Posix Shell builtins: diff --git a/src/ext/echo.rs b/src/ext/echo.rs deleted file mode 100644 index 299c747..0000000 --- a/src/ext/echo.rs +++ /dev/null @@ -1,19 +0,0 @@ -use crate::ext::Command; -use anyhow::Result; - -pub struct Echo {} - -impl Command for Echo { - fn name() -> String { - String::from("echo") - } - - fn create() -> Self { - Self {} - } - - fn exec(&mut self, args: Vec<&str>) -> Result { - println!("{}", args.join(" ")); - Ok(true) - } -} diff --git a/src/ext/printenv.rs b/src/ext/printenv.rs deleted file mode 100644 index 539b159..0000000 --- a/src/ext/printenv.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::ext::Command; -use anyhow::Result; - -pub struct Printenv {} - -impl Command for Printenv { - fn name() -> String { - String::from("printenv") - } - - fn create() -> Self { - Self {} - } - - fn exec(&mut self, args: Vec<&str>) -> Result { - if args.len() > 0 { - let name = args[0]; - match std::env::var(name) { - Ok(val) => { - println!("{}", val); - Ok(true) - } - Err(e) => { - println!("ERROR {}", e); - Ok(false) - } - } - } else { - println!("which variable you fucking dork"); - Ok(false) - } - } -} diff --git a/src/interactive.rs b/src/interactive.rs index 68eb91e..af3e97a 100644 --- a/src/interactive.rs +++ b/src/interactive.rs @@ -1,5 +1,5 @@ use crate::{ - builtins, edit, + builtin, edit, log::*, output, run::{self, Eval}, diff --git a/src/main.rs b/src/main.rs index ad8f107..c11e34b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ /// hi /// builtin functions -mod builtins; +mod builtin; /// all of the errors for the clyde project live in this module mod error; diff --git a/src/run.rs b/src/run.rs index 42a6599..8cd2039 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,4 +1,4 @@ -use crate::{builtins, error::ExecError, input, output}; +use crate::{builtin, error::ExecError, input, output}; use std::{collections::HashMap, process}; /// Eval represents anything that can be evaluated at runtime. @@ -50,14 +50,14 @@ impl Eval for Value { pub struct State { #[allow(unused)] variables: HashMap<&'static str, Value>, - pub(crate) builtins: HashMap<&'static str, builtins::Builtin>, + pub(crate) builtins: HashMap<&'static str, builtin::Builtin>, } impl State { pub fn new() -> Self { Self { variables: HashMap::new(), - builtins: builtins::all(), + builtins: builtin::all(), } } @@ -65,7 +65,7 @@ impl State { self.builtins.contains_key(name) } - pub fn builtin(&self, name: &str) -> Option { + pub fn builtin(&self, name: &str) -> Option { self.builtins.get(name).copied() } } diff --git a/src/syntax.rs b/src/syntax.rs index d50e7a8..5e91814 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -1,5 +1,5 @@ use crate::{ - builtins::Builtin, + builtin::Builtin, error::{ExecError, ParseError}, lex::{Lexer, Token}, parse,