moving stuff from syntax to runtime

main
Jordan Orelli 11 months ago
parent 680adc5882
commit 948326df4f

@ -1,6 +1,6 @@
use crate::{ use crate::{
error::ExecError, error::ExecError,
syntax::{self, State}, runtime::{State, Value},
}; };
pub enum Builtin { pub enum Builtin {
@ -15,18 +15,14 @@ impl Builtin {
} }
} }
pub fn call( pub fn call(&self, _: &mut State, args: &Vec<Value>) -> Result<Value, ExecError> {
&self,
_: &mut State,
args: &Vec<syntax::Value>,
) -> Result<syntax::Value, ExecError> {
match self { match self {
Builtin::Echo => { Builtin::Echo => {
let args: Result<Vec<String>, ExecError> = let args: Result<Vec<String>, ExecError> =
args.into_iter().map(|arg| arg.try_as_string()).collect(); args.into_iter().map(|arg| arg.try_as_string()).collect();
let args = args?; let args = args?;
println!("{}", args.join(" ")); println!("{}", args.join(" "));
Ok(syntax::Value::None) Ok(Value::None)
} }
} }
} }

@ -3,7 +3,7 @@ use crate::{
ext::{Command, Echo, Printenv, Tail, Which}, ext::{Command, Echo, Printenv, Tail, Which},
input, input,
log::*, log::*,
output, syntax, output, runtime, syntax,
}; };
use std::{ use std::{
@ -21,7 +21,7 @@ pub struct Session {
pub input: input::Reader, pub input: input::Reader,
pub output: output::Writer, pub output: output::Writer,
pub editor: edit::Buffer, pub editor: edit::Buffer,
pub state: syntax::State, pub state: runtime::State,
} }
impl Session { impl Session {
@ -30,7 +30,7 @@ impl Session {
input: input::Reader::new()?, input: input::Reader::new()?,
output: output::Writer::stdout()?, output: output::Writer::stdout()?,
editor: edit::Buffer::new(), editor: edit::Buffer::new(),
state: syntax::State::new(), state: runtime::State::new(),
}) })
} }

@ -31,13 +31,15 @@ mod parse;
mod prompt; mod prompt;
mod runtime;
/// syntax and semantic analysis /// syntax and semantic analysis
mod syntax; mod syntax;
/// topoglyph is a real word, i promise /// topoglyph is a real word, i promise
mod topo; mod topo;
use crate::{interactive::Session, log::*, prompt::Prompt, syntax::Eval}; use crate::{interactive::Session, log::*, prompt::Prompt, runtime::Eval};
use std::io::Write; use std::io::Write;
@ -86,7 +88,7 @@ fn main() -> Result<()> {
match syntax::parse(&s) { match syntax::parse(&s) {
Ok(tree) => { Ok(tree) => {
debug!(" {:?}", tree); debug!(" {:?}", tree);
let mut state = syntax::State::new(); let mut state = runtime::State::new();
if let Err(e) = tree.eval(&mut state) { if let Err(e) = tree.eval(&mut state) {
error!("{e:?}"); error!("{e:?}");
_ = session.render_error(e); _ = session.render_error(e);

@ -0,0 +1,53 @@
use crate::error::ExecError;
use std::{collections::HashMap, process};
pub trait Eval {
fn eval(&self, ctx: &mut State) -> Result<Value, ExecError>;
}
#[derive(Debug, Clone)]
pub enum Value {
None,
Text(String),
ExitStatus(process::ExitStatus),
}
impl Value {
pub fn try_to_string(self) -> Result<String, ExecError> {
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<String, ExecError> {
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<Value, ExecError> {
Ok(self.clone())
}
}
pub struct State {
variables: HashMap<&'static str, Value>,
}
impl State {
pub fn new() -> Self {
Self {
variables: HashMap::new(),
}
}
}

@ -3,28 +3,10 @@ use crate::{
error::{ExecError, ParseError}, error::{ExecError, ParseError},
lex::{Lexer, Token}, lex::{Lexer, Token},
log::debug, log::debug,
parse, syntax, parse,
runtime::{Eval, State, Value},
}; };
use std::{ use std::{collections::HashSet, process};
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<Value, ExecError>;
}
#[derive(Debug)] #[derive(Debug)]
pub enum Element { 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<Value, ExecError> {
Ok(self.clone())
}
}
impl Value {
pub fn try_to_string(self) -> Result<String, ExecError> {
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<String, ExecError> {
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)] #[derive(Debug)]
pub struct Block { pub struct Block {
commands: Vec<Command>, commands: Vec<Command>,

Loading…
Cancel
Save