From 1d4e2ae6f557082cae1d468ccd9cd0c2c873c0cc Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Sat, 7 Mar 2026 12:36:03 +0100 Subject: variable assignments --- src/main.rs | 3 +++ src/run/mod.rs | 47 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index efcbd3c..925c62c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::ffi::OsStr; use std::fs; use std::io::{self, IsTerminal, Read, Write}; @@ -62,6 +63,7 @@ pub struct Session { history: Vec, dispatch: CommandDispatch, prev_path: BString, + vars: HashMap, /// n before end of history.len() /// 0 == not checking history @@ -202,6 +204,7 @@ fn event_loop() { dispatch: CommandDispatch::new(), prev_path: vec![b'.'], history_visit: 0, + vars: HashMap::new(), }; print!("{}", se.prompt()); 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, + pipes: parse::Pipes, capture: Option<&mut Vec>, ) -> 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) -> Result<(), ExecError> { + self.se.lock().unwrap().vars.insert(va.var, va.val); + Ok(()) + } + + fn execute_fun_decl(&mut self, fd: parse::FunDecl) -> Result<(), ExecError> { + todo!() + } + + fn execute( + &mut self, + ast: Ast, + capture: Option<&mut Vec>, + ) -> 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 { + 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)), -- cgit v1.2.3