Browse Source

Create DoWhile command class and Parser

Create test cases for IfThenElseIfThenElse chain and DoWhile command
Lucas de Souza 6 years ago
parent
commit
3135d5bd33
5 changed files with 93 additions and 1 deletions
  1. 13 0
      js/ast/commands/doWhile.js
  2. 5 0
      js/ast/commands/while.js
  3. 23 1
      js/ast/ivprogParser.js
  4. 28 0
      tests/test10.spec.js
  5. 24 0
      tests/test11.spec.js

+ 13 - 0
js/ast/commands/doWhile.js

@@ -0,0 +1,13 @@
+import { While } from './while';
+
+export class DoWhile extends While {
+
+  constructor(condition, commandBlock) {
+    super(condition, commandBlock);
+  }
+
+  get testFirst () {
+    return false;
+  }
+  
+}

+ 5 - 0
js/ast/commands/while.js

@@ -1,4 +1,5 @@
 export class While {
+
   constructor (expression, commandBlock) {
     this.expression = expression;
     this.commandBlock = commandBlock;
@@ -7,4 +8,8 @@ export class While {
   get commands () {
     return this.commandBlock.commands;
   }
+
+  get testFirst () {
+  	return true;
+  }
 }

+ 23 - 1
js/ast/ivprogParser.js

@@ -493,7 +493,7 @@ export class IVProgParser {
       } else if (token.type === this.lexerClass.RK_SWITCH) {
         
       } else if (token.type === this.lexerClass.RK_DO) {
-        
+        cmd = this.parseDoWhile();
       } else if (token.type === this.lexerClass.RK_IF) {
         cmd = this.parseIfThenElse();
       }
@@ -510,6 +510,28 @@ export class IVProgParser {
     return new Commands.CommandBlock(variablesDecl, commands);
   }
 
+  parseDoWhile () {
+    this.pos++;
+    this.consumeNewLines();
+    const commandsBlock = this.parseCommandBlock();
+    this.consumeNewLines(); //Maybe not...
+    const whileToken = this.getToken();
+    if (whileToken !== this.lexerClass.RK_WHILE) {
+      throw SyntaxError.createError(this.lexer.literalNames[this.lexerClass.RK_WHILE], whileToken);
+    }
+    this.pos++;
+    this.checkOpenParenthesis();
+    this.pos++;
+    this.consumeNewLines();
+    const condition = this.parseExpressionOR();
+    this.consumeNewLines();
+    this.checkCloseParenthesis();
+    this.pos++;
+    this.checkEOS();
+
+    return new Commands.DoWhile(condition, commandsBlock);
+  }
+
   parseIfThenElse () {
     this.pos++;
     this.checkOpenParenthesis();

+ 28 - 0
tests/test10.spec.js

@@ -0,0 +1,28 @@
+import Lexers from './../grammar/';
+import {
+    IVProgParser
+} from './../js/ast/ivprogParser';
+import {
+    SyntaxError
+} from './../js/ast/SyntaxError';
+
+describe('IfThenElseIfThenElse command chain', () => {
+    let input = `funcao inteiro test(real i) {
+      inteiro a = 5 + i
+      a = 5 + G[i][6]
+      se (a > 5) {
+        a = 5
+      } senao se (a<5) {
+        a = 0
+      } senao {
+        a = -1
+      }
+    }`;
+    const lexer = Lexers['pt_br'];
+
+    it(`should not result in SyntaxError`, () => {
+        const as = new IVProgParser(input, lexer);
+        const fun = as.parseFunction.bind(as);
+        expect(fun).not.toThrow();
+    });
+});

+ 24 - 0
tests/test11.spec.js

@@ -0,0 +1,24 @@
+import Lexers from './../grammar/';
+import {
+    IVProgParser
+} from './../js/ast/ivprogParser';
+import {
+    SyntaxError
+} from './../js/ast/SyntaxError';
+
+describe('DoWhile command', () => {
+    let input = `funcao inteiro test(real i) {
+      inteiro a = 5 + i
+      a = 5 + G[i][6]
+      faca {
+        a = a + 1
+      } enquanto (a > 5)
+    }`;
+    const lexer = Lexers['pt_br'];
+
+    it(`should not result in SyntaxError`, () => {
+        const as = new IVProgParser(input, lexer);
+        const fun = as.parseFunction.bind(as);
+        expect(fun).not.toThrow();
+    });
+});