diff --git a/lab17/compiler.js b/lab17/compiler.js index 01240a5..7a4791d 100644 --- a/lab17/compiler.js +++ b/lab17/compiler.js @@ -140,12 +140,12 @@ class Compiler { // Booleans will be stored as either 1 for true, or as a 0 for false. return; } else if (ast.type === VAR) { - // - // ***YOUR CODE HERE*** - // // We look up the offset for a variable and push the offset // value on to the stack. The 'MLOAD' operation will // retrieve the value stored at that position in the memory. + this.writeOp('PUSH1'); + this.writeByte(this.varMap[ast.value]); + this.writeOp('MLOAD'); return; } @@ -167,9 +167,6 @@ class Compiler { break; case "define": - // - // ***YOUR CODE HERE*** - // // The define function lets us store variables. // // The variable name is stored in 'second.value'. @@ -182,6 +179,14 @@ class Compiler { // // Increment this.varOffset so that it points to the next // 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; case "if": diff --git a/lab17/op-codes.js b/lab17/op-codes.js index 3ee9473..ea24746 100644 --- a/lab17/op-codes.js +++ b/lab17/op-codes.js @@ -18,6 +18,15 @@ let opcodes = { let v2 = vm.stack.pop(); 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) => { // Does nothing. We could check to make sure that jumps // always land at JUMPDEST opcodes, but it is not totally