feat: add conversion to tests
This commit is contained in:
parent
59a8f6477b
commit
b8c5c268d4
|
@ -1,7 +1,7 @@
|
|||
use std::fs;
|
||||
|
||||
use uplc::{
|
||||
ast::{DeBruijn, FakeNamedDeBruijn, Program},
|
||||
ast::{DeBruijn, Name, Program},
|
||||
parser,
|
||||
};
|
||||
|
||||
|
@ -46,7 +46,9 @@ fn main() -> anyhow::Result<()> {
|
|||
UplcCommand::Unflat { input, print } => {
|
||||
let bytes = std::fs::read(&input)?;
|
||||
|
||||
let program = Program::<FakeNamedDeBruijn>::from_flat(&bytes)?;
|
||||
let program = Program::<DeBruijn>::from_flat(&bytes)?;
|
||||
|
||||
let program: Program<Name> = program.try_into()?;
|
||||
|
||||
if print {
|
||||
println!("{:#?}", program);
|
||||
|
|
|
@ -160,6 +160,50 @@ impl Converter {
|
|||
Ok(converted_term)
|
||||
}
|
||||
|
||||
pub fn debruijn_to_name(&mut self, term: Term<DeBruijn>) -> Result<Term<Name>, Error> {
|
||||
let converted_term = match term {
|
||||
Term::Var(index) => Term::Var(Name {
|
||||
text: String::from("i"),
|
||||
unique: self.get_unique(index)?,
|
||||
}),
|
||||
Term::Delay(term) => Term::Delay(Box::new(self.debruijn_to_name(*term)?)),
|
||||
Term::Lambda {
|
||||
parameter_name,
|
||||
body,
|
||||
} => {
|
||||
self.declare_binder();
|
||||
|
||||
let unique = self.get_unique(parameter_name)?;
|
||||
|
||||
let name = Name {
|
||||
text: String::from("i"),
|
||||
unique,
|
||||
};
|
||||
|
||||
self.start_scope();
|
||||
|
||||
let body = self.debruijn_to_name(*body)?;
|
||||
|
||||
self.end_scope();
|
||||
|
||||
Term::Lambda {
|
||||
parameter_name: name,
|
||||
body: Box::new(body),
|
||||
}
|
||||
}
|
||||
Term::Apply { function, argument } => Term::Apply {
|
||||
function: Box::new(self.debruijn_to_name(*function)?),
|
||||
argument: Box::new(self.debruijn_to_name(*argument)?),
|
||||
},
|
||||
Term::Constant(constant) => Term::Constant(constant),
|
||||
Term::Force(term) => Term::Force(Box::new(self.debruijn_to_name(*term)?)),
|
||||
Term::Error => Term::Error,
|
||||
Term::Builtin(builtin) => Term::Builtin(builtin),
|
||||
};
|
||||
|
||||
Ok(converted_term)
|
||||
}
|
||||
|
||||
pub fn named_debruijn_to_debruijn(&mut self, term: Term<NamedDeBruijn>) -> Term<DeBruijn> {
|
||||
match term {
|
||||
Term::Var(name) => Term::Var(name.into()),
|
||||
|
@ -182,10 +226,6 @@ impl Converter {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn debruijn_to_name(&mut self, _term: Term<DeBruijn>) -> Result<Term<Name>, Error> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn debruijn_to_named_debruijn(&mut self, term: Term<DeBruijn>) -> Term<NamedDeBruijn> {
|
||||
match term {
|
||||
Term::Var(name) => Term::Var(name.into()),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/// e2e encoding/decoding tests
|
||||
use crate::{
|
||||
ast::{DeBruijn, Program},
|
||||
ast::{DeBruijn, Name, Program},
|
||||
parser,
|
||||
};
|
||||
|
||||
|
@ -11,7 +11,7 @@ fn integer() {
|
|||
|
||||
let parsed_program = parser::program(code).unwrap();
|
||||
|
||||
let debruijn_program: Program<DeBruijn> = parsed_program.try_into().unwrap();
|
||||
let debruijn_program: Program<DeBruijn> = parsed_program.clone().try_into().unwrap();
|
||||
|
||||
let decoded_program: Program<DeBruijn> = Program::from_flat(bytes).unwrap();
|
||||
|
||||
|
@ -20,6 +20,10 @@ fn integer() {
|
|||
let encoded_program = debruijn_program.to_flat().unwrap();
|
||||
|
||||
assert_eq!(encoded_program, bytes);
|
||||
|
||||
let name_program: Program<Name> = decoded_program.try_into().unwrap();
|
||||
|
||||
assert_eq!(parsed_program, name_program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -29,7 +33,7 @@ fn jpg() {
|
|||
|
||||
let parsed_program = parser::program(code).unwrap();
|
||||
|
||||
let debruijn_program: Program<DeBruijn> = parsed_program.try_into().unwrap();
|
||||
let debruijn_program: Program<DeBruijn> = parsed_program.clone().try_into().unwrap();
|
||||
|
||||
let decoded_program: Program<DeBruijn> = Program::from_flat(bytes).unwrap();
|
||||
|
||||
|
@ -38,4 +42,8 @@ fn jpg() {
|
|||
let encoded_program = debruijn_program.to_flat().unwrap();
|
||||
|
||||
assert_eq!(encoded_program, bytes);
|
||||
|
||||
let name_program: Program<Name> = decoded_program.try_into().unwrap();
|
||||
|
||||
assert_eq!(parsed_program, name_program);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue