瀏覽代碼

[FIX] Bug where Function was comparing returntType to a string instead of Symbol

[FIX] ActualParameters using not declared variable list

Include test cases for the bugs above
Lucas de Souza 6 年之前
父節點
當前提交
080909aafe
共有 4 個文件被更改,包括 75 次插入4 次删除
  1. 3 1
      js/ast/commands/function.js
  2. 5 3
      js/ast/ivprogParser.js
  3. 33 0
      tests/test15.spec.js
  4. 34 0
      tests/test16.spec.js

+ 3 - 1
js/ast/commands/function.js

@@ -1,3 +1,5 @@
+import { Types } from './../types';
+
 export class Function {
 
   constructor(name, returnType, formalParameters, commandBlock) {
@@ -8,7 +10,7 @@ export class Function {
   }
 
   get isMain () {
-    return this.name === null && this.returnType === 'void';
+    return this.name === null && this.returnType === Types.VOID;
   }
 
   get commands () {

+ 5 - 3
js/ast/ivprogParser.js

@@ -532,8 +532,10 @@ export class IVProgParser {
         // TODO better error message
         throw new Error(`Cannot declare variable here (line ${token.line})`);
       }
-      this.pos++;
-      cmd = this.parseDeclararion(token);
+      this.pushScope(IVProgParser.BASE);
+      const varType = this.parseType();
+      this.popScope();
+      cmd = this.parseDeclararion(varType);
       this.checkEOS();
       this.pos++;
     } else if (token.type === this.lexerClass.ID) {
@@ -951,7 +953,7 @@ export class IVProgParser {
     this.checkOpenParenthesis();
     this.pos++;
     this.consumeNewLines();
-    list = this.parseExpressionList();
+    const list = this.parseExpressionList();
     this.consumeNewLines();
     this.checkCloseParenthesis();
     this.pos++;

+ 33 - 0
tests/test15.spec.js

@@ -0,0 +1,33 @@
+import Lexers from './../grammar/';
+import {
+    IVProgParser
+} from './../js/ast/ivprogParser';
+
+describe('A start function with no return type', () => {
+    let input = `programa {
+      
+      const real PI = 5.5
+      inteiro V = -10*2
+
+      funcao inicio() {
+        test(PI)
+        retorne
+      }
+
+      funcao inteiro test(real i) {
+        escolha (i) {
+          caso 1:
+            retorne 0
+          caso contrario:
+            retorne 4
+        }
+      }
+    }`;
+    const lexer = Lexers['pt_br'];
+
+    it(`should not result in SyntaxError`, () => {
+        const as = new IVProgParser(input, lexer);
+        const fun = as.parseTree.bind(as);
+        expect(fun).not.toThrow();
+    });
+});

+ 34 - 0
tests/test16.spec.js

@@ -0,0 +1,34 @@
+import Lexers from './../grammar/';
+import {
+    IVProgParser
+} from './../js/ast/ivprogParser';
+
+describe('Variable declaration inside a function', () => {
+    let input = `programa {
+      
+      const real PI = 5.5
+      inteiro V = -10*2
+
+      funcao inicio() {
+        inteiro a = 6
+        test(PI)
+        retorne
+      }
+
+      funcao inteiro test(real i) {
+        escolha (i) {
+          caso 1:
+            retorne 0
+          caso contrario:
+            retorne 4
+        }
+      }
+    }`;
+    const lexer = Lexers['pt_br'];
+
+    it(`should not result in SyntaxError`, () => {
+        const as = new IVProgParser(input, lexer);
+        const fun = as.parseTree.bind(as);
+        expect(fun).not.toThrow();
+    });
+});