瀏覽代碼

Refactor Store to receive the name of the function as a parameter

* Global store is name as $global following internal standards

* Main function store is named as 'main'

* Enable controll over direct recursive calls

* Enable control over indirect recursive calls with the store stack
Lucas de Souza 5 年之前
父節點
當前提交
35034cf41c
共有 2 個文件被更改,包括 7 次插入8 次删除
  1. 5 7
      js/processor/ivprogProcessor.js
  2. 2 1
      js/processor/store/store.js

+ 5 - 7
js/processor/ivprogProcessor.js

@@ -29,7 +29,7 @@ export class IVProgProcessor {
 
   constructor (ast) {
     this.ast = ast;
-    this.globalStore = new Store();
+    this.globalStore = new Store("$global");
     this.stores = [this.globalStore];
     this.context = [Context.BASE];
     this.input = null;
@@ -101,7 +101,8 @@ export class IVProgProcessor {
   }
 
   runFunction (func, actualParameters, store) {
-    let funcStore = new Store();
+    const funcName = func.isMain ? 'main' : func.name;
+    let funcStore = new Store(funcName);
     funcStore.extendStore(this.globalStore);
     let returnStoreObject = null;
     if(func.returnType instanceof CompoundType) {
@@ -113,10 +114,7 @@ export class IVProgProcessor {
     } else {
       returnStoreObject = new StoreObject(func.returnType, null);
     }
-    const funcName = func.isMain ? 'main' : func.name;
-    const funcNameStoreObject = new StoreObject(Types.STRING, funcName, true);
     funcStore.insertStore('$', returnStoreObject);
-    funcStore.insertStore('$name', funcNameStoreObject);
     const newFuncStore$ = this.associateParameters(func.formalParameters, actualParameters, store, funcStore);
     return newFuncStore$.then(sto => {
       this.context.push(Context.FUNCTION);
@@ -420,7 +418,7 @@ export class IVProgProcessor {
     try {
       const funcType = store.applyStore('$');
       const $value = this.evaluateExpression(store, cmd.expression);
-      const funcName = store.applyStore('$name');
+      const funcName = store.name;
       return $value.then(vl => {
 
         if(vl === null && funcType.isCompatible(Types.VOID)) {
@@ -430,7 +428,7 @@ export class IVProgProcessor {
         if (vl === null || !funcType.type.isCompatible(vl.type)) {
           // TODO: Better error message -- Inform line and column from token!!!!
           // THIS IF SHOULD BE IN A SEMANTIC ANALYSER
-          return Promise.reject(new Error(`Function ${funcName.value} must return ${funcType.type} instead of ${vl.type}.`));
+          return Promise.reject(new Error(`Function ${funcName} must return ${funcType.type} instead of ${vl.type}.`));
         } else {
           let realValue = this.parseStoreObjectValue(vl);
           store.updateStore('$', realValue);

+ 2 - 1
js/processor/store/store.js

@@ -2,7 +2,8 @@ import { Modes } from './../modes';
 
 export class Store {
 
-  constructor() {
+  constructor(name) {
+    this.name = name;
     this.store = {};
     this.nextStore = null;
     this.mode = Modes.RUN;