parse type terms

This commit is contained in:
Michael Sippel 2025-01-31 17:56:08 +01:00
parent d66dc0e78b
commit f0f8adbbbc
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 51 additions and 20 deletions

View file

@ -6,3 +6,4 @@ edition = "2024"
[dependencies] [dependencies]
chumsky = "0.9.0" chumsky = "0.9.0"
ariadne = "0.2" ariadne = "0.2"
laddertypes = { path = "../lib-laddertypes", features = ["pretty"] }

View file

@ -2,15 +2,21 @@ use {
ariadne::{Color, Label, Report, ReportKind, Source}, ariadne::{Color, Label, Report, ReportKind, Source},
chumsky::{ chumsky::{
prelude::*, text::* prelude::*, text::*
} },
laddertypes::{
dict::TypeDict,
parser::ParseLadderType,
unparser::UnparseLadderType, BimapTypeDict
},
std::sync::{Arc, RwLock}
}; };
#[derive(Debug)] #[derive(Debug)]
struct Morphism { struct Morphism {
symbol: String, symbol: String,
src_type: String,
dst_type: String,
type_args: Vec<(String, String)>, type_args: Vec<(String, String)>,
src_type: laddertypes::TypeTerm,
dst_type: laddertypes::TypeTerm,
locations: Vec<String> locations: Vec<String>
} }
@ -20,7 +26,10 @@ struct Morphism {
* '-->' DST-TYPE * '-->' DST-TYPE
* '@' [ LOCATION ':' ] * '@' [ LOCATION ':' ]
*/ */
fn parser() -> impl Parser<char, Vec<Morphism>, Error = Simple<char>> { fn parser(
type_dict: Arc<RwLock< BimapTypeDict >>
) -> impl Parser<char, Vec<Morphism>, Error = Simple<char>> {
ident().padded() ident().padded()
.then( .then(
ident().padded() ident().padded()
@ -37,16 +46,26 @@ fn parser() -> impl Parser<char, Vec<Morphism>, Error = Simple<char>> {
.then( .then(
none_of(":\n").repeated().separated_by(just(':')) none_of(":\n").repeated().separated_by(just(':'))
) )
.map(|(((symbol, type_args), ((src_type, _), (dst_type, _))), locations)| { .map(
Morphism { move |(((symbol, type_args), ((src_type, _), (dst_type, _))), locations)| {
symbol, let mut type_dict = type_dict.write().unwrap();
src_type: src_type.iter().collect(), let type_args : Vec<_> = type_args.into_iter().map(|(v,k)| (v,k.into_iter().collect())).collect();
dst_type: dst_type.iter().collect(), for (var, kind) in type_args.iter() {
type_args: type_args.into_iter().map(|(v,k)| (v,k.into_iter().collect())).collect(), type_dict.add_varname(var.clone());
locations: locations.into_iter().map(|l| l.into_iter().collect()).collect() }
}
}) let src_type = type_dict.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type");
.separated_by(text::newline()) let dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type");
Morphism {
symbol,
type_args,
src_type,
dst_type,
locations: locations.into_iter().map(|l| l.into_iter().collect()).collect()
}
})
.separated_by(text::newline())
} }
fn main() { fn main() {
@ -59,21 +78,32 @@ fn main() {
@lib/libmorph_posint.so:src/posint.c @lib/libmorph_posint.so:src/posint.c
morph_string_as_nullterm_to_length_prefix () morph_string_as_nullterm_to_length_prefix ()
[~<ValueDelim '\0'> Char ~ Ascii] <Seq~<ValueDelim '\\0'> Char ~ Ascii>
--> [~<LengthPrefix x86.UInt64> Char ~ Ascii] --> <Seq~<LengthPrefix x86.UInt64> Char ~ Ascii>
@lib/libmorph_length-prefix.so:src/length_prefix.c @lib/libmorph_length-prefix.so:src/length_prefix.c
morph_string_as_length_prefix_to_nullterm () morph_string_as_length_prefix_to_nullterm ()
[~<LengthPrefix x86.UInt64> Char ~ Ascii] <Seq~<LengthPrefix x86.UInt64> Char ~ Ascii>
--> [~<ValueDelim '\0'> Char ~ Ascii] --> <Seq~<ValueDelim '\\0'> Char ~ Ascii>
@lib/libmorph_length-prefix.so:src/length_prefix.c @lib/libmorph_length-prefix.so:src/length_prefix.c
"; ";
let result = parser().parse(src); let type_dict = Arc::new(RwLock::new(BimapTypeDict::new()));
let result = parser(type_dict.clone()).parse(src);
match result { match result {
Ok(morphisms) => { Ok(morphisms) => {
println!("parse ok.\n{:?}", morphisms); println!("parse ok.");
let mut dict = type_dict.write().unwrap();
for m in morphisms {
println!("{}\n {}\n---> \n {}\n", m.symbol,
m.src_type.normalize().sugar(&mut *dict).pretty(&mut *dict, 1),
m.dst_type.normalize().sugar(&mut *dict).pretty(&mut *dict, 1),
);
}
} }
Err(errs) => { Err(errs) => {
errs.into_iter().for_each(|e| { errs.into_iter().for_each(|e| {