cl-interpret: Pure value stack v1, references v2

References actually work! :D
They can also be stale :(
This commit is contained in:
John 2025-03-11 05:01:49 -05:00
parent 06bcb6b7c6
commit 4bc088f277
6 changed files with 203 additions and 140 deletions

View File

@ -5,10 +5,7 @@ use crate::{
env::Environment, env::Environment,
error::{Error, IResult}, error::{Error, IResult},
}; };
use std::{ use std::io::{stdout, Write};
io::{stdout, Write},
slice,
};
/// A function built into the interpreter. /// A function built into the interpreter.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -78,7 +75,7 @@ pub macro builtin(
$(#[$($meta)*])* $(#[$($meta)*])*
fn $name(_env: &mut Environment, _args: &[ConValue]) -> IResult<ConValue> { fn $name(_env: &mut Environment, _args: &[ConValue]) -> IResult<ConValue> {
// Set up the builtin! environment // Set up the builtin! environment
$(let $env = _env;)? $(#[allow(unused)]let $env = _env;)?
// Allow for single argument `fn foo(args @ ..)` pattern // Allow for single argument `fn foo(args @ ..)` pattern
#[allow(clippy::redundant_at_rest_pattern, irrefutable_let_patterns)] #[allow(clippy::redundant_at_rest_pattern, irrefutable_let_patterns)]
let [$($arg),*] = _args else { let [$($arg),*] = _args else {
@ -152,19 +149,21 @@ pub const Builtins: &[Builtin] = &builtins![
Ok(()) Ok(())
} }
fn builtins() @env { // fn builtins() @env {
for builtin in env.builtins().values().flatten() { // for builtin in env.builtins().values().flatten() {
println!("{builtin}"); // println!("{builtin}");
} // }
Ok(()) // Ok(())
} // }
/// Returns the length of the input list as a [ConValue::Int] /// Returns the length of the input list as a [ConValue::Int]
fn len(list) @env { fn len(list) @env {
Ok(match list { Ok(match list {
ConValue::Empty => 0, ConValue::Empty => 0,
ConValue::String(s) => s.chars().count() as _, ConValue::String(s) => s.chars().count() as _,
ConValue::Ref(r) => return len(env, slice::from_ref(&r.borrow())), ConValue::Ref(r) => {
return len(env, &[env.get_id(*r).ok_or(Error::StackOverflow(*r))?.clone()])
}
ConValue::Array(t) => t.len() as _, ConValue::Array(t) => t.len() as _,
ConValue::Tuple(t) => t.len() as _, ConValue::Tuple(t) => t.len() as _,
_ => Err(Error::TypeError)?, _ => Err(Error::TypeError)?,
@ -176,6 +175,10 @@ pub const Builtins: &[Builtin] = &builtins![
Ok(ConValue::Empty) Ok(ConValue::Empty)
} }
fn slice_of(ConValue::Ref(arr), ConValue::Int(start)) {
Ok(ConValue::Slice(*arr, *start as usize))
}
/// Returns a shark /// Returns a shark
fn shark() { fn shark() {
Ok('\u{1f988}') Ok('\u{1f988}')
@ -330,9 +333,9 @@ pub const Math: &[Builtin] = &builtins![
} }
/// Does the opposite of `&` /// Does the opposite of `&`
fn deref(tail) { fn deref(tail) @env {
Ok(match tail { Ok(match tail {
ConValue::Ref(v) => v.take(), ConValue::Ref(v) => env.get_id(*v).cloned().ok_or(Error::StackOverflow(*v))?,
_ => tail.clone(), _ => tail.clone(),
}) })
} }

View File

@ -9,7 +9,7 @@ use super::{
function::Function, function::Function,
Callable, Environment, Callable, Environment,
}; };
use std::{cell::RefCell, collections::HashMap, ops::*, rc::Rc}; use std::{collections::HashMap, ops::*, rc::Rc};
/* /*
A Value can be: A Value can be:
@ -33,7 +33,6 @@ struct {
} }
*/ */
type Integer = isize; type Integer = isize;
/// A Conlang value stores data in the interpreter /// A Conlang value stores data in the interpreter
@ -53,7 +52,9 @@ pub enum ConValue {
/// A string /// A string
String(Sym), String(Sym),
/// A reference /// A reference
Ref(Rc<RefCell<ConValue>>), Ref(usize),
/// A reference to an array
Slice(usize, usize),
/// An Array /// An Array
Array(Box<[ConValue]>), Array(Box<[ConValue]>),
/// A tuple /// A tuple
@ -90,7 +91,7 @@ impl ConValue {
Self::Struct(Box::new((name, values))) Self::Struct(Box::new((name, values)))
} }
pub fn index(&self, index: &Self) -> IResult<ConValue> { pub fn index(&self, index: &Self, env: &Environment) -> IResult<ConValue> {
let Self::Int(index) = index else { let Self::Int(index) = index else {
Err(Error::TypeError)? Err(Error::TypeError)?
}; };
@ -104,6 +105,10 @@ impl ConValue {
.get(*index as usize) .get(*index as usize)
.cloned() .cloned()
.ok_or(Error::OobIndex(*index as usize, arr.len())), .ok_or(Error::OobIndex(*index as usize, arr.len())),
ConValue::Slice(id, start) => env
.get_id(*id)
.ok_or(Error::StackOverflow(*id))?
.index(&ConValue::Int((*index as usize + start) as isize), env),
_ => Err(Error::TypeError), _ => Err(Error::TypeError),
} }
} }
@ -295,7 +300,8 @@ impl std::fmt::Display for ConValue {
ConValue::Bool(v) => v.fmt(f), ConValue::Bool(v) => v.fmt(f),
ConValue::Char(v) => v.fmt(f), ConValue::Char(v) => v.fmt(f),
ConValue::String(v) => v.fmt(f), ConValue::String(v) => v.fmt(f),
ConValue::Ref(v) => write!(f, "&{}", v.borrow()), ConValue::Ref(v) => write!(f, "&<{}>", v),
ConValue::Slice(v, len) => write!(f, "&<{v}>[{len}..]"),
ConValue::Array(array) => { ConValue::Array(array) => {
'['.fmt(f)?; '['.fmt(f)?;
for (idx, element) in array.iter().enumerate() { for (idx, element) in array.iter().enumerate() {

View File

@ -19,30 +19,35 @@ use std::{
type StackFrame = HashMap<Sym, Option<ConValue>>; type StackFrame = HashMap<Sym, Option<ConValue>>;
#[derive(Clone, Debug, Default)]
struct EnvFrame {
/// The length of the array when this stack frame was constructed
pub name: Option<&'static str>,
pub base: usize,
pub binds: HashMap<Sym, usize>,
}
/// Implements a nested lexical scope /// Implements a nested lexical scope
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Environment { pub struct Environment {
builtin: StackFrame, values: Vec<Option<ConValue>>,
global: Vec<(StackFrame, &'static str)>, frames: Vec<EnvFrame>,
frames: Vec<(StackFrame, &'static str)>,
} }
impl Display for Environment { impl Display for Environment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (frame, name) in self for EnvFrame { name, base: _, binds } in self.frames.iter().rev() {
.global writeln!(
.iter() f,
.rev() "--- {} ---",
.take(2) if let Some(name) = name { name } else { "" }
.rev() )?;
.chain(self.frames.iter()) for (var, val) in binds {
{
writeln!(f, "--- {name} ---")?;
for (var, val) in frame {
write!(f, "{var}: ")?; write!(f, "{var}: ")?;
match val { match self.values.get(*val) {
Some(value) => writeln!(f, "\t{value}"), Some(Some(value)) => writeln!(f, "\t{value}"),
None => writeln!(f, "<undefined>"), Some(None) => writeln!(f, "<undefined>"),
None => writeln!(f, "ERROR: {var} address blows the stack!"),
}? }?
} }
} }
@ -51,18 +56,18 @@ impl Display for Environment {
} }
impl Default for Environment { impl Default for Environment {
fn default() -> Self { fn default() -> Self {
Self { let mut values = Vec::new();
builtin: to_hashmap(Builtins.iter().chain(Math.iter())),
global: vec![(HashMap::new(), "globals")],
frames: vec![],
}
}
}
fn to_hashmap(from: impl IntoIterator<Item = &'static Builtin>) -> HashMap<Sym, Option<ConValue>> { let mut builtins = EnvFrame { name: Some("builtin"), base: 0, binds: HashMap::new() };
from.into_iter() for bu in Builtins.iter().chain(Math.iter()) {
.map(|v| (v.name(), Some(v.into()))) builtins.binds.insert(bu.name(), values.len());
.collect() values.push(Some(bu.into()));
}
let globals =
EnvFrame { name: Some("globals"), base: values.len(), binds: HashMap::new() };
Self { values, frames: vec![builtins, globals] }
}
} }
impl Environment { impl Environment {
@ -71,36 +76,12 @@ impl Environment {
} }
/// Creates an [Environment] with no [builtins](super::builtin) /// Creates an [Environment] with no [builtins](super::builtin)
pub fn no_builtins() -> Self { pub fn no_builtins() -> Self {
Self { let globals = EnvFrame { name: Some("globals"), base: 0, binds: HashMap::new() };
builtin: HashMap::new(),
global: vec![(Default::default(), "globals")], Self { values: Vec::new(), frames: vec![Default::default(), globals] }
frames: vec![],
}
}
pub fn builtins(&self) -> &StackFrame {
&self.builtin
}
pub fn add_builtin(&mut self, builtin: &'static Builtin) -> &mut Self {
self.builtin.insert(builtin.name(), Some(builtin.into()));
self
}
pub fn add_builtins(&mut self, builtins: &'static [Builtin]) {
for builtin in builtins {
self.add_builtin(builtin);
}
}
pub fn push_frame(&mut self, name: &'static str, frame: StackFrame) {
self.frames.push((frame, name));
}
pub fn pop_frame(&mut self) -> Option<(StackFrame, &'static str)> {
self.frames.pop()
} }
/// Reflexively evaluates a node
pub fn eval(&mut self, node: &impl Interpret) -> IResult<ConValue> { pub fn eval(&mut self, node: &impl Interpret) -> IResult<ConValue> {
node.interpret(self) node.interpret(self)
} }
@ -108,102 +89,148 @@ impl Environment {
/// Calls a function inside the interpreter's scope, /// Calls a function inside the interpreter's scope,
/// and returns the result /// and returns the result
pub fn call(&mut self, name: Sym, args: &[ConValue]) -> IResult<ConValue> { pub fn call(&mut self, name: Sym, args: &[ConValue]) -> IResult<ConValue> {
// FIXME: Clone to satisfy the borrow checker let function = self.get(name)?;
let function = self.get(name)?.clone();
function.call(self, args) function.call(self, args)
} }
/// Adds builtins
///
/// # Panics
///
/// Will panic if globals table is non-empty!
pub fn add_builtins(&mut self, builtins: &'static [Builtin]) {
let Self { values, frames } = self;
// Globals must be EMPTY to add builtins
assert_eq!(frames[1].binds.len(), 0);
for builtin in builtins {
if let Some(EnvFrame { name: _, base: _, binds }) = frames.first_mut() {
binds.insert(builtin.name(), values.len());
values.push(Some(builtin.into()));
}
}
frames[1].base = values.len();
}
pub fn push_frame(&mut self, name: &'static str, frame: StackFrame) {
self.enter(name);
for (k, v) in frame {
self.insert(k, v);
}
}
pub fn pop_frame(&mut self) -> Option<(StackFrame, &'static str)> {
let mut out = HashMap::new();
let EnvFrame { name, base, binds } = self.frames.pop()?;
for (k, v) in binds {
out.insert(k, self.values.get_mut(v).and_then(std::mem::take));
}
self.values.truncate(base);
Some((out, name.unwrap_or("")))
}
/// Enters a nested scope, returning a [`Frame`] stack-guard. /// Enters a nested scope, returning a [`Frame`] stack-guard.
/// ///
/// [`Frame`] implements Deref/DerefMut for [`Environment`]. /// [`Frame`] implements Deref/DerefMut for [`Environment`].
pub fn frame(&mut self, name: &'static str) -> Frame { pub fn frame(&mut self, name: &'static str) -> Frame {
Frame::new(self, name) Frame::new(self, name)
} }
/// Resolves a variable mutably. /// Resolves a variable mutably.
/// ///
/// Returns a mutable reference to the variable's record, if it exists. /// Returns a mutable reference to the variable's record, if it exists.
pub fn get_mut(&mut self, id: Sym) -> IResult<&mut Option<ConValue>> { pub fn get_mut(&mut self, name: Sym) -> IResult<&mut Option<ConValue>> {
for (frame, _) in self.frames.iter_mut().rev() { let id = self.id_of(name)?;
if let Some(var) = frame.get_mut(&id) { self.values.get_mut(id).ok_or(Error::NotDefined(name))
return Ok(var);
}
}
for (frame, _) in self.global.iter_mut().rev() {
if let Some(var) = frame.get_mut(&id) {
return Ok(var);
}
}
self.builtin.get_mut(&id).ok_or(Error::NotDefined(id))
} }
/// Resolves a variable immutably. /// Resolves a variable immutably.
/// ///
/// Returns a reference to the variable's contents, if it is defined and initialized. /// Returns a reference to the variable's contents, if it is defined and initialized.
pub fn get(&self, id: Sym) -> IResult<ConValue> { pub fn get(&self, name: Sym) -> IResult<ConValue> {
for (frame, _) in self.frames.iter().rev() { let id = self.id_of(name)?;
match frame.get(&id) { match self.values.get(id).ok_or(Error::NotDefined(name))? {
Some(Some(var)) => return Ok(var.clone()), Some(value) => Ok(value.clone()),
Some(None) => return Err(Error::NotInitialized(id)), None => Err(Error::NotInitialized(name)),
_ => (),
} }
} }
for (frame, _) in self.global.iter().rev() {
match frame.get(&id) {
Some(Some(var)) => return Ok(var.clone()),
Some(None) => return Err(Error::NotInitialized(id)),
_ => (),
}
}
self.builtin
.get(&id)
.cloned()
.flatten()
.ok_or(Error::NotDefined(id))
}
pub(crate) fn get_local(&self, id: Sym) -> IResult<ConValue> { pub(crate) fn get_local(&self, id: Sym) -> IResult<ConValue> {
for (frame, _) in self.frames.iter().rev() { for EnvFrame { binds, .. } in self.frames.iter().skip(2).rev() {
match frame.get(&id) { if let Some(var) = binds.get(&id) {
Some(Some(var)) => return Ok(var.clone()), if let Some(var) = self.values.get(*var) {
Some(None) => return Err(Error::NotInitialized(id)), return match var {
_ => (), Some(value) => Ok(value.clone()),
None => Err(Error::NotInitialized(id)),
};
} }
} }
Err(Error::NotInitialized(id)) }
Err(Error::NotDefined(id))
}
pub fn id_of(&self, name: Sym) -> IResult<usize> {
for EnvFrame { binds, .. } in self.frames.iter().rev() {
if let Some(id) = binds.get(&name).copied() {
return Ok(id);
}
}
Err(Error::NotDefined(name))
}
pub fn get_id(&self, id: usize) -> Option<&ConValue> {
self.values.get(id)?.as_ref()
}
pub fn get_id_mut(&mut self, id: usize) -> Option<&mut Option<ConValue>> {
self.values.get_mut(id)
} }
/// Inserts a new [ConValue] into this [Environment] /// Inserts a new [ConValue] into this [Environment]
pub fn insert(&mut self, id: Sym, value: Option<ConValue>) { pub fn insert(&mut self, k: Sym, v: Option<ConValue>) {
if let Some((frame, _)) = self.frames.last_mut() { if self.bind_raw(k, self.values.len()).is_some() {
frame.insert(id, value); self.values.push(v);
} else if let Some((frame, _)) = self.global.last_mut() {
frame.insert(id, value);
} }
} }
/// A convenience function for registering a [FnDecl] as a [Function] /// A convenience function for registering a [FnDecl] as a [Function]
pub fn insert_fn(&mut self, decl: &FnDecl) { pub fn insert_fn(&mut self, decl: &FnDecl) {
let FnDecl { name, .. } = decl; let FnDecl { name, .. } = decl;
let (name, function) = (name, Rc::new(Function::new(decl))); let (name, function) = (*name, Rc::new(Function::new(decl)));
if let Some((frame, _)) = self.frames.last_mut() { self.insert(name, Some(ConValue::Function(function.clone())));
frame.insert(*name, Some(ConValue::Function(function.clone())));
} else if let Some((frame, _)) = self.global.last_mut() {
frame.insert(*name, Some(ConValue::Function(function.clone())));
}
// Tell the function to lift its upvars now, after it's been declared // Tell the function to lift its upvars now, after it's been declared
function.lift_upvars(self); function.lift_upvars(self);
} }
/// Inserts a temporary/anonymous variable, and returns its address
pub fn insert_temporary(&mut self, value: ConValue) -> IResult<usize> {
let adr = self.values.len();
self.values.push(Some(value));
Ok(adr)
}
pub fn bind_raw(&mut self, name: Sym, id: usize) -> Option<()> {
let EnvFrame { name: _, base: _, binds } = self.frames.last_mut()?;
binds.insert(name, id);
Some(())
}
} }
/// Functions which aid in the implementation of [`Frame`] /// Functions which aid in the implementation of [`Frame`]
impl Environment { impl Environment {
/// Enters a scope, creating a new namespace for variables /// Enters a scope, creating a new namespace for variables
fn enter(&mut self, name: &'static str) -> &mut Self { fn enter(&mut self, name: &'static str) -> &mut Self {
self.frames.push((Default::default(), name)); let new_frame =
EnvFrame { name: Some(name), base: self.values.len(), binds: HashMap::new() };
self.frames.push(new_frame);
self self
} }
/// Exits the scope, destroying all local variables and /// Exits the scope, destroying all local variables and
/// returning the outer scope, if there is one /// returning the outer scope, if there is one
fn exit(&mut self) -> &mut Self { fn exit(&mut self) -> &mut Self {
self.frames.pop(); if let Some(frame) = self.frames.pop() {
self.values.truncate(frame.base);
}
self self
} }
} }

View File

@ -20,6 +20,8 @@ pub enum Error {
Continue, Continue,
/// Underflowed the stack /// Underflowed the stack
StackUnderflow, StackUnderflow,
/// Overflowed the stack
StackOverflow(usize),
/// Exited the last scope /// Exited the last scope
ScopeExit, ScopeExit,
/// Type incompatibility /// Type incompatibility
@ -70,6 +72,9 @@ impl std::fmt::Display for Error {
Error::BadBreak(value) => write!(f, "rogue break: {value}"), Error::BadBreak(value) => write!(f, "rogue break: {value}"),
Error::Continue => "continue".fmt(f), Error::Continue => "continue".fmt(f),
Error::StackUnderflow => "Stack underflow".fmt(f), Error::StackUnderflow => "Stack underflow".fmt(f),
Error::StackOverflow(id) => {
write!(f, "Attempt to access <{id}> resulted in stack overflow.")
}
Error::ScopeExit => "Exited the last scope. This is a logic bug.".fmt(f), Error::ScopeExit => "Exited the last scope. This is a logic bug.".fmt(f),
Error::TypeError => "Incompatible types".fmt(f), Error::TypeError => "Incompatible types".fmt(f),
Error::NotIterable => "`in` clause of `for` loop did not yield an iterable".fmt(f), Error::NotIterable => "`in` clause of `for` loop did not yield an iterable".fmt(f),

View File

@ -8,7 +8,7 @@
use super::*; use super::*;
use cl_ast::{ast_visitor::Visit, *}; use cl_ast::{ast_visitor::Visit, *};
use cl_structures::intern::interned::Interned; use cl_structures::intern::interned::Interned;
use std::{borrow::Borrow, cell::RefCell, rc::Rc}; use std::borrow::Borrow;
/// A work-in-progress tree walk interpreter for Conlang /// A work-in-progress tree walk interpreter for Conlang
pub trait Interpret { pub trait Interpret {
/// Interprets this thing in the given [`Environment`]. /// Interprets this thing in the given [`Environment`].
@ -86,6 +86,7 @@ impl Interpret for Static {
} }
} }
impl Interpret for Module { impl Interpret for Module {
// TODO: Keep modules around somehow, rather than putting them on the stack
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
let Self { name, kind } = self; let Self { name, kind } = self;
env.push_frame(Interned::to_ref(name), Default::default()); env.push_frame(Interned::to_ref(name), Default::default());
@ -203,6 +204,7 @@ impl Interpret for Use {
impl Interpret for UseTree { impl Interpret for UseTree {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> { fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
// TODO: raw-bind use items
type Bindings = HashMap<Sym, ConValue>; type Bindings = HashMap<Sym, ConValue>;
use std::collections::HashMap; use std::collections::HashMap;
@ -371,8 +373,8 @@ mod assignment {
pattern::substitution(pat, value).map_err(|_| Error::PatFailed(pat.clone().into()))? pattern::substitution(pat, value).map_err(|_| Error::PatFailed(pat.clone().into()))?
{ {
match env.get_mut(*name)? { match env.get_mut(*name)? {
Some(ConValue::Ref(r)) => { &mut Some(ConValue::Ref(id)) => {
r.replace(value); *(env.get_id_mut(id).ok_or(Error::StackOverflow(id))?) = Some(value);
} }
other => *other = Some(value), other => *other = Some(value),
} }
@ -388,15 +390,18 @@ mod assignment {
ExprKind::Member(member) => *addrof_member(env, member)? = value, ExprKind::Member(member) => *addrof_member(env, member)? = value,
ExprKind::Index(index) => *addrof_index(env, index)? = value, ExprKind::Index(index) => *addrof_index(env, index)? = value,
ExprKind::Path(path) => *addrof_path(env, &path.parts)? = Some(value), ExprKind::Path(path) => *addrof_path(env, &path.parts)? = Some(value),
ExprKind::Unary(Unary { kind: UnaryKind::Deref, tail }) => match addrof(env, tail)? {
&mut ConValue::Ref(r) => {
*env.get_id_mut(r).ok_or(Error::StackOverflow(r))? = Some(value)
}
_ => Err(Error::NotAssignable)?,
},
_ => Err(Error::NotAssignable)?, _ => Err(Error::NotAssignable)?,
} }
Ok(()) Ok(())
} }
pub(super) fn addrof<'e>( pub(super) fn addrof<'e>(env: &'e mut Environment, pat: &Expr) -> IResult<&'e mut ConValue> {
env: &'e mut Environment,
pat: &Expr,
) -> IResult<&'e mut ConValue> {
match &pat.kind { match &pat.kind {
ExprKind::Path(path) => addrof_path(env, &path.parts)? ExprKind::Path(path) => addrof_path(env, &path.parts)?
.as_mut() .as_mut()
@ -484,6 +489,7 @@ mod assignment {
a.get_mut(*i as usize) a.get_mut(*i as usize)
.ok_or(Error::OobIndex(*i as usize, a_len)) .ok_or(Error::OobIndex(*i as usize, a_len))
} }
(ConValue::Slice(_, _), _) => Err(Error::TypeError),
_ => Err(Error::NotIndexable), _ => Err(Error::NotIndexable),
} }
} }
@ -641,16 +647,23 @@ impl Interpret for Unary {
} }
} }
fn cast(value: ConValue, ty: Sym) -> IResult<ConValue> { fn cast(env: &Environment, value: ConValue, ty: Sym) -> IResult<ConValue> {
let value = match value { let value = match value {
ConValue::Empty => 0, ConValue::Empty => 0,
ConValue::Int(i) => i as _, ConValue::Int(i) => i as _,
ConValue::Bool(b) => b as _, ConValue::Bool(b) => b as _,
ConValue::Char(c) => c as _, ConValue::Char(c) => c as _,
ConValue::Ref(v) => return cast((*v).borrow().clone(), ty), ConValue::Ref(v) => {
return cast(
env,
env.get_id(v).cloned().ok_or(Error::StackUnderflow)?,
ty,
)
}
// TODO: This, better // TODO: This, better
ConValue::Float(_) if ty.starts_with('f') => return Ok(value), ConValue::Float(_) if ty.starts_with('f') => return Ok(value),
ConValue::Float(f) => f as _, ConValue::Float(f) => f as _,
_ if (*ty).eq("str") => return Ok(ConValue::String(format!("{value}").into())),
_ => Err(Error::TypeError)?, _ => Err(Error::TypeError)?,
}; };
Ok(match &*ty { Ok(match &*ty {
@ -681,7 +694,7 @@ impl Interpret for Cast {
Err(Error::TypeError)? Err(Error::TypeError)?
}; };
match parts.as_slice() { match parts.as_slice() {
[PathPart::Ident(ty)] => cast(value, *ty), [PathPart::Ident(ty)] => cast(env, value, *ty),
_ => Err(Error::TypeError), _ => Err(Error::TypeError),
} }
} }
@ -724,7 +737,7 @@ impl Interpret for Index {
let Self { head, indices } = self; let Self { head, indices } = self;
let mut head = head.interpret(env)?; let mut head = head.interpret(env)?;
for index in indices { for index in indices {
head = head.index(&index.interpret(env)?)?; head = head.index(&index.interpret(env)?, env)?;
} }
Ok(head) Ok(head)
} }
@ -803,8 +816,15 @@ impl Interpret for AddrOf {
let Self { mutable: _, expr } = self; let Self { mutable: _, expr } = self;
match &expr.kind { match &expr.kind {
ExprKind::Index(_) => todo!("AddrOf array index"), ExprKind::Index(_) => todo!("AddrOf array index"),
ExprKind::Path(_) => todo!("Path traversal in addrof"), ExprKind::Path(Path { parts, .. }) => match parts.as_slice() {
_ => Ok(ConValue::Ref(Rc::new(RefCell::new(expr.interpret(env)?)))), [PathPart::Ident(name)] => Ok(ConValue::Ref(env.id_of(*name)?)),
_ => todo!("Path traversal in AddrOf(\"{self}\")"),
},
_ => {
let value = expr.interpret(env)?;
let temp = env.insert_temporary(value)?;
Ok(ConValue::Ref(temp))
}
} }
} }
} }

View File

@ -123,7 +123,9 @@ pub fn append_sub<'pat>(
Ok(()) Ok(())
} }
(Pattern::Ref(_, pat), ConValue::Ref(r)) => append_sub(sub, pat, r.borrow().clone()), (Pattern::Ref(_, pat), ConValue::Ref(r)) => {
todo!("Dereference <{r}> in pattern matching {pat}")
}
(Pattern::Array(patterns), ConValue::Array(values)) => { (Pattern::Array(patterns), ConValue::Array(values)) => {
match rest_binding(sub, patterns, values.into_vec().into())? { match rest_binding(sub, patterns, values.into_vec().into())? {