1
0
mirror of https://github.com/osmarks/random-stuff synced 2025-09-12 07:16:00 +00:00

declassify some projects

This commit is contained in:
2023-06-19 14:09:54 +01:00
parent 307c513a9b
commit 30a1e0e358
40 changed files with 2828 additions and 8 deletions

View File

@@ -0,0 +1,85 @@
pub fn entry(s: &str) -> i32 {
let digit = |c: u8| (c as i32) - 48;
let mut acc = 0;
let mut pos = 0;
let b = s.as_bytes();
loop {
match b[pos] {
b'+' => {
acc += {
pos += 1;
let mut acc = 0;
while (pos + 1) < b.len() && (b[pos + 1] == b'/' || b[pos + 1] == b'*') {
println!("DIV or MUL {} {} {}", b[pos], b[pos + 1], b[pos + 2]);
if acc == 0 {
acc = digit(b[pos])
}
if b[pos + 1] == b'/' {
acc /= digit(b[pos + 2])
} else {
acc *= digit(b[pos + 2])
}
pos += 2;
}
if acc == 0 {
digit(b[pos])
} else {
acc
}
};
pos += 1;
},
b'-' => {
acc -= {
pos += 1;
let mut acc = -1;
while (pos + 1) < b.len() && (b[pos + 1] == b'/' || b[pos + 1] == b'*') {
println!("DIV or MUL {} {} {}", b[pos], b[pos + 1], b[pos + 2]);
if acc == -1 {
acc = digit(b[pos])
}
if b[pos + 1] == b'/' {
acc /= digit(b[pos + 2])
} else {
acc *= digit(b[pos + 2])
}
pos += 2;
}
if acc == -1 {
digit(b[pos])
} else {
acc
}
};
pos += 1;
},
x => {
acc += {
let mut acc = 0;
while (pos + 1) < b.len() && (b[pos + 1] == b'/' || b[pos + 1] == b'*') {
println!("DIV or MUL {} {} {}", b[pos], b[pos + 1], b[pos + 2]);
if acc == 0 {
acc = digit(b[pos])
}
if b[pos + 1] == b'/' {
acc /= digit(b[pos + 2])
} else {
acc *= digit(b[pos + 2])
}
pos += 2;
}
if acc == 0 {
digit(b[pos])
} else {
acc
}
};
pos += 1
}
}
if pos >= b.len() {
break
}
}
acc
}

View File

@@ -0,0 +1,16 @@
use pyo3::prelude::*;
mod entry_impl;
use entry_impl::entry;
#[pyfunction]
fn wrapped_entry(s: &str) -> PyResult<i32> {
Ok(entry(s))
}
#[pymodule]
fn entry_rs(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(wrapped_entry, m)?)?;
Ok(())
}