lab17: part 4

This commit is contained in:
2026-04-23 00:31:22 -07:00
parent b7285c39d6
commit c11daa808f
2 changed files with 20 additions and 6 deletions

View File

@@ -140,12 +140,12 @@ class Compiler {
// Booleans will be stored as either 1 for true, or as a 0 for false. // Booleans will be stored as either 1 for true, or as a 0 for false.
return; return;
} else if (ast.type === VAR) { } else if (ast.type === VAR) {
//
// ***YOUR CODE HERE***
//
// We look up the offset for a variable and push the offset // We look up the offset for a variable and push the offset
// value on to the stack. The 'MLOAD' operation will // value on to the stack. The 'MLOAD' operation will
// retrieve the value stored at that position in the memory. // retrieve the value stored at that position in the memory.
this.writeOp('PUSH1');
this.writeByte(this.varMap[ast.value]);
this.writeOp('MLOAD');
return; return;
} }
@@ -167,9 +167,6 @@ class Compiler {
break; break;
case "define": case "define":
//
// ***YOUR CODE HERE***
//
// The define function lets us store variables. // The define function lets us store variables.
// //
// The variable name is stored in 'second.value'. // The variable name is stored in 'second.value'.
@@ -182,6 +179,14 @@ class Compiler {
// //
// Increment this.varOffset so that it points to the next // Increment this.varOffset so that it points to the next
// position in memory. // position in memory.
this.varMap[second.value] = this.varOffset;
rest.forEach((x) => {
this.writeBytecode(x);
});
this.writeOp('PUSH1');
this.writeByte(this.varOffset);
this.writeOp('MSTORE');
this.varOffset++;
break; break;
case "if": case "if":

View File

@@ -18,6 +18,15 @@ let opcodes = {
let v2 = vm.stack.pop(); let v2 = vm.stack.pop();
vm.stack.push(v1-v2); vm.stack.push(v1-v2);
}}, }},
0x52: { mnemonic: 'MLOAD', evaluate: (vm) => {
let offset = vm.stack.pop();
vm.stack.push(vm.memory[offset]);
}},
0x53: { mnemonic: 'MSTORE', evaluate: (vm) => {
let offset = vm.stack.pop();
let value = vm.stack.pop();
vm.memory[offset] = value;
}},
0x5B: { mnemonic: 'JUMPDEST', evaluate: (vm) => { 0x5B: { mnemonic: 'JUMPDEST', evaluate: (vm) => {
// Does nothing. We could check to make sure that jumps // Does nothing. We could check to make sure that jumps
// always land at JUMPDEST opcodes, but it is not totally // always land at JUMPDEST opcodes, but it is not totally