/*
 * Whitespace-JS / inst/heap.js
 * copyright (c) 2016 Susisu
 */

"use strict";

function end() {
    module.exports = Object.freeze({
        HeapAccess,
        Store,
        Retrieve
    });
}

const inst = require("./inst.js");

class HeapAccess extends inst.Instruction {
    constructor(pos) {
        super(pos);
    }

    toWhitespace() {
        return inst.TAB + inst.TAB + this.toSubWhitespace();
    }

    toSubWhitespace() {
        throw new Error("unknown instruction");
    }
}

class Store extends HeapAccess {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.SPACE;
    }

    toUnWhitespace() {
        return "store";
    }
}

class Retrieve extends HeapAccess {
    constructor(pos) {
        super(pos);
    }

    toSubWhitespace() {
        return inst.TAB;
    }

    toUnWhitespace() {
        return "retrieve";
    }
}

end();

@@ -683,24 +683,161 @@
 tore%22;%0A    %7D
+%0A%0A    doSub(vm) %7B%0A        let n   = vm.state.popStack();%0A        let loc = vm.state.popStack();%0A        vm.state.storeHeap(loc, n);%0A    %7D
 %0A%7D%0A%0Aclass Re
@@ -1021,23 +1021,118 @@
 ieve%22;%0A    %7D
+%0A%0A    doSub(vm) %7B%0A        vm.state.pushStack(vm.state.retrieveHeap(vm.state.popStack()));%0A    %7D
 %0A%7D%0A%0Aend();%0A

Add heap access instructions