From 29d1acd6814a2bf1c43448648e27d29b04430b52 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 3 Oct 2023 03:30:38 +0200 Subject: [PATCH] implement unparse() --- src/lib.rs | 1 + src/unparser.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/unparser.rs diff --git a/src/lib.rs b/src/lib.rs index 3f725bc..517b36d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/unparser.rs b/src/unparser.rs new file mode 100644 index 0000000..ccf754d --- /dev/null +++ b/src/unparser.rs @@ -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 + } + } + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>\\