frustration/frustration.rs

321 lines
7.9 KiB
Rust
Raw Normal View History

2022-03-24 08:01:51 +01:00
use std::cmp;
2022-03-22 04:45:42 +01:00
use std::io;
use std::process;
use std::convert::TryInto;
2022-03-22 04:45:42 +01:00
//const CORE_SIZE: usize = 65408
2022-03-26 10:15:00 +01:00
const CORE_SIZE: usize = 192;
2022-03-22 04:45:42 +01:00
2022-03-26 09:01:56 +01:00
#[derive(Debug, Eq, PartialEq)]
2022-03-24 08:01:51 +01:00
enum State {
Compiling,
Interpreting
}
2022-03-22 04:45:42 +01:00
#[derive(Debug)]
struct Core {
ram: [u8; CORE_SIZE],
2022-03-24 08:01:51 +01:00
ip: u16,
dp: u16, // newest link field, or 0
here: u16, // first unused byte
state: State,
dstack: [u16; 16],
2022-03-24 08:01:51 +01:00
tds: usize, // post-incremented; exceeds top by one
rstack: [u16; 16],
2022-03-24 08:01:51 +01:00
trs: usize, // post-incremented; exceeds top by one
}
type Primitive = fn(&mut Core);
struct ShortName {
bytes: [u8; 3],
length: u8
}
fn truncate_name(name: &str) -> ShortName {
let name_bytes = name.as_bytes();
let mut out = ShortName {
bytes: *b" ",
length: name_bytes.len() as u8 };
let n = std::cmp::min(3, out.length) as usize;
out.bytes[0..n].copy_from_slice(&name_bytes[0..n]);
2022-03-24 08:01:51 +01:00
return out;
2022-03-22 04:45:42 +01:00
}
2022-03-24 08:01:51 +01:00
struct TableEntry {
f: Primitive,
2022-03-26 23:20:52 +01:00
name: Option<ShortName>,
2022-03-26 09:01:56 +01:00
immediate: bool
2022-03-24 08:01:51 +01:00
}
2022-03-26 23:05:00 +01:00
const PRIMITIVES: [TableEntry; 13] = [
2022-03-26 23:20:52 +01:00
TableEntry {f: ret , name: None, immediate: false},
TableEntry {f: lit , name: None, immediate: false},
TableEntry {f: call , name: Some(ShortName {bytes: *b"cal", length: 4}), immediate: false},
TableEntry {f: dot , name: Some(ShortName {bytes: *b". ", length: 1}), immediate: false},
TableEntry {f: comma_ , name: Some(ShortName {bytes: *b", ", length: 1}), immediate: false},
TableEntry {f: store , name: Some(ShortName {bytes: *b"! ", length: 1}), immediate: false},
TableEntry {f: load , name: Some(ShortName {bytes: *b"@ ", length: 1}), immediate: false},
TableEntry {f: add , name: Some(ShortName {bytes: *b"+ ", length: 1}), immediate: false},
TableEntry {f: sub , name: Some(ShortName {bytes: *b"- ", length: 1}), immediate: false},
TableEntry {f: mul , name: Some(ShortName {bytes: *b"* ", length: 1}), immediate: false},
TableEntry {f: div , name: Some(ShortName {bytes: *b"/ ", length: 1}), immediate: false},
TableEntry {f: lbracket, name: Some(ShortName {bytes: *b"[ ", length: 1}), immediate: true},
TableEntry {f: rbracket, name: Some(ShortName {bytes: *b"] ", length: 1}), immediate: false}
2022-03-24 08:01:51 +01:00
];
2022-03-22 04:45:42 +01:00
fn new_core() -> Core {
2022-03-24 08:01:51 +01:00
let mut c = Core {
ram: [0; CORE_SIZE], ip: 0, dp: 0, here: 2, state: State::Interpreting,
dstack: [0; 16], tds: 0,
rstack: [0; 16], trs: 0 };
2022-03-24 08:01:51 +01:00
init_dictionary(&mut c);
return c;
}
// --- Dictionary management ---
2022-03-24 08:01:51 +01:00
fn create(c: &mut Core, name: ShortName) {
let addr: usize = c.here as usize;
c.ram[addr+0..=addr+1].copy_from_slice(&c.dp.to_le_bytes());
c.dp = addr as u16;
2022-03-26 09:01:56 +01:00
c.ram[addr+2] = name.length & 0x7f;
2022-03-24 08:01:51 +01:00
c.ram[addr+3..=addr+5].copy_from_slice(&name.bytes);
c.here = (addr+6) as u16;
}
fn find(c: &mut Core, name: ShortName) -> Option<u16> {
let mut addr = c.dp as usize;
while addr != 0 {
2022-03-26 09:01:56 +01:00
if (c.ram[addr+2] & 0x7f) == name.length {
if c.ram[addr+3..=addr+5] == name.bytes {
return Some((addr+6) as u16);
}
}
addr = u16::from_le_bytes(c.ram[addr..=addr+1].try_into().unwrap()) as usize;
}
return None;
}
2022-03-24 08:01:51 +01:00
fn init_dictionary(c: &mut Core) {
let mut opcode = 65535;
for p in PRIMITIVES {
2022-03-26 23:20:52 +01:00
match p.name {
Some(name) => {
create(c, name);
if p.immediate {
immediate(c);
}
comma(c, opcode);
comma(c, 65535); // ret
}
None => {}
2022-03-26 09:01:56 +01:00
}
2022-03-24 08:01:51 +01:00
opcode -= 1;
2022-03-26 23:20:52 +01:00
}
2022-03-24 08:01:51 +01:00
}
2022-03-26 22:59:25 +01:00
fn smudge(c: &mut Core) {
c.ram[(c.dp as usize) + 2] ^= 0x40;
}
fn immediate(c: &mut Core) {
c.ram[(c.dp as usize) + 2] ^= 0x80;
}
fn is_immediate(c: &mut Core, addr: u16) -> bool {
return (c.ram[(addr as usize) - 4] & 0x80) != 0;
}
fn comma(c: &mut Core, val: u16) {
let addr = c.here as usize;
c.ram[addr..=addr+1].copy_from_slice(&val.to_le_bytes());
c.here += 2;
}
fn comma_(c: &mut Core) {
let val = pop(c);
comma(c, val);
}
// --- Memory management ---
fn store(c: &mut Core) {
let addr = pop(c) as usize;
let val = pop(c);
c.ram[addr..=addr+1].copy_from_slice(&val.to_le_bytes());
}
fn load(c: &mut Core) {
let addr = pop(c) as usize;
push(c, u16::from_le_bytes(c.ram[addr..=addr+1].try_into().unwrap()));
}
// --- Stack management ---
2022-03-22 04:45:42 +01:00
fn push(c: &mut Core, val: u16) {
c.dstack[c.tds] = val;
c.tds += 1;
}
fn pop(c: &mut Core) -> u16 {
c.tds -= 1;
return c.dstack[c.tds];
}
fn to_r(c: &mut Core, val: u16) {
c.rstack[c.trs] = val;
c.trs += 1;
}
fn from_r(c: &mut Core) -> u16 {
c.trs -= 1;
return c.rstack[c.trs];
}
2022-03-26 23:05:00 +01:00
fn call(c: &mut Core) {
to_r(c, c.ip);
c.ip = pop(c);
}
2022-03-26 22:59:25 +01:00
// note: this is an inline primitive, not a dict entry
fn ret(c: &mut Core) {
if c.trs == 0 {
}
c.ip = from_r(c);
}
// --- Inner interpreter ---
fn fetch(c: &mut Core) -> u16 {
let ip = c.ip as usize;
let opcode = u16::from_le_bytes(c.ram[ip..=ip+1].try_into().unwrap());
c.ip += 2;
return opcode;
}
fn execute(c: &mut Core, opcode: u16) {
let primitive_index = (65535 - opcode) as usize;
if primitive_index < PRIMITIVES.len() {
(PRIMITIVES[primitive_index].f)(c);
} else {
// call
to_r(c, c.ip);
c.ip = opcode;
}
}
fn step(c: &mut Core) {
let opcode = fetch(c);
execute(c, opcode);
}
fn inner(c: &mut Core) {
loop {
step(c);
println!("ip={} trs={}", c.ip, c.trs);
if c.trs == 0 {
break;
}
}
2022-03-22 04:45:42 +01:00
}
2022-03-26 22:59:25 +01:00
// --- Math ---
2022-03-26 10:15:00 +01:00
2022-03-24 08:01:51 +01:00
fn dot(c: &mut Core) {
print!("{} ", pop(c));
}
2022-03-26 22:21:02 +01:00
// note: this is an inline primitive, not a dict entry
2022-03-26 10:15:00 +01:00
fn lit(c: &mut Core) {
2022-03-26 22:21:02 +01:00
let ip = c.ip as usize;
push(c, u16::from_le_bytes(c.ram[ip..=ip+1].try_into().unwrap()));
c.ip += 2;
2022-03-26 10:15:00 +01:00
}
2022-03-22 04:45:42 +01:00
fn add(c: &mut Core) {
let v1 = pop(c);
let v2 = pop(c);
push(c, v1 + v2);
}
fn sub(c: &mut Core) {
let v1 = pop(c);
let v2 = pop(c);
push(c, v2 - v1);
}
fn mul(c: &mut Core) {
let v1 = pop(c);
let v2 = pop(c);
push(c, v1 * v2);
}
fn div(c: &mut Core) {
let v1 = pop(c);
let v2 = pop(c);
push(c, v2 / v1);
}
2022-03-26 22:59:25 +01:00
// --- Outer interpreter ---
2022-03-26 23:20:52 +01:00
fn lbracket(c: &mut Core) {
c.state = State::Interpreting;
}
fn rbracket(c: &mut Core) {
c.state = State::Compiling;
}
2022-03-23 05:13:17 +01:00
fn outer(c: &mut Core, s: &str) {
2022-03-22 04:45:42 +01:00
let ss = s.trim();
let tokens = ss.split(" ");
for t in tokens {
match t {
2022-03-24 08:01:51 +01:00
"dmp" => {
println!("{:?}", c);
},
2022-03-22 04:45:42 +01:00
_ => {
match find(c, truncate_name(t)) {
Some(addr) => {
2022-03-26 09:01:56 +01:00
if c.state == State::Interpreting || is_immediate(c, addr) {
c.ip = addr;
inner(c);
} else {
comma(c, addr);
}
}
None => {
let val = t.parse::<u16>();
match val {
Ok(n) => {
2022-03-26 22:59:25 +01:00
match c.state {
State::Interpreting => { push(c, n) }
State::Compiling => {
2022-03-26 22:21:02 +01:00
comma(c, 65534); // lit
comma(c, n);
}
}
}
Err(_) => { if t != "" { println!("{}?", t) }}
}
}
2022-03-22 04:45:42 +01:00
}
}
}
}
}
fn main() {
let mut c = new_core();
loop {
let mut buf = String::new();
match io::stdin().read_line(&mut buf) {
2022-03-23 05:13:17 +01:00
Ok(_) => { outer(&mut c, &buf); println!(" ok");}
Err(_) => { break; }
2022-03-22 04:45:42 +01:00
}
}
}