user-callable return-stack juggling words that work correctly

This commit is contained in:
psf 2022-03-27 00:44:04 -07:00
parent 3aa9c06fd3
commit 72dae5a249

View file

@ -1,5 +1,5 @@
use std::io;
//use std::process;
use std::process;
use std::convert::TryInto;
//const CORE_SIZE: usize = 65408
@ -56,7 +56,7 @@ struct TableEntry {
immediate: bool
}
const PRIMITIVES: [TableEntry; 21] = [
const PRIMITIVES: [TableEntry; 24] = [
TableEntry {f: ret , name: None, immediate: false},
TableEntry {f: lit , name: None, immediate: false},
TableEntry {f: add , name: Some(ShortName {bytes: *b"+ ", length: 1}), immediate: false},
@ -68,15 +68,18 @@ const PRIMITIVES: [TableEntry; 21] = [
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: dump , name: Some(ShortName {bytes: *b"dum", length: 4}), immediate: false},
TableEntry {f: from_r_ , name: Some(ShortName {bytes: *b"r> ", length: 2}), immediate: false},
TableEntry {f: immediate,name: Some(ShortName {bytes: *b"imm", length: 9}), immediate: false},
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: mul , name: Some(ShortName {bytes: *b"* ", length: 1}), immediate: false},
TableEntry {f: ret_ , name: Some(ShortName {bytes: *b"ret", length: 3}), immediate: false},
TableEntry {f: rbracket, name: Some(ShortName {bytes: *b"] ", length: 1}), immediate: false},
TableEntry {f: store , name: Some(ShortName {bytes: *b"! ", length: 1}), immediate: false},
TableEntry {f: sub , name: Some(ShortName {bytes: *b"- ", length: 1}), immediate: false},
TableEntry {f: swap , name: Some(ShortName {bytes: *b"swa", length: 4}), immediate: false},
TableEntry {f: tick , name: Some(ShortName {bytes: *b"' ", length: 1}), immediate: false},
TableEntry {f: to_r_ , name: Some(ShortName {bytes: *b">r ", length: 2}), immediate: false},
TableEntry {f: word , name: Some(ShortName {bytes: *b"wor", length: 4}), immediate: false}
];
@ -237,11 +240,25 @@ fn to_r(c: &mut Core, val: u16) {
c.trs += 1;
}
fn to_r_(c: &mut Core) {
let r1 = from_r(c);
let r2 = pop(c);
to_r(c, r2);
to_r(c, r1);
}
fn from_r(c: &mut Core) -> u16 {
c.trs -= 1;
return c.rstack[c.trs];
}
fn from_r_(c: &mut Core) {
let r1 = from_r(c);
let r2 = from_r(c);
to_r(c, r1);
push(c, r2);
}
fn call(c: &mut Core) {
to_r(c, c.ip);
c.ip = pop(c);
@ -249,9 +266,17 @@ fn call(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);
}
fn ret_(c: &mut Core) {
_ = from_r(c);
ret(c);
}
// --- Inner interpreter ---
fn fetch(c: &mut Core) -> u16 {