autoexec, better size accounting

This commit is contained in:
psf 2022-03-27 13:56:39 -07:00
parent 995e712359
commit 0b11491963

View file

@ -1,9 +1,6 @@
use std::io;
use std::convert::TryInto;
//const CORE_SIZE: usize = 65408
const CORE_SIZE: usize = 512;
#[derive(Debug, Eq, PartialEq)]
enum State {
Compiling,
@ -17,18 +14,22 @@ enum Post {
WarmReset,
}
const ADDRESS_SPACE: usize = 65535;
const STACK_WORDS: usize = 16;
const RAM_BYTES: usize = ADDRESS_SPACE - 2*2*STACK_WORDS;
#[derive(Debug)]
struct Core {
ram: [u8; CORE_SIZE],
ram: [u8; RAM_BYTES],
ip: u16,
dp: u16, // newest link field, or 0
here: u16, // first unused byte
state: State,
next_token: Option<String>,
post: Post,
dstack: [u16; 16],
dstack: [u16; STACK_WORDS],
tds: usize, // post-incremented; exceeds top by one
rstack: [u16; 16],
rstack: [u16; STACK_WORDS],
trs: usize, // post-incremented; exceeds top by one
}
@ -87,17 +88,46 @@ const PRIMITIVES: [TableEntry; 27] = [
fn new_core() -> Core {
let mut c = Core {
ram: [0; CORE_SIZE], ip: 0, dp: 0, here: 2, state: State::Interpreting,
ram: [0; RAM_BYTES], ip: 0, dp: 0, here: 2, state: State::Interpreting,
next_token: None,
post: Post::Nothing,
dstack: [0; 16], tds: 0,
rstack: [0; 16], trs: 0 };
dstack: [0; STACK_WORDS], tds: 0,
rstack: [0; STACK_WORDS], trs: 0 };
init_dictionary(&mut c);
let autoexec = [
"create : ] create smudge ] [ 65535 ,",
"create ; ] unsmudge 65535 , [ ' [ , 65535 , immediate",
": recursive unsmudge ; immediate"
];
for s in autoexec {
outer(&mut c, s);
}
return c;
}
// --- Dictionary management ---
fn init_dictionary(c: &mut Core) {
let mut opcode = 65535;
for p in PRIMITIVES {
match p.name {
Some(name) => {
create(c, name);
if p.immediate {
immediate(c);
}
comma(c, opcode);
comma(c, 65535); // ret
}
None => {}
}
opcode -= 1;
}
}
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());
@ -131,24 +161,6 @@ fn find(c: &mut Core, name: ShortName) -> Option<u16> {
return None;
}
fn init_dictionary(c: &mut Core) {
let mut opcode = 65535;
for p in PRIMITIVES {
match p.name {
Some(name) => {
create(c, name);
if p.immediate {
immediate(c);
}
comma(c, opcode);
comma(c, 65535); // ret
}
None => {}
}
opcode -= 1;
}
}
fn smudge(c: &mut Core) {
c.ram[(c.dp as usize) + 2] |= 0x40;
}
@ -463,15 +475,7 @@ fn main() {
}
/*
create dog ] r> drop dup . 1 - dup ? dog ret [
create dog smudge ] dog ret [ smudge
100 dog
create : ] create smudge ] [ 65535 ,
create ; ] unsmudge 65535 , [ ' [ , 65535 , immediate
: dog 33 . ;
: dog [ unsmudge ] r> drop dup . 1 - dup ? dog ;
: dog recursive r> drop dup . 1 - dup ? dog ;
: dog dog ;
100 dog
*/