Skip to content

Commit 429e398

Browse files
Add better error messages
1 parent ca4b10e commit 429e398

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/main/scala/Chapter2Lib.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ object Lvar {
198198
extends Lint.Interpretable {
199199
override def interpExpr(e: Expr, nv: List[(String,InterpreterValue)], inputs: ListBuffer[Long], outputs: ListBuffer[Long]): InterpreterValue = e match {
200200
case Var(x) =>
201-
nv.find({ case (y, _) => x == y }).get._2
201+
nv.find({ case (y, _) => x == y }).getOrElse(throw new RuntimeException("The interpreter to read variable " + x + " that had no value."))._2
202202
case Let(x, e, body) =>
203203
interpExpr(body, (x, interpExpr(e, nv, inputs, outputs)) :: nv, inputs, outputs)
204204
case Prim(Print(), List(e)) =>

src/main/scala/X86Lib.scala

+4-5
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,15 @@ object X86Interpreter {
132132
val strict: Boolean) {
133133

134134

135-
136135
def read(arg: Arg): Long = arg match {
137136
case Imm(v) => v
138-
case Reg(reg) => this.registers(reg)
137+
case Reg(reg) => this.registers.getOrElse(reg, throw new RuntimeException("The interpreter to read register " + reg + " that had no value."))
139138
case Deref(reg, offset) =>
140-
val addr = this.registers(reg) + offset
139+
val addr = this.registers.getOrElse(reg, throw new RuntimeException("The interpreter to read register " + reg + " that had no value.")) + offset
141140
assert(addr % 8 == 0, "Offset reads not supported")
142141
if (strict) assert(addr >= registers(RSP()), "Write outside of stack")
143142
this.memory(addr)
144-
case XVar(name) => this.variables(name)
143+
case XVar(name) => this.variables.getOrElse(name, throw new RuntimeException("The interpreter to read variable " + name + " that had no value."))
145144
case ByteRegister(reg) => reg match {
146145
case AL() => this.registers(RAX()).toByte
147146
case BL() => this.registers(RBX()).toByte
@@ -158,7 +157,7 @@ object X86Interpreter {
158157
case Imm(_) => assert(false, "Write to immediate value")
159158
case Reg(reg) => this.registers(reg) = w
160159
case Deref(reg, offset) =>
161-
val addr = this.registers(reg) + offset
160+
val addr = this.registers.getOrElse(reg, throw new RuntimeException("The interpreter to read register " + reg + " that had no value.")) + offset
162161
assert(addr % 8 == 0, "Offset reads not supported")
163162
if (strict) assert(addr >= registers(RSP()), "Write outside of stack")
164163
this.memory(addr) = w

0 commit comments

Comments
 (0)