some prototype mini utils

This commit is contained in:
Michael Sippel 2021-04-19 05:02:46 +02:00
parent e21b888b6c
commit 921377c2cc
Signed by: senvas
GPG key ID: F96CF119C34B64A6
6 changed files with 163 additions and 0 deletions

9
math/fib/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "fib"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nested = { path = "../../nested" }

58
math/fib/src/main.rs Normal file
View file

@ -0,0 +1,58 @@
use std::{
fs::File,
io::{Read, Write},
os::unix::io::FromRawFd
};
fn fib(n: u64) -> u64 {
let mut y = 0;
let mut y1 = 1;
let mut y2 = 0;
for _ in 0 .. n {
y = y1 + y2;
y2 = y1;
y1 = y;
}
y
}
fn main() {
nested::magic_header();
eprintln!(" Fibonacci Sequence");
nested::magic_header();
eprintln!("
interface (Sequence ) 0 1");
let mut f0 = unsafe { File::from_raw_fd(0) };
eprintln!("
> 0: n
( )
( MachineInt )
( MachineWord )
( Pipe Shot MachineWord )
");
let mut f1 = unsafe { File::from_raw_fd(1) };
eprintln!("
< 1: n'th fibonacci number
( )
( MachineInt )
( MachineWord )
( Pipe Shot MachineWord )
");
nested::magic_header();
let mut bytes = [0 as u8; 8];
f0.read_exact(&mut bytes);
let n = u64::from_le_bytes(bytes);
bytes = fib(n).to_le_bytes();
f1.write(&bytes);
}

9
math/int2str/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "int2str"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nested = { path = "../../nested" }

38
math/int2str/src/main.rs Normal file
View file

@ -0,0 +1,38 @@
use std::{
fs::File,
io::{Read, Write},
os::unix::io::FromRawFd
};
fn main() {
nested::magic_header();
eprintln!(" Human-readably Print MachineInt");
nested::magic_header();
let mut f0 = unsafe { File::from_raw_fd(0) };
eprintln!("
> 0:
( )
( MachineInt )
( MachineWord )
( Array 8 MachineSlab )
( Pipe Shot (Array 8 MachineSlab) )
");
eprintln!("
< 1:
( )
( Sequence (Digit 10) )
( Sequence ASCII )
( Sequence MachineSlab )
( Pipe Shot (Sequence MachineSlab) )
");
nested::magic_header();
let mut bytes = [0 as u8; 8];
f0.read_exact(&mut bytes);
println!("{}", u64::from_le_bytes(bytes));
}

9
math/str2int/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "str2int"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nested = { path = "../../nested" }

40
math/str2int/src/main.rs Normal file
View file

@ -0,0 +1,40 @@
use std::{
fs::File,
io::{Read, Write},
os::unix::io::FromRawFd
};
fn main() {
nested::magic_header();
eprintln!(" Parse MachineInt from String");
nested::magic_header();
let mut f0 = unsafe { File::from_raw_fd(0) };
eprintln!("
> 0:
( )
( Sequence (Digit 10) )
( Sequence ASCII )
( Sequence MachineSlab )
( Pipe Shot (Sequence MachineSlab) )
");
let mut f1 = unsafe { File::from_raw_fd(1) };
eprintln!("
< 1:
( )
( MachineInt )
( MachineWord )
( Array 8 MachineSlab )
( Pipe Shot (Array 8 MachineSlab) )
");
nested::magic_header();
let mut chars = Vec::new();
f0.read_to_end(&mut chars);
chars.retain(|c| (*c as char).is_numeric());
f1.write(&u64::from_str_radix(&String::from_utf8_lossy(&chars), 10).unwrap().to_le_bytes());
}