2023-09-08 13:40:06 +02:00
|
|
|
|
|
2023-08-18 00:16:16 +02:00
|
|
|
|
use {
|
2023-09-08 13:40:06 +02:00
|
|
|
|
r3vi::{
|
2024-06-03 17:02:08 +02:00
|
|
|
|
view::{OuterViewPort, singleton::*, list::*}
|
2023-09-08 13:40:06 +02:00
|
|
|
|
},
|
2024-08-06 16:20:02 +02:00
|
|
|
|
laddertypes::{TypeTerm, MorphismType},
|
2023-08-18 00:16:16 +02:00
|
|
|
|
crate::{
|
2024-08-06 16:20:02 +02:00
|
|
|
|
repr_tree::{ReprTree, ReprTreeExt, ReprLeaf, Context, GenericReprTreeMorphism},
|
2023-08-18 00:16:16 +02:00
|
|
|
|
editors::{
|
|
|
|
|
list::*,
|
2023-08-21 16:31:44 +02:00
|
|
|
|
integer::*
|
2023-08-18 00:16:16 +02:00
|
|
|
|
},
|
|
|
|
|
},
|
2023-08-21 16:31:44 +02:00
|
|
|
|
std::sync::{Arc, RwLock}
|
2023-08-18 00:16:16 +02:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-10 16:17:24 +01:00
|
|
|
|
pub fn init_ctx(ctx: Arc<RwLock<Context>>) {
|
2024-05-25 00:39:47 +02:00
|
|
|
|
// TODO: proper scoping
|
|
|
|
|
ctx.write().unwrap().add_varname("SrcRadix");
|
|
|
|
|
ctx.write().unwrap().add_varname("DstRadix");
|
|
|
|
|
|
2024-07-21 18:16:43 +02:00
|
|
|
|
|
2024-08-06 16:20:02 +02:00
|
|
|
|
let posint_seq_morph_big_to_little = GenericReprTreeMorphism::new(
|
|
|
|
|
Context::parse(&ctx, "ℕ ~ <PosInt Radix BigEndian>
|
|
|
|
|
~ <Seq <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >"),
|
|
|
|
|
Context::parse(&ctx, "ℕ ~ <PosInt Radix LittleEndian>
|
|
|
|
|
~ <Seq <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >"),
|
|
|
|
|
{
|
2024-06-03 17:02:08 +02:00
|
|
|
|
let ctx = ctx.clone();
|
|
|
|
|
move |src_rt, σ| {
|
2024-08-06 16:20:02 +02:00
|
|
|
|
let src_digits = src_rt
|
|
|
|
|
.descend(Context::parse(&ctx, "
|
2024-06-03 17:02:08 +02:00
|
|
|
|
<PosInt Radix BigEndian>
|
2024-08-06 16:20:02 +02:00
|
|
|
|
~ <Seq <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >
|
|
|
|
|
")
|
2024-06-03 17:02:08 +02:00
|
|
|
|
.apply_substitution(&|k|σ.get(k).cloned())
|
|
|
|
|
.clone()
|
2024-08-06 16:20:02 +02:00
|
|
|
|
).expect("cant descend")
|
|
|
|
|
.view_seq::< u64 >();
|
2024-06-03 17:02:08 +02:00
|
|
|
|
|
2024-07-21 18:16:43 +02:00
|
|
|
|
src_rt.attach_leaf_to(Context::parse(&ctx, "
|
2024-06-03 17:02:08 +02:00
|
|
|
|
<PosInt Radix LittleEndian>
|
2024-08-06 16:20:02 +02:00
|
|
|
|
~ <Seq <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >
|
2024-06-03 17:02:08 +02:00
|
|
|
|
").apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
2024-07-21 18:16:43 +02:00
|
|
|
|
src_digits.reverse()
|
2024-08-06 16:20:02 +02:00
|
|
|
|
);
|
2024-06-03 17:02:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-08-06 16:20:02 +02:00
|
|
|
|
let posint_list_morph_big_to_little = GenericReprTreeMorphism::new(
|
|
|
|
|
Context::parse(&ctx, "ℕ ~ <PosInt Radix BigEndian>
|
|
|
|
|
~ <Seq~List <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >"),
|
|
|
|
|
Context::parse(&ctx, "ℕ ~ <PosInt Radix LittleEndian>
|
|
|
|
|
~ <Seq~List <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >"),
|
2024-05-25 00:39:47 +02:00
|
|
|
|
{
|
|
|
|
|
let ctx = ctx.clone();
|
|
|
|
|
move |src_rt, σ| {
|
2024-08-06 16:20:02 +02:00
|
|
|
|
let src_digits = src_rt
|
|
|
|
|
.descend(Context::parse(&ctx, "
|
|
|
|
|
<PosInt Radix BigEndian>
|
|
|
|
|
~ <Seq~List <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >
|
2024-05-25 00:39:47 +02:00
|
|
|
|
")
|
2024-08-06 16:20:02 +02:00
|
|
|
|
.apply_substitution(&|k|σ.get(k).cloned())
|
|
|
|
|
.clone()
|
|
|
|
|
).expect("cant descend")
|
|
|
|
|
.view_list::< u64 >();
|
2024-05-25 00:39:47 +02:00
|
|
|
|
|
2024-08-06 16:20:02 +02:00
|
|
|
|
src_rt.attach_leaf_to(Context::parse(&ctx, "
|
|
|
|
|
<PosInt Radix LittleEndian>
|
|
|
|
|
~ <Seq~List <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >
|
|
|
|
|
").apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
2024-07-21 18:16:43 +02:00
|
|
|
|
src_digits.reverse()
|
2024-08-06 16:20:02 +02:00
|
|
|
|
);
|
2023-08-18 00:16:16 +02:00
|
|
|
|
}
|
2024-05-25 00:39:47 +02:00
|
|
|
|
}
|
2023-08-18 00:16:16 +02:00
|
|
|
|
);
|
|
|
|
|
|
2024-08-06 16:20:02 +02:00
|
|
|
|
let posint_list_morph_little_to_big = GenericReprTreeMorphism::new(
|
|
|
|
|
Context::parse(&ctx, "ℕ ~ <PosInt Radix LittleEndian>
|
|
|
|
|
~ <Seq~List <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >"),
|
|
|
|
|
Context::parse(&ctx, "ℕ ~ <PosInt Radix BigEndian>
|
|
|
|
|
~ <Seq~List <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >"),
|
2024-05-25 00:39:47 +02:00
|
|
|
|
{
|
|
|
|
|
let ctx = ctx.clone();
|
|
|
|
|
move |src_rt, σ| {
|
2024-08-06 16:20:02 +02:00
|
|
|
|
let src_digits = src_rt
|
|
|
|
|
.descend(Context::parse(&ctx, "
|
|
|
|
|
<PosInt Radix LittleEndian>
|
|
|
|
|
~ <Seq~List <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >
|
2024-05-25 00:39:47 +02:00
|
|
|
|
")
|
2024-08-06 16:20:02 +02:00
|
|
|
|
.apply_substitution(&|k|σ.get(k).cloned())
|
|
|
|
|
.clone()
|
|
|
|
|
).expect("cant descend")
|
|
|
|
|
.view_list::< u64 >();
|
2024-07-21 18:16:43 +02:00
|
|
|
|
|
|
|
|
|
src_rt.attach_leaf_to(Context::parse(&ctx, "
|
|
|
|
|
<PosInt Radix BigEndian>
|
2024-08-06 16:20:02 +02:00
|
|
|
|
~ <Seq~List <Digit Radix> ~ ℤ_2^64 ~ machine.UInt64 >
|
2024-07-21 18:16:43 +02:00
|
|
|
|
").apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
|
|
|
|
src_digits.reverse()
|
2024-08-06 16:20:02 +02:00
|
|
|
|
);
|
2024-06-03 17:02:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-25 00:39:47 +02:00
|
|
|
|
|
2024-08-06 16:20:02 +02:00
|
|
|
|
let posint_list_morph_radix = GenericReprTreeMorphism::new(
|
|
|
|
|
Context::parse(&ctx, "
|
2024-06-03 17:02:08 +02:00
|
|
|
|
ℕ
|
|
|
|
|
~ <PosInt SrcRadix LittleEndian>
|
2024-08-01 18:35:57 +02:00
|
|
|
|
~ <Seq <Digit SrcRadix>>
|
|
|
|
|
~ <List <Digit SrcRadix>
|
2024-06-03 17:02:08 +02:00
|
|
|
|
~ ℤ_2^64
|
|
|
|
|
~ machine.UInt64>
|
|
|
|
|
"),
|
2024-08-06 16:20:02 +02:00
|
|
|
|
Context::parse(&ctx, "
|
2024-06-03 17:02:08 +02:00
|
|
|
|
ℕ
|
|
|
|
|
~ <PosInt DstRadix LittleEndian>
|
2024-08-01 18:35:57 +02:00
|
|
|
|
~ <Seq <Digit DstRadix>>
|
|
|
|
|
~ <List <Digit DstRadix>
|
2024-06-03 17:02:08 +02:00
|
|
|
|
~ ℤ_2^64
|
2024-08-01 18:35:57 +02:00
|
|
|
|
~ machine.UInt64>
|
2024-08-06 16:20:02 +02:00
|
|
|
|
"),
|
2024-05-25 00:39:47 +02:00
|
|
|
|
{
|
|
|
|
|
let ctx = ctx.clone();
|
|
|
|
|
move |src_rt, σ| {
|
|
|
|
|
let src_radix = match σ.get(&laddertypes::TypeID::Var(
|
|
|
|
|
ctx.read().unwrap().get_var_typeid("SrcRadix").unwrap()
|
|
|
|
|
)) {
|
2024-05-25 19:21:16 +02:00
|
|
|
|
Some(laddertypes::TypeTerm::Num(n)) => *n as u64,
|
2024-05-25 00:39:47 +02:00
|
|
|
|
_ => 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let dst_radix = match σ.get(&laddertypes::TypeID::Var(
|
|
|
|
|
ctx.read().unwrap().get_var_typeid("DstRadix").unwrap()
|
|
|
|
|
)) {
|
2024-05-25 19:21:16 +02:00
|
|
|
|
Some(laddertypes::TypeTerm::Num(n)) => *n as u64,
|
2024-05-25 00:39:47 +02:00
|
|
|
|
_ => 0
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-21 18:16:43 +02:00
|
|
|
|
let src_digits_rt = src_rt.descend(Context::parse(&ctx, "
|
2024-05-25 00:39:47 +02:00
|
|
|
|
<PosInt SrcRadix LittleEndian>
|
2024-08-01 18:35:57 +02:00
|
|
|
|
~ <Seq <Digit SrcRadix>>
|
|
|
|
|
~ <List <Digit SrcRadix>
|
|
|
|
|
~ ℤ_2^64
|
|
|
|
|
~ machine.UInt64 >
|
2024-07-21 18:16:43 +02:00
|
|
|
|
").apply_substitution(&|k|σ.get(k).cloned()).clone()
|
2024-05-25 00:39:47 +02:00
|
|
|
|
).expect("cant descend repr tree");
|
|
|
|
|
|
|
|
|
|
let dst_digits_port =
|
2024-08-01 18:35:57 +02:00
|
|
|
|
src_digits_rt.view_list::<u64>()
|
|
|
|
|
.to_sequence()
|
2024-05-25 00:39:47 +02:00
|
|
|
|
.to_positional_uint( src_radix )
|
2024-08-01 18:35:57 +02:00
|
|
|
|
.transform_radix( dst_radix )
|
|
|
|
|
.to_list();
|
2024-05-25 00:39:47 +02:00
|
|
|
|
|
2024-07-21 18:16:43 +02:00
|
|
|
|
src_rt.attach_leaf_to(
|
|
|
|
|
Context::parse(&ctx, "
|
2024-08-01 18:35:57 +02:00
|
|
|
|
<PosInt DstRadix LittleEndian>
|
|
|
|
|
~ <Seq <Digit DstRadix> >
|
|
|
|
|
~ <List <Digit DstRadix>
|
|
|
|
|
~ ℤ_2^64
|
|
|
|
|
~ machine.UInt64 >
|
2024-07-21 18:16:43 +02:00
|
|
|
|
").apply_substitution(&|k|σ.get(k).cloned()).clone(),
|
|
|
|
|
dst_digits_port
|
|
|
|
|
);
|
2023-08-18 00:16:16 +02:00
|
|
|
|
}
|
2024-08-06 16:20:02 +02:00
|
|
|
|
|
2024-05-25 00:39:47 +02:00
|
|
|
|
}
|
2023-08-18 00:16:16 +02:00
|
|
|
|
);
|
2024-08-06 16:20:02 +02:00
|
|
|
|
|
|
|
|
|
ctx.write().unwrap().morphisms.add_morphism( posint_seq_morph_big_to_little );
|
|
|
|
|
ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_big_to_little );
|
|
|
|
|
ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_little_to_big );
|
|
|
|
|
ctx.write().unwrap().morphisms.add_morphism( posint_list_morph_radix );
|
2023-08-18 00:16:16 +02:00
|
|
|
|
}
|
|
|
|
|
|