From 4a718a7ceebb94c5ea704a232a3e2f3025dcb6c2 Mon Sep 17 00:00:00 2001 From: Peter Fidelman Date: Sat, 26 Mar 2022 14:59:25 -0700 Subject: [PATCH] prettification --- frustration.rs | 113 ++++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/frustration.rs b/frustration.rs index cac45f4..c7153cd 100644 --- a/frustration.rs +++ b/frustration.rs @@ -86,26 +86,6 @@ fn create(c: &mut Core, name: ShortName) { c.here = (addr+6) as u16; } -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 lbracket(c: &mut Core) { - c.state = State::Interpreting; -} - -fn rbracket(c: &mut Core) { - c.state = State::Compiling; -} - fn find(c: &mut Core, name: ShortName) -> Option { let mut addr = c.dp as usize; while addr != 0 { @@ -134,6 +114,52 @@ fn init_dictionary(c: &mut Core) { } } +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); +} + +fn lbracket(c: &mut Core) { + c.state = State::Interpreting; +} + +fn rbracket(c: &mut Core) { + c.state = State::Compiling; +} + +// --- 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 --- + fn push(c: &mut Core, val: u16) { c.dstack[c.tds] = val; c.tds += 1; @@ -154,6 +180,16 @@ fn from_r(c: &mut Core) -> u16 { return c.rstack[c.trs]; } +// 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()); @@ -187,40 +223,12 @@ fn inner(c: &mut Core) { } } -// note: this is an inline primitive, not a dict entry -fn ret(c: &mut Core) { - if c.trs == 0 { - std::process::exit(0); - } - c.ip = from_r(c); -} +// --- Math --- fn dot(c: &mut Core) { print!("{} ", pop(c)); } -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); -} - -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())); -} - // note: this is an inline primitive, not a dict entry fn lit(c: &mut Core) { let ip = c.ip as usize; @@ -252,6 +260,8 @@ fn div(c: &mut Core) { push(c, v2 / v1); } +// --- Outer interpreter --- + fn outer(c: &mut Core, s: &str) { let ss = s.trim(); let tokens = ss.split(" "); @@ -281,7 +291,7 @@ fn outer(c: &mut Core, s: &str) { let val = t.parse::(); match val { Ok(n) => { - match(c.state) { + match c.state { State::Interpreting => { push(c, n) } State::Compiling => { comma(c, 65534); // lit @@ -307,7 +317,4 @@ fn main() { Err(_) => { break; } } } - //push(&mut c, 56); push(&mut c, 9); mul(&mut c); dot(&mut c); - //let v = pop(&mut c); call(&mut c, v); - //println!("{:?}", c); }