Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gzanitti committed Aug 13, 2024
1 parent c05d545 commit f04832a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
2 changes: 1 addition & 1 deletion ast/src/analyzed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ pub struct PolynomialReference {
/// Guaranteed to be Some(_) after type checking is completed.
pub type_args: Option<Vec<Type>>,
///
pub resolved_impls: BTreeMap<Vec<Type>, Box<Expression>>,
pub resolved_impls: BTreeMap<Vec<Type>, usize>,
}

#[derive(
Expand Down
41 changes: 14 additions & 27 deletions pil-analyzer/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ pub enum Value<'a, T> {
Enum(&'a str, Option<Vec<Arc<Self>>>),
BuiltinFunction(BuiltinFunction),
Expression(AlgebraicExpression<T>),
TraitFunction(Closure<'a, T>),
}

impl<'a, T: FieldElement> From<T> for Value<'a, T> {
Expand Down Expand Up @@ -216,12 +215,6 @@ impl<'a, T: FieldElement> Value<'a, T> {
Value::Enum(name, _) => name.to_string(),
Value::BuiltinFunction(b) => format!("builtin_{b:?}"),
Value::Expression(_) => "expr".to_string(),
Value::TraitFunction(trait_function) => {
format!(
"trait_function<{}>",
trait_function.type_args.values().format(", ")
)
}
}
}

Expand Down Expand Up @@ -377,11 +370,6 @@ impl<'a, T: Display> Display for Value<'a, T> {
}
Value::BuiltinFunction(b) => write!(f, "{b:?}"),
Value::Expression(e) => write!(f, "{e}"),
Value::TraitFunction(trait_function) => write!(
f,
"trait_function<{}>",
trait_function.type_args.values().format(", ")
),
}
}
}
Expand Down Expand Up @@ -833,7 +821,7 @@ impl<'a, 'b, T: FieldElement, S: SymbolLookup<'a, T>> Evaluator<'a, 'b, T, S> {
.and_then(|type_args| poly.resolved_impls.get(type_args).as_ref().copied());

match impl_pos {
Some(expr) => {
Some(index) => {
let local_type_args = poly
.type_args
.clone()
Expand All @@ -849,16 +837,20 @@ impl<'a, 'b, T: FieldElement, S: SymbolLookup<'a, T>> Evaluator<'a, 'b, T, S> {
})
.unwrap();

let Expression::LambdaExpression(_, body) = &expr.as_ref() else {
unreachable!()
};
// let Expression::LambdaExpression(_, body) = &expr.as_ref() else {
// unreachable!()
// };

Value::Closure(Closure {
lambda: body,
environment: vec![],
type_args: local_type_args.clone(),
})
.into()
let symbol = self.symbols.lookup(&poly.name, &type_args)?;
println!("{:?}", symbol);
symbol

// Value::Closure(Closure {
// lambda: body,
// environment: vec![],
// type_args: local_type_args.clone(),
// })
// .into()
}
None => self.symbols.lookup(&poly.name, &type_args)?,
}
Expand Down Expand Up @@ -1004,11 +996,6 @@ impl<'a, 'b, T: FieldElement, S: SymbolLookup<'a, T>> Evaluator<'a, 'b, T, S> {
lambda,
environment,
type_args,
})
| Value::TraitFunction(Closure {
lambda,
environment,
type_args,
}) => {
assert_eq!(lambda.params.len(), arguments.len());
let matched_arguments =
Expand Down
11 changes: 5 additions & 6 deletions pil-analyzer/src/traits_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<'a> TraitsProcessor<'a> {

pub fn traits_resolution(
&self,
references: &mut Vec<&mut Reference>,
references: &mut [&mut Reference],
traits_functions_defs: HashMap<String, FunctionValueDefinition>,
) {
for r in references.iter_mut() {
Expand All @@ -44,25 +44,24 @@ impl<'a> TraitsProcessor<'a> {
&self,
collected_ref: (String, &Vec<Type>),
traits_functions_definition: &HashMap<String, FunctionValueDefinition>,
) -> Option<(String, BTreeMap<Vec<Type>, Box<Expression>>)> {
) -> Option<(String, BTreeMap<Vec<Type>, usize>)> {
let mut resolved_impl_pos = BTreeMap::new();

let Some(FunctionValueDefinition::TraitFunction(ref trait_decl, ref trait_fn)) =
traits_functions_definition.get(&collected_ref.0)
else {
return None;
};

if let Some(impls) = self.implementations.get(&trait_decl.name) {
for impl_ in impls.iter() {
for (index, impl_) in impls.iter().enumerate() {
let Some(impl_fn) = impl_.function_by_name(&trait_fn.name) else {
panic!(
"Could not find function {} for {}",
trait_fn.name, trait_decl.name
);
};

resolved_impl_pos.insert(collected_ref.1.clone(), impl_fn.body.clone());
resolved_impl_pos.insert(collected_ref.1.clone(), index);
}
}

Expand All @@ -76,7 +75,7 @@ impl<'a> TraitsProcessor<'a> {
fn update_reference(
ref_name: &str,
c: &mut Reference,
resolved_impl_pos: &BTreeMap<Vec<Type>, Box<Expression>>,
resolved_impl_pos: &BTreeMap<Vec<Type>, usize>,
) {
if let Reference::Poly(PolynomialReference {
name,
Expand Down

0 comments on commit f04832a

Please sign in to comment.