moving stuff from syntax to runtime

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

@ -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<syntax::Value>,
) -> Result<syntax::Value, ExecError> {
pub fn call(&self, _: &mut State, args: &Vec<Value>) -> Result<Value, ExecError> {
match self {
Builtin::Echo => {
let args: Result<Vec<String>, ExecError> =
args.into_iter().map(|arg| arg.try_as_string()).collect();
let args = args?;
println!("{}", args.join(" "));
Ok(syntax::Value::None)
Ok(Value::None)
}
}
}

@ -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(),
})
}

@ -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);

@ -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},
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<Value, ExecError>;
}
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<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)]
pub struct Block {
commands: Vec<Command>,

Loading…
Cancel
Save