Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: printing Var(27) and beyond overflowed ASCII #55

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub use self::Notation::*;
pub use self::Term::*;
use self::TermError::*;
use std::borrow::Cow;
use std::char::from_u32;
use std::error::Error;
use std::fmt;

Expand Down Expand Up @@ -504,6 +503,21 @@ impl fmt::Display for Term {
}
}

fn base26_encode(mut n: u32) -> String {
let mut buf = Vec::<u32>::new();
n += 1;
while n > 0 {
let m = n % 26;
buf.push(if m == 0 { 26 } else { m });
n = (n - 1) / 26
}
println!("{:?}", buf);
buf.iter()
.map(|u| char::from_u32(u + 'a' as u32 - 1).expect("error while printing term"))
.rev()
.collect::<String>()
AgentElement marked this conversation as resolved.
Show resolved Hide resolved
}

fn show_precedence_cla(
term: &Term,
context_precedence: usize,
Expand All @@ -513,23 +527,20 @@ fn show_precedence_cla(
match term {
Var(0) => "undefined".to_owned(),
Var(i) => {
if depth >= *i as u32 {
from_u32(depth + 97 - *i as u32)
.expect("error while printing term")
.to_string()
let cast = *i as u32;
AgentElement marked this conversation as resolved.
Show resolved Hide resolved
let ix = if cast <= depth {
ljedrz marked this conversation as resolved.
Show resolved Hide resolved
depth - cast
} else {
// use a different name than bound variables
from_u32(max_depth + 96 + *i as u32 - depth)
.expect("error while printing term")
.to_string()
}
max_depth + cast - depth - 1
};
base26_encode(ix)
}
Abs(ref t) => {
let ret = {
format!(
"{}{}.{}",
LAMBDA,
from_u32(depth + 97).expect("error while printing term"),
base26_encode(depth),
show_precedence_cla(t, 0, max_depth, depth + 1)
)
};
Expand Down Expand Up @@ -678,6 +689,13 @@ mod tests {
&app(abs(Var(1)), app(abs(app(Var(10), Var(1))), Var(10))).to_string(),
"(λa.a) ((λa.j a) k)"
);

assert_eq!(
abs!(27, app!(Var(28), Var(29), Var(30), Var(50), Var(702), Var(703))).to_string(),
"λa.λb.λc.λd.λe.λf.λg.λh.λi.λj.λk.λl.λm.λn.λo.λp.λq.λr.λs.λt.λu.λv.λw.λx.λy.λz.λaa.ab ac ad ax zz aaa"
);
assert_eq!(Var(26).to_string(), "z");
assert_eq!(Var(27).to_string(), "aa");
}

#[test]
Expand Down
Loading