ソースを参照

Implement skeleton of the IVProg AST processor

Lucas de Souza 5 年 前
コミット
caaecb9381

+ 11 - 0
js/processor/ivprogProcessor.js

@@ -0,0 +1,11 @@
+export class IVProgProcessor {
+
+  constructor(ast, store) {
+    this.ast = ast;
+    this.store = store;
+  }
+
+  interpretAST () {
+    this.initGlobal();
+  }
+}

+ 49 - 0
js/processor/store/store.js

@@ -0,0 +1,49 @@
+export class Store {
+
+  constructor () {
+    this.store = {};
+    this.nextStore = null;
+  }
+
+  extendStore (nextStore) {
+    return Object.assign(new Store, {
+      store: this.store,
+      nextStore: nextStore
+    });
+  }
+
+  findVariable (id) {
+    if(!this.store[id]) {
+      if (this.nextStore === null) {
+        throw new Error("Undefined variable: " + id);
+      } else {
+        return this.nextStore.findVariable(id);
+      }
+    }
+    return this.store[id];
+  }
+
+  isDefined (id) {
+    if(!this.store[id]) {
+      if (this.nextStore === null) {
+        return false;
+      } else {
+        return this.nextStore.isDefined(id);
+      }
+    }
+    return true;
+  }
+
+  updateVariable (id, storeObj) {
+    if(!this.isDefined(id)) {
+      this.store[id] =  storeObj;
+    } else {
+      const old = this.findVariable(id);
+      if (storeObj.type !== old.type) {
+        throw new Error(`${storeObj.value} is not compatible with ${old.type}`);
+      } else {
+        this.store[id] = storeObj;
+      }
+    }
+  }
+}

+ 7 - 0
js/processor/store/storeObject.js

@@ -0,0 +1,7 @@
+export class StoreObject {
+
+  constructor(type, value) {
+    this.type = type;
+    this.value = value;
+  }
+}