From a6e611ad2fa828d6baff5d47b273bd82024b5368 Mon Sep 17 00:00:00 2001 From: Peter Fidelman Date: Sat, 26 Mar 2022 15:20:52 -0700 Subject: [PATCH] further prettification --- frustration.rs | 72 +++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/frustration.rs b/frustration.rs index 95c3c08..0f4a766 100644 --- a/frustration.rs +++ b/frustration.rs @@ -45,26 +45,24 @@ fn truncate_name(name: &str) -> ShortName { struct TableEntry { f: Primitive, - name: ShortName, - dict: bool, + name: Option, immediate: bool } -// NOTE: lazy_static or SyncLazy would allow nicer init const PRIMITIVES: [TableEntry; 13] = [ - TableEntry {f: ret , name: ShortName {bytes: *b"ret", length: 3}, dict: false, immediate: false}, - TableEntry {f: lit , name: ShortName {bytes: *b"lit", length: 3}, dict: false, immediate: false}, - TableEntry {f: call , name: ShortName {bytes: *b"cal", length: 4}, dict: true, immediate: false}, - TableEntry {f: dot , name: ShortName {bytes: *b". ", length: 1}, dict: true, immediate: false}, - TableEntry {f: comma_ , name: ShortName {bytes: *b", ", length: 1}, dict: true, immediate: false}, - TableEntry {f: store , name: ShortName {bytes: *b"! ", length: 1}, dict: true, immediate: false}, - TableEntry {f: load , name: ShortName {bytes: *b"@ ", length: 1}, dict: true, immediate: false}, - TableEntry {f: add , name: ShortName {bytes: *b"+ ", length: 1}, dict: true, immediate: false}, - TableEntry {f: sub , name: ShortName {bytes: *b"- ", length: 1}, dict: true, immediate: false}, - TableEntry {f: mul , name: ShortName {bytes: *b"* ", length: 1}, dict: true, immediate: false}, - TableEntry {f: div , name: ShortName {bytes: *b"/ ", length: 1}, dict: true, immediate: false}, - TableEntry {f: lbracket, name: ShortName {bytes: *b"[ ", length: 1}, dict: true, immediate: true}, - TableEntry {f: rbracket, name: ShortName {bytes: *b"] ", length: 1}, dict: true, immediate: false} + TableEntry {f: ret , name: None, immediate: false}, + TableEntry {f: lit , name: None, immediate: false}, + TableEntry {f: call , name: Some(ShortName {bytes: *b"cal", length: 4}), immediate: false}, + TableEntry {f: dot , name: Some(ShortName {bytes: *b". ", length: 1}), immediate: false}, + TableEntry {f: comma_ , name: Some(ShortName {bytes: *b", ", length: 1}), immediate: false}, + TableEntry {f: store , name: Some(ShortName {bytes: *b"! ", length: 1}), immediate: false}, + TableEntry {f: load , name: Some(ShortName {bytes: *b"@ ", length: 1}), immediate: false}, + TableEntry {f: add , name: Some(ShortName {bytes: *b"+ ", length: 1}), immediate: false}, + TableEntry {f: sub , name: Some(ShortName {bytes: *b"- ", length: 1}), immediate: false}, + TableEntry {f: mul , name: Some(ShortName {bytes: *b"* ", length: 1}), immediate: false}, + TableEntry {f: div , name: Some(ShortName {bytes: *b"/ ", length: 1}), immediate: false}, + TableEntry {f: lbracket, name: Some(ShortName {bytes: *b"[ ", length: 1}), immediate: true}, + TableEntry {f: rbracket, name: Some(ShortName {bytes: *b"] ", length: 1}), immediate: false} ]; fn new_core() -> Core { @@ -103,16 +101,19 @@ fn find(c: &mut Core, name: ShortName) -> Option { fn init_dictionary(c: &mut Core) { let mut opcode = 65535; for p in PRIMITIVES { - if p.dict { - create(c, p.name); - if p.immediate { - immediate(c); - } - comma(c, opcode); - comma(c, 65535); // ret + match p.name { + Some(name) => { + create(c, name); + if p.immediate { + immediate(c); + } + comma(c, opcode); + comma(c, 65535); // ret + } + None => {} } opcode -= 1; - } + } } fn smudge(c: &mut Core) { @@ -138,14 +139,6 @@ fn comma_(c: &mut Core) { comma(c, val); } -fn lbracket(c: &mut Core) { - c.state = State::Interpreting; -} - -fn rbracket(c: &mut Core) { - c.state = State::Compiling; -} - // --- Memory management --- fn store(c: &mut Core) { @@ -268,18 +261,19 @@ fn div(c: &mut Core) { // --- 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) { let ss = s.trim(); let tokens = ss.split(" "); for t in tokens { match t { - "s" => { - step(c); - }, - "g" => { - c.ip = 0; - inner(c); - }, "dmp" => { println!("{:?}", c); },