Browse Source

Merge branch 'matrixInitVal' of LInE/ivprog into master

Lucas de Souza 5 years ago
parent
commit
8ed5ad70bc

+ 19 - 54
index.html

@@ -33,67 +33,32 @@
           programa {
 
             funcao inicio() {
-              inteiro a[16] = {1,8,3,5,7,4,2,1,8,3,5,7,-4,2,10,-1}
-              inteiro tam = numero_elementos(a) -1
-              inteiro i
-              a = mergeSort(a,0,tam)
-              para(i = 0; i< tam; i = i + 1) {
-                escreva(a[i]+", ")
-              }
-              escreva(a[i])
-            }
-        
-            funcao inteiro[] mergeSort(inteiro a[], inteiro init, inteiro end) {
-              inteiro meio = (init+end)/2
-              inteiro size1 = meio - init + 1, size2 = end - meio, sizeF = end - init + 1
-              inteiro p1[size1]
-              inteiro p2[size2]
-              inteiro f[sizeF]
-              se (init < end) {
-                p1 = mergeSort(a, init, meio)
-                p2 = mergeSort(a, meio+1, end)
-                f = merge(p1,p2)
-                retorne f
-              } senao {
-                retorne {a[init]}
-              }
+              cadeia a = "mustache"
+              cadeia b = "mostacheh"
+              escreva(editDist(a,b,comprimento(a), comprimento(b)))
             }
         
-            funcao inteiro[] merge(inteiro p1[], inteiro p2[]) {
-              inteiro lenp1 = numero_elementos(p1)
-              inteiro lenp2 = numero_elementos(p2)
-              inteiro sizeF = lenp1 + lenp2
-              inteiro f[sizeF]
-              inteiro i = 0, a = 0, b =0
-              enquanto(i < lenp1+lenp2) {
-                se(a < lenp1) {
-                  se(b <lenp2) {
-                    se(p1[a] <= p2[b]) {
-                      f[i] = p1[a]
-                      a = a + 1
-                      i = i + 1
-                    } senao {
-                      f[i] = p2[b]
-                      b = b + 1
-                      i = i + 1
-                    }
+            funcao inteiro editDist(cadeia str1 , cadeia str2 , inteiro m ,inteiro n) {
+              inteiro l = m + 1
+              inteiro c = n + 1
+              inteiro i, j
+              inteiro mat[l][c]
+              para(i = 0; i <= m; i = i + 1 ) {
+                para(j = 0; j <= n; j = j + 1) {
+                  se (i==0) {
+                    mat[i][j] = j
+                  } senao se (j == 0) {
+                    mat[i][j] = i
+                  } senao se (char_at(str1, i-1) == char_at(str2, j-1)) {
+                    mat[i][j] = mat[i-1][j-1]
                   } senao {
-                    enquanto(a < lenp1) {
-                      f[i] = p1[a]
-                      a = a + 1
-                      i = i + 1
-                    }
-                  }
-                } senao {
-                  enquanto(b < lenp2) {
-                    f[i] = p2[b]
-                    b = b + 1
-                    i = i + 1
+                    mat[i][j] = 1 + Matematica.minimo({mat[i][j-1], mat[i-1][j], mat[i-1][j-1]})
                   }
                 }
               }
-              retorne f
+              retorne mat[m][n]
             }
+        
           }
         </textarea>
       </div>

+ 34 - 6
js/processor/ivprogProcessor.js

@@ -142,9 +142,15 @@ export class IVProgProcessor {
             }
             calleeStore.insertStore(formalParameter.id, ref);
           } else {
-            calleeStore.insertStore(formalParameter.id, stoObj);
+            let realValue = this.parseStoreObjectValue(stoObj);
+            calleeStore.insertStore(formalParameter.id, realValue);
           }
         } else {
+          console.log("######");
+          console.log(stoObj);
+          console.log(stoObj.value);
+          console.log(stoObj.type);
+          console.log(stoObj.refValue);
           throw new Error(`Parameter ${formalParameter.id} is not compatible with the value given.`);
         }
       }
@@ -387,7 +393,8 @@ export class IVProgProcessor {
           // THIS IF SHOULD BE IN A SEMANTIC ANALYSER
           return Promise.reject(new Error(`Function ${funcName.value} must return ${funcType.type} instead of ${vl.type}.`));
         } else {
-          store.updateStore('$', vl);
+          let realValue = this.parseStoreObjectValue(vl);
+          store.updateStore('$', realValue);
           store.mode = Modes.RETURN;
           return Promise.resolve(store);
         }
@@ -410,7 +417,8 @@ export class IVProgProcessor {
     try {
       const $value = this.evaluateExpression(store, cmd.expression);
       return $value.then( vl => {
-        store.updateStore(cmd.id, vl) 
+        let realValue = this.parseStoreObjectValue(vl);
+        store.updateStore(cmd.id, realValue) 
         return store;
       });
     } catch (error) {
@@ -448,7 +456,7 @@ export class IVProgProcessor {
           }
           column = columnSO.number;
         }
-        const value = results[2];
+        const value = this.parseStoreObjectValue(results[2]);
         if (line >= mustBeArray.lines) {
           // TODO: better error message
           return Promise.reject(new Error(`${exp.id}: index out of bounds: ${lines}`));
@@ -522,8 +530,8 @@ export class IVProgProcessor {
           } else {
             realValue = new StoreObjectArray(cmd.type, line, column, [])
             if(column !== null) {
-              for (let i = 0; i < column; i++) {
-                realValue.value.push([]);
+              for (let i = 0; i < line; i++) {
+                realValue.value.push(new StoreObjectArray(new CompoundType(cmd.type.innerType, 1), column, null, []));
               }
             }
           }
@@ -816,4 +824,24 @@ export class IVProgProcessor {
     });
   }
 
+  parseStoreObjectValue (vl) {
+    let realValue = vl;
+    if(vl instanceof StoreObjectArrayAddress) {      
+      if(vl.type instanceof CompoundType) {
+        switch(vl.type.dimensions) {
+          case 1: {
+            realValue = new StoreObjectArray(vl.type, vl.value);
+            break;
+          }
+          default: {
+            throw new Error("Three dimensional array address...");
+          }
+        }
+      } else {
+        realValue = new StoreObject(vl.type, vl.value);
+      }
+    }
+    return realValue;
+  }
+
 }

+ 1 - 1
js/processor/lib/math.js

@@ -179,7 +179,7 @@ export function createMaxFun () {
 export function createMinFun () {
   const minFun = (sto, _) => {
     const x = sto.applyStore('x');
-    const numbers = x.value.map(stoObj => stoObj.number);
+    const numbers = x.value.map(stoObj => stoObj.value.toNumber());
     const result = BigNumber.min(numbers);
     const temp = new StoreObject(x.type.innerType, result);
     return Promise.resolve(sto.updateStore('$', temp));

+ 3 - 3
js/processor/lib/strings.js

@@ -34,7 +34,7 @@ export function createSubstringFun () {
 export function createLengthFun () {
   const lengthFun = (sto, _) => {
     const str = sto.applyStore("str");
-    const temp = new StoreObject(Types.INTEGER, toInt(value.length));
+    const temp = new StoreObject(Types.INTEGER, toInt(str.value.length));
     return Promise.resolve(sto.updateStore("$", temp));
   }
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(lengthFun)]);
@@ -74,10 +74,10 @@ export function createrCharAtFun () {
   const charAtFun = (sto, _) => {
     const str = sto.applyStore("str");
     const idx = sto.applyStore("index");
-    if (idx.value < 0 || idx.value >= str.value.length) {
+    if (idx.value.toNumber() < 0 || idx.value.toNumber() >= str.value.length) {
       return Promise.reject(new Error("invalid string position"));
     }
-    const temp = new StoreObject(Types.STRING, str.value.charAt(idx.value));
+    const temp = new StoreObject(Types.STRING, str.value.charAt(idx.value.toNumber()));
     return Promise.resolve(sto.updateStore("$", temp));
   }
   const block = new Commands.CommandBlock([],  [new Commands.SysCall(charAtFun)]);

+ 3 - 3
js/processor/store/storeObjectArrayAddress.js

@@ -1,6 +1,5 @@
 import { StoreObject } from './storeObject';
 import { StoreObjectArray } from './storeObjectArray';
-import { Types } from '../../typeSystem/types';
 import { CompoundType } from '../../typeSystem/compoundType';
 
 export class StoreObjectArrayAddress extends StoreObject {
@@ -24,7 +23,8 @@ export class StoreObjectArrayAddress extends StoreObject {
   get refValue () {
     const refLine = this.store.applyStore(this.refID).value[this.line];
     if (this.column !== null) {
-      return refLine.value[this.column];
+      const refColumn = refLine.value[this.column];
+      return refColumn;
     }
     return refLine;
   }
@@ -38,7 +38,7 @@ export class StoreObjectArrayAddress extends StoreObject {
   }
 
   get lines () {
-    if(this.type !== Types.ARRAY) {
+    if(!(this.type instanceof CompoundType)) {
       return null;
     }
     return this.refValue.value.length;

+ 57 - 0
tests/test66.spec.js

@@ -0,0 +1,57 @@
+
+
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { SemanticAnalyser } from './../js/processor/semantic/semanticAnalyser';
+import { LanguageService } from '../js/services/languageService';
+import { OutputTest } from '../js/util/outputTest';
+import { IVProgProcessor } from '../js/processor/ivprogProcessor';
+
+describe('Non initialized matrix', function () {
+
+  localStorage.setItem('ivprog.lang', 'pt');
+
+  const code = `programa {
+
+    funcao inicio() {
+      cadeia a = "mustache"
+      cadeia b = "mostacheh"
+      escreva(editDist(a,b,comprimento(a), comprimento(b)))
+    }
+  
+    funcao inteiro editDist(cadeia str1 , cadeia str2 , inteiro m ,inteiro n) {
+      inteiro l = m + 1
+      inteiro c = n + 1
+      inteiro i, j
+      inteiro mat[l][c]
+      para(i = 0; i <= m; i = i + 1 ) {
+        para(j = 0; j <= n; j = j + 1) {
+          se (i==0) {
+            mat[i][j] = j
+          } senao se (j == 0) {
+            mat[i][j] = i
+          } senao se (char_at(str1, i-1) == char_at(str2, j-1)) {
+            mat[i][j] = mat[i-1][j-1]
+          } senao {
+            mat[i][j] = 1 + Matematica.minimo({mat[i][j-1], mat[i-1][j], mat[i-1][j-1]})
+          }
+        }
+      }
+      retorne mat[m][n]
+  } 
+  
+  }`
+
+  const lexer = LanguageService.getCurrentLexer();
+  const out = new OutputTest();
+
+  it(`should not throw an exception`, function (done) {
+    const parser = new IVProgParser(code, lexer);
+    const sem = new SemanticAnalyser(parser.parseTree());
+    const exec = new IVProgProcessor(sem.analyseTree());
+    exec.registerOutput(out);
+    exec.interpretAST().then(_ => {
+      expect(out.list.length).toEqual(1);
+      done()
+    }).catch( err => done(err));
+  });
+});