cl-ast: Add inline closure expressions

This commit is contained in:
2025-05-18 03:57:20 -04:00
parent 6c6d2d04a7
commit e6156343c3
19 changed files with 211 additions and 41 deletions

View File

@@ -0,0 +1,68 @@
use crate::{
Callable,
convalue::ConValue,
env::{Environment, Place},
error::{Error, ErrorKind, IResult},
function::collect_upvars::CollectUpvars,
interpret::Interpret,
pattern,
};
use cl_ast::{Sym, ast_visitor::Visit};
use std::{collections::HashMap, fmt::Display};
/// Represents an ad-hoc anonymous function
/// which captures surrounding state by reference.
#[derive(Clone, Debug)]
pub struct Closure {
decl: cl_ast::Closure,
lift: HashMap<Sym, Place>,
}
impl Closure {
const NAME: &'static str = "{closure}";
}
impl Closure {
pub fn new(env: &mut Environment, decl: &cl_ast::Closure) -> Self {
let lift = CollectUpvars::new(env).visit(decl).finish();
Self { decl: decl.clone(), lift }
}
}
impl Display for Closure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { decl, lift: _ } = self;
write!(f, "{decl}")
}
}
impl Callable for Closure {
fn call(&self, env: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
let Self { decl, lift } = self;
let mut env = env.frame(Self::NAME);
// place lifts in scope
for (name, place) in lift {
env.insert(*name, Some(ConValue::Ref(*place)));
}
let mut env = env.frame("args");
for (name, value) in pattern::substitution(&decl.arg, ConValue::Tuple(args.into()))? {
env.insert(*name, Some(value));
}
let res = decl.body.interpret(&mut env);
drop(env);
match res {
Err(Error { kind: ErrorKind::Return(value), .. }) => Ok(value),
Err(Error { kind: ErrorKind::Break(value), .. }) => Err(Error::BadBreak(value)),
other => other,
}
}
fn name(&self) -> cl_ast::Sym {
"{closure}".into()
}
}

View File

@@ -3,7 +3,7 @@
//! The most permanent fix is a temporary one.
use cl_ast::{Expr, Sym, format::FmtAdapter};
use crate::env::Place;
use crate::{closure::Closure, env::Place};
use super::{
Callable, Environment,
@@ -71,6 +71,8 @@ pub enum ConValue {
Quote(Box<Expr>),
/// A callable thing
Function(Rc<Function>),
/// A closure, capturing by reference
Closure(Rc<Closure>),
/// A built-in function
Builtin(&'static Builtin),
}
@@ -140,6 +142,7 @@ impl Callable for ConValue {
fn name(&self) -> Sym {
match self {
ConValue::Function(func) => func.name(),
ConValue::Closure(func) => func.name(),
ConValue::Builtin(func) => func.name(),
_ => "".into(),
}
@@ -147,6 +150,7 @@ impl Callable for ConValue {
fn call(&self, interpreter: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
match self {
Self::Function(func) => func.call(interpreter, args),
Self::Closure(func) => func.call(interpreter, args),
Self::Builtin(func) => func.call(interpreter, args),
_ => Err(Error::NotCallable(self.clone())),
}
@@ -368,6 +372,9 @@ impl std::fmt::Display for ConValue {
ConValue::Function(func) => {
write!(f, "{}", func.decl())
}
ConValue::Closure(func) => {
write!(f, "{}", func.as_ref())
}
ConValue::Builtin(func) => {
write!(f, "{}", func.description())
}

View File

@@ -159,30 +159,14 @@ impl Environment {
}
}
pub(crate) fn get_local(&self, name: Sym) -> IResult<ConValue> {
for EnvFrame { binds, .. } in self.frames.iter().skip(2).rev() {
if let Some(var) = binds.get(&name) {
if let Some(var) = self.values.get(*var) {
return match var {
Some(value) => Ok(value.clone()),
None => Err(Error::NotInitialized(name)),
};
}
}
}
Err(Error::NotDefined(name))
}
/// Resolves the [Place] associated with a [Sym]
pub fn id_of(&self, name: Sym) -> IResult<Place> {
for EnvFrame { binds, .. } in self.frames.iter().rev() {
if let Some(id) = binds.get(&name).copied() {
return Ok(Place::Local(id));
}
}
self.global
.contains_key(&name)
.then_some(Place::Global(name))
.ok_or(Error::NotDefined(name))
Ok(Place::Global(name))
}
pub fn get_id(&self, at: Place) -> Option<&ConValue> {

View File

@@ -1,5 +1,5 @@
//! Collects the "Upvars" of a function at the point of its creation, allowing variable capture
use crate::{convalue::ConValue, env::Environment};
use crate::env::{Environment, Place};
use cl_ast::{
Function, Let, Path, PathPart, Pattern, Sym,
ast_visitor::{visit::*, walk::Walk},
@@ -7,13 +7,18 @@ use cl_ast::{
use std::collections::{HashMap, HashSet};
pub fn collect_upvars(f: &Function, env: &Environment) -> super::Upvars {
CollectUpvars::new(env).get_upvars(f)
CollectUpvars::new(env)
.visit(f)
.finish()
.into_iter()
.map(|(k, v)| (k, env.get_id(v).cloned()))
.collect()
}
#[derive(Clone, Debug)]
pub struct CollectUpvars<'env> {
env: &'env Environment,
upvars: HashMap<Sym, Option<ConValue>>,
upvars: HashMap<Sym, Place>,
blacklist: HashSet<Sym>,
}
@@ -21,9 +26,9 @@ impl<'env> CollectUpvars<'env> {
pub fn new(env: &'env Environment) -> Self {
Self { upvars: HashMap::new(), blacklist: HashSet::new(), env }
}
pub fn get_upvars(mut self, f: &cl_ast::Function) -> HashMap<Sym, Option<ConValue>> {
self.visit_function(f);
self.upvars
pub fn finish(&mut self) -> HashMap<Sym, Place> {
std::mem::take(&mut self.upvars)
}
pub fn add_upvar(&mut self, name: &Sym) {
@@ -31,8 +36,8 @@ impl<'env> CollectUpvars<'env> {
if blacklist.contains(name) || upvars.contains_key(name) {
return;
}
if let Ok(upvar) = env.get_local(*name) {
upvars.insert(*name, Some(upvar));
if let Ok(place) = env.id_of(*name) {
upvars.insert(*name, place);
}
}

View File

@@ -295,6 +295,7 @@ impl Interpret for ExprKind {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
match self {
ExprKind::Empty => Ok(ConValue::Empty),
ExprKind::Closure(v) => v.interpret(env),
ExprKind::Quote(q) => q.interpret(env),
ExprKind::Let(v) => v.interpret(env),
ExprKind::Match(v) => v.interpret(env),
@@ -324,6 +325,14 @@ impl Interpret for ExprKind {
}
}
impl Interpret for Closure {
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
Ok(ConValue::Closure(
crate::closure::Closure::new(env, self).into(),
))
}
}
impl Interpret for Quote {
fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
// TODO: squoosh down into a ConValue?

View File

@@ -23,6 +23,8 @@ pub mod interpret;
pub mod function;
pub mod closure;
pub mod builtin;
pub mod pattern;