further prettification

This commit is contained in:
Peter Fidelman 2022-03-26 15:20:52 -07:00
parent b7c07685c3
commit a6e611ad2f

View file

@ -45,26 +45,24 @@ fn truncate_name(name: &str) -> ShortName {
struct TableEntry { struct TableEntry {
f: Primitive, f: Primitive,
name: ShortName, name: Option<ShortName>,
dict: bool,
immediate: bool immediate: bool
} }
// NOTE: lazy_static or SyncLazy would allow nicer init
const PRIMITIVES: [TableEntry; 13] = [ const PRIMITIVES: [TableEntry; 13] = [
TableEntry {f: ret , name: ShortName {bytes: *b"ret", length: 3}, dict: false, immediate: false}, TableEntry {f: ret , name: None, immediate: false},
TableEntry {f: lit , name: ShortName {bytes: *b"lit", length: 3}, dict: false, immediate: false}, TableEntry {f: lit , name: None, immediate: false},
TableEntry {f: call , name: ShortName {bytes: *b"cal", length: 4}, dict: true, immediate: false}, TableEntry {f: call , name: Some(ShortName {bytes: *b"cal", length: 4}), immediate: false},
TableEntry {f: dot , name: ShortName {bytes: *b". ", length: 1}, dict: true, immediate: false}, TableEntry {f: dot , name: Some(ShortName {bytes: *b". ", length: 1}), immediate: false},
TableEntry {f: comma_ , name: ShortName {bytes: *b", ", length: 1}, dict: true, immediate: false}, TableEntry {f: comma_ , name: Some(ShortName {bytes: *b", ", length: 1}), immediate: false},
TableEntry {f: store , name: ShortName {bytes: *b"! ", length: 1}, dict: true, immediate: false}, TableEntry {f: store , name: Some(ShortName {bytes: *b"! ", length: 1}), immediate: false},
TableEntry {f: load , name: ShortName {bytes: *b"@ ", length: 1}, dict: true, immediate: false}, TableEntry {f: load , name: Some(ShortName {bytes: *b"@ ", length: 1}), immediate: false},
TableEntry {f: add , name: ShortName {bytes: *b"+ ", length: 1}, dict: true, immediate: false}, TableEntry {f: add , name: Some(ShortName {bytes: *b"+ ", length: 1}), immediate: false},
TableEntry {f: sub , name: ShortName {bytes: *b"- ", length: 1}, dict: true, immediate: false}, TableEntry {f: sub , name: Some(ShortName {bytes: *b"- ", length: 1}), immediate: false},
TableEntry {f: mul , name: ShortName {bytes: *b"* ", length: 1}, dict: true, immediate: false}, TableEntry {f: mul , name: Some(ShortName {bytes: *b"* ", length: 1}), immediate: false},
TableEntry {f: div , name: ShortName {bytes: *b"/ ", length: 1}, dict: true, immediate: false}, TableEntry {f: div , name: Some(ShortName {bytes: *b"/ ", length: 1}), immediate: false},
TableEntry {f: lbracket, name: ShortName {bytes: *b"[ ", length: 1}, dict: true, immediate: true}, TableEntry {f: lbracket, name: Some(ShortName {bytes: *b"[ ", length: 1}), immediate: true},
TableEntry {f: rbracket, name: ShortName {bytes: *b"] ", length: 1}, dict: true, immediate: false} TableEntry {f: rbracket, name: Some(ShortName {bytes: *b"] ", length: 1}), immediate: false}
]; ];
fn new_core() -> Core { fn new_core() -> Core {
@ -103,16 +101,19 @@ fn find(c: &mut Core, name: ShortName) -> Option<u16> {
fn init_dictionary(c: &mut Core) { fn init_dictionary(c: &mut Core) {
let mut opcode = 65535; let mut opcode = 65535;
for p in PRIMITIVES { for p in PRIMITIVES {
if p.dict { match p.name {
create(c, p.name); Some(name) => {
if p.immediate { create(c, name);
immediate(c); if p.immediate {
} immediate(c);
comma(c, opcode); }
comma(c, 65535); // ret comma(c, opcode);
comma(c, 65535); // ret
}
None => {}
} }
opcode -= 1; opcode -= 1;
} }
} }
fn smudge(c: &mut Core) { fn smudge(c: &mut Core) {
@ -138,14 +139,6 @@ fn comma_(c: &mut Core) {
comma(c, val); comma(c, val);
} }
fn lbracket(c: &mut Core) {
c.state = State::Interpreting;
}
fn rbracket(c: &mut Core) {
c.state = State::Compiling;
}
// --- Memory management --- // --- Memory management ---
fn store(c: &mut Core) { fn store(c: &mut Core) {
@ -268,18 +261,19 @@ fn div(c: &mut Core) {
// --- Outer interpreter --- // --- Outer interpreter ---
fn lbracket(c: &mut Core) {
c.state = State::Interpreting;
}
fn rbracket(c: &mut Core) {
c.state = State::Compiling;
}
fn outer(c: &mut Core, s: &str) { fn outer(c: &mut Core, s: &str) {
let ss = s.trim(); let ss = s.trim();
let tokens = ss.split(" "); let tokens = ss.split(" ");
for t in tokens { for t in tokens {
match t { match t {
"s" => {
step(c);
},
"g" => {
c.ip = 0;
inner(c);
},
"dmp" => { "dmp" => {
println!("{:?}", c); println!("{:?}", c);
}, },