interpreter: revert storing environments in functions

This change broke recursion entirely
This commit is contained in:
John 2024-01-09 22:42:15 -06:00
parent 99ade72756
commit d9d8c70556

View File

@ -1,5 +1,4 @@
//! Interprets an AST as a program //! Interprets an AST as a program
#![allow(deprecated)] // TODO: REMOVE
use crate::ast::preamble::*; use crate::ast::preamble::*;
use env::Environment; use env::Environment;
@ -634,13 +633,13 @@ pub mod function {
pub struct Function { pub struct Function {
/// Stores the contents of the function declaration /// Stores the contents of the function declaration
decl: Box<FnDecl>, decl: Box<FnDecl>,
/// Stores the enclosing scope of the function // /// Stores the enclosing scope of the function
env: Box<Environment>, // env: Box<Environment>,
} }
impl Function { impl Function {
pub fn new(decl: &FnDecl, env: Environment) -> Self { pub fn new(decl: &FnDecl) -> Self {
Self { decl: decl.clone().into(), env: Box::new(env) } Self { decl: decl.clone().into() }
} }
} }
@ -648,13 +647,13 @@ pub mod function {
fn name(&self) -> &str { fn name(&self) -> &str {
&self.decl.name.symbol.name &self.decl.name.symbol.name
} }
fn call(&self, _env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> { fn call(&self, env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
// Check arg mapping // Check arg mapping
if args.len() != self.decl.args.len() { if args.len() != self.decl.args.len() {
return Err(Error::ArgNumber { want: self.decl.args.len(), got: args.len() }); return Err(Error::ArgNumber { want: self.decl.args.len(), got: args.len() });
} }
// TODO: Isolate cross-function scopes! // TODO: Isolate cross-function scopes!
let mut env = self.env.clone(); // let mut env = self.env.clone();
let mut frame = env.frame("fn args"); let mut frame = env.frame("fn args");
for (Name { symbol: Identifier { name, .. }, .. }, value) in for (Name { symbol: Identifier { name, .. }, .. }, value) in
self.decl.args.iter().zip(args) self.decl.args.iter().zip(args)
@ -841,7 +840,7 @@ pub mod env {
pub fn insert_fn(&mut self, decl: &FnDecl) { pub fn insert_fn(&mut self, decl: &FnDecl) {
let (name, function) = ( let (name, function) = (
decl.name.symbol.name.clone(), decl.name.symbol.name.clone(),
Some(Function::new(decl, self.clone()).into()), Some(Function::new(decl).into()),
); );
if let Some((frame, _)) = self.frames.last_mut() { if let Some((frame, _)) = self.frames.last_mut() {
frame.insert(name, function); frame.insert(name, function);