From ee1ef9700dfdaecbb477ed6ca383db84eb348115 Mon Sep 17 00:00:00 2001 From: Ava Hahn Date: Mon, 17 Apr 2023 22:11:49 -0700 Subject: [PATCH] cd function Signed-off-by: Ava Hahn --- Readme.org | 30 +++++++++++----------- snippets/userlib.rls | 12 ++++++++- src/lex.rs | 3 ++- src/run.rs | 4 +-- src/stl.rs | 11 ++++++++ src/stl/posix.rs | 60 +++++++++++++++++++++++++++++++++----------- 6 files changed, 87 insertions(+), 33 deletions(-) diff --git a/Readme.org b/Readme.org index 345ec56..e019a7d 100644 --- a/Readme.org +++ b/Readme.org @@ -483,16 +483,15 @@ Note: this section only tracks the state of incomplete TODO items. Having everyt ** TODO Pre-alpha tasks - Shell module + - strip final newline from load-to-string + - cd with no args goes home + - cd sets . symbol - ignore job control signals (if needed) - background processes - be able to list all background processes with j function - be able to fg a bg process - be able to bg and fg process (ctrl z?) - - changedir/cd - - pwd - Documentation! - - Do I need some linemode management stuff? - - Function stubs for all posix functions go into minimal shell (posix stub functions) - Escape sequences in strings - logging library - make const all the error messages @@ -534,9 +533,12 @@ Note: this section only tracks the state of incomplete TODO items. Having everyt - give a pretty pastel white, pink and blue theme - store in repo after giving presentation -** TODO alpha tasks -- TYPE SYSTEM HAS NO CTR::NONE -- SEG USES Option() AND IF POSSIBLE NO BOX (maybe strings or symbols or numbers are boxed instead?) +** TODO v1.0 tasks +- export function (dump symbol definitions to script and/or append) + - then write doc on interactive development +- completion: + - complete string: path search + - complete symbol: syms search - Write next_has member function for &Seg and simplify stdlib and probably also eval/sym - Rename to Flesh - Can pass args to relish scripts (via interpreter) @@ -544,27 +546,23 @@ Note: this section only tracks the state of incomplete TODO items. Having everyt - History length configurable - Search delim configurable - master branch -> main branch -- squash top of main and keep pre-alpha history in a historical branch -- delete snippets/legacy too - Create a dedicated community channel on matrix.sunnypup.io -- get type function - Lex function - Read function (Input + Lex) -** TODO post-alpha tasks +** TODO v1.1 tasks +- execute configurable function on cd - Post to relevant channels - Custom ast pretty print - Implement Compose for lambdas - Document this in relevant readme sections + - Document this in relevant readme sections - File operations - read-to-string - write-to-file - file exists - color control library -- emacs syntax highlighting and/or LSP implementation -- GNU Guix package -** TODO Second release tasks +** TODO v1.2 release tasks - Network library - HTTP Client - TCP Stream client @@ -572,4 +570,6 @@ Note: this section only tracks the state of incomplete TODO items. Having everyt - TCP Listener - HTTP Listener - UDP Listener +- emacs syntax highlighting and/or LSP implementation +- GNU Guix package - Bindings for the simplest possible UI library diff --git a/snippets/userlib.rls b/snippets/userlib.rls index a85ecfe..af8dd29 100644 --- a/snippets/userlib.rls +++ b/snippets/userlib.rls @@ -108,4 +108,14 @@ Returns true if the list contains the element.' 'Takes one argument. adds a directory to PATH' (path) (set (q PATH) - (concat PATH ':' path))) \ No newline at end of file + (concat PATH ':' path))) + +(def display-paths +'prints out each element of $PATH one by one' + () + (let ((path-iter (pop (get-paths)))) + (while (gt? (len path-iter) 1) + (let ((pat (car path-iter)) + (rem (cdr path-iter))) + (echo pat) + (set (q path-iter) (pop rem)))))) diff --git a/src/lex.rs b/src/lex.rs index 6a01804..d2a5fbd 100644 --- a/src/lex.rs +++ b/src/lex.rs @@ -218,7 +218,8 @@ fn tok_is_symbol(token: &str) -> Option { t != '?' && t != '=' && t != '.' && - t != '/' + t != '/' && + t != '.' { return None; } diff --git a/src/run.rs b/src/run.rs index 62ebf77..35a9371 100644 --- a/src/run.rs +++ b/src/run.rs @@ -26,7 +26,7 @@ use std::env::{vars, var, current_dir}; use std::rc::Rc; fn l_prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result { - Ok(Ctr::String("λ".to_string())) + Ok(Ctr::String(">".to_string())) } fn r_prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result { @@ -34,7 +34,7 @@ fn r_prompt_default_callback(_: &Seg, _: &mut SymTable) -> Result { } fn prompt_delimiter_default_callback(_: &Seg, _: &mut SymTable) -> Result { - Ok(Ctr::String(">".to_string())) + Ok(Ctr::String("λ ".to_string())) } fn get_paths() -> Vec { diff --git a/src/stl.rs b/src/stl.rs index 51091ef..7aefafd 100644 --- a/src/stl.rs +++ b/src/stl.rs @@ -648,6 +648,17 @@ pub fn dynamic_stdlib(syms: &mut SymTable, shell: Option. */ -use crate::segment::{Ctr, Seg}; -use crate::sym::{SymTable, ValueType, Symbol, Args}; -use crate::eval::eval; -use crate::run; -use std::collections::VecDeque; -use std::cell::RefCell; -use std::rc::Rc; -use std::fs::File; -use std::process::{Command, Child, Stdio}; -use nix::unistd; -use ctrlc; -use std::fs::OpenOptions; +use { + crate::{ + segment::{Ctr, Seg}, + sym::{ + SymTable, ValueType, + Symbol, Args, + }, + eval::eval, + run, + }, + + std::{ + collections::VecDeque, + cell::RefCell, + rc::Rc, + fs::{File, OpenOptions}, + path::Path, + process::{ + Command, Child, + Stdio, + }, + env::set_current_dir, + }, + + nix::unistd, + ctrlc, +}; pub struct ShellState { pub parent_pid: unistd::Pid, @@ -54,7 +69,8 @@ pub fn args_from_ast(ast: &Seg, syms: &mut SymTable) -> Vec { _ => args.push(res_ctr.to_string()) == (), } } else { - eprintln!("couldnt eval args!") != () + eprintln!("couldnt eval args: {}", + eval_res.err().unwrap()) != () } }, Ctr::Seg(ref form) => { @@ -67,7 +83,8 @@ pub fn args_from_ast(ast: &Seg, syms: &mut SymTable) -> Vec { _ => args.push(res_ctr.to_string()) == (), } } else { - eprintln!("couldnt eval args!") != () + eprintln!("couldnt eval args: {}", + eval_res.err().unwrap()) != () } }, Ctr::Lambda(_) => eprintln!("lambda passed as shell parameter") != (), @@ -443,6 +460,21 @@ fn bg_callback(_ast: &Seg, _syms: &mut SymTable, _state: &mut ShellState) -> Res unimplemented!() } +pub const CD_DOCSTRING: &str = + "Expects 1 argument (a string). Changes to a new directory"; +pub fn cd_callback(ast: &Seg, _syms: &mut SymTable) -> Result { + if let Ctr::String(ref dir) = *ast.car { + let dirp = Path::new(dir); + if let Err(s) = set_current_dir(&dirp) { + Err(format!("{}", s)) + } else { + Ok(Ctr::None) + } + } else { + Err(format!("impossible err: arg not a string")) + } +} + pub fn load_posix_shell(syms: &mut SymTable, shell_state: Rc>) { let pid = unistd::getpid(); let pgid_res = unistd::getpgid(Some(pid));