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
|
2021-06-07 16:18:07 -05:00
|
|
|
//! AbleScript snippet. You can then call [ExecEnv::eval_stmts] to
|
2021-05-25 13:26:01 -05:00
|
|
|
//! evaluate or execute any number of expressions or statements.
|
2021-05-20 18:18:01 -05:00
|
|
|
|
|
|
|
#[deny(missing_docs)]
|
2021-06-02 15:29:31 -05:00
|
|
|
use std::{
|
2021-06-07 19:57:44 -05:00
|
|
|
collections::HashMap,
|
2021-06-02 15:29:31 -05:00
|
|
|
io::{stdout, Write},
|
2021-06-07 19:57:44 -05:00
|
|
|
ops::Range,
|
2021-06-07 16:18:07 -05:00
|
|
|
process::exit,
|
2021-06-07 19:57:44 -05:00
|
|
|
usize,
|
2021-06-02 15:29:31 -05:00
|
|
|
};
|
2021-05-20 18:18:01 -05:00
|
|
|
|
2021-06-07 16:18:07 -05:00
|
|
|
use rand::random;
|
|
|
|
|
2021-05-20 18:18:01 -05:00
|
|
|
use crate::{
|
2021-06-07 19:57:44 -05:00
|
|
|
ast::{Expr, Iden, Stmt, StmtKind},
|
2021-05-26 21:30:12 -05:00
|
|
|
base_55,
|
2021-05-20 18:18:01 -05:00
|
|
|
error::{Error, ErrorKind},
|
2021-06-02 15:29:31 -05:00
|
|
|
variables::{Functio, Value, Variable},
|
2021-05-20 18:18:01 -05:00
|
|
|
};
|
|
|
|
|
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 {
|
2021-06-07 19:57:44 -05:00
|
|
|
/// We ran out of statements to execute.
|
|
|
|
Finished,
|
2021-05-25 21:22:38 -05:00
|
|
|
|
2021-06-07 19:57:44 -05:00
|
|
|
/// A `break` statement occurred at the given span, and was not
|
|
|
|
/// caught by a `loop` statement up to this point.
|
|
|
|
Break(Range<usize>),
|
2021-05-25 21:22:38 -05:00
|
|
|
|
2021-06-07 19:57:44 -05:00
|
|
|
/// A `hopback` statement occurred at the given span, and was not
|
|
|
|
/// caught by a `loop` statement up to this point.
|
|
|
|
Hopback(Range<usize>),
|
2021-05-25 21:22:38 -05:00
|
|
|
}
|
|
|
|
|
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-06-08 19:21:33 -05:00
|
|
|
// We always need at least one stackframe.
|
|
|
|
stack: vec![Default::default()],
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 19:21:33 -05:00
|
|
|
/// Execute a set of Statements in the root stack frame. Return an
|
|
|
|
/// error if one or more of the Stmts failed to evaluate, or if a
|
|
|
|
/// `break` or `hopback` statement occurred at the top level.
|
2021-06-07 19:57:44 -05:00
|
|
|
pub fn eval_stmts(&mut self, stmts: &[Stmt]) -> Result<(), Error> {
|
2021-06-08 19:21:33 -05:00
|
|
|
match self.eval_stmts_hs(stmts, false)? {
|
2021-06-07 19:57:44 -05:00
|
|
|
HaltStatus::Finished => Ok(()),
|
|
|
|
HaltStatus::Break(span) | HaltStatus::Hopback(span) => 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,
|
2021-06-07 19:57:44 -05:00
|
|
|
span: span,
|
2021-05-25 21:22:38 -05:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 16:18:07 -05:00
|
|
|
/// The same as `eval_stmts`, but report "break" and "hopback"
|
2021-06-08 19:21:33 -05:00
|
|
|
/// exit codes as normal conditions in a HaltStatus enum, and
|
|
|
|
/// create a new stack frame if `stackframe` is true.
|
2021-05-25 21:55:02 -05:00
|
|
|
///
|
|
|
|
/// `interpret`-internal code should typically prefer this
|
2021-06-07 16:18:07 -05:00
|
|
|
/// function over `eval_stmts`.
|
2021-06-08 19:21:33 -05:00
|
|
|
fn eval_stmts_hs(&mut self, stmts: &[Stmt], stackframe: bool) -> Result<HaltStatus, Error> {
|
2021-05-25 13:26:01 -05:00
|
|
|
let init_depth = self.stack.len();
|
|
|
|
|
2021-06-08 19:21:33 -05:00
|
|
|
if stackframe {
|
|
|
|
self.stack.push(Default::default());
|
|
|
|
}
|
|
|
|
|
2021-06-07 19:57:44 -05:00
|
|
|
let mut final_result = Ok(HaltStatus::Finished);
|
2021-06-07 16:18:07 -05:00
|
|
|
for stmt in stmts {
|
|
|
|
final_result = self.eval_stmt(stmt);
|
2021-06-07 19:57:44 -05:00
|
|
|
if !matches!(final_result, Ok(HaltStatus::Finished)) {
|
2021-05-25 21:22:38 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 19:21:33 -05:00
|
|
|
|
|
|
|
if stackframe {
|
|
|
|
self.stack.pop();
|
|
|
|
}
|
2021-05-25 13:26:01 -05:00
|
|
|
|
|
|
|
// 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 an Expr, returning its value or an error.
|
|
|
|
fn eval_expr(&self, expr: &Expr) -> Result<Value, Error> {
|
2021-06-07 16:18:07 -05:00
|
|
|
use crate::ast::BinOpKind::*;
|
|
|
|
use crate::ast::ExprKind::*;
|
2021-05-20 18:18:01 -05:00
|
|
|
use Value::*;
|
|
|
|
|
2021-06-07 16:18:07 -05:00
|
|
|
Ok(match &expr.kind {
|
|
|
|
BinOp { lhs, rhs, kind } => {
|
|
|
|
let lhs = self.eval_expr(&lhs)?;
|
|
|
|
let rhs = self.eval_expr(&rhs)?;
|
|
|
|
match kind {
|
2021-06-02 18:41:20 -05:00
|
|
|
// Arithmetic operators.
|
2021-06-07 16:18:07 -05:00
|
|
|
Add | Subtract | Multiply | Divide => {
|
2021-06-07 19:57:44 -05:00
|
|
|
let lhs = lhs.to_i32(&expr.span)?;
|
|
|
|
let rhs = rhs.to_i32(&expr.span)?;
|
2021-06-07 16:18:07 -05:00
|
|
|
|
|
|
|
let res = match kind {
|
|
|
|
Add => lhs.checked_add(rhs),
|
|
|
|
Subtract => lhs.checked_sub(rhs),
|
|
|
|
Multiply => lhs.checked_mul(rhs),
|
|
|
|
Divide => lhs.checked_div(rhs),
|
2021-06-02 18:41:20 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
.ok_or(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
2021-06-07 16:18:07 -05:00
|
|
|
span: expr.span.clone(),
|
2021-06-02 18:41:20 -05:00
|
|
|
})?;
|
|
|
|
Int(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Numeric comparisons.
|
2021-06-07 16:18:07 -05:00
|
|
|
Less | Greater => {
|
2021-06-07 19:57:44 -05:00
|
|
|
let lhs = lhs.to_i32(&expr.span)?;
|
|
|
|
let rhs = rhs.to_i32(&expr.span)?;
|
2021-06-02 18:41:20 -05:00
|
|
|
|
2021-06-07 16:18:07 -05:00
|
|
|
let res = match kind {
|
|
|
|
Less => lhs < rhs,
|
|
|
|
Greater => lhs > rhs,
|
2021-06-02 18:41:20 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
Bool(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// General comparisons.
|
2021-06-07 16:18:07 -05:00
|
|
|
Equal | NotEqual => {
|
|
|
|
let res = match kind {
|
|
|
|
Equal => lhs == rhs,
|
|
|
|
NotEqual => lhs != rhs,
|
2021-06-02 18:41:20 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
Bool(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logical connectives.
|
2021-06-07 16:18:07 -05:00
|
|
|
And | Or => {
|
2021-06-07 19:57:44 -05:00
|
|
|
let lhs = lhs.to_bool();
|
|
|
|
let rhs = rhs.to_bool();
|
2021-06-07 16:18:07 -05:00
|
|
|
let res = match kind {
|
|
|
|
And => lhs && rhs,
|
|
|
|
Or => lhs || rhs,
|
2021-06-02 18:41:20 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
Bool(res)
|
|
|
|
}
|
|
|
|
}
|
2021-05-23 18:46:42 -05:00
|
|
|
}
|
2021-06-07 19:57:44 -05:00
|
|
|
Not(expr) => Bool(!self.eval_expr(&expr)?.to_bool()),
|
2021-05-20 18:18:01 -05:00
|
|
|
Literal(value) => value.clone(),
|
2021-06-07 19:57:44 -05:00
|
|
|
|
|
|
|
// TODO: not too happy with constructing an artificial
|
|
|
|
// Iden here.
|
|
|
|
Variable(name) => self.get_var(&Iden {
|
|
|
|
iden: name.to_owned(),
|
|
|
|
span: expr.span.clone(),
|
|
|
|
})?,
|
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-06-07 16:18:07 -05:00
|
|
|
match &stmt.kind {
|
|
|
|
StmtKind::Print(expr) => {
|
2021-05-20 18:18:01 -05:00
|
|
|
println!("{}", self.eval_expr(expr)?);
|
|
|
|
}
|
2021-06-07 16:18:07 -05:00
|
|
|
StmtKind::Var { iden, init } => {
|
2021-05-25 13:26:01 -05:00
|
|
|
let init = match init {
|
|
|
|
Some(e) => self.eval_expr(e)?,
|
|
|
|
None => Value::Nul,
|
|
|
|
};
|
|
|
|
|
2021-06-07 16:18:07 -05:00
|
|
|
self.decl_var(&iden.iden, init);
|
2021-05-23 18:46:42 -05:00
|
|
|
}
|
2021-06-07 16:18:07 -05:00
|
|
|
StmtKind::Functio {
|
2021-05-23 18:46:42 -05:00
|
|
|
iden: _,
|
|
|
|
args: _,
|
|
|
|
body: _,
|
|
|
|
} => todo!(),
|
2021-06-07 16:18:07 -05:00
|
|
|
// This is missing from StmtKind after the interpreter
|
|
|
|
// rewrite; presumably, parsing is not yet implemented for
|
|
|
|
// it. ~~Alex
|
|
|
|
// StmtKind::BfFDeclaration { iden, body } => {
|
|
|
|
// self.decl_var(
|
|
|
|
// &iden.0,
|
|
|
|
// Value::Functio(Functio::BfFunctio(body.as_bytes().into())),
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
StmtKind::If { cond, body } => {
|
2021-06-07 19:57:44 -05:00
|
|
|
if self.eval_expr(cond)?.to_bool() {
|
2021-06-08 19:21:33 -05:00
|
|
|
return self.eval_stmts_hs(&body.block, true);
|
2021-05-23 18:46:42 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-07 16:18:07 -05:00
|
|
|
StmtKind::Call { iden, args } => {
|
2021-06-07 19:57:44 -05:00
|
|
|
let func = self.get_var(&iden)?;
|
2021-06-02 15:29:31 -05:00
|
|
|
match func {
|
|
|
|
Value::Functio(func) => {
|
|
|
|
match func {
|
|
|
|
Functio::BfFunctio(body) => {
|
|
|
|
let mut input: Vec<u8> = vec![];
|
|
|
|
for arg in args {
|
2021-06-02 18:20:30 -05:00
|
|
|
self.eval_expr(arg)?.bf_write(&mut input);
|
2021-06-02 15:29:31 -05:00
|
|
|
}
|
|
|
|
println!("input = {:?}", input);
|
|
|
|
let mut output = vec![];
|
|
|
|
|
|
|
|
crate::brian::interpret_with_io(&body, &input as &[_], &mut output)
|
|
|
|
.map_err(|e| Error {
|
|
|
|
kind: ErrorKind::BfInterpretError(e),
|
2021-06-07 16:18:07 -05:00
|
|
|
span: stmt.span.clone(),
|
2021-06-02 15:29:31 -05:00
|
|
|
})?;
|
|
|
|
|
|
|
|
// I guess Brainfuck functions write
|
|
|
|
// output to stdout? It's not quite
|
|
|
|
// clear to me what else to do. ~~Alex
|
|
|
|
stdout()
|
|
|
|
.write_all(&output)
|
|
|
|
.expect("Failed to write to stdout");
|
|
|
|
}
|
|
|
|
Functio::AbleFunctio(_) => {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(Error {
|
2021-06-07 16:18:07 -05:00
|
|
|
kind: ErrorKind::TypeError(iden.iden.to_owned()),
|
|
|
|
span: stmt.span.clone(),
|
2021-06-02 15:29:31 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 16:18:07 -05:00
|
|
|
StmtKind::Loop { body } => loop {
|
2021-06-08 19:21:33 -05:00
|
|
|
let res = self.eval_stmts_hs(&body.block, true)?;
|
2021-05-25 21:22:38 -05:00
|
|
|
match res {
|
2021-06-07 19:57:44 -05:00
|
|
|
HaltStatus::Finished => {}
|
|
|
|
HaltStatus::Break(_) => break,
|
|
|
|
HaltStatus::Hopback(_) => continue,
|
2021-05-23 18:46:42 -05:00
|
|
|
}
|
2021-05-25 21:22:38 -05:00
|
|
|
},
|
2021-06-07 16:18:07 -05:00
|
|
|
// This is missing as well. ~~Alex
|
|
|
|
// StmtKind::VarAssignment { iden, value } => {
|
|
|
|
// self.get_var_mut(&iden.0)?.value = self.eval_expr(value)?;
|
|
|
|
// }
|
|
|
|
StmtKind::Break => {
|
2021-06-07 19:57:44 -05:00
|
|
|
return Ok(HaltStatus::Break(stmt.span.clone()));
|
2021-05-25 21:22:38 -05:00
|
|
|
}
|
2021-06-07 16:18:07 -05:00
|
|
|
StmtKind::HopBack => {
|
2021-06-07 19:57:44 -05:00
|
|
|
return Ok(HaltStatus::Hopback(stmt.span.clone()));
|
2021-05-25 21:22:38 -05:00
|
|
|
}
|
2021-06-07 16:18:07 -05:00
|
|
|
StmtKind::Melo(iden) => {
|
2021-06-07 19:57:44 -05:00
|
|
|
self.get_var_mut(&iden)?.melo = true;
|
2021-06-07 16:18:07 -05:00
|
|
|
}
|
|
|
|
StmtKind::Rlyeh => {
|
|
|
|
// Maybe print a creepy error message or something
|
|
|
|
// here at some point. ~~Alex
|
|
|
|
exit(random());
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
|
|
|
}
|
2021-05-23 18:46:42 -05:00
|
|
|
|
2021-06-07 19:57:44 -05:00
|
|
|
Ok(HaltStatus::Finished)
|
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.
|
2021-06-07 19:57:44 -05:00
|
|
|
fn get_var(&self, name: &Iden) -> Result<Value, Error> {
|
2021-05-26 21:30:12 -05:00
|
|
|
// One-letter names are reserved as base55 numbers.
|
2021-06-07 19:57:44 -05:00
|
|
|
let mut chars = name.iden.chars();
|
2021-05-26 21:30:12 -05:00
|
|
|
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()
|
2021-06-07 19:57:44 -05:00
|
|
|
.find_map(|scope| scope.variables.get(&name.iden))
|
2021-05-25 13:26:01 -05:00
|
|
|
{
|
|
|
|
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 {
|
2021-06-07 19:57:44 -05:00
|
|
|
kind: ErrorKind::MeloVariable(name.iden.to_owned()),
|
|
|
|
span: name.span.clone(),
|
2021-05-25 13:26:01 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(Error {
|
2021-06-07 19:57:44 -05:00
|
|
|
kind: ErrorKind::UnknownVariable(name.iden.to_owned()),
|
|
|
|
span: name.span.clone(),
|
2021-05-25 13:26:01 -05:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable reference to a variable. Throw an error if the
|
|
|
|
/// variable is inaccessible or banned.
|
2021-06-07 19:57:44 -05:00
|
|
|
fn get_var_mut(&mut self, name: &Iden) -> 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()
|
2021-06-07 19:57:44 -05:00
|
|
|
.find_map(|scope| scope.variables.get_mut(&name.iden))
|
2021-05-25 13:26:01 -05:00
|
|
|
{
|
|
|
|
Some(var) => {
|
|
|
|
if !var.melo {
|
|
|
|
Ok(var)
|
|
|
|
} else {
|
|
|
|
Err(Error {
|
2021-06-07 19:57:44 -05:00
|
|
|
kind: ErrorKind::MeloVariable(name.iden.to_owned()),
|
|
|
|
span: name.span.clone(),
|
2021-05-25 13:26:01 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(Error {
|
2021-06-07 19:57:44 -05:00
|
|
|
kind: ErrorKind::UnknownVariable(name.iden.to_owned()),
|
|
|
|
span: name.span.clone(),
|
2021-05-25 13:26:01 -05:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2021-06-02 15:29:31 -05:00
|
|
|
|
|
|
|
/// Declares a new variable, with the given initial value.
|
|
|
|
fn decl_var(&mut self, name: &str, value: Value) {
|
|
|
|
self.stack
|
|
|
|
.iter_mut()
|
|
|
|
.last()
|
|
|
|
.expect("Declaring variable on empty stack")
|
|
|
|
.variables
|
|
|
|
.insert(name.to_owned(), Variable { melo: false, value });
|
|
|
|
}
|
2021-05-20 18:18:01 -05:00
|
|
|
}
|
2021-05-27 10:05:57 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-07 16:18:07 -05:00
|
|
|
use crate::ast::ExprKind;
|
|
|
|
|
2021-05-27 10:05:57 -05:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_expression_test() {
|
|
|
|
// Check that 2 + 2 = 4.
|
2021-06-07 17:35:49 -05:00
|
|
|
let env = ExecEnv::new();
|
2021-05-27 10:05:57 -05:00
|
|
|
assert_eq!(
|
2021-06-07 16:18:07 -05:00
|
|
|
env.eval_expr(&Expr {
|
|
|
|
kind: ExprKind::BinOp {
|
|
|
|
lhs: Box::new(Expr {
|
|
|
|
kind: ExprKind::Literal(Value::Int(2)),
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1,
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
|
|
|
rhs: Box::new(Expr {
|
|
|
|
kind: ExprKind::Literal(Value::Int(2)),
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1,
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
|
|
|
kind: crate::ast::BinOpKind::Add,
|
|
|
|
},
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1
|
2021-06-07 16:18:07 -05:00
|
|
|
})
|
2021-05-27 10:05:57 -05:00
|
|
|
.unwrap(),
|
|
|
|
Value::Int(4)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn type_errors() {
|
|
|
|
// The sum of an integer and a boolean results in a type
|
|
|
|
// error.
|
2021-06-07 17:35:49 -05:00
|
|
|
let env = ExecEnv::new();
|
2021-05-27 10:05:57 -05:00
|
|
|
assert!(matches!(
|
2021-06-07 16:18:07 -05:00
|
|
|
env.eval_expr(&Expr {
|
|
|
|
kind: ExprKind::BinOp {
|
|
|
|
lhs: Box::new(Expr {
|
|
|
|
kind: ExprKind::Literal(Value::Int(2)),
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1,
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
|
|
|
rhs: Box::new(Expr {
|
|
|
|
kind: ExprKind::Literal(Value::Bool(true)),
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1,
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
|
|
|
kind: crate::ast::BinOpKind::Add,
|
|
|
|
},
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
2021-05-27 10:05:57 -05:00
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::TypeError(_),
|
2021-06-07 15:21:21 -05:00
|
|
|
span: _,
|
2021-05-27 10:05:57 -05:00
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow_should_not_panic() {
|
|
|
|
// Integer overflow should throw a recoverable error instead
|
|
|
|
// of panicking.
|
2021-06-07 17:35:49 -05:00
|
|
|
let env = ExecEnv::new();
|
2021-05-27 10:05:57 -05:00
|
|
|
assert!(matches!(
|
2021-06-07 16:18:07 -05:00
|
|
|
env.eval_expr(&Expr {
|
|
|
|
kind: ExprKind::BinOp {
|
|
|
|
lhs: Box::new(Expr {
|
|
|
|
kind: ExprKind::Literal(Value::Int(i32::MAX)),
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1,
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
|
|
|
rhs: Box::new(Expr {
|
|
|
|
kind: ExprKind::Literal(Value::Int(1)),
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1,
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
|
|
|
kind: crate::ast::BinOpKind::Add,
|
|
|
|
},
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
2021-05-27 10:05:57 -05:00
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
2021-06-07 15:21:21 -05:00
|
|
|
span: _,
|
2021-05-27 10:05:57 -05:00
|
|
|
})
|
|
|
|
));
|
2021-05-30 13:24:16 -05:00
|
|
|
|
|
|
|
// And the same for divide by zero.
|
|
|
|
assert!(matches!(
|
2021-06-07 16:18:07 -05:00
|
|
|
env.eval_expr(&Expr {
|
|
|
|
kind: ExprKind::BinOp {
|
|
|
|
lhs: Box::new(Expr {
|
|
|
|
kind: ExprKind::Literal(Value::Int(1)),
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1,
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
|
|
|
rhs: Box::new(Expr {
|
|
|
|
kind: ExprKind::Literal(Value::Int(0)),
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1,
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
2021-06-07 17:35:49 -05:00
|
|
|
kind: crate::ast::BinOpKind::Divide,
|
2021-06-07 16:18:07 -05:00
|
|
|
},
|
2021-06-07 19:57:44 -05:00
|
|
|
span: 1..1
|
2021-06-07 16:18:07 -05:00
|
|
|
}),
|
2021-05-30 13:24:16 -05:00
|
|
|
Err(Error {
|
|
|
|
kind: ErrorKind::ArithmeticError,
|
2021-06-07 15:21:21 -05:00
|
|
|
span: _,
|
2021-05-30 13:24:16 -05:00
|
|
|
})
|
|
|
|
));
|
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();
|
2021-06-07 19:57:44 -05:00
|
|
|
env.eval_stmts(&ast).map(|()| Value::Nul)
|
2021-05-27 10:05:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-07 17:35:49 -05:00
|
|
|
#[ignore = "doesn't make sense anymore due to separation of statements & expressions"]
|
2021-05-27 10:05:57 -05:00
|
|
|
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]
|
2021-06-07 17:35:49 -05:00
|
|
|
#[ignore = "doesn't make sense anymore due to separation of statements & expressions"]
|
2021-05-27 10:05:57 -05:00
|
|
|
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]
|
2021-06-07 17:35:49 -05:00
|
|
|
#[ignore = "doesn't make sense anymore due to separation of statements & expressions"]
|
2021-05-27 10:05:57 -05:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|