moving stuff from syntax to runtime
parent
680adc5882
commit
948326df4f
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue