Add test for nested lambdas
This commit is contained in:
parent
888e64d485
commit
35bdd542f1
|
@ -1,5 +1,7 @@
|
||||||
#![cfg_attr(test, allow(non_snake_case))]
|
#![cfg_attr(test, allow(non_snake_case))]
|
||||||
|
|
||||||
|
use std::cell::{Cell, RefCell};
|
||||||
|
use std::collections::HashMap;
|
||||||
use uplc::ast::{Constant, Name, Program, Term, Unique};
|
use uplc::ast::{Constant, Name, Program, Term, Unique};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -10,12 +12,16 @@ pub struct Builder {
|
||||||
term: Term<Name>,
|
term: Term<Name>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Empty {
|
pub struct NeedsTerm {
|
||||||
version: (usize, usize, usize),
|
version: (usize, usize, usize),
|
||||||
|
// TODO: Hide these two behind interface
|
||||||
|
next_unique: Cell<isize>,
|
||||||
|
names: RefCell<HashMap<String, Unique>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LambdaBuilder<T> {
|
pub struct LambdaBuilder<T> {
|
||||||
outer: T,
|
outer: T,
|
||||||
|
parameter_name: Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WithTerm
|
pub trait WithTerm
|
||||||
|
@ -25,25 +31,23 @@ where
|
||||||
type Next;
|
type Next;
|
||||||
|
|
||||||
fn next(self, term: Term<Name>) -> Self::Next;
|
fn next(self, term: Term<Name>) -> Self::Next;
|
||||||
|
fn get_name(&self, name_str: &str) -> Name;
|
||||||
|
|
||||||
fn with_constant_int(self, int: isize) -> Self::Next {
|
fn with_constant_int(self, int: isize) -> Self::Next {
|
||||||
let term = Term::Constant(Constant::Integer(int));
|
let term = Term::Constant(Constant::Integer(int));
|
||||||
self.next(term)
|
self.next(term)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_lambda(self) -> Self::Next {
|
fn with_lambda(self, name_str: &str) -> LambdaBuilder<Self> {
|
||||||
let text = "i_0".to_string();
|
let parameter_name = self.get_name(name_str);
|
||||||
let unique = Unique::new(0);
|
LambdaBuilder {
|
||||||
let parameter_name = Name { text, unique };
|
outer: self,
|
||||||
let term = Term::Lambda {
|
|
||||||
parameter_name,
|
parameter_name,
|
||||||
body: Box::new(Term::Constant(Constant::Integer(1))),
|
}
|
||||||
};
|
|
||||||
self.next(term)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WithTerm for Empty {
|
impl WithTerm for NeedsTerm {
|
||||||
type Next = Builder;
|
type Next = Builder;
|
||||||
fn next(self, term: Term<Name>) -> Self::Next {
|
fn next(self, term: Term<Name>) -> Self::Next {
|
||||||
Builder {
|
Builder {
|
||||||
|
@ -51,20 +55,50 @@ impl WithTerm for Empty {
|
||||||
term,
|
term,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Remove mut?
|
||||||
|
fn get_name(&self, name_str: &str) -> Name {
|
||||||
|
let mut names = self.names.borrow_mut();
|
||||||
|
if let Some(unique) = names.get(name_str) {
|
||||||
|
Name {
|
||||||
|
text: name_str.to_string(),
|
||||||
|
unique: *unique,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let next_unique = self.next_unique.get();
|
||||||
|
self.next_unique.set(next_unique + 1);
|
||||||
|
let unique = Unique::new(next_unique);
|
||||||
|
names.insert(name_str.to_string(), unique.clone());
|
||||||
|
Name {
|
||||||
|
text: name_str.to_string(),
|
||||||
|
unique,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: WithTerm> WithTerm for LambdaBuilder<T> {
|
impl<T: WithTerm> WithTerm for LambdaBuilder<T> {
|
||||||
type Next = T::Next;
|
type Next = T::Next;
|
||||||
|
|
||||||
fn next(self, term: Term<Name>) -> Self::Next {
|
fn next(self, term: Term<Name>) -> Self::Next {
|
||||||
|
let term = Term::Lambda {
|
||||||
|
parameter_name: self.parameter_name,
|
||||||
|
body: Box::new(term),
|
||||||
|
};
|
||||||
self.outer.next(term)
|
self.outer.next(term)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_name(&self, name_str: &str) -> Name {
|
||||||
|
self.outer.get_name(name_str)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Builder {
|
impl Builder {
|
||||||
pub fn new(maj: usize, min: usize, patch: usize) -> Empty {
|
pub fn new(maj: usize, min: usize, patch: usize) -> NeedsTerm {
|
||||||
Empty {
|
NeedsTerm {
|
||||||
version: (maj, min, patch),
|
version: (maj, min, patch),
|
||||||
|
next_unique: Cell::new(0),
|
||||||
|
names: RefCell::new(HashMap::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,20 +41,24 @@ fn build_named__with_lam() {
|
||||||
(lam i_0 (con integer 1))
|
(lam i_0 (con integer 1))
|
||||||
)";
|
)";
|
||||||
let expected = parser::program(code).unwrap();
|
let expected = parser::program(code).unwrap();
|
||||||
let actual = Builder::new(1, 2, 3).with_lambda().build_named();
|
let actual = Builder::new(1, 2, 3)
|
||||||
|
.with_lambda("i_0")
|
||||||
|
.with_constant_int(1)
|
||||||
|
.build_named();
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[test]
|
#[test]
|
||||||
// fn build_named__with_nested_lam() {
|
fn build_named__with_nested_lam() {
|
||||||
// let code = r"(program
|
let code = r"(program
|
||||||
// 1.2.3
|
1.2.3
|
||||||
// (lam i_0 (lam i_1 (con integer 1)))
|
(lam i_0 (lam i_1 (con integer 1)))
|
||||||
// )";
|
)";
|
||||||
// let expected = parser::program(code).unwrap();
|
let expected = parser::program(code).unwrap();
|
||||||
// let actual = Builder::default()
|
let actual = Builder::new(1, 2, 3)
|
||||||
// .with_version(1, 2, 3)
|
.with_lambda("i_0")
|
||||||
// .with_lambda(1)
|
.with_lambda("i_1")
|
||||||
// .build_named();
|
.with_constant_int(1)
|
||||||
// assert_eq!(expected, actual);
|
.build_named();
|
||||||
// }
|
assert_eq!(expected, actual); // TODO: This should fail
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue