Lucas de Souza 5 tahun lalu
induk
melakukan
6178b3f75b
2 mengubah file dengan 61 tambahan dan 8 penghapusan
  1. 32 8
      js/processor/ivprogProcessor.js
  2. 29 0
      tests/test52.spec.js

+ 32 - 8
js/processor/ivprogProcessor.js

@@ -687,14 +687,38 @@ export class IVProgProcessor {
         }
         case Operators.MOD.ord:
           return new StoreObject(resultType, left.value % right.value);
-        case Operators.GT.ord:
-          return new StoreObject(resultType, left.value > right.value);
-        case Operators.GE.ord:
-          return new StoreObject(resultType, left.value >= right.value);
-        case Operators.LT.ord:
-          return new StoreObject(resultType, left.value < right.value);
-        case Operators.LE.ord:
-          return new StoreObject(resultType, left.value <= right.value);
+        case Operators.GT.ord: {
+          if (left.type === Types.STRING) {
+            result = left.value.length > right.value.length;
+          } else {
+            result = left.value > right.value;
+          }
+          return new StoreObject(resultType, result);
+        }
+        case Operators.GE.ord: {
+          if (left.type === Types.STRING) {
+            result = left.value.length >= right.value.length;
+          } else {
+            result = left.value >= right.value;
+          }
+          return new StoreObject(resultType, result);
+        }
+        case Operators.LT.ord: {
+          if (left.type === Types.STRING) {
+            result = left.value.length < right.value.length;
+          } else {
+            result = left.value < right.value;
+          }
+          return new StoreObject(resultType, result);
+        }
+        case Operators.LE.ord: {
+          if (left.type === Types.STRING) {
+            result = left.value.length <= right.value.length;
+          } else {
+            result = left.value <= right.value;
+          }
+          return new StoreObject(resultType, result);
+        }
         case Operators.EQ.ord:
           return new StoreObject(resultType, left.value === right.value);
         case Operators.NEQ.ord:

+ 29 - 0
tests/test52.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';
+import { OutputTest } from '../js/util/outputTest';
+import { IVProgProcessor } from '../js/processor/ivprogProcessor';
+
+describe('A relational operation between strings ', function () {
+
+  const code = `programa {
+
+    funcao inicio() {
+      cadeia a = "ABC", b = "DEF"
+      logico c = a > b
+    }
+  }`;
+
+  localStorage.setItem('ivprog.lang', 'pt');
+
+  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(_ => done()).catch(err => done(err));
+  });
+});