Skip to content

Commit

Permalink
Added stackTrace which was somehow missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeStrout committed Oct 23, 2022
1 parent db7f563 commit b10b837
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
16 changes: 16 additions & 0 deletions Farmtronics/M1/M1API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,22 @@ public static void Init(Shell shell) {
f.code = (context, partialResult) => {
return new Intrinsic.Result(WorldInfo());
};

// stackTrace: get a list describing the call stack.
f = Intrinsic.Create("stackTrace");
f.code = (context, partialResult) => {
TAC.Machine vm = context.vm;
if (vm.globalContext.variables.ContainsKey(_stackAtBreak)) {
// We have a stored stack from a break or exit.
// So, display that. This gets cleared by 'run' so should never interfere with
// showing a more up-to-date stack during a run.
return new Intrinsic.Result(vm.globalContext.variables.map[_stackAtBreak]);
}
// Otherwise, build a stack now from the state of the VM.
ValList result = StackList(context.vm);
return new Intrinsic.Result(result);
};

}

static bool DisallowAllAssignment(Value key, Value value) {
Expand Down
23 changes: 18 additions & 5 deletions Farmtronics/M1/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ public bool allowControlCBreak {
ValString curScreenColor;

public TextDisplay textDisplay { get { return console.display; } }


ValList stackAtLastErr;

public Shell() {
console = new Console(this);

// prepare the interpreter
interpreter = new Interpreter(null, PrintLineWithTaskCheck, PrintLine);
interpreter = new Interpreter(null, PrintLineWithTaskCheck, PrintErrLine);
interpreter.implicitOutput = PrintLine;
interpreter.hostData = this;
}
Expand Down Expand Up @@ -275,7 +277,8 @@ public void Break(bool silent=false) {
if (!silent && !allowControlCBreak) return;

// grab the full stack and tuck it away for future reference
ValList stack = M1API.StackList(interpreter.vm);
ValList stack = stackAtLastErr;
if (stack == null) stack = M1API.StackList(interpreter.vm);

// also find the first non-null entry, to display right away
SourceLoc loc = null;
Expand Down Expand Up @@ -349,8 +352,8 @@ void Clear() {

public void Exit() {
if (interpreter.Running()) {
//interpreter.vm.globalContext.variables.SetElem(MiniMicroAPI._stackAtBreak,
// MiniMicroAPI.StackList(interpreter.vm));
interpreter.vm.globalContext.variables.SetElem(M1API._stackAtBreak,
M1API.StackList(interpreter.vm));
interpreter.Stop();
}
}
Expand All @@ -361,6 +364,16 @@ public void PrintLine(string line) {
disp.Print(disp.delimiter);
}

public void PrintErrLine(string line) {
if (interpreter.vm != null) {
stackAtLastErr = M1API.StackList(interpreter.vm);
interpreter.vm.globalContext.variables.SetElem(M1API._stackAtBreak, stackAtLastErr);
} else {
stackAtLastErr = new ValList(); // empty list signifies error without a VM, e.g. at compile time.
}
PrintLine(line);
}

void PrintLineWithTaskCheck(string line) {
PrintLine(line);
ToDoManager.NotePrintOutput(line);
Expand Down

0 comments on commit b10b837

Please sign in to comment.