implement unparse()

This commit is contained in:
Michael Sippel 2023-10-03 03:30:38 +02:00
parent 167da369af
commit 29d1acd681
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 48 additions and 0 deletions

View file

@ -4,6 +4,7 @@ pub mod dict;
pub mod term;
pub mod lexer;
pub mod parser;
pub mod unparser;
pub mod curry;
pub mod lnf;
pub mod subtype;

47
src/unparser.rs Normal file
View file

@ -0,0 +1,47 @@
use crate::{dict::*, term::*};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\
impl TypeDict {
pub fn unparse(&self, t: &TypeTerm) -> String {
match t {
TypeTerm::TypeID(id) => self.get_typename(id).unwrap(),
TypeTerm::Num(n) => format!("{}", n),
TypeTerm::Char(c) => match c {
'\0' => "'\\0'".into(),
'\n' => "'\\n'".into(),
'\t' => "'\\t'".into(),
'\'' => "'\\''".into(),
c => format!("'{}'", c)
},
TypeTerm::Ladder(rungs) => {
let mut s = String::new();
let mut first = true;
for r in rungs.iter() {
if !first {
s.push('~');
}
first = false;
s.push_str(&mut self.unparse(r));
}
s
}
TypeTerm::App(args) => {
let mut s = String::new();
s.push('<');
let mut first = true;
for r in args.iter() {
if !first {
s.push(' ');
}
first = false;
s.push_str(&mut self.unparse(r));
}
s.push('>');
s
}
}
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\