prettification

This commit is contained in:
Peter Fidelman 2022-03-26 14:59:25 -07:00
parent 885f6b2a40
commit 4a718a7cee

View file

@ -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<u16> {
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::<u16>();
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);
}