updating builtin definitions

main
Jordan Orelli 9 months ago
parent 621f64d3f1
commit 9c7d64e512

@ -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<Value, ExecError> {
self.resolve().call(ctx, args)
}
}
pub fn all() -> HashMap<&'static str, Builtin> {
use Builtin::*;
HashMap::from([("echo", Echo), ("printenv", Printenv)])
}

@ -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<Value, ExecError> {
let args = args
.into_iter()
.map(|v| v.try_as_str())
.collect::<Result<Vec<_>, _>>()?;
_ = write!(ctx.stdout, "{}\n", args.join(" "));
Ok(Value::None)
}
}

@ -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<Value, ExecError> {
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)
}
}

@ -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<Value, ExecError> {
match self {
Builtin::Echo => Echo.call(ctx, args),
}
}
}
pub struct Echo;
impl Call for Echo {
fn call(&self, ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
let args = args
.into_iter()
.map(|v| v.try_as_str())
.collect::<Result<Vec<_>, _>>()?;
_ = write!(ctx.stdout, "{}\n", args.join(" "));
Ok(Value::None)
}
}
pub fn all() -> HashMap<&'static str, Builtin> {
use Builtin::*;
HashMap::from([("echo", Echo)])
}

@ -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:

@ -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<bool> {
println!("{}", args.join(" "));
Ok(true)
}
}

@ -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<bool> {
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)
}
}
}

@ -1,5 +1,5 @@
use crate::{
builtins, edit,
builtin, edit,
log::*,
output,
run::{self, Eval},

@ -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;

@ -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<builtins::Builtin> {
pub fn builtin(&self, name: &str) -> Option<builtin::Builtin> {
self.builtins.get(name).copied()
}
}

@ -1,5 +1,5 @@
use crate::{
builtins::Builtin,
builtin::Builtin,
error::{ExecError, ParseError},
lex::{Lexer, Token},
parse,

Loading…
Cancel
Save