From 948326df4f18a03fa2f56ea2e61282e615a04160 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 3 Feb 2024 13:32:48 -0600 Subject: [PATCH] moving stuff from syntax to runtime --- src/builtins.rs | 10 +++----- src/interactive.rs | 6 ++--- src/main.rs | 6 +++-- src/runtime.rs | 53 +++++++++++++++++++++++++++++++++++++++++ src/syntax.rs | 59 +++------------------------------------------- 5 files changed, 66 insertions(+), 68 deletions(-) create mode 100644 src/runtime.rs diff --git a/src/builtins.rs b/src/builtins.rs index d2c77c5..1253280 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -1,6 +1,6 @@ use crate::{ error::ExecError, - syntax::{self, State}, + runtime::{State, Value}, }; pub enum Builtin { @@ -15,18 +15,14 @@ impl Builtin { } } - pub fn call( - &self, - _: &mut State, - args: &Vec, - ) -> Result { + pub fn call(&self, _: &mut State, args: &Vec) -> Result { match self { Builtin::Echo => { let args: Result, ExecError> = args.into_iter().map(|arg| arg.try_as_string()).collect(); let args = args?; println!("{}", args.join(" ")); - Ok(syntax::Value::None) + Ok(Value::None) } } } diff --git a/src/interactive.rs b/src/interactive.rs index b2a39ac..b723a07 100644 --- a/src/interactive.rs +++ b/src/interactive.rs @@ -3,7 +3,7 @@ use crate::{ ext::{Command, Echo, Printenv, Tail, Which}, input, log::*, - output, syntax, + output, runtime, syntax, }; use std::{ @@ -21,7 +21,7 @@ pub struct Session { pub input: input::Reader, pub output: output::Writer, pub editor: edit::Buffer, - pub state: syntax::State, + pub state: runtime::State, } impl Session { @@ -30,7 +30,7 @@ impl Session { input: input::Reader::new()?, output: output::Writer::stdout()?, editor: edit::Buffer::new(), - state: syntax::State::new(), + state: runtime::State::new(), }) } diff --git a/src/main.rs b/src/main.rs index 9b537f4..324e1b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,13 +31,15 @@ mod parse; mod prompt; +mod runtime; + /// syntax and semantic analysis mod syntax; /// topoglyph is a real word, i promise mod topo; -use crate::{interactive::Session, log::*, prompt::Prompt, syntax::Eval}; +use crate::{interactive::Session, log::*, prompt::Prompt, runtime::Eval}; use std::io::Write; @@ -86,7 +88,7 @@ fn main() -> Result<()> { match syntax::parse(&s) { Ok(tree) => { debug!(" {:?}", tree); - let mut state = syntax::State::new(); + let mut state = runtime::State::new(); if let Err(e) = tree.eval(&mut state) { error!("{e:?}"); _ = session.render_error(e); diff --git a/src/runtime.rs b/src/runtime.rs new file mode 100644 index 0000000..6b5e122 --- /dev/null +++ b/src/runtime.rs @@ -0,0 +1,53 @@ +use crate::error::ExecError; +use std::{collections::HashMap, process}; + +pub trait Eval { + fn eval(&self, ctx: &mut State) -> Result; +} + +#[derive(Debug, Clone)] +pub enum Value { + None, + Text(String), + ExitStatus(process::ExitStatus), +} + +impl Value { + pub fn try_to_string(self) -> Result { + match self { + Value::None => Err(ExecError::type_error("expected text value, saw None value")), + Value::Text(v) => Ok(v), + Value::ExitStatus(_) => Err(ExecError::type_error( + "expected text value, saw ExitStatus value", + )), + } + } + + pub fn try_as_string(&self) -> Result { + match self { + Value::None => Err(ExecError::type_error("expected text value, saw None value")), + Value::Text(v) => Ok(v.clone()), + Value::ExitStatus(_) => Err(ExecError::type_error( + "expected text value, saw ExitStatus value", + )), + } + } +} + +impl Eval for Value { + fn eval(&self, _: &mut State) -> Result { + Ok(self.clone()) + } +} + +pub struct State { + variables: HashMap<&'static str, Value>, +} + +impl State { + pub fn new() -> Self { + Self { + variables: HashMap::new(), + } + } +} diff --git a/src/syntax.rs b/src/syntax.rs index be84476..401f7ce 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -3,28 +3,10 @@ use crate::{ error::{ExecError, ParseError}, lex::{Lexer, Token}, log::debug, - parse, syntax, + parse, + runtime::{Eval, State, Value}, }; -use std::{ - collections::{HashMap, HashSet}, - process, -}; - -pub struct State { - variables: HashMap<&'static str, syntax::Value>, -} - -impl State { - pub fn new() -> Self { - Self { - variables: HashMap::new(), - } - } -} - -pub trait Eval { - fn eval(&self, ctx: &mut State) -> Result; -} +use std::{collections::HashSet, process}; #[derive(Debug)] pub enum Element { @@ -44,41 +26,6 @@ impl Eval for Element { } } -#[derive(Debug, Clone)] -pub enum Value { - None, - Text(String), - ExitStatus(process::ExitStatus), -} - -impl Eval for Value { - fn eval(&self, _: &mut State) -> Result { - Ok(self.clone()) - } -} - -impl Value { - pub fn try_to_string(self) -> Result { - match self { - Value::None => Err(ExecError::type_error("expected text value, saw None value")), - Value::Text(v) => Ok(v), - Value::ExitStatus(_) => Err(ExecError::type_error( - "expected text value, saw ExitStatus value", - )), - } - } - - pub fn try_as_string(&self) -> Result { - match self { - Value::None => Err(ExecError::type_error("expected text value, saw None value")), - Value::Text(v) => Ok(v.clone()), - Value::ExitStatus(_) => Err(ExecError::type_error( - "expected text value, saw ExitStatus value", - )), - } - } -} - #[derive(Debug)] pub struct Block { commands: Vec,