lab17: part 5

This commit is contained in:
2026-04-23 01:24:04 -07:00
parent c11daa808f
commit 95bb649249
3 changed files with 51 additions and 6 deletions

View File

@@ -134,10 +134,9 @@ class Compiler {
this.writeByte(ast.value);
return;
} else if (ast.type === BOOL) {
//
// ***YOUR CODE HERE***
//
// Booleans will be stored as either 1 for true, or as a 0 for false.
this.writeOp('PUSH1');
this.writeByte(ast.value ? 1 : 0);
return;
} else if (ast.type === VAR) {
// We look up the offset for a variable and push the offset
@@ -190,12 +189,35 @@ class Compiler {
break;
case "if":
//
// ***YOUR CODE HERE***
//
// EXTRA CREDIT!
// Add support for if expressions.
// The cond.scm file gives you some good examples.
this.writeBytecode(second);
// get the offset program counter to change it later
this.writeOp('PUSH1');
const counterOffset = this.offset;
this.writeByte(0x00);
this.writeOp('JUMPI');
// else
this.writeBytecode(rest[1]);
// change this to after the then
this.writeOp('PUSH1');
const elseOffset = this.offset;
this.writeByte(0x00);
this.writeOp('JUMP');
// then
const thenOffset = this.offset;
this.writeOp('JUMPDEST');
this.writeBytecode(rest[0]);
const endOffset = this.offset;
this.writeOp('JUMPDEST');
// update the offsets
this.bytecode[counterOffset] = thenOffset;
this.bytecode[elseOffset] = endOffset;
break;
case "+":