type term: change display parenthesis to <>, allow \n to as newline character literal

This commit is contained in:
Michael Sippel 2023-05-19 11:21:29 +02:00
parent b0f30e9198
commit 52efcfc6c6
Signed by: senvas
GPG key ID: 060F22F65102F95C

View file

@ -9,6 +9,13 @@ use {
#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum TypeTerm { pub enum TypeTerm {
/* TODO: refactor into this:
Nominal {
id: u64,
args: Vec< TypeTerm >
},
Ladder(Vec< TypeTerm >),
*/
Type { Type {
id: u64, id: u64,
args: Vec< TypeLadder > args: Vec< TypeLadder >
@ -70,11 +77,24 @@ impl TypeTerm {
match f { match f {
Some(f) => { Some(f) => {
if atom.chars().nth(0).unwrap().is_numeric() { let mut chars = atom.chars();
let first = chars.next().unwrap();
if first.is_numeric() {
f.num_arg(i64::from_str_radix(atom, 10).unwrap()); f.num_arg(i64::from_str_radix(atom, 10).unwrap());
} else if first == '\'' {
if let Some(mut c) = chars.next() {
if c == '\\' {
if let Some('n') = chars.next() {
c = '\n';
}
}
f.char_arg(c);
}
} else { } else {
f.arg(TypeTerm::new( f.arg(TypeTerm::new(
names.get(atom).expect(&format!("invalid atom {}", atom)).clone(), names.get(atom)
.expect(&format!("invalid atom {}", atom)).clone()
)); ));
} }
} }
@ -97,11 +117,11 @@ impl TypeTerm {
TypeTerm::Type { id, args } => { TypeTerm::Type { id, args } => {
if args.len() > 0 { if args.len() > 0 {
format!( format!(
"({}{})", "<{}{}>",
names[&TypeID::Fun(*id)], names[&TypeID::Fun(*id)],
if args.len() > 0 { if args.len() > 0 {
args.iter().fold(String::new(), |str, term| { args.iter().fold(String::new(), |str, term| {
format!(" {}{}", str, term.to_str1(names)) format!("{} {}", str, term.to_str1(names))
}) })
} else { } else {
String::new() String::new()
@ -113,6 +133,7 @@ impl TypeTerm {
} }
TypeTerm::Num(n) => format!("{}", n), TypeTerm::Num(n) => format!("{}", n),
TypeTerm::Char('\n') => format!("'\\n'"),
TypeTerm::Char(c) => format!("'{}'", c), TypeTerm::Char(c) => format!("'{}'", c),
TypeTerm::Var(varid) => format!("T"), TypeTerm::Var(varid) => format!("T"),
} }
@ -122,11 +143,11 @@ impl TypeTerm {
pub fn to_str(&self, names: &HashMap<TypeID, String>) -> String { pub fn to_str(&self, names: &HashMap<TypeID, String>) -> String {
match self { match self {
TypeTerm::Type { id, args } => format!( TypeTerm::Type { id, args } => format!(
"( {} {})", "<{}{}>",
names[&TypeID::Fun(*id)], names[&TypeID::Fun(*id)],
if args.len() > 0 { if args.len() > 0 {
args.iter().fold(String::new(), |str, term| { args.iter().fold(String::new(), |str, term| {
format!("{}{} ", str, term.to_str1(names)) format!("{} {}", str, term.to_str1(names))
}) })
} else { } else {
String::new() String::new()
@ -134,6 +155,7 @@ impl TypeTerm {
), ),
TypeTerm::Num(n) => format!("{}", n), TypeTerm::Num(n) => format!("{}", n),
TypeTerm::Char('\n') => format!("'\\n'"),
TypeTerm::Char(c) => format!("'{}'", c), TypeTerm::Char(c) => format!("'{}'", c),
TypeTerm::Var(varid) => format!("T"), TypeTerm::Var(varid) => format!("T"),
} }