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