2023-10-26 19:48:44 +00:00
|
|
|
//! Interprets an AST as a program
|
2024-01-04 08:18:09 +00:00
|
|
|
#![allow(deprecated)] // TODO: REMOVE
|
2023-10-26 19:48:44 +00:00
|
|
|
|
|
|
|
use crate::ast::preamble::*;
|
2023-10-30 04:47:00 +00:00
|
|
|
use error::{Error, IResult};
|
|
|
|
use scope::Environment;
|
2023-10-26 19:48:44 +00:00
|
|
|
use temp_type_impl::ConValue;
|
|
|
|
|
2023-10-30 04:47:00 +00:00
|
|
|
/// Callable types can be called from within a Conlang program
|
|
|
|
pub trait Callable: std::fmt::Debug {
|
|
|
|
/// Calls this [Callable] in the provided [Interpreter], with [ConValue] args \
|
|
|
|
/// The Callable is responsible for checking the argument count and validating types
|
2024-01-04 08:18:09 +00:00
|
|
|
fn call(&self, interpreter: &mut Interpreter, args: &[ConValue]) -> IResult<ConValue>;
|
2023-10-30 04:47:00 +00:00
|
|
|
/// Returns the common name of this identifier.
|
|
|
|
fn name(&self) -> &str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// [BuiltIn]s are [Callable]s with bespoke definitions
|
|
|
|
pub trait BuiltIn: std::fmt::Debug + Callable {}
|
|
|
|
|
2023-10-26 19:48:44 +00:00
|
|
|
pub mod temp_type_impl {
|
2023-10-26 19:51:13 +00:00
|
|
|
//! Temporary implementations of Conlang values
|
2023-10-30 04:47:00 +00:00
|
|
|
//!
|
|
|
|
//! The most permanent fix is a temporary one.
|
|
|
|
use super::{
|
|
|
|
error::{Error, IResult},
|
|
|
|
function::Function,
|
|
|
|
BuiltIn, Callable, Interpreter,
|
|
|
|
};
|
2023-10-26 19:48:44 +00:00
|
|
|
use std::ops::*;
|
|
|
|
/// A Conlang value
|
|
|
|
///
|
2023-10-30 04:47:00 +00:00
|
|
|
/// This is a hack to work around the fact that Conlang doesn't
|
|
|
|
/// have a functioning type system yet :(
|
2023-10-29 06:13:48 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2023-10-26 19:48:44 +00:00
|
|
|
pub enum ConValue {
|
|
|
|
/// The empty/unit `()` type
|
2023-10-29 06:13:48 +00:00
|
|
|
#[default]
|
2023-10-26 19:48:44 +00:00
|
|
|
Empty,
|
|
|
|
/// An integer
|
|
|
|
Int(i128),
|
|
|
|
/// A boolean
|
|
|
|
Bool(bool),
|
|
|
|
/// A unicode character
|
|
|
|
Char(char),
|
|
|
|
/// A string
|
|
|
|
String(String),
|
2023-10-30 04:47:00 +00:00
|
|
|
/// A tuple
|
|
|
|
Tuple(Vec<ConValue>),
|
2023-10-27 02:51:18 +00:00
|
|
|
/// An exclusive range
|
|
|
|
RangeExc(i128, i128),
|
|
|
|
/// An inclusive range
|
|
|
|
RangeInc(i128, i128),
|
2023-10-30 04:47:00 +00:00
|
|
|
/// A callable thing
|
|
|
|
Function(Function),
|
|
|
|
/// A built-in function
|
|
|
|
BuiltIn(&'static dyn BuiltIn),
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
impl ConValue {
|
|
|
|
/// Gets whether the current value is true or false
|
|
|
|
pub fn truthy(&self) -> IResult<bool> {
|
|
|
|
match self {
|
|
|
|
ConValue::Bool(v) => Ok(*v),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?,
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-27 02:51:18 +00:00
|
|
|
pub fn range_exc(self, other: Self) -> IResult<Self> {
|
|
|
|
let (Self::Int(a), Self::Int(b)) = (self, other) else {
|
2023-10-30 04:47:00 +00:00
|
|
|
Err(Error::TypeError)?
|
2023-10-27 02:51:18 +00:00
|
|
|
};
|
|
|
|
Ok(Self::RangeExc(a, b.saturating_sub(1)))
|
|
|
|
}
|
|
|
|
pub fn range_inc(self, other: Self) -> IResult<Self> {
|
|
|
|
let (Self::Int(a), Self::Int(b)) = (self, other) else {
|
2023-10-30 04:47:00 +00:00
|
|
|
Err(Error::TypeError)?
|
2023-10-27 02:51:18 +00:00
|
|
|
};
|
|
|
|
Ok(Self::RangeInc(a, b))
|
|
|
|
}
|
2023-10-26 19:48:44 +00:00
|
|
|
cmp! {
|
|
|
|
lt: false, <;
|
|
|
|
lt_eq: true, <=;
|
|
|
|
eq: true, ==;
|
|
|
|
neq: false, !=;
|
|
|
|
gt_eq: true, >=;
|
|
|
|
gt: false, >;
|
|
|
|
}
|
2023-10-29 06:13:48 +00:00
|
|
|
assign! {
|
|
|
|
add_assign: +;
|
|
|
|
bitand_assign: &;
|
|
|
|
bitor_assign: |;
|
|
|
|
bitxor_assign: ^;
|
|
|
|
div_assign: /;
|
|
|
|
mul_assign: *;
|
|
|
|
rem_assign: %;
|
|
|
|
shl_assign: <<;
|
|
|
|
shr_assign: >>;
|
|
|
|
sub_assign: -;
|
|
|
|
}
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
|
|
|
|
impl Callable for ConValue {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
ConValue::Function(func) => func.name(),
|
|
|
|
ConValue::BuiltIn(func) => func.name(),
|
|
|
|
_ => "",
|
|
|
|
}
|
|
|
|
}
|
2024-01-04 08:18:09 +00:00
|
|
|
fn call(&self, interpreter: &mut Interpreter, args: &[ConValue]) -> IResult<ConValue> {
|
2023-10-30 04:47:00 +00:00
|
|
|
match self {
|
|
|
|
Self::Function(func) => func.call(interpreter, args),
|
|
|
|
Self::BuiltIn(func) => func.call(interpreter, args),
|
|
|
|
_ => Err(Error::NotCallable(self.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 19:48:44 +00:00
|
|
|
/// Templates comparison functions for [ConValue]
|
|
|
|
macro cmp ($($fn:ident: $empty:literal, $op:tt);*$(;)?) {$(
|
|
|
|
/// TODO: Remove when functions are implemented:
|
|
|
|
/// Desugar into function calls
|
|
|
|
pub fn $fn(&self, other: &Self) -> IResult<Self> {
|
|
|
|
match (self, other) {
|
|
|
|
(Self::Empty, Self::Empty) => Ok(Self::Bool($empty)),
|
|
|
|
(Self::Int(a), Self::Int(b)) => Ok(Self::Bool(a $op b)),
|
|
|
|
(Self::Bool(a), Self::Bool(b)) => Ok(Self::Bool(a $op b)),
|
|
|
|
(Self::Char(a), Self::Char(b)) => Ok(Self::Bool(a $op b)),
|
|
|
|
(Self::String(a), Self::String(b)) => Ok(Self::Bool(a $op b)),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)*}
|
2023-10-29 06:13:48 +00:00
|
|
|
macro assign($( $fn: ident: $op: tt );*$(;)?) {$(
|
|
|
|
pub fn $fn(&mut self, other: Self) -> IResult<()> {
|
|
|
|
*self = (std::mem::take(self) $op other)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
)*}
|
2023-10-26 19:48:44 +00:00
|
|
|
/// Implements [From] for an enum with 1-tuple variants
|
|
|
|
macro from ($($T:ty => $v:expr),*$(,)?) {
|
|
|
|
$(impl From<$T> for ConValue {
|
|
|
|
fn from(value: $T) -> Self { $v(value.into()) }
|
|
|
|
})*
|
|
|
|
}
|
|
|
|
from! {
|
|
|
|
i128 => ConValue::Int,
|
|
|
|
bool => ConValue::Bool,
|
|
|
|
char => ConValue::Char,
|
|
|
|
&str => ConValue::String,
|
|
|
|
String => ConValue::String,
|
2023-10-30 04:47:00 +00:00
|
|
|
Function => ConValue::Function,
|
|
|
|
Vec<ConValue> => ConValue::Tuple,
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
impl From<()> for ConValue {
|
|
|
|
fn from(_: ()) -> Self {
|
|
|
|
Self::Empty
|
|
|
|
}
|
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
impl From<&[ConValue]> for ConValue {
|
|
|
|
fn from(value: &[ConValue]) -> Self {
|
|
|
|
match value.len() {
|
|
|
|
0 => Self::Empty,
|
|
|
|
1 => value[0].clone(),
|
|
|
|
_ => Self::Tuple(value.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 19:48:44 +00:00
|
|
|
|
|
|
|
/// Implements binary [std::ops] traits for [ConValue]
|
|
|
|
///
|
|
|
|
/// TODO: Desugar operators into function calls
|
|
|
|
macro ops($($trait:ty: $fn:ident = [$($match:tt)*])*) {
|
|
|
|
$(impl $trait for ConValue {
|
|
|
|
type Output = IResult<Self>;
|
|
|
|
/// TODO: Desugar operators into function calls
|
|
|
|
fn $fn(self, rhs: Self) -> Self::Output {Ok(match (self, rhs) {$($match)*})}
|
|
|
|
})*
|
|
|
|
}
|
|
|
|
ops! {
|
|
|
|
Add: add = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a + b),
|
|
|
|
(ConValue::String(a), ConValue::String(b)) => ConValue::String(a + &b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
BitAnd: bitand = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a & b),
|
|
|
|
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a & b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
BitOr: bitor = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a | b),
|
|
|
|
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a | b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
BitXor: bitxor = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a ^ b),
|
|
|
|
(ConValue::Bool(a), ConValue::Bool(b)) => ConValue::Bool(a ^ b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
Div: div = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a / b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
Mul: mul = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a * b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
Rem: rem = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a % b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
Shl: shl = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a << b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
Shr: shr = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a >> b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
Sub: sub = [
|
|
|
|
(ConValue::Empty, ConValue::Empty) => ConValue::Empty,
|
|
|
|
(ConValue::Int(a), ConValue::Int(b)) => ConValue::Int(a - b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?
|
2023-10-26 19:48:44 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
impl Neg for ConValue {
|
|
|
|
type Output = IResult<Self>;
|
|
|
|
fn neg(self) -> Self::Output {
|
|
|
|
Ok(match self {
|
|
|
|
ConValue::Empty => ConValue::Empty,
|
|
|
|
ConValue::Int(v) => ConValue::Int(-v),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?,
|
2023-10-26 19:48:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Not for ConValue {
|
|
|
|
type Output = IResult<Self>;
|
|
|
|
fn not(self) -> Self::Output {
|
|
|
|
Ok(match self {
|
|
|
|
ConValue::Empty => ConValue::Empty,
|
|
|
|
ConValue::Int(v) => ConValue::Int(!v),
|
|
|
|
ConValue::Bool(v) => ConValue::Bool(!v),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?,
|
2023-10-26 19:48:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::fmt::Display for ConValue {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
ConValue::Empty => "Empty".fmt(f),
|
|
|
|
ConValue::Int(v) => v.fmt(f),
|
|
|
|
ConValue::Bool(v) => v.fmt(f),
|
2023-10-30 04:47:00 +00:00
|
|
|
ConValue::Char(v) => v.fmt(f),
|
|
|
|
ConValue::String(v) => v.fmt(f),
|
2023-10-27 02:51:18 +00:00
|
|
|
ConValue::RangeExc(a, b) => write!(f, "{a}..{}", b + 1),
|
|
|
|
ConValue::RangeInc(a, b) => write!(f, "{a}..={b}"),
|
2023-10-30 04:47:00 +00:00
|
|
|
ConValue::Tuple(tuple) => {
|
|
|
|
'('.fmt(f)?;
|
|
|
|
for (idx, element) in tuple.iter().enumerate() {
|
|
|
|
if idx > 0 {
|
|
|
|
", ".fmt(f)?
|
|
|
|
}
|
|
|
|
element.fmt(f)?
|
|
|
|
}
|
|
|
|
')'.fmt(f)
|
|
|
|
}
|
|
|
|
ConValue::Function(func) => {
|
|
|
|
write!(f, "fn {}", func.name())
|
|
|
|
}
|
|
|
|
ConValue::BuiltIn(func) => {
|
|
|
|
write!(f, "internal fn {}", func.name())
|
|
|
|
}
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A work-in-progress tree walk interpreter for Conlang
|
2023-10-30 04:47:00 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2023-10-26 19:48:44 +00:00
|
|
|
pub struct Interpreter {
|
2023-10-29 06:13:48 +00:00
|
|
|
scope: Box<Environment>,
|
2023-10-26 19:48:44 +00:00
|
|
|
stack: Vec<ConValue>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Interpreter {
|
|
|
|
/// Creates a new [Interpreter]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
/// Interprets the [Start] of a syntax tree
|
|
|
|
pub fn interpret(&mut self, start: &Start) -> IResult<()> {
|
|
|
|
self.visit(start)
|
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
/// Calls a function inside the interpreter's scope,
|
|
|
|
/// and returns the result
|
|
|
|
pub fn call(&mut self, name: &str, args: &[ConValue]) -> IResult<ConValue> {
|
|
|
|
let function = self.resolve(name)?;
|
|
|
|
function.call(self, args)?;
|
|
|
|
self.pop()
|
|
|
|
}
|
2023-10-27 02:51:18 +00:00
|
|
|
/// Evaluates a single [Expression](expression::Expr) and returns the value stack.
|
|
|
|
pub fn eval(&mut self, expr: &expression::Expr) -> IResult<Vec<ConValue>> {
|
2023-10-26 19:48:44 +00:00
|
|
|
self.visit_expr(expr)?;
|
2023-10-27 02:51:18 +00:00
|
|
|
Ok(std::mem::take(&mut self.stack))
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
fn push(&mut self, value: impl Into<ConValue>) {
|
|
|
|
self.stack.push(value.into())
|
|
|
|
}
|
|
|
|
fn peek(&mut self) -> IResult<&ConValue> {
|
2023-10-30 04:47:00 +00:00
|
|
|
self.stack.last().ok_or(Error::StackUnderflow)
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
fn pop(&mut self) -> IResult<ConValue> {
|
2023-10-30 04:47:00 +00:00
|
|
|
self.stack.pop().ok_or(Error::StackUnderflow)
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
fn pop_two(&mut self) -> IResult<(ConValue, ConValue)> {
|
|
|
|
Ok((self.pop()?, self.pop()?))
|
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
fn resolve(&mut self, value: &str) -> IResult<ConValue> {
|
|
|
|
self.scope.get(value).cloned()
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<IResult<()>> for Interpreter {
|
|
|
|
fn visit_program(&mut self, prog: &Program) -> IResult<()> {
|
|
|
|
for stmt in &prog.0 {
|
|
|
|
self.visit_statement(stmt)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, stmt: &Stmt) -> IResult<()> {
|
|
|
|
match stmt {
|
2023-10-29 06:13:48 +00:00
|
|
|
Stmt::Let(l) => self.visit_let(l),
|
2023-10-30 04:47:00 +00:00
|
|
|
Stmt::Fn(f) => self.visit_fn_decl(f),
|
2023-10-26 19:48:44 +00:00
|
|
|
Stmt::Expr(e) => {
|
|
|
|
self.visit_expr(e)?;
|
|
|
|
self.pop().map(drop)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
fn visit_let(&mut self, stmt: &Let) -> IResult<()> {
|
2024-01-04 08:18:09 +00:00
|
|
|
let Let { name: Name { name: Identifier { name, .. }, .. }, init, .. } = stmt;
|
2023-10-29 06:13:48 +00:00
|
|
|
if let Some(init) = init {
|
|
|
|
self.visit_expr(init)?;
|
|
|
|
let init = self.pop()?;
|
|
|
|
self.scope.insert(name, Some(init));
|
|
|
|
} else {
|
|
|
|
self.scope.insert(name, None);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-30 04:47:00 +00:00
|
|
|
fn visit_fn_decl(&mut self, function: &FnDecl) -> IResult<()> {
|
|
|
|
// register the function in the current environment
|
|
|
|
self.scope.insert_fn(function);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
fn visit_block(&mut self, block: &expression::Block) -> IResult<()> {
|
|
|
|
for stmt in &block.statements {
|
|
|
|
self.visit_statement(stmt)?;
|
|
|
|
}
|
|
|
|
if let Some(expr) = block.expr.as_ref() {
|
|
|
|
self.visit_expr(expr)
|
|
|
|
} else {
|
|
|
|
self.push(ConValue::Empty);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 04:47:00 +00:00
|
|
|
fn visit_tuple(&mut self, tuple: &Tuple) -> IResult<()> {
|
|
|
|
let mut out = vec![];
|
|
|
|
for expr in &tuple.elements {
|
|
|
|
self.visit_expr(expr)?;
|
|
|
|
out.push(self.pop()?);
|
|
|
|
}
|
|
|
|
self.push(out);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_fn_call(&mut self, call: &FnCall) -> IResult<()> {
|
|
|
|
// evaluate the callee
|
|
|
|
self.visit_primary(&call.callee)?;
|
|
|
|
for args in &call.args {
|
|
|
|
self.visit_tuple(args)?;
|
|
|
|
let (ConValue::Tuple(args), callee) = self.pop_two()? else {
|
|
|
|
Err(Error::TypeError)?
|
|
|
|
};
|
2024-01-04 08:18:09 +00:00
|
|
|
let return_value = callee.call(self, &args)?;
|
|
|
|
self.push(return_value);
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
fn visit_assign(&mut self, assign: &math::Assign) -> IResult<()> {
|
|
|
|
use operator::Assign;
|
|
|
|
let math::Assign { target, operator, init } = assign;
|
|
|
|
self.visit_operation(init)?;
|
|
|
|
let init = self.pop()?;
|
2024-01-04 08:18:09 +00:00
|
|
|
let resolved = self.scope.get_mut(&target.name)?;
|
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
if let Assign::Assign = operator {
|
|
|
|
use std::mem::discriminant as variant;
|
|
|
|
// runtime typecheck
|
|
|
|
match resolved.as_mut() {
|
|
|
|
Some(value) if variant(value) == variant(&init) => {
|
|
|
|
*value = init;
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
2023-10-29 06:13:48 +00:00
|
|
|
None => *resolved = Some(init),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::TypeError)?,
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
2023-10-29 06:13:48 +00:00
|
|
|
self.push(ConValue::Empty);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
let Some(target) = resolved.as_mut() else {
|
2024-01-04 08:18:09 +00:00
|
|
|
Err(Error::NotInitialized(target.name.to_owned()))?
|
2023-10-29 06:13:48 +00:00
|
|
|
};
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
match operator {
|
|
|
|
Assign::AddAssign => target.add_assign(init)?,
|
|
|
|
Assign::SubAssign => target.sub_assign(init)?,
|
|
|
|
Assign::MulAssign => target.mul_assign(init)?,
|
|
|
|
Assign::DivAssign => target.div_assign(init)?,
|
|
|
|
Assign::RemAssign => target.rem_assign(init)?,
|
|
|
|
Assign::BitAndAssign => target.bitand_assign(init)?,
|
|
|
|
Assign::BitOrAssign => target.bitor_assign(init)?,
|
|
|
|
Assign::BitXorAssign => target.bitxor_assign(init)?,
|
|
|
|
Assign::ShlAssign => target.shl_assign(init)?,
|
|
|
|
Assign::ShrAssign => target.shr_assign(init)?,
|
|
|
|
_ => (),
|
|
|
|
}
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
self.push(ConValue::Empty);
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_binary(&mut self, bin: &math::Binary) -> IResult<()> {
|
|
|
|
let Binary { first, other } = bin;
|
|
|
|
|
|
|
|
self.visit_operation(first)?;
|
|
|
|
for (op, other) in other {
|
|
|
|
match op {
|
|
|
|
operator::Binary::LogAnd => {
|
|
|
|
if self.peek()?.truthy()? {
|
|
|
|
self.pop()?;
|
|
|
|
self.visit_operation(other)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
operator::Binary::LogOr => {
|
|
|
|
if !self.peek()?.truthy()? {
|
|
|
|
self.pop()?;
|
|
|
|
self.visit_operation(other)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
operator::Binary::LogXor => {
|
|
|
|
let first = self.pop()?.truthy()?;
|
|
|
|
self.visit_operation(other)?;
|
|
|
|
let second = self.pop()?.truthy()?;
|
|
|
|
self.push(first ^ second);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.visit_operation(other)?;
|
|
|
|
self.visit_binary_op(op)?;
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_unary(&mut self, unary: &math::Unary) -> IResult<()> {
|
2024-01-04 08:18:09 +00:00
|
|
|
let Unary { operand, operators } = unary;
|
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
self.visit_operation(operand)?;
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
for op in operators.iter().rev() {
|
|
|
|
self.visit_unary_op(op)?;
|
|
|
|
}
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_assign_op(&mut self, _: &operator::Assign) -> IResult<()> {
|
|
|
|
unimplemented!("visit_assign_op is implemented in visit_operation")
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_binary_op(&mut self, op: &operator::Binary) -> IResult<()> {
|
|
|
|
use operator::Binary;
|
|
|
|
let (second, first) = self.pop_two()?;
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
let out = match op {
|
2023-10-26 19:48:44 +00:00
|
|
|
Binary::Mul => first * second,
|
|
|
|
Binary::Div => first / second,
|
|
|
|
Binary::Rem => first % second,
|
|
|
|
Binary::Add => first + second,
|
|
|
|
Binary::Sub => first - second,
|
|
|
|
Binary::Lsh => first << second,
|
|
|
|
Binary::Rsh => first >> second,
|
|
|
|
Binary::BitAnd => first & second,
|
|
|
|
Binary::BitOr => first | second,
|
|
|
|
Binary::BitXor => first ^ second,
|
|
|
|
Binary::LogAnd | Binary::LogOr | Binary::LogXor => {
|
|
|
|
unimplemented!("Implemented in visit_operation")
|
|
|
|
}
|
2023-10-27 02:51:18 +00:00
|
|
|
Binary::RangeExc => first.range_exc(second),
|
|
|
|
Binary::RangeInc => first.range_inc(second),
|
2023-10-26 19:48:44 +00:00
|
|
|
Binary::Less => first.lt(&second),
|
|
|
|
Binary::LessEq => first.lt_eq(&second),
|
|
|
|
Binary::Equal => first.eq(&second),
|
|
|
|
Binary::NotEq => first.neq(&second),
|
|
|
|
Binary::GreaterEq => first.gt_eq(&second),
|
|
|
|
Binary::Greater => first.gt(&second),
|
2023-10-29 06:13:48 +00:00
|
|
|
}?;
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
self.push(out);
|
2023-10-26 19:48:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_unary_op(&mut self, op: &operator::Unary) -> IResult<()> {
|
|
|
|
let operand = self.pop()?;
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-26 19:48:44 +00:00
|
|
|
self.push(match op {
|
|
|
|
operator::Unary::RefRef => todo!(),
|
|
|
|
operator::Unary::Ref => todo!(),
|
|
|
|
operator::Unary::Deref => todo!(),
|
|
|
|
operator::Unary::Neg => (-operand)?,
|
|
|
|
operator::Unary::Not => (!operand)?,
|
|
|
|
operator::Unary::At => todo!(),
|
|
|
|
operator::Unary::Hash => {
|
|
|
|
println!("{operand}");
|
|
|
|
operand
|
|
|
|
}
|
|
|
|
operator::Unary::Tilde => todo!(),
|
|
|
|
});
|
2024-01-04 08:18:09 +00:00
|
|
|
|
2023-10-26 19:48:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-04 08:18:09 +00:00
|
|
|
fn visit_if(&mut self, expr: &If) -> IResult<()> {
|
2023-10-26 19:48:44 +00:00
|
|
|
self.visit_expr(&expr.cond)?;
|
|
|
|
if self.pop()?.truthy()? {
|
|
|
|
self.visit_block(&expr.body)?;
|
|
|
|
} else if let Some(block) = &expr.else_ {
|
|
|
|
self.visit_else(block)?;
|
2023-10-29 06:13:48 +00:00
|
|
|
} else {
|
|
|
|
self.push(ConValue::Empty)
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-04 08:18:09 +00:00
|
|
|
fn visit_while(&mut self, expr: &While) -> IResult<()> {
|
2023-10-26 19:48:44 +00:00
|
|
|
while {
|
|
|
|
self.visit_expr(&expr.cond)?;
|
|
|
|
self.pop()?.truthy()?
|
|
|
|
} {
|
|
|
|
let Err(out) = self.visit_block(&expr.body) else {
|
2023-10-27 02:51:18 +00:00
|
|
|
// Every expression returns a value. If allowed to pile up, they'll overflow the
|
|
|
|
// stack.
|
|
|
|
self.pop()?;
|
2023-10-26 19:48:44 +00:00
|
|
|
continue;
|
|
|
|
};
|
2023-10-30 04:47:00 +00:00
|
|
|
match out {
|
|
|
|
Error::Continue => continue,
|
|
|
|
Error::Break(value) => {
|
2023-10-26 19:48:44 +00:00
|
|
|
self.push(value);
|
2023-10-29 06:13:48 +00:00
|
|
|
return Ok(());
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
r => Err(r)?,
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-29 06:13:48 +00:00
|
|
|
if let Some(r#else) = &expr.else_ {
|
2023-10-26 19:48:44 +00:00
|
|
|
self.visit_else(r#else)?;
|
2023-10-29 06:13:48 +00:00
|
|
|
} else {
|
|
|
|
self.push(ConValue::Empty);
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_for(&mut self, expr: &control::For) -> IResult<()> {
|
2023-10-29 06:13:48 +00:00
|
|
|
self.scope.enter();
|
2023-10-27 02:51:18 +00:00
|
|
|
self.visit_expr(&expr.iter)?;
|
|
|
|
let bounds = match self.pop()? {
|
|
|
|
ConValue::RangeExc(a, b) | ConValue::RangeInc(a, b) => (a, b),
|
2023-10-30 04:47:00 +00:00
|
|
|
_ => Err(Error::NotIterable)?,
|
2023-10-27 02:51:18 +00:00
|
|
|
};
|
2023-10-29 06:13:48 +00:00
|
|
|
for loop_var in bounds.0..=bounds.1 {
|
2024-01-04 08:18:09 +00:00
|
|
|
self.scope.insert(&expr.var.name, Some(loop_var.into()));
|
2023-10-27 02:51:18 +00:00
|
|
|
let Err(out) = self.visit_block(&expr.body) else {
|
|
|
|
self.pop()?;
|
|
|
|
continue;
|
|
|
|
};
|
2023-10-30 04:47:00 +00:00
|
|
|
match out {
|
|
|
|
Error::Continue => continue,
|
|
|
|
Error::Break(value) => {
|
2023-10-27 02:51:18 +00:00
|
|
|
self.push(value);
|
2023-10-29 06:13:48 +00:00
|
|
|
return Ok(());
|
2023-10-27 02:51:18 +00:00
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
r => Err(r)?,
|
2023-10-27 02:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-29 06:13:48 +00:00
|
|
|
if let Some(r#else) = &expr.else_ {
|
2023-10-27 02:51:18 +00:00
|
|
|
self.visit_else(r#else)?;
|
2023-10-29 06:13:48 +00:00
|
|
|
} else {
|
|
|
|
self.push(ConValue::Empty)
|
2023-10-27 02:51:18 +00:00
|
|
|
}
|
2023-10-29 06:13:48 +00:00
|
|
|
self.scope.exit()?;
|
2023-10-27 02:51:18 +00:00
|
|
|
Ok(())
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_else(&mut self, else_: &control::Else) -> IResult<()> {
|
2023-10-30 04:47:00 +00:00
|
|
|
self.visit_expr(&else_.expr)
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_continue(&mut self, _: &control::Continue) -> IResult<()> {
|
|
|
|
Err(Error::cnt())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_break(&mut self, brk: &control::Break) -> IResult<()> {
|
|
|
|
Err(Error::brk({
|
|
|
|
self.visit_expr(&brk.expr)?;
|
|
|
|
self.pop()?
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_return(&mut self, ret: &control::Return) -> IResult<()> {
|
|
|
|
Err(Error::ret({
|
|
|
|
self.visit_expr(&ret.expr)?;
|
|
|
|
self.pop()?
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_identifier(&mut self, ident: &Identifier) -> IResult<()> {
|
2024-01-04 08:18:09 +00:00
|
|
|
let value = self.resolve(&ident.name)?;
|
2023-10-29 06:13:48 +00:00
|
|
|
self.push(value);
|
|
|
|
Ok(())
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_string_literal(&mut self, string: &str) -> IResult<()> {
|
|
|
|
self.push(string);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_char_literal(&mut self, char: &char) -> IResult<()> {
|
|
|
|
self.push(*char);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bool_literal(&mut self, bool: &bool) -> IResult<()> {
|
|
|
|
self.push(*bool);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_float_literal(&mut self, float: &literal::Float) -> IResult<()> {
|
|
|
|
todo!("visit floats in interpreter: {float:?}")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_int_literal(&mut self, int: &u128) -> IResult<()> {
|
|
|
|
self.push((*int) as i128);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_empty(&mut self) -> IResult<()> {
|
|
|
|
self.push(());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 04:47:00 +00:00
|
|
|
impl Default for Interpreter {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { scope: Environment::new().into(), stack: Default::default() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod function {
|
|
|
|
//! Represents a block of code which lives inside the Interpreter
|
2024-01-04 08:18:09 +00:00
|
|
|
use super::{
|
|
|
|
// scope::Environment,
|
|
|
|
Callable,
|
|
|
|
ConValue,
|
|
|
|
Error,
|
|
|
|
FnDecl,
|
|
|
|
IResult,
|
|
|
|
Identifier,
|
|
|
|
Interpreter,
|
|
|
|
};
|
|
|
|
use crate::ast::{preamble::Name, visitor::Visitor};
|
2023-10-30 04:47:00 +00:00
|
|
|
/// Represents a block of code which persists inside the Interpreter
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Function {
|
|
|
|
/// Stores the contents of the function declaration
|
|
|
|
declaration: Box<FnDecl>,
|
|
|
|
// /// Stores the enclosing scope of the function
|
|
|
|
// TODO: Capture variables
|
2024-01-04 08:18:09 +00:00
|
|
|
//environment: Box<Environment>,
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Function {
|
|
|
|
pub fn new(declaration: &FnDecl) -> Self {
|
2024-01-04 08:18:09 +00:00
|
|
|
Self {
|
|
|
|
declaration: declaration.clone().into(),
|
|
|
|
//environment: Box::new(Environment::new()),
|
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Callable for Function {
|
|
|
|
fn name(&self) -> &str {
|
2024-01-04 08:18:09 +00:00
|
|
|
&self.declaration.name.name.name
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
2024-01-04 08:18:09 +00:00
|
|
|
fn call(&self, interpreter: &mut Interpreter, args: &[ConValue]) -> IResult<ConValue> {
|
2023-10-30 04:47:00 +00:00
|
|
|
// Check arg mapping
|
|
|
|
if args.len() != self.declaration.args.len() {
|
|
|
|
return Err(Error::ArgNumber {
|
|
|
|
want: self.declaration.args.len(),
|
|
|
|
got: args.len(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// TODO: Isolate cross-function scopes!
|
|
|
|
interpreter.scope.enter();
|
2024-01-04 08:18:09 +00:00
|
|
|
for (Name { name: Identifier { name, .. }, .. }, value) in
|
|
|
|
self.declaration.args.iter().zip(args)
|
|
|
|
{
|
|
|
|
interpreter.scope.insert(name, Some(value.clone()));
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
|
|
|
match interpreter.visit_block(&self.declaration.body) {
|
|
|
|
Err(Error::Return(value)) => interpreter.push(value),
|
|
|
|
Err(Error::Break(value)) => Err(Error::BadBreak(value))?,
|
|
|
|
Err(e) => Err(e)?,
|
2024-01-04 08:18:09 +00:00
|
|
|
Ok(()) => (),
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
|
|
|
interpreter.scope.exit()?;
|
2024-01-04 08:18:09 +00:00
|
|
|
interpreter.pop()
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod builtin {
|
|
|
|
mod builtin_imports {
|
|
|
|
pub use crate::interpreter::{
|
2024-01-04 08:18:09 +00:00
|
|
|
error::IResult, temp_type_impl::ConValue, BuiltIn, Callable, Interpreter,
|
2023-10-30 04:47:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
use super::BuiltIn;
|
|
|
|
/// Builtins to load when a new interpreter is created
|
2024-01-04 08:18:09 +00:00
|
|
|
pub const DEFAULT_BUILTINS: &[&dyn BuiltIn] = &[&print::Print, &dbg::Dbg, &dump::Dump];
|
2023-10-30 04:47:00 +00:00
|
|
|
|
|
|
|
mod print {
|
|
|
|
//! Implements the unstable `print(...)` builtin
|
|
|
|
use super::builtin_imports::*;
|
|
|
|
/// Implements the `print(...)` builtin
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Print;
|
|
|
|
impl BuiltIn for Print {}
|
|
|
|
#[rustfmt::skip]
|
|
|
|
impl Callable for Print {
|
|
|
|
fn name(&self) -> &'static str { "print" }
|
2024-01-04 08:18:09 +00:00
|
|
|
fn call(&self, _inter: &mut Interpreter, args: &[ConValue]) -> IResult<ConValue> {
|
|
|
|
for arg in args {
|
|
|
|
print!("{arg}")
|
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
println!();
|
2024-01-04 08:18:09 +00:00
|
|
|
Ok(ConValue::Empty)
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mod dbg {
|
|
|
|
//! Implements the unstable `dbg(...)` builtin
|
|
|
|
use super::builtin_imports::*;
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Dbg;
|
|
|
|
impl BuiltIn for Dbg {}
|
|
|
|
#[rustfmt::skip]
|
|
|
|
impl Callable for Dbg {
|
|
|
|
fn name(&self) -> &str { "dbg" }
|
2024-01-04 08:18:09 +00:00
|
|
|
fn call(&self, _inter: &mut Interpreter, args: &[ConValue]) -> IResult<ConValue> {
|
2023-10-30 04:47:00 +00:00
|
|
|
println!("{args:?}");
|
2024-01-04 08:18:09 +00:00
|
|
|
Ok(args.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod dump {
|
|
|
|
use super::builtin_imports::*;
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Dump;
|
|
|
|
impl BuiltIn for Dump {}
|
|
|
|
impl Callable for Dump {
|
|
|
|
fn call(&self, interpreter: &mut Interpreter, _args: &[ConValue]) -> IResult<ConValue> {
|
|
|
|
println!("Scope:\n{}", interpreter.scope);
|
|
|
|
println!("Stack:{:#?}", interpreter.stack);
|
|
|
|
Ok(ConValue::Empty)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"dump"
|
2023-10-30 04:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
pub mod scope {
|
|
|
|
//! Lexical and non-lexical scoping for variables
|
2023-10-30 04:47:00 +00:00
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
use super::{
|
2023-10-30 04:47:00 +00:00
|
|
|
builtin::DEFAULT_BUILTINS,
|
|
|
|
error::{Error, IResult},
|
|
|
|
function::Function,
|
2023-10-29 06:13:48 +00:00
|
|
|
temp_type_impl::ConValue,
|
2023-10-30 04:47:00 +00:00
|
|
|
FnDecl,
|
2023-10-29 06:13:48 +00:00
|
|
|
};
|
2024-01-04 08:18:09 +00:00
|
|
|
use std::{collections::HashMap, fmt::Display};
|
2023-10-29 06:13:48 +00:00
|
|
|
|
|
|
|
/// Implements a nested lexical scope
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Environment {
|
|
|
|
outer: Option<Box<Self>>,
|
2023-10-30 04:47:00 +00:00
|
|
|
vars: HashMap<String, Option<ConValue>>,
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 08:18:09 +00:00
|
|
|
impl Display for Environment {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
for (var, val) in &self.vars {
|
|
|
|
writeln!(f, "{var}: {}", if val.is_some() { "..." } else { "None" })?;
|
|
|
|
}
|
|
|
|
"--- Frame ---\n".fmt(f)?;
|
|
|
|
if let Some(outer) = &self.outer {
|
|
|
|
outer.fmt(f)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 06:13:48 +00:00
|
|
|
impl Environment {
|
2023-10-30 04:47:00 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut out = Self::default();
|
|
|
|
for &builtin in DEFAULT_BUILTINS {
|
|
|
|
out.insert(builtin.name(), Some(ConValue::BuiltIn(builtin)))
|
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
2023-10-29 06:13:48 +00:00
|
|
|
/// Enter a nested scope
|
2023-10-30 04:47:00 +00:00
|
|
|
pub fn enter(&mut self) {
|
2023-10-29 06:13:48 +00:00
|
|
|
let outer = std::mem::take(self);
|
2023-10-30 04:47:00 +00:00
|
|
|
self.outer = Some(outer.into());
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
|
|
|
/// Exits the scope, destroying all local variables and
|
|
|
|
/// returning the outer scope, if there is one
|
|
|
|
pub fn exit(&mut self) -> IResult<()> {
|
|
|
|
if let Some(outer) = std::mem::take(&mut self.outer) {
|
|
|
|
*self = *outer;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2023-10-30 04:47:00 +00:00
|
|
|
Err(Error::ScopeExit)
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Resolves a variable mutably
|
2023-10-30 04:47:00 +00:00
|
|
|
///
|
|
|
|
/// Returns a mutable reference to the variable's record, if it exists
|
|
|
|
pub fn get_mut(&mut self, id: &str) -> IResult<&mut Option<ConValue>> {
|
2023-10-29 06:13:48 +00:00
|
|
|
match self.vars.get_mut(id) {
|
2023-10-30 04:47:00 +00:00
|
|
|
Some(var) => Ok(var),
|
|
|
|
None => self
|
|
|
|
.outer
|
|
|
|
.as_mut()
|
|
|
|
.ok_or_else(|| Error::NotDefined(id.into()))?
|
|
|
|
.get_mut(id),
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Resolves a variable immutably
|
2023-10-30 04:47:00 +00:00
|
|
|
pub fn get(&self, id: &str) -> IResult<&ConValue> {
|
2023-10-29 06:13:48 +00:00
|
|
|
match self.vars.get(id) {
|
2023-10-30 04:47:00 +00:00
|
|
|
Some(var) => var.as_ref().ok_or_else(|| Error::NotInitialized(id.into())),
|
|
|
|
None => self
|
|
|
|
.outer
|
|
|
|
.as_ref()
|
|
|
|
.ok_or_else(|| Error::NotDefined(id.into()))?
|
|
|
|
.get(id),
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
pub fn insert(&mut self, id: &str, value: Option<ConValue>) {
|
|
|
|
self.vars.insert(id.to_string(), value);
|
|
|
|
}
|
|
|
|
/// A convenience function for registering a [FnDecl] as a [Function]
|
|
|
|
pub fn insert_fn(&mut self, decl: &FnDecl) {
|
2024-01-04 08:18:09 +00:00
|
|
|
self.vars.insert(
|
|
|
|
decl.name.name.name.clone(),
|
|
|
|
Some(Function::new(decl).into()),
|
|
|
|
);
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:48:44 +00:00
|
|
|
pub mod error {
|
|
|
|
//! The [Error] type represents any error thrown by the [Interpreter](super::Interpreter)
|
2023-10-29 06:13:48 +00:00
|
|
|
|
2023-10-26 19:48:44 +00:00
|
|
|
use super::temp_type_impl::ConValue;
|
|
|
|
|
|
|
|
pub type IResult<T> = Result<T, Error>;
|
|
|
|
/// Represents any error thrown by the [Interpreter](super::Interpreter)
|
|
|
|
impl Error {
|
2023-10-30 04:47:00 +00:00
|
|
|
/// Creates a [Return](Error::Return) error, with the given [value](ConValue)
|
2023-10-26 19:48:44 +00:00
|
|
|
pub fn ret(value: ConValue) -> Self {
|
2023-10-30 04:47:00 +00:00
|
|
|
Error::Return(value)
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
/// Creates a [Break](Error::Break) error, with the given [value](ConValue)
|
2023-10-26 19:48:44 +00:00
|
|
|
pub fn brk(value: ConValue) -> Self {
|
2023-10-30 04:47:00 +00:00
|
|
|
Error::Break(value)
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
/// Creates a [Continue](Error::Continue) error
|
2023-10-26 19:48:44 +00:00
|
|
|
pub fn cnt() -> Self {
|
2023-10-30 04:47:00 +00:00
|
|
|
Error::Continue
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The reason for the [Error]
|
|
|
|
#[derive(Clone, Debug)]
|
2023-10-30 04:47:00 +00:00
|
|
|
pub enum Error {
|
2023-10-26 19:48:44 +00:00
|
|
|
/// Propagate a Return value
|
|
|
|
Return(ConValue),
|
|
|
|
/// Propagate a Break value
|
|
|
|
Break(ConValue),
|
2023-10-30 04:47:00 +00:00
|
|
|
/// Break propagated across function bounds
|
|
|
|
BadBreak(ConValue),
|
2023-10-26 19:48:44 +00:00
|
|
|
/// Continue to the next iteration of a loop
|
|
|
|
Continue,
|
|
|
|
/// Underflowed the stack
|
|
|
|
StackUnderflow,
|
2023-10-29 06:13:48 +00:00
|
|
|
/// Exited the last scope
|
|
|
|
ScopeExit,
|
2023-10-26 19:48:44 +00:00
|
|
|
/// Type incompatibility
|
|
|
|
// TODO: store the type information in this error
|
|
|
|
TypeError,
|
2023-10-27 02:51:18 +00:00
|
|
|
/// In clause of For loop didn't yield a Range
|
|
|
|
NotIterable,
|
2023-10-29 06:13:48 +00:00
|
|
|
/// A name was not defined in scope before being used
|
2023-10-30 04:47:00 +00:00
|
|
|
NotDefined(String),
|
2023-10-29 06:13:48 +00:00
|
|
|
/// A name was defined but not initialized
|
2023-10-30 04:47:00 +00:00
|
|
|
NotInitialized(String),
|
|
|
|
/// A value was called, but is not callable
|
|
|
|
NotCallable(ConValue),
|
|
|
|
/// A function was called with the wrong number of arguments
|
|
|
|
ArgNumber { want: usize, got: usize },
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Error {}
|
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2023-10-30 04:47:00 +00:00
|
|
|
Error::Return(value) => write!(f, "return {value}"),
|
|
|
|
Error::Break(value) => write!(f, "break {value}"),
|
|
|
|
Error::BadBreak(value) => write!(f, "rogue break: {value}"),
|
|
|
|
Error::Continue => "continue".fmt(f),
|
|
|
|
Error::StackUnderflow => "Stack underflow".fmt(f),
|
|
|
|
Error::ScopeExit => "Exited the last scope. This is a logic bug.".fmt(f),
|
|
|
|
Error::TypeError => "Incompatible types".fmt(f),
|
|
|
|
Error::NotIterable => "`in` clause of `for` loop did not yield an iterable".fmt(f),
|
|
|
|
Error::NotDefined(value) => {
|
|
|
|
write!(f, "{value} not bound. Did you mean `let {value};`?")
|
|
|
|
}
|
|
|
|
Error::NotInitialized(value) => {
|
|
|
|
write!(f, "{value} bound, but not initialized")
|
|
|
|
}
|
|
|
|
Error::NotCallable(value) => {
|
|
|
|
write!(f, "{value} is not a function, and cannot be called")
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
2023-10-30 04:47:00 +00:00
|
|
|
Error::ArgNumber { want, got } => {
|
|
|
|
write!(f, "Expected {want} arguments, got {got}")
|
2023-10-29 06:13:48 +00:00
|
|
|
}
|
2023-10-26 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|