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::{
|
2021-05-26 21:30:12 -05:00
|
|
|
base_55,
|
2021-05-20 18:18:01 -05:00
|
|
|
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-25 21:55:02 -05:00
|
|
|
/// A set of visible variable and function definitions in a single
|
|
|
|
/// stack frame.
|
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:55:02 -05:00
|
|
|
/// The reason a successful series of statements halted.
|
|
|
|
enum HaltStatus {
|
|
|
|
/// The last statement in the list evaluated to this value.
|
2021-05-25 21:22:38 -05:00
|
|
|
Value(Value),
|
|
|
|
|
2021-05-25 21:55:02 -05:00
|
|
|
/// A `break` statement occurred and was not caught by a `loop`
|
|
|
|
/// statement.
|
2021-05-25 21:22:38 -05:00
|
|
|
Break,
|
|
|
|
|
2021-05-25 21:55:02 -05:00
|
|
|
/// A `hopback` statement occurred and was not caught by a `loop`
|
|
|
|
/// statement.
|
2021-05-25 21:22:38 -05:00
|
|
|
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
|
2021-05-25 21:55:02 -05:00
|
|
|
/// of the Items failed to evaluate or if a `break` or `hopback`
|
|
|
|
/// statement occurred at the top level.
|
2021-05-20 18:18:01 -05:00
|
|
|
pub fn eval_items(&mut self, items: &[Item]) -> Result<Value, Error> {
|
2021-05-25 21:55:02 -05:00
|
|
|
match self.eval_items_hs(items)? {
|
|
|
|
HaltStatus::Value(v) => Ok(v),
|
|
|
|
HaltStatus::Break | HaltStatus::Hopback => Err(Error {
|
2021-05-25 21:22:38 -05:00
|
|
|
// It's an error to issue a `break` outside of a
|
|
|
|
// `loop` statement.
|
|
|
|
kind: ErrorKind::TopLevelBreak,
|
|
|
|
position: 0..0,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 21:55:02 -05:00
|
|
|
/// The same as `eval_items`, but report "break" and "hopback"
|
|
|
|
/// exit codes as normal conditions in a HaltStatus enum.
|
|
|
|
///
|
|
|
|
/// `interpret`-internal code should typically prefer this
|
|
|
|
/// function over `eval_items`.
|
|
|
|
fn eval_items_hs(&mut self, items: &[Item]) -> Result<HaltStatus, Error> {
|
2021-05-25 13:26:01 -05:00
|
|
|
let init_depth = self.stack.len();
|
|
|
|
|
|
|
|
self.stack.push(Default::default());
|
2021-05-25 21:55:02 -05:00
|
|
|
let mut final_result = Ok(HaltStatus::Value(Value::Nul));
|
2021-05-25 21:22:38 -05:00
|
|
|
for item in items {
|
|
|
|
final_result = self.eval_item(item);
|
2021-05-25 21:55:02 -05:00
|
|
|
if !matches!(final_result, Ok(HaltStatus::Value(_))) {
|
2021-05-25 21:22:38 -05:00
|
|
|
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:55:02 -05:00
|
|
|
fn eval_item(&mut self, item: &Item) -> Result<HaltStatus, Error> {
|
2021-05-20 18:18:01 -05:00
|
|
|
match item {
|
2021-05-25 21:55:02 -05:00
|
|
|
Item::Expr(expr) => self.eval_expr(expr).map(|v| HaltStatus::Value(v)),
|
2021-05-25 21:22:38 -05:00
|
|
|
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::*;
|
|
|
|
|
2021-05-30 13:26:10 -05:00
|
|
|
// NOTE(Alex): This is really quite horrible. I think the only
|
|
|
|
// real way to clean it up would be to re-engineer the AST's
|
|
|
|
// representation to be more hierarchical: rather than having
|
|
|
|
// e.g. "Expr::Add" and "Expr::Subtract" each with a "left"
|
|
|
|
// and "right" struct member, have something like
|
|
|
|
// "Expr::Binary { oper: BinOp, left: Box<Expr>, right:
|
|
|
|
// Box<Expr> }". That way we could factor out a whole bunch of
|
|
|
|
// common code here.
|
|
|
|
//
|
|
|
|
// That work should probably wait for Ondra's new parser to
|
|
|
|
// come in, however.
|
2021-05-20 18:18:01 -05:00
|
|
|
Ok(match expr {
|
2021-05-30 13:26:10 -05:00
|
|
|
Add { left, right } => Int(i32::try_from(self.eval_expr(left)?)?
|
|
|
|
.checked_add(i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
.ok_or(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
|
|
|
position: 0..0,
|
|
|
|
})?),
|
|
|
|
Subtract { left, right } => Int(i32::try_from(self.eval_expr(left)?)?
|
|
|
|
.checked_sub(i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
.ok_or(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
|
|
|
position: 0..0,
|
|
|
|
})?),
|
|
|
|
Multiply { left, right } => Int(i32::try_from(self.eval_expr(left)?)?
|
|
|
|
.checked_mul(i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
.ok_or(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
|
|
|
position: 0..0,
|
|
|
|
})?),
|
|
|
|
Divide { left, right } => Int(i32::try_from(self.eval_expr(left)?)?
|
|
|
|
.checked_div(i32::try_from(self.eval_expr(right)?)?)
|
|
|
|
.ok_or(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
|
|
|
position: 0..0,
|
|
|
|
})?),
|
2021-05-20 18:18:01 -05:00
|
|
|
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-26 21:30:12 -05:00
|
|
|
Identifier(Iden(name)) => self.get_var(name)?,
|
2021-05-20 18:18:01 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform the action indicated by a statement.
|
2021-05-25 21:55:02 -05:00
|
|
|
fn eval_stmt(&mut self, stmt: &Stmt) -> Result<HaltStatus, 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:55:02 -05:00
|
|
|
return self.eval_items_hs(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 {
|
2021-05-25 21:55:02 -05:00
|
|
|
let res = self.eval_items_hs(body)?;
|
2021-05-25 21:22:38 -05:00
|
|
|
match res {
|
2021-05-25 21:55:02 -05:00
|
|
|
HaltStatus::Value(_) => {}
|
|
|
|
HaltStatus::Break => break,
|
|
|
|
HaltStatus::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 => {
|
2021-05-25 21:55:02 -05:00
|
|
|
return Ok(HaltStatus::Break);
|
2021-05-25 21:22:38 -05:00
|
|
|
}
|
|
|
|
Stmt::HopBack => {
|
2021-05-25 21:55:02 -05:00
|
|
|
return Ok(HaltStatus::Hopback);
|
2021-05-25 21:22:38 -05:00
|
|
|
}
|
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:55:02 -05:00
|
|
|
Ok(HaltStatus::Value(Value::Nul))
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
2021-05-25 13:26:01 -05:00
|
|
|
|
2021-05-26 21:30:12 -05:00
|
|
|
/// Get the value of a variable. Throw an error if the variable is
|
|
|
|
/// inaccessible or banned.
|
|
|
|
fn get_var(&self, name: &str) -> Result<Value, Error> {
|
|
|
|
// One-letter names are reserved as base55 numbers.
|
|
|
|
let mut chars = name.chars();
|
|
|
|
if let (Some(first), None) = (chars.next(), chars.next()) {
|
|
|
|
return Ok(Value::Int(base_55::char2num(first)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, search for the name in the stack from top to
|
|
|
|
// bottom.
|
2021-05-25 13:26:01 -05:00
|
|
|
match self
|
|
|
|
.stack
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find_map(|scope| scope.variables.get(name))
|
|
|
|
{
|
|
|
|
Some(var) => {
|
|
|
|
if !var.melo {
|
2021-05-26 21:30:12 -05:00
|
|
|
Ok(var.value.clone())
|
2021-05-25 13:26:01 -05:00
|
|
|
} 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> {
|
2021-05-26 21:30:12 -05:00
|
|
|
// This function has a lot of duplicated code with `get_var`,
|
|
|
|
// which I feel like is a bad sign...
|
2021-05-25 13:26:01 -05:00
|
|
|
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
|
|
|
}
|
2021-05-27 10:05:57 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_expression_test() {
|
|
|
|
// Check that 2 + 2 = 4.
|
|
|
|
let mut env = ExecEnv::new();
|
|
|
|
assert_eq!(
|
|
|
|
env.eval_items(&[Item::Expr(Expr::Add {
|
|
|
|
left: Box::new(Expr::Literal(Value::Int(2))),
|
|
|
|
right: Box::new(Expr::Literal(Value::Int(2))),
|
|
|
|
})])
|
|
|
|
.unwrap(),
|
|
|
|
Value::Int(4)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_errors() {
|
|
|
|
// The sum of an integer and a boolean results in a type
|
|
|
|
// error.
|
|
|
|
let mut env = ExecEnv::new();
|
|
|
|
assert!(matches!(
|
|
|
|
env.eval_items(&[Item::Expr(Expr::Add {
|
|
|
|
left: Box::new(Expr::Literal(Value::Int(i32::MAX))),
|
|
|
|
right: Box::new(Expr::Literal(Value::Bool(false))),
|
|
|
|
})]),
|
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::TypeError(_),
|
|
|
|
position: _,
|
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow_should_not_panic() {
|
|
|
|
// Integer overflow should throw a recoverable error instead
|
|
|
|
// of panicking.
|
|
|
|
let mut env = ExecEnv::new();
|
|
|
|
assert!(matches!(
|
|
|
|
env.eval_items(&[Item::Expr(Expr::Add {
|
|
|
|
left: Box::new(Expr::Literal(Value::Int(i32::MAX))),
|
|
|
|
right: Box::new(Expr::Literal(Value::Int(1))),
|
|
|
|
})]),
|
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
|
|
|
position: _,
|
|
|
|
})
|
|
|
|
));
|
2021-05-30 13:24:16 -05:00
|
|
|
|
|
|
|
// And the same for divide by zero.
|
|
|
|
assert!(matches!(
|
|
|
|
env.eval_items(&[Item::Expr(Expr::Divide {
|
|
|
|
left: Box::new(Expr::Literal(Value::Int(1))),
|
|
|
|
right: Box::new(Expr::Literal(Value::Int(0))),
|
|
|
|
})]),
|
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
|
|
|
position: _,
|
|
|
|
})
|
|
|
|
));
|
2021-05-27 10:05:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// From here on out, I'll use this function to parse and run
|
|
|
|
// expressions, because writing out abstract syntax trees by hand
|
|
|
|
// takes forever and is error-prone.
|
|
|
|
fn eval(env: &mut ExecEnv, src: &str) -> Result<Value, Error> {
|
|
|
|
let mut parser = crate::parser::Parser::new(src);
|
|
|
|
|
|
|
|
// We can assume there won't be any syntax errors in the
|
|
|
|
// interpreter tests.
|
|
|
|
let ast = parser.init().unwrap();
|
|
|
|
env.eval_items(&ast)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn variable_decl_and_assignment() {
|
|
|
|
// Declaring and reading from a variable.
|
|
|
|
assert_eq!(
|
|
|
|
eval(&mut ExecEnv::new(), "var foo = 32; foo + 1").unwrap(),
|
|
|
|
Value::Int(33)
|
|
|
|
);
|
|
|
|
|
|
|
|
// It should be possible to overwrite variables as well.
|
|
|
|
assert_eq!(
|
|
|
|
eval(&mut ExecEnv::new(), "var bar = 10; bar = 20; bar").unwrap(),
|
|
|
|
Value::Int(20)
|
|
|
|
);
|
2021-05-30 13:24:16 -05:00
|
|
|
|
|
|
|
// But variable assignment should be illegal when the variable
|
|
|
|
// hasn't been declared in advance.
|
|
|
|
eval(&mut ExecEnv::new(), "baz = 10;").unwrap_err();
|
2021-05-27 10:05:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn variable_persistence() {
|
|
|
|
// Global variables should persist between invocations of
|
|
|
|
// ExecEnv::eval_items().
|
|
|
|
let mut env = ExecEnv::new();
|
|
|
|
eval(&mut env, "var foo = 32;").unwrap();
|
|
|
|
assert_eq!(eval(&mut env, "foo").unwrap(), Value::Int(32));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scope_visibility_rules() {
|
|
|
|
// Declaration and assignment of variables declared in an `if`
|
|
|
|
// statement should have no effect on those declared outside
|
|
|
|
// of it.
|
|
|
|
assert_eq!(
|
|
|
|
eval(
|
|
|
|
&mut ExecEnv::new(),
|
|
|
|
"var foo = 1; if (true) { var foo = 2; foo = 3; } foo"
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
Value::Int(1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|