2025-01-29 11:33:01 +01:00
|
|
|
use {
|
|
|
|
ariadne::{Color, Label, Report, ReportKind, Source},
|
|
|
|
chumsky::{
|
|
|
|
prelude::*, text::*
|
2025-01-31 17:56:08 +01:00
|
|
|
},
|
|
|
|
laddertypes::{
|
|
|
|
dict::TypeDict,
|
|
|
|
parser::ParseLadderType,
|
|
|
|
unparser::UnparseLadderType, BimapTypeDict
|
|
|
|
},
|
|
|
|
std::sync::{Arc, RwLock}
|
2025-01-29 11:33:01 +01:00
|
|
|
};
|
|
|
|
|
2025-02-03 17:55:18 +01:00
|
|
|
/* for a given ladder type `t`, get the corresponding C type
|
|
|
|
*/
|
|
|
|
pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_pointer: bool) -> Option<String> {
|
|
|
|
let lnf = t.normalize().decurry().get_lnf_vec();
|
|
|
|
|
|
|
|
match lnf.last() {
|
|
|
|
Some(t) => {
|
|
|
|
if t == &dict.parse("Byte").expect("parse")
|
|
|
|
|| t == &dict.parse("x86.UInt8").expect("parse")
|
|
|
|
|| t == &dict.parse("<StaticLength 8 Bit>").expect("parse")
|
|
|
|
{
|
|
|
|
Some("uint8_t".into())
|
|
|
|
} else if t == &dict.parse("x86.UInt16").expect("parse") {
|
|
|
|
Some("uint16_t".into())
|
|
|
|
} else if t == &dict.parse("x86.UInt32").expect("parse") {
|
|
|
|
Some("uint32_t".into())
|
|
|
|
} else if t == &dict.parse("x86.UInt64").expect("parse") {
|
|
|
|
Some("uint64_t".into())
|
|
|
|
} else {
|
|
|
|
match t {
|
|
|
|
laddertypes::TypeTerm::App(args) => {
|
|
|
|
if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"LengthPrefix".into()).unwrap())
|
|
|
|
{
|
|
|
|
let item_c_type : String = get_c_repr_type(dict, args[2].clone(), false)?;
|
|
|
|
match item_c_type.as_str() {
|
|
|
|
"uint8_t" => Some(format!("LengthPrefixUInt8Array")),
|
|
|
|
"uint16_t" => Some(format!("LengthPrefixUInt16Array")),
|
|
|
|
"uint32_t" => Some(format!("LengthPrefixUInt32Array")),
|
|
|
|
"uint64_t" => Some(format!("LengthPrefixUInt64Array")),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"ValueDelim".into()).unwrap())
|
|
|
|
{
|
|
|
|
let c_type = get_c_repr_type(dict, args[2].clone(), false)?;
|
|
|
|
if skip_pointer {
|
|
|
|
Some(c_type)
|
|
|
|
} else {
|
|
|
|
Some(format!("{} *", c_type))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-29 11:33:01 +01:00
|
|
|
#[derive(Debug)]
|
2025-02-03 17:55:18 +01:00
|
|
|
struct LdmcMorphism {
|
2025-01-29 11:33:01 +01:00
|
|
|
symbol: String,
|
|
|
|
type_args: Vec<(String, String)>,
|
2025-01-31 17:56:08 +01:00
|
|
|
src_type: laddertypes::TypeTerm,
|
|
|
|
dst_type: laddertypes::TypeTerm,
|
2025-01-29 11:33:01 +01:00
|
|
|
locations: Vec<String>
|
|
|
|
}
|
|
|
|
|
2025-02-03 17:55:18 +01:00
|
|
|
impl LdmcMorphism {
|
|
|
|
pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict) -> String {
|
|
|
|
format!("int {} ({} const * restrict src, {} * restrict dst);",
|
|
|
|
self.symbol,
|
|
|
|
get_c_repr_type(dict, self.src_type.clone(), true).expect("cant get c-repr type for src type"),
|
|
|
|
get_c_repr_type(dict, self.dst_type.clone(), true).expect("cant get c-repr type for dst type"))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_call(&self, dict: &mut impl TypeDict) {
|
|
|
|
let src_c_type = get_c_repr_type(dict, self.src_type.clone(), true).expect("cant get c-repr type for src type");
|
|
|
|
let dst_c_type = get_c_repr_type(dict, self.dst_type.clone(), true).expect("cant get c-repr type for dst type");
|
|
|
|
|
|
|
|
let src_buf = "bufA";
|
|
|
|
let dst_buf = "bufB";
|
|
|
|
println!(
|
|
|
|
"{}
|
|
|
|
{} const * restrict src = {};
|
|
|
|
{} * restrict dst = {};
|
|
|
|
{} ( src, dst );
|
|
|
|
{}",
|
|
|
|
'{',
|
|
|
|
src_c_type, src_buf,
|
|
|
|
dst_c_type, dst_buf,
|
|
|
|
self.symbol,
|
|
|
|
'}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-29 11:33:01 +01:00
|
|
|
/* morphism-base text format:
|
|
|
|
* NAME '(' [TYPE-ARG-NAME ':' KIND] ')'
|
|
|
|
* SRC-TYPE
|
|
|
|
* '-->' DST-TYPE
|
|
|
|
* '@' [ LOCATION ':' ]
|
|
|
|
*/
|
2025-01-31 17:56:08 +01:00
|
|
|
fn parser(
|
|
|
|
type_dict: Arc<RwLock< BimapTypeDict >>
|
2025-02-03 17:55:18 +01:00
|
|
|
) -> impl Parser<char, Vec<LdmcMorphism>, Error = Simple<char>> {
|
2025-01-31 17:56:08 +01:00
|
|
|
|
2025-01-29 11:33:01 +01:00
|
|
|
ident().padded()
|
|
|
|
.then(
|
|
|
|
ident().padded()
|
|
|
|
.then_ignore(just(':').padded())
|
|
|
|
.then(none_of(",)").repeated().padded())
|
|
|
|
.separated_by(just(',').padded())
|
|
|
|
.delimited_by(just('('), just(')'))
|
|
|
|
)
|
|
|
|
.then_ignore(just('\n'))
|
|
|
|
.then(
|
|
|
|
take_until(just("-->").ignored())
|
|
|
|
.then(take_until(just('@').ignored()))
|
|
|
|
)
|
|
|
|
.then(
|
|
|
|
none_of(":\n").repeated().separated_by(just(':'))
|
|
|
|
)
|
2025-01-31 17:56:08 +01:00
|
|
|
.map(
|
|
|
|
move |(((symbol, type_args), ((src_type, _), (dst_type, _))), locations)| {
|
|
|
|
let mut type_dict = type_dict.write().unwrap();
|
|
|
|
let type_args : Vec<_> = type_args.into_iter().map(|(v,k)| (v,k.into_iter().collect())).collect();
|
|
|
|
for (var, kind) in type_args.iter() {
|
|
|
|
type_dict.add_varname(var.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
let src_type = type_dict.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type");
|
|
|
|
let dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type");
|
|
|
|
|
2025-02-03 17:55:18 +01:00
|
|
|
LdmcMorphism {
|
2025-01-31 17:56:08 +01:00
|
|
|
symbol,
|
|
|
|
type_args,
|
|
|
|
src_type,
|
|
|
|
dst_type,
|
|
|
|
locations: locations.into_iter().map(|l| l.into_iter().collect()).collect()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.separated_by(text::newline())
|
2025-01-29 11:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2025-01-31 17:56:08 +01:00
|
|
|
let type_dict = Arc::new(RwLock::new(BimapTypeDict::new()));
|
2025-02-03 17:55:18 +01:00
|
|
|
|
2025-02-03 18:41:14 +01:00
|
|
|
for mb_path in std::env::args().skip(1) {
|
|
|
|
let src = std::fs::read_to_string(
|
|
|
|
mb_path
|
|
|
|
).expect("read");
|
|
|
|
|
|
|
|
let result = parser(type_dict.clone()).parse(src.clone());
|
|
|
|
match result {
|
|
|
|
Ok(morphisms) => {
|
|
|
|
println!("parse ok.");
|
|
|
|
let mut dict = type_dict.write().unwrap();
|
|
|
|
|
|
|
|
for m in morphisms {
|
|
|
|
println!("{}\n {}\n---> \n {}\n{}\n\n", m.symbol,
|
|
|
|
m.src_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1),
|
|
|
|
m.dst_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1),
|
|
|
|
|
|
|
|
m.expected_c_type_signature(&mut *dict),
|
|
|
|
);
|
|
|
|
|
|
|
|
m.generate_call(&mut *dict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(errs) => {
|
|
|
|
errs.into_iter().for_each(|e| {
|
|
|
|
Report::build(ReportKind::Error, (), e.span().start)
|
|
|
|
.with_message(e.to_string())
|
|
|
|
.with_label(
|
|
|
|
Label::new(e.span())
|
|
|
|
.with_message(e)
|
|
|
|
.with_color(Color::Red),
|
|
|
|
)
|
|
|
|
.finish()
|
|
|
|
.print(Source::from(&src))
|
|
|
|
.unwrap()
|
|
|
|
});
|
2025-01-31 17:56:08 +01:00
|
|
|
}
|
2025-01-29 11:33:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|