[tmp]202610032223

This commit is contained in:
starcells committed 2026-10-03 22:23:04 +08:00
commit dd400d5eb4
6 files changed
+167

No files matched your search

+1
View File
@@ -0,0 +1 @@
/target
Generated
+7
View File
@@ -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"
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "r8algernon"
version = "0.1.0"
edition = "2024"
[dependencies]
+19
View File
@@ -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;
}
+65
View File
@@ -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<ROT8Ins>,
begin: ROT8Addr,
end:ROT8Addr,
is_branch: bool,
}
impl TranslateToROT8 for ROT8Slice{
type Error = &'static str; //FIXME
fn to_rot8_tape(&self) -> Result<ROT8Tape,Self::Error> {
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
+69
View File
@@ -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<ROT8RawIns>;
pub trait ExportToStr{
fn to_string(&self) -> Result<String,ROT8RawIns>;
}
pub trait TranslateToROT8{
type Error;
fn to_rot8_tape(&self) -> Result<ROT8Tape,Self::Error>;
}
static CHAR_TO_ROT8_MAP:LazyLock<HashMap<char,ROT8RawIns>> = 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<HashMap<ROT8RawIns,char>> = LazyLock::new(||{
CHAR_TO_ROT8_MAP.iter().map(|(k,v)|(*v,*k)).collect()
});
impl ExportToStr for ROT8Tape{
fn to_string(&self) -> Result<String,ROT8RawIns> {
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<ROT8Tape,Self::Error>{
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)
}
}