Forráskód Böngészése

Fix relational operation resulting in an invalid type

Lucas de Souza 5 éve
szülő
commit
dc6669676f
2 módosított fájl, 34 hozzáadás és 1 törlés
  1. 5 1
      js/processor/compatibilityTable.js
  2. 29 0
      tests/test51.spec.js

+ 5 - 1
js/processor/compatibilityTable.js

@@ -123,7 +123,11 @@ const unaryMap = buildUnaryCompatibilityTable();
 
 export function resultTypeAfterInfixOp (operator, leftExpressionType, rightExpressionType) {
   try {
-    return infixMap.get(operator)[leftExpressionType.ord][rightExpressionType.ord];
+    const resultType = infixMap.get(operator)[leftExpressionType.ord][rightExpressionType.ord];
+    if (resultType === null || resultType === undefined) {
+      return Types.UNDEFINED
+    }
+    return resultType;
   } catch (e) {
     if (e instanceof TypeError) {
       return Types.UNDEFINED;

+ 29 - 0
tests/test51.spec.js

@@ -0,0 +1,29 @@
+import { IVProgParser } from './../js/ast/ivprogParser';
+import { SemanticAnalyser } from './../js/processor/semantic/semanticAnalyser';
+import { LanguageService } from '../js/services/languageService';
+
+describe('A invalid relational operation inside an if', function () {
+
+  const code = `programa {
+
+    funcao inicio() {
+      inteiro a = 5
+      se ( a * 2.3 > 8) {
+        a = 8
+      } senao {
+        a = -1
+      }
+    }
+  }`;
+
+  localStorage.setItem('ivprog.lang', 'pt');
+
+  const lexer = LanguageService.getCurrentLexer();
+
+  it(`should throw an exception`, function () {
+    const parser = new IVProgParser(code, lexer);
+    const sem = new SemanticAnalyser(parser.parseTree());
+    const fun = sem.analyseTree.bind(sem);
+    expect(fun).toThrow();
+  });
+});