trace mode

This commit is contained in:
zeroflag 2021-06-30 13:40:54 +02:00
parent 2290dc5070
commit 5105ab2351

View file

@ -37,6 +37,7 @@ public class Fcl {
private final Object[] heap; private final Object[] heap;
private int dp = SCRATCH_SIZE; private int dp = SCRATCH_SIZE;
private int ip = 0; private int ip = 0;
private boolean trace = false;
class ColonDef implements Word { class ColonDef implements Word {
private final int address; private final int address;
@ -350,6 +351,7 @@ public class Fcl {
addPrimitive("nil", () -> stack.push(Nil.INSTANCE)); addPrimitive("nil", () -> stack.push(Nil.INSTANCE));
addPrimitive("here", () -> stack.push(new Num(dp))); addPrimitive("here", () -> stack.push(new Num(dp)));
addPrimitive("interpret", () -> mode = Mode.INTERPRET); addPrimitive("interpret", () -> mode = Mode.INTERPRET);
addPrimitive("trace", () -> trace = stack.pop().boolValue());
addPrimitive("lit", () -> stack.push((Obj)heap[ip++])); addPrimitive("lit", () -> stack.push((Obj)heap[ip++]));
addPrimitive(">r", () -> rstack.push(stack.pop())); addPrimitive(">r", () -> rstack.push(stack.pop()));
addPrimitive("r>", () -> stack.push(rstack.pop())); addPrimitive("r>", () -> stack.push(rstack.pop()));
@ -529,18 +531,25 @@ public class Fcl {
Word word = dict.at(name); Word word = dict.at(name);
switch (mode) { switch (mode) {
case INTERPRET: case INTERPRET:
if (word != null) if (word != null) {
trace("exec " + word.name());
word.enter(); word.enter();
else } else {
trace("push " + name);
stack.push(recognize(name)); stack.push(recognize(name));
}
break; break;
case COMPILE: case COMPILE:
if (word != null) { if (word != null) {
if (dict.isImmediate(name)) if (dict.isImmediate(name)) {
trace("exec " + word.name());
word.enter(); word.enter();
else
heap[dp++] = word;
} else { } else {
trace(word.name() + " ,");
heap[dp++] = word;
}
} else {
trace("lit " + name + " ,");
heap[dp++] = dict.at("lit"); heap[dp++] = dict.at("lit");
heap[dp++] = recognize(name); heap[dp++] = recognize(name);
} }
@ -548,6 +557,13 @@ public class Fcl {
} }
} }
private void trace(String str) {
if (trace) {
transcript.show("T/" + mode.toString().charAt(0) + " " + str);
transcript.cr();
}
}
private Obj recognize(String token) { private Obj recognize(String token) {
Obj str = recognizeStr(token); Obj str = recognizeStr(token);
if (str != null) return str; if (str != null) return str;