variable, constant, forget

This commit is contained in:
psf 2022-03-27 21:17:01 -07:00
parent 182c374117
commit c0687799ec

View file

@ -56,7 +56,7 @@ struct TableEntry {
immediate: bool immediate: bool
} }
const PRIMITIVES: [TableEntry; 27] = [ const PRIMITIVES: [TableEntry; 31] = [
TableEntry {f: ret , name: None, immediate: false}, TableEntry {f: ret , name: None, immediate: false},
TableEntry {f: lit , name: None, immediate: false}, TableEntry {f: lit , name: None, immediate: false},
TableEntry {f: add , name: Some(ShortName {bytes: *b"+ ", length: 1}), immediate: false}, TableEntry {f: add , name: Some(ShortName {bytes: *b"+ ", length: 1}), immediate: false},
@ -65,12 +65,16 @@ const PRIMITIVES: [TableEntry; 27] = [
TableEntry {f: create_d, name: Some(ShortName {bytes: *b"cre", length: 6}), immediate: false}, TableEntry {f: create_d, name: Some(ShortName {bytes: *b"cre", length: 6}), immediate: false},
TableEntry {f: div , name: Some(ShortName {bytes: *b"/ ", length: 1}), immediate: false}, TableEntry {f: div , name: Some(ShortName {bytes: *b"/ ", length: 1}), immediate: false},
TableEntry {f: dot , name: Some(ShortName {bytes: *b". ", length: 1}), immediate: false}, TableEntry {f: dot , name: Some(ShortName {bytes: *b". ", length: 1}), immediate: false},
TableEntry {f: dots , name: Some(ShortName {bytes: *b".s ", length: 2}), immediate: false},
TableEntry {f: drop , name: Some(ShortName {bytes: *b"dro", length: 4}), immediate: false}, TableEntry {f: drop , name: Some(ShortName {bytes: *b"dro", length: 4}), immediate: false},
TableEntry {f: dup , name: Some(ShortName {bytes: *b"dup", length: 3}), immediate: false}, TableEntry {f: dup , name: Some(ShortName {bytes: *b"dup", length: 3}), immediate: false},
TableEntry {f: dump , name: Some(ShortName {bytes: *b"dum", length: 4}), immediate: false}, TableEntry {f: dump , name: Some(ShortName {bytes: *b"dum", length: 4}), immediate: false},
TableEntry {f: forget , name: Some(ShortName {bytes: *b"for", length: 6}), immediate: false},
TableEntry {f: from_r_d, name: Some(ShortName {bytes: *b"r> ", length: 2}), immediate: false}, TableEntry {f: from_r_d, name: Some(ShortName {bytes: *b"r> ", length: 2}), immediate: false},
TableEntry {f: here , name: Some(ShortName {bytes: *b"her", length: 4}), immediate: false},
TableEntry {f: if_skip ,name: Some(ShortName {bytes: *b"? ", length: 1}), immediate: false}, TableEntry {f: if_skip ,name: Some(ShortName {bytes: *b"? ", length: 1}), immediate: false},
TableEntry {f: immediate,name: Some(ShortName {bytes: *b"imm", length: 9}), immediate: false}, TableEntry {f: immediate,name: Some(ShortName {bytes: *b"imm", length: 9}), immediate: false},
TableEntry {f: latest , name: Some(ShortName {bytes: *b"lat", length: 6}), immediate: false},
TableEntry {f: lbracket, name: Some(ShortName {bytes: *b"[ ", length: 1}), immediate: true}, TableEntry {f: lbracket, name: Some(ShortName {bytes: *b"[ ", length: 1}), immediate: true},
TableEntry {f: load , name: Some(ShortName {bytes: *b"@ ", length: 1}), immediate: false}, TableEntry {f: load , name: Some(ShortName {bytes: *b"@ ", length: 1}), immediate: false},
TableEntry {f: mul , name: Some(ShortName {bytes: *b"* ", length: 1}), immediate: false}, TableEntry {f: mul , name: Some(ShortName {bytes: *b"* ", length: 1}), immediate: false},
@ -96,9 +100,12 @@ fn new_core() -> Core {
init_dictionary(&mut c); init_dictionary(&mut c);
let autoexec = [ let autoexec = [
"create : ] create smudge ] [ 65535 ,", "create : ] create smudge ] [ 65535 ,",
"create ; ] unsmudge 65535 , [ ' [ , 65535 , immediate", "create ; ] unsmudge 65535 , [ ' [ , 65535 , immediate",
": recursive unsmudge ; immediate" ": recursive unsmudge ; immediate",
": literal 65534 , , ; immediate",
": constant create [ ' literal , ] [ ' ret ] literal , ;",
": variable create here 6 + [ ' literal , ] [ ' ret ] literal , 0 , ;"
]; ];
for s in autoexec { for s in autoexec {
@ -227,6 +234,13 @@ fn load(c: &mut Core) {
push(c, u16::from_le_bytes(c.ram[addr..=addr+1].try_into().unwrap())); push(c, u16::from_le_bytes(c.ram[addr..=addr+1].try_into().unwrap()));
} }
fn forget(c: &mut Core) {
let xt = pop(c);
c.here = xt - 6;
let i = c.here as usize;
c.dp = u16::from_le_bytes(c.ram[i..=i+1].try_into().unwrap());
}
// --- Stack management --- // --- Stack management ---
fn push(c: &mut Core, val: u16) { fn push(c: &mut Core, val: u16) {
@ -316,6 +330,12 @@ fn dot(c: &mut Core) {
print!("{} ", pop(c)); print!("{} ", pop(c));
} }
fn dots(c: &mut Core) {
for i in &c.dstack[0..c.tds] {
print!("{} ", i);
}
}
fn dump(c: &mut Core) { fn dump(c: &mut Core) {
println!("{:?}", c); println!("{:?}", c);
} }
@ -363,7 +383,6 @@ fn div(c: &mut Core) {
push(c, v2.saturating_div(v1)); push(c, v2.saturating_div(v1));
} }
// --- Inner interpreter --- // --- Inner interpreter ---
fn fetch(c: &mut Core) -> u16 { fn fetch(c: &mut Core) -> u16 {
@ -409,6 +428,14 @@ fn rbracket(c: &mut Core) {
c.state = State::Compiling; c.state = State::Compiling;
} }
fn latest(c: &mut Core) {
push(c, c.dp);
}
fn here(c: &mut Core) {
push(c, c.here);
}
fn outer(c: &mut Core, s: &str) { fn outer(c: &mut Core, s: &str) {
let ss = s.trim(); let ss = s.trim();
let mut tokens = ss.split(" ").peekable(); let mut tokens = ss.split(" ").peekable();