From 72dae5a249027471a288e361f593abfad287a33b Mon Sep 17 00:00:00 2001 From: psf Date: Sun, 27 Mar 2022 00:44:04 -0700 Subject: [PATCH] user-callable return-stack juggling words that work correctly --- frustration.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/frustration.rs b/frustration.rs index 63d87ba..0597579 100644 --- a/frustration.rs +++ b/frustration.rs @@ -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 {