2021-05-20 18:18:01 -05:00
|
|
|
//! Expression evaluator and statement interpreter.
|
|
|
|
//!
|
|
|
|
//! To interpret a piece of AbleScript code, you first need to
|
2021-05-25 13:26:01 -05:00
|
|
|
//! construct an [ExecEnv], which is responsible for storing the stack
|
|
|
|
//! of local variable and function definitions accessible from an
|
|
|
|
//! AbleScript snippet. You can then call [ExecEnv::eval_items] to
|
|
|
|
//! evaluate or execute any number of expressions or statements.
|
2021-05-20 18:18:01 -05:00
|
|
|
|
|
|
|
#[deny(missing_docs)]
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
error::{Error, ErrorKind},
|
|
|
|
parser::item::{Expr, Iden, Item, Stmt},
|
|
|
|
variables::{Value, Variable},
|
|
|
|
};
|
|
|
|
|
2021-05-25 13:26:01 -05:00
|
|
|
/// An environment for executing AbleScript code.
|
|
|
|
pub struct ExecEnv {
|
|
|
|
/// The stack, ordered such that `stack[stack.len() - 1]` is the
|
|
|
|
/// top-most (newest) stack frame, and `stack[0]` is the
|
|
|
|
/// bottom-most (oldest) stack frame.
|
|
|
|
stack: Vec<Scope>,
|
|
|
|
}
|
|
|
|
|
2021-05-20 18:18:01 -05:00
|
|
|
/// A set of visible variable and function definitions, which serves
|
|
|
|
/// as a context in which expressions can be evaluated.
|
2021-05-25 13:26:01 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Scope {
|
2021-05-20 18:18:01 -05:00
|
|
|
/// The mapping from variable names to values.
|
|
|
|
variables: HashMap<String, Variable>,
|
2021-05-25 13:26:01 -05:00
|
|
|
// In the future, this will store functio definitions and possibly
|
|
|
|
// other information.
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
|
|
|
|
2021-05-25 21:22:38 -05:00
|
|
|
/// The result of successfully executing a set of statements.
|
|
|
|
enum ControlFlow {
|
|
|
|
/// The statements evaluated to this value.
|
|
|
|
Value(Value),
|
|
|
|
|
|
|
|
/// A "break" statement occurred at the top level.
|
|
|
|
Break,
|
|
|
|
|
|
|
|
/// A "hopback" statement occurred at the top level.
|
|
|
|
Hopback,
|
|
|
|
}
|
|
|
|
|
2021-05-25 13:26:01 -05:00
|
|
|
impl ExecEnv {
|
2021-05-20 18:18:01 -05:00
|
|
|
/// Create a new Scope with no predefined variable definitions or
|
|
|
|
/// other information.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2021-05-25 13:26:01 -05:00
|
|
|
stack: Default::default(),
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 13:26:01 -05:00
|
|
|
/// Evaluate a set of Items in their own stack frame. Return the
|
|
|
|
/// value of the last Item evaluated, or an error if one or more
|
|
|
|
/// of the Items failed to evaluate.
|
2021-05-20 18:18:01 -05:00
|
|
|
pub fn eval_items(&mut self, items: &[Item]) -> Result<Value, Error> {
|
2021-05-25 21:22:38 -05:00
|
|
|
match self.eval_items_cf(items)? {
|
|
|
|
ControlFlow::Value(v) => Ok(v),
|
|
|
|
ControlFlow::Break | ControlFlow::Hopback => Err(Error {
|
|
|
|
// It's an error to issue a `break` outside of a
|
|
|
|
// `loop` statement.
|
|
|
|
kind: ErrorKind::TopLevelBreak,
|
|
|
|
position: 0..0,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The same as `eval_items`, but reports "break" and "hopback"
|
|
|
|
/// exit codes as normal conditions in a ControlFlow enum.
|
|
|
|
fn eval_items_cf(&mut self, items: &[Item]) -> Result<ControlFlow, Error> {
|
2021-05-25 13:26:01 -05:00
|
|
|
let init_depth = self.stack.len();
|
|
|
|
|
|
|
|
self.stack.push(Default::default());
|
2021-05-25 21:22:38 -05:00
|
|
|
let mut final_result = Ok(ControlFlow::Value(Value::Nul));
|
|
|
|
for item in items {
|
|
|
|
final_result = self.eval_item(item);
|
|
|
|
if !matches!(final_result, Ok(ControlFlow::Value(_))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 13:26:01 -05:00
|
|
|
self.stack.pop();
|
|
|
|
|
|
|
|
// Invariant: stack size must have net 0 change.
|
|
|
|
debug_assert_eq!(self.stack.len(), init_depth);
|
2021-05-25 21:22:38 -05:00
|
|
|
final_result
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate a single Item, returning its value or an error.
|
2021-05-25 21:22:38 -05:00
|
|
|
fn eval_item(&mut self, item: &Item) -> Result<ControlFlow, Error> {
|
2021-05-20 18:18:01 -05:00
|
|
|
match item {
|
2021-05-25 21:22:38 -05:00
|
|
|
Item::Expr(expr) => self.eval_expr(expr).map(|v| ControlFlow::Value(v)),
|
|
|
|
Item::Stmt(stmt) => self.eval_stmt(stmt),
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate an Expr, returning its value or an error.
|
|
|
|
fn eval_expr(&self, expr: &Expr) -> Result<Value, Error> {
|
|
|
|
use Expr::*;
|
|
|
|
use Value::*;
|
|
|
|
|
|
|
|
// NOTE(Alex): This is quite nasty, and should probably be
|
|
|
|
// re-done using macros or something.
|
|
|
|
Ok(match expr {
|
|
|
|
Add { left, right } => {
|
|
|
|
Int(i32::try_from(self.eval_expr(left)?)? + i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
}
|
|
|
|
Subtract { left, right } => {
|
|
|
|
Int(i32::try_from(self.eval_expr(left)?)? - i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
}
|
|
|
|
Multiply { left, right } => {
|
|
|
|
Int(i32::try_from(self.eval_expr(left)?)? * i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
}
|
|
|
|
Divide { left, right } => {
|
|
|
|
Int(i32::try_from(self.eval_expr(left)?)? / i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
}
|
|
|
|
Lt { left, right } => {
|
|
|
|
Bool(i32::try_from(self.eval_expr(left)?)? < i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
}
|
|
|
|
Gt { left, right } => {
|
|
|
|
Bool(i32::try_from(self.eval_expr(left)?)? > i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
}
|
|
|
|
Eq { left, right } => Bool(self.eval_expr(left)? == self.eval_expr(right)?),
|
|
|
|
Neq { left, right } => Bool(self.eval_expr(left)? != self.eval_expr(right)?),
|
2021-05-23 18:46:42 -05:00
|
|
|
And { left, right } => {
|
|
|
|
Bool(bool::from(self.eval_expr(left)?) && bool::from(self.eval_expr(right)?))
|
|
|
|
}
|
|
|
|
Or { left, right } => {
|
|
|
|
Bool(bool::from(self.eval_expr(left)?) || bool::from(self.eval_expr(right)?))
|
|
|
|
}
|
|
|
|
Not(expr) => Bool(!bool::from(self.eval_expr(expr)?)),
|
2021-05-20 18:18:01 -05:00
|
|
|
Literal(value) => value.clone(),
|
2021-05-25 13:26:01 -05:00
|
|
|
Identifier(Iden(name)) => self.get_var(name)?.value.clone(),
|
2021-05-20 18:18:01 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform the action indicated by a statement.
|
2021-05-25 21:22:38 -05:00
|
|
|
fn eval_stmt(&mut self, stmt: &Stmt) -> Result<ControlFlow, Error> {
|
2021-05-20 18:18:01 -05:00
|
|
|
match stmt {
|
|
|
|
Stmt::Print(expr) => {
|
|
|
|
println!("{}", self.eval_expr(expr)?);
|
|
|
|
}
|
2021-05-23 18:46:42 -05:00
|
|
|
Stmt::VariableDeclaration { iden, init } => {
|
2021-05-25 13:26:01 -05:00
|
|
|
let init = match init {
|
|
|
|
Some(e) => self.eval_expr(e)?,
|
|
|
|
None => Value::Nul,
|
|
|
|
};
|
|
|
|
|
|
|
|
// There's always at least one stack frame on the
|
|
|
|
// stack if we're evaluating something, so we can
|
|
|
|
// `unwrap` here.
|
|
|
|
self.stack.iter_mut().last().unwrap().variables.insert(
|
2021-05-23 18:46:42 -05:00
|
|
|
iden.0.clone(),
|
|
|
|
Variable {
|
|
|
|
melo: false,
|
2021-05-25 13:26:01 -05:00
|
|
|
value: init,
|
2021-05-23 18:46:42 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Stmt::FunctionDeclaration {
|
|
|
|
iden: _,
|
|
|
|
args: _,
|
|
|
|
body: _,
|
|
|
|
} => todo!(),
|
|
|
|
Stmt::BfFDeclaration { iden: _, body: _ } => todo!(),
|
|
|
|
Stmt::If { cond, body } => {
|
|
|
|
if self.eval_expr(cond)?.into() {
|
2021-05-25 21:22:38 -05:00
|
|
|
return self.eval_items_cf(body);
|
2021-05-23 18:46:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Stmt::FunctionCall { iden: _, args: _ } => todo!(),
|
2021-05-25 21:22:38 -05:00
|
|
|
Stmt::Loop { body } => loop {
|
|
|
|
let res = self.eval_items_cf(body)?;
|
|
|
|
match res {
|
|
|
|
ControlFlow::Value(_) => {}
|
|
|
|
ControlFlow::Break => break,
|
|
|
|
ControlFlow::Hopback => continue,
|
2021-05-23 18:46:42 -05:00
|
|
|
}
|
2021-05-25 21:22:38 -05:00
|
|
|
},
|
2021-05-23 18:46:42 -05:00
|
|
|
Stmt::VarAssignment { iden, value } => {
|
2021-05-25 13:26:01 -05:00
|
|
|
self.get_var_mut(&iden.0)?.value = self.eval_expr(value)?;
|
2021-05-23 18:46:42 -05:00
|
|
|
}
|
2021-05-25 21:22:38 -05:00
|
|
|
Stmt::Break => {
|
|
|
|
return Ok(ControlFlow::Break);
|
|
|
|
}
|
|
|
|
Stmt::HopBack => {
|
|
|
|
return Ok(ControlFlow::Hopback);
|
|
|
|
}
|
2021-05-23 18:46:42 -05:00
|
|
|
Stmt::Melo(iden) => {
|
2021-05-25 13:26:01 -05:00
|
|
|
self.get_var_mut(&iden.0)?.melo = true;
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
|
|
|
}
|
2021-05-23 18:46:42 -05:00
|
|
|
|
2021-05-25 21:22:38 -05:00
|
|
|
Ok(ControlFlow::Value(Value::Nul))
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
2021-05-25 13:26:01 -05:00
|
|
|
|
|
|
|
/// Get a shared reference to the value of a variable. Throw an
|
|
|
|
/// error if the variable is inaccessible or banned.
|
|
|
|
fn get_var(&self, name: &str) -> Result<&Variable, Error> {
|
|
|
|
match self
|
|
|
|
.stack
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find_map(|scope| scope.variables.get(name))
|
|
|
|
{
|
|
|
|
Some(var) => {
|
|
|
|
if !var.melo {
|
|
|
|
Ok(var)
|
|
|
|
} else {
|
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::MeloVariable(name.to_owned()),
|
|
|
|
// TODO: figure out some way to avoid this
|
|
|
|
// 0..0 dumbness
|
|
|
|
position: 0..0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(Error {
|
|
|
|
kind: ErrorKind::UnknownVariable(name.to_owned()),
|
|
|
|
position: 0..0,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable reference to a variable. Throw an error if the
|
|
|
|
/// variable is inaccessible or banned.
|
|
|
|
fn get_var_mut(&mut self, name: &str) -> Result<&mut Variable, Error> {
|
|
|
|
// FIXME: This function is almost exactly the same as get_var.
|
|
|
|
match self
|
|
|
|
.stack
|
|
|
|
.iter_mut()
|
|
|
|
.rev()
|
|
|
|
.find_map(|scope| scope.variables.get_mut(name))
|
|
|
|
{
|
|
|
|
Some(var) => {
|
|
|
|
if !var.melo {
|
|
|
|
Ok(var)
|
|
|
|
} else {
|
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::MeloVariable(name.to_owned()),
|
|
|
|
position: 0..0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(Error {
|
|
|
|
kind: ErrorKind::UnknownVariable(name.to_owned()),
|
|
|
|
position: 0..0,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|