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-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-07-23 21:19:30 +00:00
|
|
|
String { val: String, span: Span },
|
|
|
|
List(Vec<Value>),
|
|
|
|
Block(BlockId),
|
2021-07-16 01:10:22 +00:00
|
|
|
Unknown,
|
|
|
|
}
|
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,
|
|
|
|
(Value::String { val: lhs, .. }, Value::String { val: rhs, .. }) => lhs == rhs,
|
|
|
|
(Value::List(l1), Value::List(l2)) => l1 == l2,
|
|
|
|
(Value::Block(b1), Value::Block(b2)) => b1 == b2,
|
|
|
|
_ => 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)
|
|
|
|
}
|
|
|
|
Value::String { val, .. } => write!(f, "{}", val),
|
|
|
|
Value::List(..) => write!(f, "<list>"),
|
|
|
|
Value::Block(..) => write!(f, "<block>"),
|
|
|
|
Value::Unknown => write!(f, "<unknown>"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-07-16 01:10:22 +00:00
|
|
|
_ => Ok(Value::Unknown),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 05:14:49 +00:00
|
|
|
pub struct State<'a> {
|
|
|
|
pub parser_state: &'a ParserState,
|
|
|
|
}
|
|
|
|
|
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-07-30 05:42:33 +00:00
|
|
|
pub parent: Option<Stack>,
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 05:42:33 +00:00
|
|
|
pub type Stack = Rc<RefCell<StackFrame>>;
|
|
|
|
|
|
|
|
impl StackFrame {
|
|
|
|
pub fn get_var(this: Stack, var_id: VarId) -> Result<Value, ShellError> {
|
|
|
|
let this = this.borrow();
|
|
|
|
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
|
|
|
_ => {
|
|
|
|
println!("var_id: {}", var_id);
|
|
|
|
Err(ShellError::InternalError("variable not found".into()))
|
|
|
|
}
|
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 05:42:33 +00:00
|
|
|
pub fn add_var(this: Stack, var_id: VarId, value: Value) {
|
|
|
|
let mut this = this.borrow_mut();
|
|
|
|
this.vars.insert(var_id, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enter_scope(this: Stack) -> Stack {
|
|
|
|
Rc::new(RefCell::new(StackFrame {
|
|
|
|
vars: HashMap::new(),
|
|
|
|
parent: Some(this),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_stack(&self) {
|
|
|
|
println!("===frame===");
|
|
|
|
for (var, val) in &self.vars {
|
|
|
|
println!("{}: {:?}", var, val);
|
|
|
|
}
|
|
|
|
if let Some(parent) = &self.parent {
|
|
|
|
parent.borrow().print_stack()
|
|
|
|
}
|
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-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 05:42:33 +00:00
|
|
|
StackFrame::add_var(stack.clone(), 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);
|
2021-07-30 05:42:33 +00:00
|
|
|
let stack = StackFrame::enter_scope(stack);
|
2021-07-23 05:14:49 +00:00
|
|
|
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 05:42:33 +00:00
|
|
|
StackFrame::add_var(stack, var_id, rhs);
|
2021-07-29 22:56:51 +00:00
|
|
|
Ok(Value::Unknown)
|
|
|
|
} 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 {
|
|
|
|
Value::Bool { val, .. } => {
|
|
|
|
if val {
|
|
|
|
let block = state.parser_state.get_block(then_block);
|
2021-07-30 05:42:33 +00:00
|
|
|
let stack = StackFrame::enter_scope(stack);
|
2021-07-29 22:56:51 +00:00
|
|
|
eval_block(state, stack, block)
|
|
|
|
} else if let Some(else_case) = else_case {
|
|
|
|
println!("{:?}", 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 05:42:33 +00:00
|
|
|
let stack = StackFrame::enter_scope(stack);
|
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 {
|
|
|
|
Ok(Value::Unknown)
|
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);
|
|
|
|
|
|
|
|
let stack = StackFrame::enter_scope(stack);
|
|
|
|
let start_time = Instant::now();
|
|
|
|
eval_block(state, stack, block)?;
|
|
|
|
let end_time = Instant::now();
|
|
|
|
println!("{} ms", (end_time - start_time).as_millis());
|
|
|
|
Ok(Value::Unknown)
|
|
|
|
} 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);
|
|
|
|
|
|
|
|
let stack = StackFrame::enter_scope(stack);
|
|
|
|
|
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 07:50:39 +00:00
|
|
|
StackFrame::add_var(stack.clone(), 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
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Value::Unknown)
|
2021-07-29 22:56:51 +00:00
|
|
|
} else {
|
|
|
|
Ok(Value::Unknown)
|
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-07-30 05:42:33 +00:00
|
|
|
Expr::Var(var_id) => StackFrame::get_var(stack, *var_id),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Call(call) => eval_call(state, stack, call),
|
|
|
|
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
|
|
|
|
Expr::Operator(_) => Ok(Value::Unknown),
|
|
|
|
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),
|
|
|
|
_ => Ok(Value::Unknown),
|
|
|
|
}
|
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 05:42:33 +00:00
|
|
|
let stack = StackFrame::enter_scope(stack);
|
2021-07-23 05:14:49 +00:00
|
|
|
eval_block(state, stack, block)
|
|
|
|
}
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::Block(block_id) => Ok(Value::Block(*block_id)),
|
|
|
|
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
|
|
|
}
|
|
|
|
Ok(Value::List(output))
|
|
|
|
}
|
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,
|
|
|
|
}),
|
|
|
|
Expr::Signature(_) => Ok(Value::Unknown),
|
|
|
|
Expr::Garbage => Ok(Value::Unknown),
|
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-07-23 05:14:49 +00:00
|
|
|
let mut last = Ok(Value::Unknown);
|
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
|
|
|
}
|