2021-07-30 06:10:40 +00:00
|
|
|
use std::{cell::RefCell, collections::HashMap, fmt::Display, rc::Rc, time::Instant};
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-07-23 21:19:30 +00:00
|
|
|
use crate::{
|
|
|
|
parser::Operator, Block, BlockId, Call, Expr, Expression, ParserState, Span, Statement, VarId,
|
|
|
|
};
|
2021-07-16 01:10:22 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ShellError {
|
|
|
|
Mismatch(String, Span),
|
|
|
|
Unsupported(Span),
|
2021-07-23 05:14:49 +00:00
|
|
|
InternalError(String),
|
2021-07-30 21:57:22 +00:00
|
|
|
VariableNotFound(Span),
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2021-07-16 01:10:22 +00:00
|
|
|
pub enum Value {
|
2021-07-24 05:57:17 +00:00
|
|
|
Bool { val: bool, span: Span },
|
2021-07-16 01:10:22 +00:00
|
|
|
Int { val: i64, span: Span },
|
2021-08-08 20:21:21 +00:00
|
|
|
Float { val: f64, span: Span },
|
2021-07-23 21:19:30 +00:00
|
|
|
String { val: String, span: Span },
|
2021-08-08 21:02:47 +00:00
|
|
|
List { val: Vec<Value>, span: Span },
|
|
|
|
Block { val: BlockId, span: Span },
|
|
|
|
Nothing { span: Span },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn as_string(&self) -> Result<String, ShellError> {
|
|
|
|
match self {
|
|
|
|
Value::String { val, .. } => Ok(val.to_string()),
|
|
|
|
_ => Err(ShellError::Mismatch("string".into(), self.span())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
match self {
|
|
|
|
Value::Bool { span, .. } => *span,
|
|
|
|
Value::Int { span, .. } => *span,
|
|
|
|
Value::Float { span, .. } => *span,
|
|
|
|
Value::String { span, .. } => *span,
|
|
|
|
Value::List { span, .. } => *span,
|
|
|
|
Value::Block { span, .. } => *span,
|
|
|
|
Value::Nothing { span, .. } => *span,
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-30 03:26:06 +00:00
|
|
|
|
2021-07-30 06:10:40 +00:00
|
|
|
impl PartialEq for Value {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(Value::Bool { val: lhs, .. }, Value::Bool { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => lhs == rhs,
|
2021-08-08 20:21:21 +00:00
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => lhs == rhs,
|
2021-07-30 06:10:40 +00:00
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
|
2021-08-08 21:02:47 +00:00
|
|
|
(Value::List { val: l1, .. }, Value::List { val: l2, .. }) => l1 == l2,
|
|
|
|
(Value::Block { val: b1, .. }, Value::Block { val: b2, .. }) => b1 == b2,
|
2021-07-30 06:10:40 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 03:26:06 +00:00
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Value::Bool { val, .. } => {
|
|
|
|
write!(f, "{}", val)
|
|
|
|
}
|
|
|
|
Value::Int { val, .. } => {
|
|
|
|
write!(f, "{}", val)
|
|
|
|
}
|
2021-08-08 20:21:21 +00:00
|
|
|
Value::Float { val, .. } => {
|
|
|
|
write!(f, "{}", val)
|
|
|
|
}
|
2021-07-30 03:26:06 +00:00
|
|
|
Value::String { val, .. } => write!(f, "{}", val),
|
2021-08-08 21:02:47 +00:00
|
|
|
Value::List { .. } => write!(f, "<list>"),
|
|
|
|
Value::Block { .. } => write!(f, "<block>"),
|
|
|
|
Value::Nothing { .. } => write!(f, ""),
|
2021-07-30 03:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 01:10:22 +00:00
|
|
|
impl Value {
|
|
|
|
pub fn add(&self, rhs: &Value) -> Result<Value, ShellError> {
|
|
|
|
match (self, rhs) {
|
|
|
|
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Int {
|
|
|
|
val: lhs + rhs,
|
|
|
|
span: Span::unknown(),
|
|
|
|
}),
|
2021-08-08 20:21:21 +00:00
|
|
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs as f64 + *rhs,
|
|
|
|
span: Span::unknown(),
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Int { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: *lhs + *rhs as f64,
|
|
|
|
span: Span::unknown(),
|
|
|
|
}),
|
|
|
|
(Value::Float { val: lhs, .. }, Value::Float { val: rhs, .. }) => Ok(Value::Float {
|
|
|
|
val: lhs + rhs,
|
|
|
|
span: Span::unknown(),
|
|
|
|
}),
|
2021-07-29 22:56:51 +00:00
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => Ok(Value::String {
|
|
|
|
val: lhs.to_string() + rhs,
|
|
|
|
span: Span::unknown(),
|
|
|
|
}),
|
|
|
|
|
2021-08-08 21:02:47 +00:00
|
|
|
_ => Err(ShellError::Mismatch("addition".into(), self.span())),
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
pub struct State<'a> {
|
|
|
|
pub parser_state: &'a ParserState,
|
|
|
|
}
|
|
|
|
|
2021-08-09 07:53:06 +00:00
|
|
|
#[derive(Debug)]
|
2021-07-30 05:42:33 +00:00
|
|
|
pub struct StackFrame {
|
2021-07-23 05:14:49 +00:00
|
|
|
pub vars: HashMap<VarId, Value>,
|
2021-08-08 21:02:47 +00:00
|
|
|
pub env_vars: HashMap<String, String>,
|
2021-07-30 05:42:33 +00:00
|
|
|
pub parent: Option<Stack>,
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 07:53:06 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2021-07-30 08:06:48 +00:00
|
|
|
pub struct Stack(Rc<RefCell<StackFrame>>);
|
2021-07-30 05:42:33 +00:00
|
|
|
|
2021-07-30 08:06:48 +00:00
|
|
|
impl Default for Stack {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stack {
|
|
|
|
pub fn new() -> Stack {
|
|
|
|
Stack(Rc::new(RefCell::new(StackFrame {
|
|
|
|
vars: HashMap::new(),
|
2021-08-08 21:02:47 +00:00
|
|
|
env_vars: HashMap::new(),
|
2021-07-30 08:06:48 +00:00
|
|
|
parent: None,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
pub fn get_var(&self, var_id: VarId) -> Result<Value, ShellError> {
|
|
|
|
let this = self.0.borrow();
|
2021-07-30 05:42:33 +00:00
|
|
|
match this.vars.get(&var_id) {
|
2021-07-23 05:14:49 +00:00
|
|
|
Some(v) => Ok(v.clone()),
|
2021-07-23 21:19:30 +00:00
|
|
|
_ => {
|
2021-07-30 21:57:22 +00:00
|
|
|
if let Some(parent) = &this.parent {
|
|
|
|
parent.get_var(var_id)
|
|
|
|
} else {
|
|
|
|
Err(ShellError::InternalError("variable not found".into()))
|
|
|
|
}
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 21:19:30 +00:00
|
|
|
|
2021-07-30 08:06:48 +00:00
|
|
|
pub fn add_var(&self, var_id: VarId, value: Value) {
|
|
|
|
let mut this = self.0.borrow_mut();
|
2021-07-30 05:42:33 +00:00
|
|
|
this.vars.insert(var_id, value);
|
|
|
|
}
|
|
|
|
|
2021-08-08 21:02:47 +00:00
|
|
|
pub fn add_env_var(&self, var: String, value: String) {
|
|
|
|
let mut this = self.0.borrow_mut();
|
|
|
|
this.env_vars.insert(var, value);
|
|
|
|
}
|
|
|
|
|
2021-07-30 08:06:48 +00:00
|
|
|
pub fn enter_scope(self) -> Stack {
|
|
|
|
Stack(Rc::new(RefCell::new(StackFrame {
|
2021-07-30 05:42:33 +00:00
|
|
|
vars: HashMap::new(),
|
2021-08-08 21:02:47 +00:00
|
|
|
env_vars: HashMap::new(),
|
2021-07-30 08:06:48 +00:00
|
|
|
parent: Some(self),
|
|
|
|
})))
|
2021-07-30 05:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_stack(&self) {
|
|
|
|
println!("===frame===");
|
2021-08-08 21:02:47 +00:00
|
|
|
println!("vars:");
|
2021-07-30 08:06:48 +00:00
|
|
|
for (var, val) in &self.0.borrow().vars {
|
2021-08-08 21:02:47 +00:00
|
|
|
println!(" {}: {:?}", var, val);
|
|
|
|
}
|
|
|
|
println!("env vars:");
|
|
|
|
for (var, val) in &self.0.borrow().env_vars {
|
|
|
|
println!(" {}: {:?}", var, val);
|
2021-07-30 05:42:33 +00:00
|
|
|
}
|
2021-07-30 08:06:48 +00:00
|
|
|
if let Some(parent) = &self.0.borrow().parent {
|
|
|
|
parent.print_stack()
|
2021-07-30 05:42:33 +00:00
|
|
|
}
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
pub fn eval_operator(
|
2021-07-23 21:19:30 +00:00
|
|
|
_state: &State,
|
2021-07-30 05:42:33 +00:00
|
|
|
_stack: Stack,
|
2021-07-23 05:14:49 +00:00
|
|
|
op: &Expression,
|
|
|
|
) -> Result<Operator, ShellError> {
|
|
|
|
match op {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Operator(operator),
|
|
|
|
..
|
|
|
|
} => Ok(operator.clone()),
|
|
|
|
Expression { span, .. } => Err(ShellError::Mismatch("operator".to_string(), *span)),
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-07-30 05:42:33 +00:00
|
|
|
fn eval_call(state: &State, stack: Stack, call: &Call) -> Result<Value, ShellError> {
|
2021-07-23 05:14:49 +00:00
|
|
|
let decl = state.parser_state.get_decl(call.decl_id);
|
|
|
|
if let Some(block_id) = decl.body {
|
2021-07-30 08:06:48 +00:00
|
|
|
let stack = stack.enter_scope();
|
2021-07-23 21:19:30 +00:00
|
|
|
for (arg, param) in call
|
|
|
|
.positional
|
|
|
|
.iter()
|
|
|
|
.zip(decl.signature.required_positional.iter())
|
|
|
|
{
|
2021-07-30 05:42:33 +00:00
|
|
|
let result = eval_expression(state, stack.clone(), arg)?;
|
2021-07-23 21:19:30 +00:00
|
|
|
let var_id = param
|
|
|
|
.var_id
|
|
|
|
.expect("internal error: all custom parameters must have var_ids");
|
|
|
|
|
2021-07-30 08:06:48 +00:00
|
|
|
stack.add_var(var_id, result);
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
let block = state.parser_state.get_block(block_id);
|
|
|
|
eval_block(state, stack, block)
|
2021-07-29 22:56:51 +00:00
|
|
|
} else if decl.signature.name == "let" {
|
|
|
|
let var_id = call.positional[0]
|
|
|
|
.as_var()
|
|
|
|
.expect("internal error: missing variable");
|
|
|
|
|
|
|
|
let keyword_expr = call.positional[1]
|
|
|
|
.as_keyword()
|
|
|
|
.expect("internal error: missing keyword");
|
|
|
|
|
2021-07-30 05:42:33 +00:00
|
|
|
let rhs = eval_expression(state, stack.clone(), keyword_expr)?;
|
2021-07-29 22:56:51 +00:00
|
|
|
|
2021-07-30 07:30:11 +00:00
|
|
|
//println!("Adding: {:?} to {}", rhs, var_id);
|
2021-07-29 22:56:51 +00:00
|
|
|
|
2021-07-30 08:06:48 +00:00
|
|
|
stack.add_var(var_id, rhs);
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
|
|
|
} else if decl.signature.name == "let-env" {
|
|
|
|
let env_var = call.positional[0]
|
|
|
|
.as_string()
|
|
|
|
.expect("internal error: missing variable");
|
|
|
|
|
|
|
|
let keyword_expr = call.positional[1]
|
|
|
|
.as_keyword()
|
|
|
|
.expect("internal error: missing keyword");
|
|
|
|
|
|
|
|
let rhs = eval_expression(state, stack.clone(), keyword_expr)?;
|
|
|
|
let rhs = rhs.as_string()?;
|
|
|
|
|
|
|
|
//println!("Adding: {:?} to {}", rhs, var_id);
|
|
|
|
|
|
|
|
stack.add_env_var(env_var, rhs);
|
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
2021-07-29 22:56:51 +00:00
|
|
|
} else if decl.signature.name == "if" {
|
|
|
|
let cond = &call.positional[0];
|
|
|
|
let then_block = call.positional[1]
|
|
|
|
.as_block()
|
|
|
|
.expect("internal error: expected block");
|
|
|
|
let else_case = call.positional.get(2);
|
|
|
|
|
2021-07-30 05:42:33 +00:00
|
|
|
let result = eval_expression(state, stack.clone(), cond)?;
|
2021-07-29 22:56:51 +00:00
|
|
|
match result {
|
2021-08-08 21:02:47 +00:00
|
|
|
Value::Bool { val, span } => {
|
2021-07-29 22:56:51 +00:00
|
|
|
if val {
|
|
|
|
let block = state.parser_state.get_block(then_block);
|
2021-07-30 08:06:48 +00:00
|
|
|
let stack = stack.enter_scope();
|
2021-07-29 22:56:51 +00:00
|
|
|
eval_block(state, stack, block)
|
|
|
|
} else if let Some(else_case) = else_case {
|
|
|
|
if let Some(else_expr) = else_case.as_keyword() {
|
|
|
|
if let Some(block_id) = else_expr.as_block() {
|
|
|
|
let block = state.parser_state.get_block(block_id);
|
2021-07-30 08:06:48 +00:00
|
|
|
let stack = stack.enter_scope();
|
2021-07-29 22:56:51 +00:00
|
|
|
eval_block(state, stack, block)
|
2021-07-24 06:44:38 +00:00
|
|
|
} else {
|
2021-07-29 22:56:51 +00:00
|
|
|
eval_expression(state, stack, else_expr)
|
2021-07-24 06:44:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-29 22:56:51 +00:00
|
|
|
eval_expression(state, stack, else_case)
|
2021-07-24 06:44:38 +00:00
|
|
|
}
|
2021-07-29 22:56:51 +00:00
|
|
|
} else {
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing { span })
|
2021-07-24 06:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-29 22:56:51 +00:00
|
|
|
_ => Err(ShellError::Mismatch("bool".into(), Span::unknown())),
|
2021-07-23 21:46:55 +00:00
|
|
|
}
|
2021-07-30 03:26:06 +00:00
|
|
|
} else if decl.signature.name == "build-string" {
|
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
for expr in &call.positional {
|
2021-07-30 05:42:33 +00:00
|
|
|
let val = eval_expression(state, stack.clone(), expr)?;
|
2021-07-30 03:26:06 +00:00
|
|
|
|
|
|
|
output.push(val.to_string());
|
|
|
|
}
|
|
|
|
Ok(Value::String {
|
|
|
|
val: output.join(""),
|
|
|
|
span: call.head,
|
|
|
|
})
|
2021-07-30 06:10:40 +00:00
|
|
|
} else if decl.signature.name == "benchmark" {
|
|
|
|
let block = call.positional[0]
|
|
|
|
.as_block()
|
|
|
|
.expect("internal error: expected block");
|
|
|
|
let block = state.parser_state.get_block(block);
|
|
|
|
|
2021-07-30 08:06:48 +00:00
|
|
|
let stack = stack.enter_scope();
|
2021-07-30 06:10:40 +00:00
|
|
|
let start_time = Instant::now();
|
|
|
|
eval_block(state, stack, block)?;
|
|
|
|
let end_time = Instant::now();
|
|
|
|
println!("{} ms", (end_time - start_time).as_millis());
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
2021-07-30 06:10:40 +00:00
|
|
|
} else if decl.signature.name == "for" {
|
|
|
|
let var_id = call.positional[0]
|
|
|
|
.as_var()
|
|
|
|
.expect("internal error: missing variable");
|
|
|
|
|
|
|
|
let keyword_expr = call.positional[1]
|
|
|
|
.as_keyword()
|
|
|
|
.expect("internal error: missing keyword");
|
|
|
|
let end_val = eval_expression(state, stack.clone(), keyword_expr)?;
|
|
|
|
|
|
|
|
let block = call.positional[2]
|
|
|
|
.as_block()
|
|
|
|
.expect("internal error: expected block");
|
|
|
|
let block = state.parser_state.get_block(block);
|
|
|
|
|
2021-07-30 08:06:48 +00:00
|
|
|
let stack = stack.enter_scope();
|
2021-07-30 06:10:40 +00:00
|
|
|
|
2021-07-30 07:50:39 +00:00
|
|
|
let mut x = Value::Int {
|
|
|
|
val: 0,
|
|
|
|
span: Span::unknown(),
|
|
|
|
};
|
2021-07-30 06:10:40 +00:00
|
|
|
|
|
|
|
loop {
|
2021-07-30 07:50:39 +00:00
|
|
|
if x == end_val {
|
2021-07-30 06:10:40 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2021-07-30 08:06:48 +00:00
|
|
|
stack.add_var(var_id, x.clone());
|
2021-07-30 07:30:11 +00:00
|
|
|
eval_block(state, stack.clone(), block)?;
|
2021-07-30 07:50:39 +00:00
|
|
|
}
|
|
|
|
if let Value::Int { ref mut val, .. } = x {
|
|
|
|
*val += 1
|
2021-07-30 06:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
2021-08-08 21:34:21 +00:00
|
|
|
} else if decl.signature.name == "vars" {
|
|
|
|
state.parser_state.print_vars();
|
|
|
|
Ok(Value::Nothing {
|
|
|
|
span: call.positional[0].span,
|
|
|
|
})
|
|
|
|
} else if decl.signature.name == "decls" {
|
|
|
|
state.parser_state.print_decls();
|
|
|
|
Ok(Value::Nothing { span: call.head })
|
|
|
|
} else if decl.signature.name == "blocks" {
|
|
|
|
state.parser_state.print_blocks();
|
|
|
|
Ok(Value::Nothing { span: call.head })
|
|
|
|
} else if decl.signature.name == "stack" {
|
|
|
|
stack.print_stack();
|
|
|
|
Ok(Value::Nothing { span: call.head })
|
2021-08-10 05:08:10 +00:00
|
|
|
} else if decl.signature.name == "def" || decl.signature.name == "alias" {
|
2021-08-09 00:19:07 +00:00
|
|
|
Ok(Value::Nothing { span: call.head })
|
2021-07-29 22:56:51 +00:00
|
|
|
} else {
|
2021-08-08 21:02:47 +00:00
|
|
|
Err(ShellError::Unsupported(call.head))
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
pub fn eval_expression(
|
|
|
|
state: &State,
|
2021-07-30 05:42:33 +00:00
|
|
|
stack: Stack,
|
2021-07-23 05:14:49 +00:00
|
|
|
expr: &Expression,
|
|
|
|
) -> Result<Value, ShellError> {
|
|
|
|
match &expr.expr {
|
2021-07-24 05:57:17 +00:00
|
|
|
Expr::Bool(b) => Ok(Value::Bool {
|
|
|
|
val: *b,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Int(i) => Ok(Value::Int {
|
|
|
|
val: *i,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 20:21:21 +00:00
|
|
|
Expr::Float(f) => Ok(Value::Float {
|
|
|
|
val: *f,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-30 21:57:22 +00:00
|
|
|
Expr::Var(var_id) => stack
|
|
|
|
.get_var(*var_id)
|
|
|
|
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Call(call) => eval_call(state, stack, call),
|
|
|
|
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
2021-07-30 05:42:33 +00:00
|
|
|
let lhs = eval_expression(state, stack.clone(), lhs)?;
|
|
|
|
let op = eval_operator(state, stack.clone(), op)?;
|
2021-07-29 22:56:51 +00:00
|
|
|
let rhs = eval_expression(state, stack, rhs)?;
|
2021-07-23 05:14:49 +00:00
|
|
|
|
|
|
|
match op {
|
|
|
|
Operator::Plus => lhs.add(&rhs),
|
2021-08-08 21:02:47 +00:00
|
|
|
_ => Ok(Value::Nothing { span: expr.span }),
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
|
|
|
|
Expr::Subexpression(block_id) => {
|
|
|
|
let block = state.parser_state.get_block(*block_id);
|
|
|
|
|
2021-07-30 08:06:48 +00:00
|
|
|
let stack = stack.enter_scope();
|
2021-07-23 05:14:49 +00:00
|
|
|
eval_block(state, stack, block)
|
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Block(block_id) => Ok(Value::Block {
|
|
|
|
val: *block_id,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::List(x) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for expr in x {
|
2021-07-30 05:42:33 +00:00
|
|
|
output.push(eval_expression(state, stack.clone(), expr)?);
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::List {
|
|
|
|
val: output,
|
|
|
|
span: expr.span,
|
|
|
|
})
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Table(_, _) => Err(ShellError::Unsupported(expr.span)),
|
2021-07-30 04:38:41 +00:00
|
|
|
Expr::Keyword(_, _, expr) => eval_expression(state, stack, expr),
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::String(s) => Ok(Value::String {
|
|
|
|
val: s.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Signature(_) => Ok(Value::Nothing { span: expr.span }),
|
|
|
|
Expr::Garbage => Ok(Value::Nothing { span: expr.span }),
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 05:42:33 +00:00
|
|
|
pub fn eval_block(state: &State, stack: Stack, block: &Block) -> Result<Value, ShellError> {
|
2021-08-08 21:02:47 +00:00
|
|
|
let mut last = Ok(Value::Nothing {
|
|
|
|
span: Span { start: 0, end: 0 },
|
|
|
|
});
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
for stmt in &block.stmts {
|
2021-07-29 22:56:51 +00:00
|
|
|
if let Statement::Expression(expression) = stmt {
|
2021-07-30 05:42:33 +00:00
|
|
|
last = Ok(eval_expression(state, stack.clone(), expression)?);
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
|
|
|
|
last
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|