time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

stack.js (920B)


      1 import { List } from "immutable";
      2 import { REOther } from "../errors/messages.js";
      3 import { ImmutableMap } from "../utility/immutableMap.js";
      4 export class Stack {
      5     constructor(stack = List()) {
      6         this.stack = stack;
      7     }
      8     static make() {
      9         return new Stack();
     10     }
     11     outOfBounds() {
     12         throw new REOther("Internal error: out of bounds stack index");
     13     }
     14     get(offset) {
     15         const size = this.stack.size;
     16         const pos = size - 1 - offset;
     17         if (pos < 0) {
     18             this.outOfBounds();
     19         }
     20         const result = this.stack.get(pos);
     21         if (!result) {
     22             this.outOfBounds();
     23         }
     24         return result.value;
     25     }
     26     push(name, value) {
     27         return new Stack(this.stack.push({ name, value }));
     28     }
     29     asBindings() {
     30         return ImmutableMap(this.stack.map((entry) => [entry.name, entry.value]));
     31     }
     32 }
     33 //# sourceMappingURL=stack.js.map