aboutsummaryrefslogtreecommitdiffstats
path: root/src/run
diff options
context:
space:
mode:
authorJonas Maier <>2026-03-07 12:36:03 +0100
committerJonas Maier <>2026-03-07 12:36:03 +0100
commit1d4e2ae6f557082cae1d468ccd9cd0c2c873c0cc (patch)
tree456ab245e4d51b4d85ec5aac3001c241adb53b9c /src/run
parentf87cb13a99fa041c3065a74ea1eab4c0a963cd7d (diff)
downloadpish-1d4e2ae6f557082cae1d468ccd9cd0c2c873c0cc.tar.gz
variable assignments
Diffstat (limited to 'src/run')
-rw-r--r--src/run/mod.rs47
1 files changed, 32 insertions, 15 deletions
diff --git a/src/run/mod.rs b/src/run/mod.rs
index 18d3f0a..db0ed05 100644
--- a/src/run/mod.rs
+++ b/src/run/mod.rs
@@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
-use crate::parse::{Ast, PreExpansion};
+use crate::parse::{Ast, PostExpansion, PreExpansion};
use crate::*;
mod builtin;
@@ -44,17 +44,11 @@ impl std::io::Write for ArcWriter {
}
impl Executor {
- fn execute(
+ fn execute_pipeline(
&mut self,
- ast: Ast<parse::PostExpansion>,
+ pipes: parse::Pipes<parse::PostExpansion>,
capture: Option<&mut Vec<u8>>,
) -> Result<(), ExecError> {
- let Ast::Pipes(pipes) = ast else {
- todo!("can only handle pipes");
- };
-
- //print!("exec {}", format!("{pipes:?}\n").replace("\n", "\r\n"));
-
let mut children = Vec::new();
let mut threads = Vec::new();
let mut prev_reader = None;
@@ -170,12 +164,10 @@ impl Executor {
for jh in threads {
match jh.join() {
Ok(Ok(())) => (),
- Ok(Err(e)) => {
- match e {
- BuiltinError::IO(_) => code = -1,
- BuiltinError::Exit(c) => code = c,
- }
- }
+ Ok(Err(e)) => match e {
+ BuiltinError::IO(_) => code = -1,
+ BuiltinError::Exit(c) => code = c,
+ },
Err(_) => code = 127,
}
}
@@ -209,12 +201,37 @@ impl Executor {
}
}
}
+
+ fn execute_var_assign(&mut self, va: parse::VarAssign<PostExpansion>) -> Result<(), ExecError> {
+ self.se.lock().unwrap().vars.insert(va.var, va.val);
+ Ok(())
+ }
+
+ fn execute_fun_decl(&mut self, fd: parse::FunDecl<PostExpansion>) -> Result<(), ExecError> {
+ todo!()
+ }
+
+ fn execute(
+ &mut self,
+ ast: Ast<parse::PostExpansion>,
+ capture: Option<&mut Vec<u8>>,
+ ) -> Result<(), ExecError> {
+ match ast {
+ Ast::FunDecl(fd) => self.execute_fun_decl(fd),
+ Ast::VarAssign(va) => self.execute_var_assign(va),
+ Ast::Pipes(pipes) => self.execute_pipeline(pipes, capture),
+ }
+ }
}
impl parse::Expander for Executor {
type Error = ExecError;
fn expand_var(&mut self, var: BString) -> Result<BString, Self::Error> {
+ if let Some(val) = self.se.lock().unwrap().vars.get(&var) {
+ return Ok(val.clone())
+ }
+
match std::env::var_os(OsStr::from_bytes(&var)) {
Some(val) => Ok(val.as_bytes().to_vec()),
None => Err(ExecError::UnknownVariable(var)),