Skip to content

Commit

Permalink
checkpoint 3
Browse files Browse the repository at this point in the history
  • Loading branch information
aljazerzen committed Jan 18, 2024
1 parent 9e6d6e9 commit ad34f4e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
19 changes: 15 additions & 4 deletions prqlc/prqlc/src/semantic/resolver/expr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use itertools::Itertools;

use prqlc_ast::{TupleField, Ty, TyKind};
use prqlc_ast::{Span, TupleField, Ty, TyKind};

use crate::ir::decl::{DeclKind, Module};
use crate::ir::pl::*;
Expand Down Expand Up @@ -202,7 +202,20 @@ impl PlFold for Resolver<'_> {
..node
},
};
let mut r = Box::new(self.static_eval(r)?);
self.finish_expr_resolve(Box::new(r), id, alias, span)
}
}

impl Resolver<'_> {
fn finish_expr_resolve(
&mut self,
expr: Box<Expr>,
id: usize,
alias: Box<Option<String>>,
span: Box<Option<Span>>,
) -> Result<Expr> {
let mut r = Box::new(self.static_eval(*expr)?);

r.id = r.id.or(Some(id));
r.alias = r.alias.or(*alias);
r.span = r.span.or(*span);
Expand Down Expand Up @@ -235,9 +248,7 @@ impl PlFold for Resolver<'_> {
}
Ok(*r)
}
}

impl Resolver<'_> {
pub fn resolve_column_exclusion(&mut self, expr: Expr) -> Result<Expr> {
let expr = self.fold_expr(expr)?;
let except = self.coerce_into_tuple(expr)?;
Expand Down
66 changes: 35 additions & 31 deletions prqlc/prqlc/src/semantic/resolver/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,47 +94,51 @@ impl Resolver<'_> {
}
} else {
// base case: materialize
log::debug!("stack_push for {}", closure.as_debug_name());
self.materialize_function(closure)?
};

let (func_env, body) = env_of_closure(closure);
// pop the env
self.root_mod.module.stack_pop(NS_PARAM).unwrap();

self.root_mod.module.stack_push(NS_PARAM, func_env);
Ok(Expr { span, ..res })
}

// fold again, to resolve inner variables & functions
let body = self.fold_expr(body)?;
fn materialize_function(&mut self, closure: Box<Func>) -> Result<Expr> {
log::debug!("stack_push for {}", closure.as_debug_name());

// remove param decls
log::debug!("stack_pop: {:?}", body.id);
let func_env = self.root_mod.module.stack_pop(NS_PARAM).unwrap();
let (func_env, body) = env_of_closure(closure);

if let ExprKind::Func(mut inner_closure) = body.kind {
// body couldn't been resolved - construct a closure to be evaluated later
self.root_mod.module.stack_push(NS_PARAM, func_env);

inner_closure.env = func_env.into_exprs();
// fold again, to resolve inner variables & functions
let body = self.fold_expr(body)?;

let (got, missing) = inner_closure.params.split_at(inner_closure.args.len());
let missing = missing.to_vec();
inner_closure.params = got.to_vec();
// remove param decls
log::debug!("stack_pop: {:?}", body.id);
let func_env = self.root_mod.module.stack_pop(NS_PARAM).unwrap();

Expr::new(ExprKind::Func(Box::new(Func {
name_hint: None,
args: vec![],
params: missing,
named_params: vec![],
body: Box::new(Expr::new(ExprKind::Func(inner_closure))),
return_ty: None,
env: HashMap::new(),
})))
} else {
// resolved, return result
body
}
};
Ok(if let ExprKind::Func(mut inner_closure) = body.kind {
// body couldn't been resolved - construct a closure to be evaluated later

// pop the env
self.root_mod.module.stack_pop(NS_PARAM).unwrap();
inner_closure.env = func_env.into_exprs();

Ok(Expr { span, ..res })
let (got, missing) = inner_closure.params.split_at(inner_closure.args.len());
let missing = missing.to_vec();
inner_closure.params = got.to_vec();

Expr::new(ExprKind::Func(Box::new(Func {
name_hint: None,
args: vec![],
params: missing,
named_params: vec![],
body: Box::new(Expr::new(ExprKind::Func(inner_closure))),
return_ty: None,
env: HashMap::new(),
})))
} else {
// resolved, return result
body
})
}

pub fn fold_function_types(&mut self, mut closure: Box<Func>) -> Result<Box<Func>> {
Expand Down

0 comments on commit ad34f4e

Please sign in to comment.