λ Support functions! #5
@@ -436,143 +436,4 @@ fn merge_prereq<T>(left: &mut Vec<T>, prereq: Option<T>) {
|
|||||||
if let Some(item) = prereq {
|
if let Some(item) = prereq {
|
||||||
left.push(item)
|
left.push(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
// use super::*;
|
|
||||||
// use crate::syntax::Location;
|
|
||||||
//
|
|
||||||
// fn one() -> syntax::Expression {
|
|
||||||
// syntax::Expression::Value(
|
|
||||||
// Location::manufactured(),
|
|
||||||
// syntax::Value::Number(None, None, 1),
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fn vec_contains<T, F: Fn(&T) -> bool>(x: &[T], f: F) -> bool {
|
|
||||||
// for x in x.iter() {
|
|
||||||
// if f(x) {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// false
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fn infer_expression(
|
|
||||||
// x: syntax::Expression,
|
|
||||||
// ) -> (ir::Expression, Vec<ir::Statement>, Vec<Constraint>, Type) {
|
|
||||||
// let mut constraints = Vec::new();
|
|
||||||
// let renames = HashMap::new();
|
|
||||||
// let mut bindings = HashMap::new();
|
|
||||||
// let (stmts, expr, ty) = convert_expression(x, &mut constraints, &renames, &mut bindings);
|
|
||||||
// (expr, stmts, constraints, ty)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fn infer_top_level(x: syntax::TopLevel) -> (Vec<ir::TopLevel>, Vec<Constraint>) {
|
|
||||||
// let mut constraints = Vec::new();
|
|
||||||
// let mut renames = HashMap::new();
|
|
||||||
// let mut bindings = HashMap::new();
|
|
||||||
// let res = convert_top_level(x, &mut constraints, &mut renames, &mut bindings);
|
|
||||||
// (res, constraints)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// #[test]
|
|
||||||
// fn constant_one() {
|
|
||||||
// let (expr, stmts, constraints, ty) = infer_expression(one());
|
|
||||||
// assert!(stmts.is_empty());
|
|
||||||
// assert!(matches!(
|
|
||||||
// expr,
|
|
||||||
// ir::Expression::Atomic(ir::ValueOrRef::Value(_, _, ir::Value::Unknown(None, 1)))
|
|
||||||
// ));
|
|
||||||
// assert!(vec_contains(&constraints, |x| matches!(
|
|
||||||
// x,
|
|
||||||
// Constraint::FitsInNumType(_, _, 1)
|
|
||||||
// )));
|
|
||||||
// assert!(vec_contains(
|
|
||||||
// &constraints,
|
|
||||||
// |x| matches!(x, Constraint::ConstantNumericType(_, t) if t == &ty)
|
|
||||||
// ));
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// #[test]
|
|
||||||
// fn one_plus_one() {
|
|
||||||
// let opo = syntax::Expression::Primitive(
|
|
||||||
// Location::manufactured(),
|
|
||||||
// "+".to_string(),
|
|
||||||
// vec![one(), one()],
|
|
||||||
// );
|
|
||||||
// let (expr, stmts, constraints, ty) = infer_expression(opo);
|
|
||||||
// assert!(stmts.is_empty());
|
|
||||||
// assert!(matches!(expr, ir::Expression::Primitive(_, t, ir::Primitive::Plus, _) if t == ty));
|
|
||||||
// assert!(vec_contains(&constraints, |x| matches!(
|
|
||||||
// x,
|
|
||||||
// Constraint::FitsInNumType(_, _, 1)
|
|
||||||
// )));
|
|
||||||
// assert!(vec_contains(
|
|
||||||
// &constraints,
|
|
||||||
// |x| matches!(x, Constraint::ConstantNumericType(_, t) if t != &ty)
|
|
||||||
// ));
|
|
||||||
// assert!(vec_contains(
|
|
||||||
// &constraints,
|
|
||||||
// |x| matches!(x, Constraint::ProperPrimitiveArgs(_, ir::Primitive::Plus, args, ret) if args.len() == 2 && ret == &ty)
|
|
||||||
// ));
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// #[test]
|
|
||||||
// fn one_plus_one_plus_one() {
|
|
||||||
// let stmt = syntax::TopLevel::parse(1, "x = 1 + 1 + 1;").expect("basic parse");
|
|
||||||
// let (stmts, constraints) = infer_top_level(stmt);
|
|
||||||
// assert_eq!(stmts.len(), 2);
|
|
||||||
// let ir::TopLevel::Statement(ir::Statement::Binding(
|
|
||||||
// _args,
|
|
||||||
// name1,
|
|
||||||
// temp_ty1,
|
|
||||||
// ir::Expression::Primitive(_, primty1, ir::Primitive::Plus, primargs1),
|
|
||||||
// )) = stmts.get(0).expect("item two")
|
|
||||||
// else {
|
|
||||||
// panic!("Failed to match first statement");
|
|
||||||
// };
|
|
||||||
// let ir::TopLevel::Statement(ir::Statement::Binding(
|
|
||||||
// _args,
|
|
||||||
// name2,
|
|
||||||
// temp_ty2,
|
|
||||||
// ir::Expression::Primitive(_, primty2, ir::Primitive::Plus, primargs2),
|
|
||||||
// )) = stmts.get(1).expect("item two")
|
|
||||||
// else {
|
|
||||||
// panic!("Failed to match second statement");
|
|
||||||
// };
|
|
||||||
// let &[ir::ValueOrRef::Value(_, ref left1ty, _), ir::ValueOrRef::Value(_, ref right1ty, _)] =
|
|
||||||
// &primargs1[..]
|
|
||||||
// else {
|
|
||||||
// panic!("Failed to match first arguments");
|
|
||||||
// };
|
|
||||||
// let &[ir::ValueOrRef::Ref(_, _, ref left2name), ir::ValueOrRef::Value(_, ref right2ty, _)] =
|
|
||||||
// &primargs2[..]
|
|
||||||
// else {
|
|
||||||
// panic!("Failed to match first arguments");
|
|
||||||
// };
|
|
||||||
// assert_ne!(name1, name2);
|
|
||||||
// assert_ne!(temp_ty1, temp_ty2);
|
|
||||||
// assert_ne!(primty1, primty2);
|
|
||||||
// assert_eq!(name1, left2name);
|
|
||||||
// assert!(vec_contains(
|
|
||||||
// &constraints,
|
|
||||||
// |x| matches!(x, Constraint::ConstantNumericType(_, t) if t == left1ty)
|
|
||||||
// ));
|
|
||||||
// assert!(vec_contains(
|
|
||||||
// &constraints,
|
|
||||||
// |x| matches!(x, Constraint::ConstantNumericType(_, t) if t == right1ty)
|
|
||||||
// ));
|
|
||||||
// assert!(vec_contains(
|
|
||||||
// &constraints,
|
|
||||||
// |x| matches!(x, Constraint::ConstantNumericType(_, t) if t == right2ty)
|
|
||||||
// ));
|
|
||||||
// for (i, s) in stmts.iter().enumerate() {
|
|
||||||
// println!("{}: {:?}", i, s);
|
|
||||||
// }
|
|
||||||
// for (i, c) in constraints.iter().enumerate() {
|
|
||||||
// println!("{}: {:?}", i, c);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user