stack ops

This commit is contained in:
psf 2022-03-27 00:15:39 -07:00
parent e594cb6298
commit 3aa9c06fd3

View file

@ -56,7 +56,7 @@ struct TableEntry {
immediate: bool
}
const PRIMITIVES: [TableEntry; 18] = [
const PRIMITIVES: [TableEntry; 21] = [
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},
@ -65,6 +65,8 @@ const PRIMITIVES: [TableEntry; 18] = [
TableEntry {f: create_ , name: Some(ShortName {bytes: *b"cre", length: 6}), 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: 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: immediate,name: Some(ShortName {bytes: *b"imm", length: 9}), immediate: false},
TableEntry {f: lbracket, name: Some(ShortName {bytes: *b"[ ", length: 1}), immediate: true},
@ -73,6 +75,7 @@ const PRIMITIVES: [TableEntry; 18] = [
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: word , name: Some(ShortName {bytes: *b"wor", length: 4}), immediate: false}
];
@ -212,6 +215,23 @@ fn pop(c: &mut Core) -> u16 {
return c.dstack[c.tds];
}
fn dup(c: &mut Core) {
let val = pop(c);
push(c, val);
push(c, val);
}
fn swap(c: &mut Core) {
let val1 = pop(c);
let val2 = pop(c);
push(c, val1);
push(c, val2);
}
fn drop(c: &mut Core) {
let _ = pop(c);
}
fn to_r(c: &mut Core, val: u16) {
c.rstack[c.trs] = val;
c.trs += 1;