Skip to content

Commit 145fdfa

Browse files
committed
Throw an error if VM code attempts to pop constant
1 parent 135c478 commit 145fdfa

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

src/CodeWriter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ void writePushPop(int command, String segment, int index) {
382382
writer.write("M=D\n"); // filename.i = *SP
383383
break;
384384
// Note: there is no case for pop constant i because constants are not actually part of the RAM
385+
case "constant": // pop constant i: pop i
386+
throw new IllegalArgumentException("Cannot pop a constant: " + index);
385387
case "this": // pop this i: pop THIS[i]
386388
writer.write("// pop this " + index + "\n"); // write a comment for readability
387389
writer.write("@" + index + "\n"); // load the index into the A register

0 commit comments

Comments
 (0)