@@ -10,7 +10,7 @@ import parse.ast._
1010import java .io .File
1111
1212// If fname starts with "code:", then it is an instance of code.
13- class RunningInstance (fname : String , c : RunningInstance , args : Array [Type ]) {
13+ class RunningInstance (fn : String , c : RunningInstance , args : Array [Type ]) {
1414 // initial stuffs
1515 var bytecode : Array [Byte ] = Array [Byte ]()
1616 /* if (!fname.startsWith("code:")) {
@@ -27,6 +27,7 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
2727 }*/
2828 val needle : Int = 0
2929 val calling = c
30+ val fname = fn
3031 var environment = new HashMap [String , Type ]()
3132 var stack = List [Type ]()
3233 var symstack = List [LValue ]()
@@ -124,6 +125,14 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
124125 (new String (bytecode.slice(strSt, strSt + length), " UTF-8" ), strSt + length)
125126 }
126127 def tus (n : Byte ) = if (n >= 0 ) n else 0x100 + n
128+ def printStackTrace = {
129+ println(" Stacktrace: " )
130+ var curNode = this
131+ while (curNode != null ) {
132+ println((curNode.needle - 2 ).toHexString + " @" + curNode.fname)
133+ curNode = curNode.calling
134+ }
135+ }
127136 def run = { // runs the bytecode
128137 var needle = 0
129138 var isDone = false
@@ -244,6 +253,7 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
244253 stack = VariableReader .readData(bytecode.slice(needle, needle + size), valtype, " [ANON]" ) :: stack
245254 needle += size
246255 } else {
256+ printStackTrace
247257 throw new RuntimeException (" Invalid command: " + cmd + " @" + (needle - 2 ).toHexString)
248258 }
249259 }
@@ -253,8 +263,9 @@ class RunningInstance(fname: String, c: RunningInstance, args: Array[Type]) {
253263 if (! stack.isEmpty && stack.head.isInstanceOf [TError ]) {
254264 val e = stack.head
255265 stack = stack.tail
266+ printStackTrace
256267 throw new RuntimeException (" Runtime " + e + " : " + (needle - 2 ).toHexString + " @" + fname + " : " +
257- cmd.toHexString)
268+ cmd.toHexString)
258269 }
259270 }
260271 }
0 commit comments