cl-ast: Add inline closure expressions
This commit is contained in:
parent
6c6d2d04a7
commit
e6156343c3
@ -317,6 +317,8 @@ pub enum ExprKind {
|
|||||||
/// An empty expression: `(` `)`
|
/// An empty expression: `(` `)`
|
||||||
#[default]
|
#[default]
|
||||||
Empty,
|
Empty,
|
||||||
|
/// A [Closure] expression: `|` [`Expr`] `|` ( -> [`Ty`])? [`Expr`]
|
||||||
|
Closure(Closure),
|
||||||
/// A [Tuple] expression: `(` [`Expr`] (`,` [`Expr`])+ `)`
|
/// A [Tuple] expression: `(` [`Expr`] (`,` [`Expr`])+ `)`
|
||||||
Tuple(Tuple),
|
Tuple(Tuple),
|
||||||
/// A [Struct creation](Structor) expression: [Path] `{` ([Fielder] `,`)* [Fielder]? `}`
|
/// A [Struct creation](Structor) expression: [Path] `{` ([Fielder] `,`)* [Fielder]? `}`
|
||||||
@ -371,6 +373,13 @@ pub enum ExprKind {
|
|||||||
Continue,
|
Continue,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A Closure [expression](Expr): `|` [`Expr`] `|` ( -> [`Ty`])? [`Expr`]
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub struct Closure {
|
||||||
|
pub arg: Box<Pattern>,
|
||||||
|
pub body: Box<Expr>,
|
||||||
|
}
|
||||||
|
|
||||||
/// A [Tuple] expression: `(` [`Expr`] (`,` [`Expr`])+ `)`
|
/// A [Tuple] expression: `(` [`Expr`] (`,` [`Expr`])+ `)`
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct Tuple {
|
pub struct Tuple {
|
||||||
|
@ -51,6 +51,7 @@ impl_from! {
|
|||||||
}
|
}
|
||||||
impl From for ExprKind {
|
impl From for ExprKind {
|
||||||
Let => ExprKind::Let,
|
Let => ExprKind::Let,
|
||||||
|
Closure => ExprKind::Closure,
|
||||||
Quote => ExprKind::Quote,
|
Quote => ExprKind::Quote,
|
||||||
Match => ExprKind::Match,
|
Match => ExprKind::Match,
|
||||||
Assign => ExprKind::Assign,
|
Assign => ExprKind::Assign,
|
||||||
|
@ -402,6 +402,7 @@ impl Display for ExprKind {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
ExprKind::Empty => "()".fmt(f),
|
ExprKind::Empty => "()".fmt(f),
|
||||||
|
ExprKind::Closure(v) => v.fmt(f),
|
||||||
ExprKind::Quote(v) => v.fmt(f),
|
ExprKind::Quote(v) => v.fmt(f),
|
||||||
ExprKind::Let(v) => v.fmt(f),
|
ExprKind::Let(v) => v.fmt(f),
|
||||||
ExprKind::Match(v) => v.fmt(f),
|
ExprKind::Match(v) => v.fmt(f),
|
||||||
@ -431,6 +432,17 @@ impl Display for ExprKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for Closure {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let Self { arg, body } = self;
|
||||||
|
match arg.as_ref() {
|
||||||
|
Pattern::Tuple(args) => separate(args, ", ")(f.delimit_with("|", "|")),
|
||||||
|
_ => arg.fmt(f),
|
||||||
|
}?;
|
||||||
|
write!(f, " {body}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Display for Quote {
|
impl Display for Quote {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let Self { quote } = self;
|
let Self { quote } = self;
|
||||||
|
@ -283,6 +283,7 @@ impl WeightOf for ExprKind {
|
|||||||
fn weight_of(&self) -> usize {
|
fn weight_of(&self) -> usize {
|
||||||
match self {
|
match self {
|
||||||
ExprKind::Empty => size_of_val(self),
|
ExprKind::Empty => size_of_val(self),
|
||||||
|
ExprKind::Closure(v) => v.weight_of(),
|
||||||
ExprKind::Quote(v) => v.weight_of(),
|
ExprKind::Quote(v) => v.weight_of(),
|
||||||
ExprKind::Let(v) => v.weight_of(),
|
ExprKind::Let(v) => v.weight_of(),
|
||||||
ExprKind::Match(v) => v.weight_of(),
|
ExprKind::Match(v) => v.weight_of(),
|
||||||
@ -312,6 +313,13 @@ impl WeightOf for ExprKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl WeightOf for Closure {
|
||||||
|
fn weight_of(&self) -> usize {
|
||||||
|
let Self { arg, body } = self;
|
||||||
|
arg.weight_of() + body.weight_of()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl WeightOf for Quote {
|
impl WeightOf for Quote {
|
||||||
fn weight_of(&self) -> usize {
|
fn weight_of(&self) -> usize {
|
||||||
let Self { quote } = self;
|
let Self { quote } = self;
|
||||||
|
@ -236,6 +236,10 @@ pub trait Fold {
|
|||||||
fn fold_expr_kind(&mut self, kind: ExprKind) -> ExprKind {
|
fn fold_expr_kind(&mut self, kind: ExprKind) -> ExprKind {
|
||||||
or_fold_expr_kind(self, kind)
|
or_fold_expr_kind(self, kind)
|
||||||
}
|
}
|
||||||
|
fn fold_closure(&mut self, value: Closure) -> Closure {
|
||||||
|
let Closure { arg, body } = value;
|
||||||
|
Closure { arg: Box::new(self.fold_pattern(*arg)), body: Box::new(self.fold_expr(*body)) }
|
||||||
|
}
|
||||||
fn fold_let(&mut self, l: Let) -> Let {
|
fn fold_let(&mut self, l: Let) -> Let {
|
||||||
let Let { mutable, name, ty, init } = l;
|
let Let { mutable, name, ty, init } = l;
|
||||||
Let {
|
Let {
|
||||||
@ -547,6 +551,7 @@ pub fn or_fold_stmt_kind<F: Fold + ?Sized>(folder: &mut F, kind: StmtKind) -> St
|
|||||||
pub fn or_fold_expr_kind<F: Fold + ?Sized>(folder: &mut F, kind: ExprKind) -> ExprKind {
|
pub fn or_fold_expr_kind<F: Fold + ?Sized>(folder: &mut F, kind: ExprKind) -> ExprKind {
|
||||||
match kind {
|
match kind {
|
||||||
ExprKind::Empty => ExprKind::Empty,
|
ExprKind::Empty => ExprKind::Empty,
|
||||||
|
ExprKind::Closure(c) => ExprKind::Closure(folder.fold_closure(c)),
|
||||||
ExprKind::Quote(q) => ExprKind::Quote(q), // quoted expressions are left unmodified
|
ExprKind::Quote(q) => ExprKind::Quote(q), // quoted expressions are left unmodified
|
||||||
ExprKind::Let(l) => ExprKind::Let(folder.fold_let(l)),
|
ExprKind::Let(l) => ExprKind::Let(folder.fold_let(l)),
|
||||||
ExprKind::Match(m) => ExprKind::Match(folder.fold_match(m)),
|
ExprKind::Match(m) => ExprKind::Match(folder.fold_match(m)),
|
||||||
|
@ -17,8 +17,9 @@ use super::walk::Walk;
|
|||||||
pub trait Visit<'a>: Sized {
|
pub trait Visit<'a>: Sized {
|
||||||
/// Visits a [Walker](Walk)
|
/// Visits a [Walker](Walk)
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit<W: Walk>(&mut self, walker: &'a W) {
|
fn visit<W: Walk>(&mut self, walker: &'a W) -> &mut Self {
|
||||||
walker.visit_in(self)
|
walker.visit_in(self);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
/// Visits the children of a [Walker](Walk)
|
/// Visits the children of a [Walker](Walk)
|
||||||
fn visit_children<W: Walk>(&mut self, walker: &'a W) {
|
fn visit_children<W: Walk>(&mut self, walker: &'a W) {
|
||||||
@ -160,6 +161,9 @@ pub trait Visit<'a>: Sized {
|
|||||||
fn visit_expr_kind(&mut self, value: &'a ExprKind) {
|
fn visit_expr_kind(&mut self, value: &'a ExprKind) {
|
||||||
value.children(self)
|
value.children(self)
|
||||||
}
|
}
|
||||||
|
fn visit_closure(&mut self, value: &'a Closure) {
|
||||||
|
value.children(self)
|
||||||
|
}
|
||||||
fn visit_quote(&mut self, value: &'a Quote) {
|
fn visit_quote(&mut self, value: &'a Quote) {
|
||||||
value.children(self)
|
value.children(self)
|
||||||
}
|
}
|
||||||
|
@ -504,25 +504,26 @@ impl Walk for ExprKind {
|
|||||||
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
|
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
|
||||||
match self {
|
match self {
|
||||||
ExprKind::Empty => {}
|
ExprKind::Empty => {}
|
||||||
|
ExprKind::Closure(value) => value.visit_in(v),
|
||||||
|
ExprKind::Tuple(value) => value.visit_in(v),
|
||||||
|
ExprKind::Structor(value) => value.visit_in(v),
|
||||||
|
ExprKind::Array(value) => value.visit_in(v),
|
||||||
|
ExprKind::ArrayRep(value) => value.visit_in(v),
|
||||||
|
ExprKind::AddrOf(value) => value.visit_in(v),
|
||||||
ExprKind::Quote(value) => value.visit_in(v),
|
ExprKind::Quote(value) => value.visit_in(v),
|
||||||
ExprKind::Let(value) => value.visit_in(v),
|
ExprKind::Literal(value) => value.visit_in(v),
|
||||||
ExprKind::Match(value) => value.visit_in(v),
|
ExprKind::Group(value) => value.visit_in(v),
|
||||||
|
ExprKind::Block(value) => value.visit_in(v),
|
||||||
ExprKind::Assign(value) => value.visit_in(v),
|
ExprKind::Assign(value) => value.visit_in(v),
|
||||||
ExprKind::Modify(value) => value.visit_in(v),
|
ExprKind::Modify(value) => value.visit_in(v),
|
||||||
ExprKind::Binary(value) => value.visit_in(v),
|
ExprKind::Binary(value) => value.visit_in(v),
|
||||||
ExprKind::Unary(value) => value.visit_in(v),
|
ExprKind::Unary(value) => value.visit_in(v),
|
||||||
ExprKind::Cast(value) => value.visit_in(v),
|
|
||||||
ExprKind::Member(value) => value.visit_in(v),
|
ExprKind::Member(value) => value.visit_in(v),
|
||||||
ExprKind::Index(value) => value.visit_in(v),
|
ExprKind::Index(value) => value.visit_in(v),
|
||||||
ExprKind::Structor(value) => value.visit_in(v),
|
ExprKind::Cast(value) => value.visit_in(v),
|
||||||
ExprKind::Path(value) => value.visit_in(v),
|
ExprKind::Path(value) => value.visit_in(v),
|
||||||
ExprKind::Literal(value) => value.visit_in(v),
|
ExprKind::Let(value) => value.visit_in(v),
|
||||||
ExprKind::Array(value) => value.visit_in(v),
|
ExprKind::Match(value) => value.visit_in(v),
|
||||||
ExprKind::ArrayRep(value) => value.visit_in(v),
|
|
||||||
ExprKind::AddrOf(value) => value.visit_in(v),
|
|
||||||
ExprKind::Block(value) => value.visit_in(v),
|
|
||||||
ExprKind::Group(value) => value.visit_in(v),
|
|
||||||
ExprKind::Tuple(value) => value.visit_in(v),
|
|
||||||
ExprKind::While(value) => value.visit_in(v),
|
ExprKind::While(value) => value.visit_in(v),
|
||||||
ExprKind::If(value) => value.visit_in(v),
|
ExprKind::If(value) => value.visit_in(v),
|
||||||
ExprKind::For(value) => value.visit_in(v),
|
ExprKind::For(value) => value.visit_in(v),
|
||||||
@ -533,6 +534,18 @@ impl Walk for ExprKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Walk for Closure {
|
||||||
|
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
|
||||||
|
v.visit_closure(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn children<'a, V: Visit<'a>>(&'a self, v: &mut V) {
|
||||||
|
let Self { arg, body } = self;
|
||||||
|
v.visit_pattern(arg);
|
||||||
|
v.visit_expr(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Walk for Tuple {
|
impl Walk for Tuple {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
|
fn visit_in<'a, V: Visit<'a>>(&'a self, v: &mut V) {
|
||||||
|
68
compiler/cl-interpret/src/closure.rs
Normal file
68
compiler/cl-interpret/src/closure.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@
|
|||||||
//! The most permanent fix is a temporary one.
|
//! The most permanent fix is a temporary one.
|
||||||
use cl_ast::{Expr, Sym, format::FmtAdapter};
|
use cl_ast::{Expr, Sym, format::FmtAdapter};
|
||||||
|
|
||||||
use crate::env::Place;
|
use crate::{closure::Closure, env::Place};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
Callable, Environment,
|
Callable, Environment,
|
||||||
@ -71,6 +71,8 @@ pub enum ConValue {
|
|||||||
Quote(Box<Expr>),
|
Quote(Box<Expr>),
|
||||||
/// A callable thing
|
/// A callable thing
|
||||||
Function(Rc<Function>),
|
Function(Rc<Function>),
|
||||||
|
/// A closure, capturing by reference
|
||||||
|
Closure(Rc<Closure>),
|
||||||
/// A built-in function
|
/// A built-in function
|
||||||
Builtin(&'static Builtin),
|
Builtin(&'static Builtin),
|
||||||
}
|
}
|
||||||
@ -140,6 +142,7 @@ impl Callable for ConValue {
|
|||||||
fn name(&self) -> Sym {
|
fn name(&self) -> Sym {
|
||||||
match self {
|
match self {
|
||||||
ConValue::Function(func) => func.name(),
|
ConValue::Function(func) => func.name(),
|
||||||
|
ConValue::Closure(func) => func.name(),
|
||||||
ConValue::Builtin(func) => func.name(),
|
ConValue::Builtin(func) => func.name(),
|
||||||
_ => "".into(),
|
_ => "".into(),
|
||||||
}
|
}
|
||||||
@ -147,6 +150,7 @@ impl Callable for ConValue {
|
|||||||
fn call(&self, interpreter: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
|
fn call(&self, interpreter: &mut Environment, args: &[ConValue]) -> IResult<ConValue> {
|
||||||
match self {
|
match self {
|
||||||
Self::Function(func) => func.call(interpreter, args),
|
Self::Function(func) => func.call(interpreter, args),
|
||||||
|
Self::Closure(func) => func.call(interpreter, args),
|
||||||
Self::Builtin(func) => func.call(interpreter, args),
|
Self::Builtin(func) => func.call(interpreter, args),
|
||||||
_ => Err(Error::NotCallable(self.clone())),
|
_ => Err(Error::NotCallable(self.clone())),
|
||||||
}
|
}
|
||||||
@ -368,6 +372,9 @@ impl std::fmt::Display for ConValue {
|
|||||||
ConValue::Function(func) => {
|
ConValue::Function(func) => {
|
||||||
write!(f, "{}", func.decl())
|
write!(f, "{}", func.decl())
|
||||||
}
|
}
|
||||||
|
ConValue::Closure(func) => {
|
||||||
|
write!(f, "{}", func.as_ref())
|
||||||
|
}
|
||||||
ConValue::Builtin(func) => {
|
ConValue::Builtin(func) => {
|
||||||
write!(f, "{}", func.description())
|
write!(f, "{}", func.description())
|
||||||
}
|
}
|
||||||
|
@ -159,30 +159,14 @@ impl Environment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_local(&self, name: Sym) -> IResult<ConValue> {
|
/// Resolves the [Place] associated with a [Sym]
|
||||||
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))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn id_of(&self, name: Sym) -> IResult<Place> {
|
pub fn id_of(&self, name: Sym) -> IResult<Place> {
|
||||||
for EnvFrame { binds, .. } in self.frames.iter().rev() {
|
for EnvFrame { binds, .. } in self.frames.iter().rev() {
|
||||||
if let Some(id) = binds.get(&name).copied() {
|
if let Some(id) = binds.get(&name).copied() {
|
||||||
return Ok(Place::Local(id));
|
return Ok(Place::Local(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.global
|
Ok(Place::Global(name))
|
||||||
.contains_key(&name)
|
|
||||||
.then_some(Place::Global(name))
|
|
||||||
.ok_or(Error::NotDefined(name))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_id(&self, at: Place) -> Option<&ConValue> {
|
pub fn get_id(&self, at: Place) -> Option<&ConValue> {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//! Collects the "Upvars" of a function at the point of its creation, allowing variable capture
|
//! 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::{
|
use cl_ast::{
|
||||||
Function, Let, Path, PathPart, Pattern, Sym,
|
Function, Let, Path, PathPart, Pattern, Sym,
|
||||||
ast_visitor::{visit::*, walk::Walk},
|
ast_visitor::{visit::*, walk::Walk},
|
||||||
@ -7,13 +7,18 @@ use cl_ast::{
|
|||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
pub fn collect_upvars(f: &Function, env: &Environment) -> super::Upvars {
|
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)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct CollectUpvars<'env> {
|
pub struct CollectUpvars<'env> {
|
||||||
env: &'env Environment,
|
env: &'env Environment,
|
||||||
upvars: HashMap<Sym, Option<ConValue>>,
|
upvars: HashMap<Sym, Place>,
|
||||||
blacklist: HashSet<Sym>,
|
blacklist: HashSet<Sym>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,9 +26,9 @@ impl<'env> CollectUpvars<'env> {
|
|||||||
pub fn new(env: &'env Environment) -> Self {
|
pub fn new(env: &'env Environment) -> Self {
|
||||||
Self { upvars: HashMap::new(), blacklist: HashSet::new(), env }
|
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);
|
pub fn finish(&mut self) -> HashMap<Sym, Place> {
|
||||||
self.upvars
|
std::mem::take(&mut self.upvars)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_upvar(&mut self, name: &Sym) {
|
pub fn add_upvar(&mut self, name: &Sym) {
|
||||||
@ -31,8 +36,8 @@ impl<'env> CollectUpvars<'env> {
|
|||||||
if blacklist.contains(name) || upvars.contains_key(name) {
|
if blacklist.contains(name) || upvars.contains_key(name) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let Ok(upvar) = env.get_local(*name) {
|
if let Ok(place) = env.id_of(*name) {
|
||||||
upvars.insert(*name, Some(upvar));
|
upvars.insert(*name, place);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,6 +295,7 @@ impl Interpret for ExprKind {
|
|||||||
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
|
fn interpret(&self, env: &mut Environment) -> IResult<ConValue> {
|
||||||
match self {
|
match self {
|
||||||
ExprKind::Empty => Ok(ConValue::Empty),
|
ExprKind::Empty => Ok(ConValue::Empty),
|
||||||
|
ExprKind::Closure(v) => v.interpret(env),
|
||||||
ExprKind::Quote(q) => q.interpret(env),
|
ExprKind::Quote(q) => q.interpret(env),
|
||||||
ExprKind::Let(v) => v.interpret(env),
|
ExprKind::Let(v) => v.interpret(env),
|
||||||
ExprKind::Match(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 {
|
impl Interpret for Quote {
|
||||||
fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
|
fn interpret(&self, _env: &mut Environment) -> IResult<ConValue> {
|
||||||
// TODO: squoosh down into a ConValue?
|
// TODO: squoosh down into a ConValue?
|
||||||
|
@ -23,6 +23,8 @@ pub mod interpret;
|
|||||||
|
|
||||||
pub mod function;
|
pub mod function;
|
||||||
|
|
||||||
|
pub mod closure;
|
||||||
|
|
||||||
pub mod builtin;
|
pub mod builtin;
|
||||||
|
|
||||||
pub mod pattern;
|
pub mod pattern;
|
||||||
|
@ -97,6 +97,7 @@ pub enum Parsing {
|
|||||||
|
|
||||||
Expr,
|
Expr,
|
||||||
ExprKind,
|
ExprKind,
|
||||||
|
Closure,
|
||||||
Assign,
|
Assign,
|
||||||
AssignKind,
|
AssignKind,
|
||||||
Binary,
|
Binary,
|
||||||
@ -214,6 +215,7 @@ impl Display for Parsing {
|
|||||||
|
|
||||||
Parsing::Expr => "an expression",
|
Parsing::Expr => "an expression",
|
||||||
Parsing::ExprKind => "an expression",
|
Parsing::ExprKind => "an expression",
|
||||||
|
Parsing::Closure => "an anonymous function",
|
||||||
Parsing::Assign => "an assignment",
|
Parsing::Assign => "an assignment",
|
||||||
Parsing::AssignKind => "an assignment operator",
|
Parsing::AssignKind => "an assignment operator",
|
||||||
Parsing::Binary => "a binary expression",
|
Parsing::Binary => "a binary expression",
|
||||||
|
@ -884,6 +884,31 @@ impl Parse<'_> for Expr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Parse<'_> for Closure {
|
||||||
|
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
||||||
|
let args = sep(
|
||||||
|
Pattern::parse,
|
||||||
|
TokenKind::Comma,
|
||||||
|
TokenKind::Bar,
|
||||||
|
Parsing::Closure,
|
||||||
|
);
|
||||||
|
|
||||||
|
let arg = match p.peek_kind(Parsing::Closure)? {
|
||||||
|
TokenKind::BarBar => {
|
||||||
|
p.consume_peeked();
|
||||||
|
Box::new(Pattern::Tuple(vec![]))
|
||||||
|
}
|
||||||
|
_ => Box::new(delim(
|
||||||
|
|p| args(p).map(Pattern::Tuple),
|
||||||
|
(TokenKind::Bar, TokenKind::Bar),
|
||||||
|
Parsing::Closure,
|
||||||
|
)(p)?),
|
||||||
|
};
|
||||||
|
let body = p.parse()?;
|
||||||
|
Ok(Closure { arg, body })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse<'_> for Quote {
|
impl Parse<'_> for Quote {
|
||||||
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
||||||
let quote = delim(
|
let quote = delim(
|
||||||
@ -1104,6 +1129,12 @@ impl Parse<'_> for MatchArm {
|
|||||||
fn ret_body(p: &mut Parser, while_parsing: Parsing) -> PResult<Option<Box<Expr>>> {
|
fn ret_body(p: &mut Parser, while_parsing: Parsing) -> PResult<Option<Box<Expr>>> {
|
||||||
Ok(match p.peek_kind(while_parsing)? {
|
Ok(match p.peek_kind(while_parsing)? {
|
||||||
TokenKind::Semi => None,
|
TokenKind::Semi => None,
|
||||||
_ => Some(Expr::parse(p)?.into()),
|
_ => Some(p.parse()?),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'t, P: Parse<'t>> Parse<'t> for Box<P> {
|
||||||
|
fn parse(p: &mut Parser<'t>) -> PResult<Self> {
|
||||||
|
p.parse().map(Box::new)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -17,6 +17,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
|
|||||||
literal_like!() => Literal::parse(p)?.into(),
|
literal_like!() => Literal::parse(p)?.into(),
|
||||||
path_like!() => exprkind_pathlike(p)?,
|
path_like!() => exprkind_pathlike(p)?,
|
||||||
TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(p)?.into(),
|
TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(p)?.into(),
|
||||||
|
TokenKind::Bar | TokenKind::BarBar => Closure::parse(p)?.into(),
|
||||||
TokenKind::Grave => Quote::parse(p)?.into(),
|
TokenKind::Grave => Quote::parse(p)?.into(),
|
||||||
TokenKind::LCurly => Block::parse(p)?.into(),
|
TokenKind::LCurly => Block::parse(p)?.into(),
|
||||||
TokenKind::LBrack => exprkind_arraylike(p)?,
|
TokenKind::LBrack => exprkind_arraylike(p)?,
|
||||||
|
@ -428,6 +428,7 @@ pub mod clangify {
|
|||||||
impl CLangify for ExprKind {
|
impl CLangify for ExprKind {
|
||||||
fn print(&self, y: &mut CLangifier) {
|
fn print(&self, y: &mut CLangifier) {
|
||||||
match self {
|
match self {
|
||||||
|
ExprKind::Closure(k) => todo!("Downgrade {k}"),
|
||||||
ExprKind::Quote(k) => k.print(y),
|
ExprKind::Quote(k) => k.print(y),
|
||||||
ExprKind::Let(k) => k.print(y),
|
ExprKind::Let(k) => k.print(y),
|
||||||
ExprKind::Match(k) => k.print(y),
|
ExprKind::Match(k) => k.print(y),
|
||||||
|
@ -370,6 +370,7 @@ pub mod yamlify {
|
|||||||
impl Yamlify for ExprKind {
|
impl Yamlify for ExprKind {
|
||||||
fn yaml(&self, y: &mut Yamler) {
|
fn yaml(&self, y: &mut Yamler) {
|
||||||
match self {
|
match self {
|
||||||
|
ExprKind::Closure(k) => k.yaml(y),
|
||||||
ExprKind::Quote(k) => k.yaml(y),
|
ExprKind::Quote(k) => k.yaml(y),
|
||||||
ExprKind::Let(k) => k.yaml(y),
|
ExprKind::Let(k) => k.yaml(y),
|
||||||
ExprKind::Match(k) => k.yaml(y),
|
ExprKind::Match(k) => k.yaml(y),
|
||||||
@ -401,6 +402,12 @@ pub mod yamlify {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl Yamlify for Closure {
|
||||||
|
fn yaml(&self, y: &mut Yamler) {
|
||||||
|
let Self { arg, body } = self;
|
||||||
|
y.key("Closure").pair("arg", arg).pair("body", body);
|
||||||
|
}
|
||||||
|
}
|
||||||
impl Yamlify for Quote {
|
impl Yamlify for Quote {
|
||||||
fn yaml(&self, y: &mut Yamler) {
|
fn yaml(&self, y: &mut Yamler) {
|
||||||
y.key("Quote").value(self);
|
y.key("Quote").value(self);
|
||||||
|
@ -30,6 +30,7 @@ impl<'a> Inference<'a> for cl_ast::ExprKind {
|
|||||||
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> Result<Handle, InferenceError> {
|
fn infer(&'a self, e: &mut InferenceEngine<'_, 'a>) -> Result<Handle, InferenceError> {
|
||||||
match self {
|
match self {
|
||||||
ExprKind::Empty => Ok(e.empty()),
|
ExprKind::Empty => Ok(e.empty()),
|
||||||
|
ExprKind::Closure(_) => todo!("Infer the type of a closure"),
|
||||||
ExprKind::Tuple(tuple) => tuple.infer(e),
|
ExprKind::Tuple(tuple) => tuple.infer(e),
|
||||||
ExprKind::Structor(structor) => structor.infer(e),
|
ExprKind::Structor(structor) => structor.infer(e),
|
||||||
ExprKind::Array(array) => array.infer(e),
|
ExprKind::Array(array) => array.infer(e),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user