mirror of
https://gitlab.cs.washington.edu/fidelp/frustration.git
synced 2024-12-25 21:58:11 +01:00
further prettification
This commit is contained in:
parent
b7c07685c3
commit
a6e611ad2f
1 changed files with 33 additions and 39 deletions
|
@ -45,26 +45,24 @@ fn truncate_name(name: &str) -> ShortName {
|
|||
|
||||
struct TableEntry {
|
||||
f: Primitive,
|
||||
name: ShortName,
|
||||
dict: bool,
|
||||
name: Option<ShortName>,
|
||||
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<u16> {
|
|||
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);
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue