example utils: improve types

This commit is contained in:
Michael Sippel 2021-05-03 19:55:29 +02:00
parent 1448f31cf4
commit 042fc6353c
Signed by: senvas
GPG key ID: F96CF119C34B64A6
4 changed files with 86 additions and 77 deletions

View file

@ -16,17 +16,17 @@ fn main() {
( ) ( )
( MachineInt ) ( MachineInt )
( MachineWord ) ( MachineWord )
( Array 8 MachineSlab ) ( Stream MachineSyllab )
( Pipe Shot (Array 8 MachineSlab) )
"); ");
eprintln!(" eprintln!("
<1: <1:
( ) ( )
( PositionalInt 10 BigEndian )
( Sequence ( Digit 10 ) ) ( Sequence ( Digit 10 ) )
( Sequence ASCII ) ( Sequence UTF-8-Char )
( Sequence MachineSlab ) ( Stream UTF-8-Char )
( Pipe Shot (Sequence MachineSlab) ) ( Stream MachineSyllab )
"); ");
nested::magic_header(); nested::magic_header();

View file

@ -21,28 +21,48 @@ use {
async fn main() { async fn main() {
let mut td = TypeDict::new(); let mut td = TypeDict::new();
for tn in vec![ for tn in vec![
"MachineWord", "MachineInt", "MachineSlab", "MachineWord", "MachineInt", "MachineSyllab",
"Vec", "NullTerminatedString", "Vec", "Stream", "Json",
"Sequence", "Ascii", "Sequence", "UTF-8-Char",
"PositionalInt", "Digit", "LittleEndian", "BigEndian", "PositionalInt", "Digit", "LittleEndian", "BigEndian",
"DiffStream", "" "DiffStream", "",
"$src_radix", "$dst_radix"
] { td.add_typename(tn.into()); } ] { td.add_typename(tn.into()); }
let radix_types = vec![ let radix_types = vec![
td.type_term_from_str("( )").unwrap(), td.type_term_from_str("( )").unwrap(),
td.type_term_from_str("( PositionalInt 10 LittleEndian )").unwrap(), td.type_term_from_str("( PositionalInt 10 LittleEndian )").unwrap(),
td.type_term_from_str("( Sequence ( Digit 10 ) )").unwrap(), td.type_term_from_str("( Sequence ( Digit 10 ) )").unwrap(),
td.type_term_from_str("( Sequence Ascii )").unwrap(), td.type_term_from_str("( Sequence UTF-8-Char )").unwrap(),
td.type_term_from_str("( Sequence MachineSlab )").unwrap() td.type_term_from_str("( Sequence MachineSyllab )").unwrap()
];
let src_types = vec![
td.type_term_from_str("( )").unwrap(),
td.type_term_from_str("( PositionalInt $src_radix LittleEndian )").unwrap(),
td.type_term_from_str("( Sequence ( Digit $src_radix ) )").unwrap(),
td.type_term_from_str("( Sequence MachineInt )").unwrap(),
td.type_term_from_str("( DiffStream ( Vec MachineInt ) )").unwrap(),
td.type_term_from_str("( Json )").unwrap(),
td.type_term_from_str("( Stream UTF-8-Char )").unwrap(),
td.type_term_from_str("( Stream MachineSyllab )").unwrap()
];
let dst_types = vec![
td.type_term_from_str("( )").unwrap(),
td.type_term_from_str("( PositionalInt $dst_radix LittleEndian )").unwrap(),
td.type_term_from_str("( Sequence ( Digit $dst_radix ) )").unwrap(),
td.type_term_from_str("( Sequence MachineInt )").unwrap(),
td.type_term_from_str("( DiffStream ( Vec MachineInt ) )").unwrap(),
td.type_term_from_str("( Json )").unwrap(),
td.type_term_from_str("( Stream UTF-8-Char )").unwrap(),
td.type_term_from_str("( Stream MachineSyllab )").unwrap()
]; ];
nested::magic_header(); nested::magic_header();
eprintln!(" Convert Radix of Positional Integer"); eprintln!(" Convert Radix of Positional Integer");
nested::magic_header(); nested::magic_header();
let mut args = std::env::args();
args.next().expect("Arg $0 missing!");
eprintln!("\n$1: src_radix"); eprintln!("\n$1: src_radix");
for t in radix_types.iter() { for t in radix_types.iter() {
eprintln!(" {}", td.type_term_to_str(t)); eprintln!(" {}", td.type_term_to_str(t));
@ -53,58 +73,29 @@ async fn main() {
eprintln!(" {}", td.type_term_to_str(t)); eprintln!(" {}", td.type_term_to_str(t));
} }
eprintln!("\n>0: n");
for t in src_types.iter() {
eprintln!(" {}", td.type_term_to_str(t));
}
eprintln!("\n<1: n");
for t in dst_types.iter() {
eprintln!(" {}", td.type_term_to_str(t));
}
nested::magic_header();
let mut args = std::env::args();
args.next().expect("Arg $0 missing!");
let src_radix_str = args.next().expect("Arg $1 required!"); let src_radix_str = args.next().expect("Arg $1 required!");
let dst_radix_str = args.next().expect("Arg $2 required!"); let dst_radix_str = args.next().expect("Arg $2 required!");
let src_radix = usize::from_str_radix(&src_radix_str, 10).expect("could not parse src_radix"); let src_radix = usize::from_str_radix(&src_radix_str, 10).expect("could not parse src_radix");
let dst_radix = usize::from_str_radix(&dst_radix_str, 10).expect("could not parse dst_radix"); let dst_radix = usize::from_str_radix(&dst_radix_str, 10).expect("could not parse dst_radix");
let in_types = vec![ assert!(src_radix > 1);
td.type_term_from_str("( )").unwrap(), assert!(dst_radix > 1);
td.type_term_from_str("( PositionalInt )").unwrap()
.num_arg(src_radix as i64)
.arg(td.type_term_from_str("( LittleEndian )").unwrap())
.clone(),
td.type_term_from_str("( Sequence )").unwrap()
.arg(
td.type_term_from_str("( Digit )").unwrap()
.num_arg(src_radix as i64).clone()
)
.clone(),
td.type_term_from_str("( Sequence MachineInt )").unwrap(),
td.type_term_from_str("( DiffStream ( Vec MachineInt ) )").unwrap(),
];
let out_types = vec![
td.type_term_from_str("( )").unwrap(),
td.type_term_from_str("( PositionalInt )").unwrap()
.num_arg(dst_radix as i64)
.arg(td.type_term_from_str("( LittleEndian )").unwrap()).clone(),
td.type_term_from_str("( Sequence )").unwrap()
.arg(
td.type_term_from_str("( Digit )").unwrap()
.num_arg(dst_radix as i64).clone()
)
.clone(),
td.type_term_from_str("( Sequence MachineInt )").unwrap(),
td.type_term_from_str("( DiffStream ( Vec MachineInt ) )").unwrap(),
];
eprintln!("\n>0: n");
for t in in_types.iter() {
eprintln!(" {}", td.type_term_to_str(t));
}
eprintln!("\n<1: n");
for t in out_types.iter() {
eprintln!(" {}", td.type_term_to_str(t));
}
nested::magic_header();
let src_digits_port = ViewPort::new(); let src_digits_port = ViewPort::new();
let dst_digits_port = ViewPort::new(); let dst_digits_port = ViewPort::new();

View file

@ -10,31 +10,51 @@ fn main() {
eprintln!(" Parse MachineInt from String"); eprintln!(" Parse MachineInt from String");
nested::magic_header(); nested::magic_header();
let mut f0 = unsafe { File::from_raw_fd(0) };
eprintln!(" eprintln!("
> 0: $1: radix
( ) ( )
( PositionalInt 10 BigEndian )
( Sequence ( Digit 10 ) ) ( Sequence ( Digit 10 ) )
( Sequence ASCII ) ( Sequence UTF-8-Char )
( Sequence MachineSlab ) ( Sequence MachineSyllab )
( Pipe Shot (Sequence MachineSlab) )
"); ");
let mut f1 = unsafe { File::from_raw_fd(1) };
eprintln!(" eprintln!("
< 1: >0: n
( )
( PositionalInt $radix BigEndian )
( Sequence ( Digit $radix ) )
( Sequence UTF-8-Char )
( Stream UTF-8-Char )
( Stream MachineSyllab )
");
eprintln!("
<1: n
( ) ( )
( MachineInt ) ( MachineInt )
( MachineWord ) ( MachineWord )
( Array 8 MachineSlab ) ( Stream MachineSyllab )
( Pipe Shot (Array 8 MachineSlab) )
"); ");
nested::magic_header(); nested::magic_header();
let mut chars = Vec::new(); let mut f0 = unsafe { File::from_raw_fd(0) };
f0.read_to_end(&mut chars); let mut f1 = unsafe { File::from_raw_fd(1) };
chars.retain(|c| (*c as char).is_numeric());
f1.write(&u64::from_str_radix(&String::from_utf8_lossy(&chars), 10).unwrap().to_le_bytes()); let mut args = std::env::args();
args.next().expect("Arg $0 missing!");
let radix_str = args.next().expect("Arg $1 required!");
let radix = u32::from_str_radix(&radix_str, 10).expect("could not parse radix");
if radix > 16 {
panic!("invalid radix! (radix<=16 required)");
}
let mut chars = Vec::new();
f0.read_to_end(&mut chars);
chars.retain(|c| (*c as char).is_alphanumeric());
f1.write(&u64::from_str_radix(&String::from_utf8_lossy(&chars), radix).unwrap().to_le_bytes());
} }

View file

@ -82,8 +82,6 @@ impl RadixProjection {
impl Observer<dyn SequenceView<Item = usize>> for RadixProjection { impl Observer<dyn SequenceView<Item = usize>> for RadixProjection {
fn reset(&mut self, view: Option<Arc<dyn SequenceView<Item = usize>>>) { fn reset(&mut self, view: Option<Arc<dyn SequenceView<Item = usize>>>) {
eprintln!("reset");
self.src_digits = view; self.src_digits = view;
} }