From dd400d5eb4d0ab23d0d7c40e087b993bff0a09c9 Mon Sep 17 00:00:00 2001 From: StarCells Date: Sat, 3 Oct 2026 22:23:04 +0800 Subject: [PATCH] [tmp]202610032223 --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 6 ++++ src/main.rs | 19 +++++++++++ src/r8algernon/mod.rs | 65 +++++++++++++++++++++++++++++++++++ src/r8algernon/rot8_ins.rs | 69 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 167 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/r8algernon/mod.rs create mode 100644 src/r8algernon/rot8_ins.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..04632d0 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "r8algernon" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5900a11 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "r8algernon" +version = "0.1.0" +edition = "2024" + +[dependencies] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8244014 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,19 @@ +mod r8algernon; +use r8algernon::simplify; +use r8algernon::{ExportToStr,TranslateToROT8}; +fn main() { + let raw_tape = ">>+-++>>[>>[>[>+]<]<]<<"; + let tape = match raw_tape.to_rot8_tape(){ + Ok(t) => t, + Err(err_char) => { + eprintln!("FAILED when translating character {} to rot8 tape",err_char); + return; + } + }; + let Ok(simplified_tape) = simplify(tape).to_string() else { + eprint!("Exporting rot8 to raw tape FAILED!"); + return; + }; + println!("{}",simplified_tape); + return; +} diff --git a/src/r8algernon/mod.rs b/src/r8algernon/mod.rs new file mode 100644 index 0000000..55d1929 --- /dev/null +++ b/src/r8algernon/mod.rs @@ -0,0 +1,65 @@ +mod rot8_ins; + +use rot8_ins::{ROT8Tape,ROT8Addr,ROT8RawIns}; + +pub use rot8_ins::{ExportToStr,TranslateToROT8}; + +use crate::r8algernon::ROT8Ins::CodeBlock; + +type r8algernon_stack_depth = u8; + +enum ROT8Ins{ + RawIns(ROT8RawIns), + CodeBlock(ROT8Slice), +} + +struct ROT8Slice{ + ins: Vec, + begin: ROT8Addr, + end:ROT8Addr, + is_branch: bool, +} + +impl TranslateToROT8 for ROT8Slice{ + type Error = &'static str; //FIXME + fn to_rot8_tape(&self) -> Result { + let mut to_return = vec![]; + + for i in self.ins.iter(){ + match i { + ROT8Ins::RawIns(ins)=>to_return.push(*ins), + _=>() + } + } + + Ok(to_return) + } +} + +pub fn simplify(s:ROT8Tape) -> ROT8Tape{ + let mut tape_slices = ROT8Slice{ins:s.into_iter().map(ROT8Ins::RawIns).collect(),begin:0,end:0,is_branch:false}; // here we cannot ensure end + tape_slices = slice_tape(tape_slices); + + for i in tape_slices.ins.iter(){ + match i{ + ROT8Ins::RawIns(_)=>(), + CodeBlock(block)=>println!("From {} To {}",block.begin,block.end), + } + } + println!("End at {}",tape_slices.end); + + tape_slices.to_rot8_tape().unwrap() +} + +fn slice_tape(s:ROT8Slice) -> ROT8Slice{ + let mut to_return = ROT8Slice { ins: vec![], begin: s.begin,end:0 ,is_branch:s.is_branch}; + let mut dataptr = s.begin; + let mut codeblock_layer:r8algernon_stack_depth=0; + + for it in s.ins{ + //TODO + } + to_return.end = dataptr; + to_return +} +//FIXME: !!! END ADDR ERROR \ No newline at end of file diff --git a/src/r8algernon/rot8_ins.rs b/src/r8algernon/rot8_ins.rs new file mode 100644 index 0000000..610c7ac --- /dev/null +++ b/src/r8algernon/rot8_ins.rs @@ -0,0 +1,69 @@ +use std::{collections::HashMap, sync::LazyLock}; + +pub type ROT8Addr = u16; + +#[derive(PartialEq,Eq,Hash,Copy,Clone)] +pub enum ROT8RawIns{ + STP, + BTP, + FLB, + ROR, + BIZ, + RNZ, + INP, + OUT, +} + +pub type ROT8Tape = Vec; +pub trait ExportToStr{ + fn to_string(&self) -> Result; +} +pub trait TranslateToROT8{ + type Error; + fn to_rot8_tape(&self) -> Result; +} + +static CHAR_TO_ROT8_MAP:LazyLock> = LazyLock::new(||{ + let mut m = HashMap::new(); + use ROT8RawIns::*; + m.insert('>',STP); + m.insert('<',BTP); + m.insert('-',FLB); + m.insert('+',ROR); + m.insert('[',BIZ); + m.insert(']',RNZ); + m.insert(',',INP); + m.insert('.',OUT); + m +}); + +static ROT8_TO_CHAR_MAP: LazyLock> = LazyLock::new(||{ + CHAR_TO_ROT8_MAP.iter().map(|(k,v)|(*v,*k)).collect() +}); + + +impl ExportToStr for ROT8Tape{ + fn to_string(&self) -> Result { + let mut tape = String::new(); + for ins in self{ + tape.push(*ROT8_TO_CHAR_MAP.get(ins).unwrap()); + } + Ok(tape) + } +} + + +impl TranslateToROT8 for &str{ + type Error = char; + fn to_rot8_tape(&self) -> Result{ + let mut tape = vec![]; + for c in self.chars(){ + if CHAR_TO_ROT8_MAP.contains_key(&c) { + tape.push(*CHAR_TO_ROT8_MAP.get(&c).unwrap()); + } else{ + return Err(c); + } + } + Ok(tape) + } +} \ No newline at end of file