Pārlūkot izejas kodu

Fix import bugs and other lib functions bugs

Lucas de Souza 6 gadi atpakaļ
vecāks
revīzija
a8d7491427

+ 2 - 6
index.html

@@ -6,12 +6,9 @@
     <link rel="stylesheet" type="text/css" href="css/ivprog-visual-1.0.css">
     <link rel="stylesheet" type="text/css" href="css/ivprog-term.css">
     <script src="js/jquery-3.3.1.min.js"></script>
-
-    <script src="build/ivprog.bundle.js"></script>
-
     <script src="js/iassign-integration-functions.js"></script>
+    <script src="build/ivprog.bundle.js"></script>
     <script src="i18n/i18n-engine.js"></script>
-
     <script src="js/semantic/semantic.min.js"></script>
     <script src="js/semantic/semantic-buttons.js"></script>
 
@@ -134,8 +131,7 @@
     </script>
 
 
-    <script src="js/semantic/semantic.min.js"></script>
-    <script src="js/semantic/semantic-buttons.js"></script>
+    
 
 
   </body>

+ 58 - 33
js/assessment/ivprogAssessment.js

@@ -13,44 +13,69 @@ export class IVProgAssessment {
   }
 
   runTest () {
-    let success = 0;
-    try {
-      // try and show error messages through domconsole
-      const parser = IVProgParser.createParser(this.textCode);
-      const semantic = new SemanticAnalyser(parser.parseTree());
-      const processor = new IVProgProcessor(semantic.analyseTree());
-      // loop test cases and show messages through domconsole
-      for (let i = 0; i < this.testCases.length; i++) {
-        const testCase = this.testCases[i];
-        const input = new InputTest(testCase.input);
-        const output = new OutputTest();
-        processor.registerInput(input);
-        processor.registerOutput(output);
-        processor.interpretAST();
+    return new Promise((resolve, _) => {
+      try {
+        // try and show error messages through domconsole
+        const parser = IVProgParser.createParser(this.textCode);
+        const semantic = new SemanticAnalyser(parser.parseTree());
+        const processor = new IVProgProcessor(semantic.analyseTree());
+        const fun = this.partialBindTestCase(this.evaluateTestCase, processor);
+        // loop test cases and show messages through domconsole
+        const tests = this.testCases.map( t => fun(t.input, t.output));
+        Promise.all(tests).then(results => {
+          const count = results.reduce((p, n) => {
+            if(n) {
+              return p + 1;
+            } else {
+              return p + 0;
+            }
+          },0);
+          const failed = this.testCases.length - count;
+          if(failed === 0) {
+            resolve(1);
+          } else {
+            resolve(count / this.testCases.length);
+          }
+        }).catch(err => {
+          this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
+          this.domConsole.err(err.message);
+          resolve(0);
+        })
+      } catch (error) {
+        this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
+        this.domConsole.err(error.message);
+        resolve(0);
+        return;
+      }
+    });
+  }
+
+  evaluateTestCase (prog, inputList, outputList) {
+    return new Promise((resolve, _) => {
+      const input = new InputTest(inputList);
+      const output = new OutputTest();
+      prog.registerInput(input);
+      prog.registerOutput(output);
+      prog.interpretAST().then( _ => {
         if (input.inputList.length !== 0 ||
-          output.list.length !== testCase.output.length) {
-          this.domConsole.err(`Caso de teste ${i+1} falhou!`);
+          output.list.length !== outputList.length) {
+          this.domConsole.err(`Caso de teste ${i + 1} falhou!`);
         } else {
-          const isOk = this.checkOutput(output.list, testCase.output);
+          const isOk = this.checkOutput(output.list, outputList);
           if(!isOk) {
-            this.domConsole.err(`Caso de teste ${i+1} falhou!`);
+            this.domConsole.err(`Caso de teste ${i + 1} falhou!`);
+            resolve(false);
           } else {
-            this.domConsole.info(`Caso de teste ${i+1} passou!`);
-            success++;
+            this.domConsole.info(`Caso de teste ${i + 1} passou!`);
+            resolve(true);
           }
         }
-      } 
-    } catch (error) {
-      this.domConsole.err("Erro durante a execução do programa");// try and show error messages through domconsole
-      this.domConsole.err(error.message);
-      return 0;
-    }
-    const failed = this.testCases.length - success;
-    if(failed === 0) {
-      return 1;
-    } else {
-      return success / this.testCases.length;
-    }
+      })
+    });
+  }
+
+  partialBindTestCase (fun, param) {
+    return (i, o) => fun(param, i, o);
   }
 
   checkOutput (aList, bList) {
@@ -58,7 +83,7 @@ export class IVProgAssessment {
       const outValue = aList[i];
       if(outValue !== bList[i]) {
         return false;
-      }          
+      }
     }
     return true;
   }

+ 5 - 2
js/io/domConsole.js

@@ -17,6 +17,7 @@ export class DOMConsole {
   constructor (elementID) {
     this.input = null;
     this.needInput = true;
+    this.anyKey = false;
     this.parent = $(elementID);
     this.setup();
     this.inputListeners = [];
@@ -34,7 +35,7 @@ export class DOMConsole {
         return;
       }
       const keyCode = event.which;
-      if (keyCode === 13) {
+      if (keyCode === 13 || this.anyKey) {
         let text = this.input.val();
         text = text.replace('[\n\r]+', '');
         this.notifyListeners(text);
@@ -57,6 +58,7 @@ export class DOMConsole {
     this.inputListeners.forEach(resolve => resolve(text));
     this.inputListeners.splice(0, this.inputListeners.length);
     this.needInput = false;
+    this.anyKey = false;
   }
 
   write (text) {
@@ -97,8 +99,9 @@ export class DOMConsole {
     this.parent.empty();
   }
 
-  requestInput (callback) {
+  requestInput (callback, anyKey = false) {
     this.inputListeners.push(callback);
+    this.anyKey = anyKey;
     this.input.focus();
     this.needInput = true;
   }

+ 1 - 1
js/main.js

@@ -1,6 +1,6 @@
 import { runner } from './runner';
 import { initVisualUI } from './visualUI/functions';
-import { LocalizedStrings, StringTypes } from './services/localizedStringsService';
+import { LocalizedStrings, StringTypes} from './services/localizedStringsService';
 
 export {
   runner,

+ 1 - 0
js/processor/lib/io.js

@@ -6,6 +6,7 @@ import { Types } from './../../typeSystem/types';
 export function createOutputFun () {
   const writeFunction = function (store, _) {
     const val = store.applyStore('p1');
+    console.log(val);
     if(val.type.isCompatible(Types.INTEGER)) {
       this.output.sendOutput(val.value.toString());
     } else if (val.type.isCompatible(Types.REAL)) {

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

@@ -17,7 +17,7 @@ export function createSubstringFun () {
     const str = sto.applyStore("str");
     const start = sto.applyStore("start");
     const end = sto.applyStore("end");
-    const result = str.value.substring(start.value, end.value);
+    const result = str.value.substring(start.value.toNumber(), end.value.toNumber());
     const temp = new StoreObject(Types.STRING, result);
     sto.mode = Modes.RETURN;
     return Promise.resolve(sto.updateStore("$", temp));

+ 5 - 2
js/runner.js

@@ -2,6 +2,7 @@ import { IVProgParser } from './ast/ivprogParser';
 import { IVProgProcessor } from './processor/ivprogProcessor';
 import {DOMConsole} from './io/domConsole';
 import { LanguageService } from './services/languageService';
+import { SemanticAnalyser } from './processor/semantic/semanticAnalyser';
 
 export function runner () {
 const ivprogLexer = LanguageService.getCurrentLexer();
@@ -29,14 +30,16 @@ try {
     const analiser = new IVProgParser(input, ivprogLexer);
     try {
       const data = analiser.parseTree();
-      const proc = new IVProgProcessor(data);
+      const semAna = new SemanticAnalyser(data);
+      const proc = new IVProgProcessor(semAna.analyseTree());
       proc.registerInput(domConsole);
       domConsole.clear();
       proc.registerOutput(domConsole);
       proc.interpretAST().then(sto => editor.load(sto.store))
-        .catch( e => alert(e));
+        .catch( e => {alert(e); console.log(e)});
     } catch (error) {
       alert(error);
+      console.log(error);
     }
     
   });

+ 1 - 1
js/services/localizedStringsService.js

@@ -1,5 +1,5 @@
 import { LanguageService } from "./languageService";
 import line_i18n from 'line-i18n'
 import Langs from './../../i18n';
-
+export const StringTypes = line_i18n.StringTypes;
 export const LocalizedStrings = Object.freeze(new line_i18n.LocalizedStrings(LanguageService, Langs, true));

+ 42 - 56
js/visualUI/functions.js

@@ -10,14 +10,16 @@ import * as VariableValueMenu from './commands/variable_value_menu';
 import { DOMConsole } from './../io/domConsole';
 import { IVProgParser } from './../ast/ivprogParser';
 import { IVProgProcessor } from './../processor/ivprogProcessor';
-import { LanguageService } from '../services/languageService';
 import WatchJS from 'melanke-watchjs';
+import { SemanticAnalyser } from '../processor/semantic/semanticAnalyser';
+import { IVProgAssessment } from '../assessment/ivprogAssessment';
 
 
 var counter_new_functions = 0;
 var counter_new_parameters = 0;
 
-let domConsole = null; 
+let domConsole = null;
+let studentGrade = null;
 const program = new Models.Program();
 /*const variable1 = new Models.Variable(Types.INTEGER, "a", 1);
 const parameter1 = new Models.Variable(Types.INTEGER, "par_1", 1);
@@ -306,29 +308,20 @@ $( document ).ready(function() {
 
 
 function runCodeAssessment () {
+  studentGrade = null;
   const strCode = CodeManagement.generate();
   if (strCode == null) {
     return;
   }
   domConsole = new DOMConsole("#ivprog-term", testCases);
   $("#ivprog-term").slideDown(500);
-  const lexer = LanguageService.getCurrentLexer();
-  const ast = new IVProgParser(strCode, lexer).parseTree();
-  const proc = new IVProgProcessor(ast); 
-  proc.registerInput(domConsole);
-  proc.registerOutput(domConsole);
-  proc.interpretAST().then( _ => {
-    domConsole.info("Programa executado com sucesso!");
-    domConsole.info("Aperte qualquer tecla para fechar...");
-    const p = new Promise((resolve, _) => {
-      domConsole.requestInput(resolve);
-    });
-    p.then( _ => {
-      domConsole.dispose();
-      domConsole = null;
-      $("#ivprog-term").hide();
-    })
-  })
+  const testCases = [];
+  const runner = new IVProgAssessment(strCode, testCases, domConsole);
+  runner.runTest().then(grade => studentGrade = grade);
+  while(studentGrade == null) {
+    continue;
+  }
+  waitToCloseConsole()
 }
 
 function runCode () {
@@ -338,45 +331,38 @@ function runCode () {
   }
   domConsole = new DOMConsole("#ivprog-term");
   $("#ivprog-term").slideDown(500);
-  const lexer = LanguageService.getCurrentLexer();
-  const ast = new IVProgParser(strCode, lexer).parseTree();
-  const proc = new IVProgProcessor(ast);
-  proc.registerInput(domConsole);
-  proc.registerOutput(domConsole);
-  proc.interpretAST().then( _ => {
-    domConsole.info("Programa executado com sucesso!");
-    domConsole.info("Aperte qualquer tecla para fechar...");
-    const p = new Promise((resolve, _) => {
-      domConsole.requestInput(resolve);
-    });
-    p.then( _ => {
-      domConsole.dispose();
-      domConsole = null;
-      $("#ivprog-term").hide();
-    })
-  })
+  try {
+    const parser = IVProgParser.createParser(strCode);
+    const analyser = new SemanticAnalyser(parser.parseTree());
+    const data = analyser.analyseTree();
+    const proc = new IVProgProcessor(data);
+    proc.registerInput(domConsole);
+    proc.registerOutput(domConsole);
+    
+    proc.interpretAST().then( _ => {
+      domConsole.info("Programa executado com sucesso!");
+      waitToCloseConsole();
+    }).catch(err => {
+      domConsole.err(err.message);
+      waitToCloseConsole();
+    }) 
+  } catch (error) {
+    domConsole.err(error.message);
+    waitToCloseConsole();
+    console.log(error);
+  }
+  
 }
 
-function runCode () {
-  const strCode = CodeManagement.generate();
-  domConsole = new DOMConsole("#ivprog-term");
-  $("#ivprog-term").slideDown(500);
-  const lexer = LanguageService.getCurrentLexer();
-  const ast = new IVProgParser(strCode, lexer).parseTree();
-  const proc = new IVProgProcessor(ast);
-  proc.registerInput(domConsole);
-  proc.registerOutput(domConsole);
-  proc.interpretAST().then( _ => {
-    domConsole.info("Programa executado com sucesso!");
-    domConsole.info("Aperte qualquer tecla para fechar...");
-    const p = new Promise((resolve, _) => {
-      domConsole.requestInput(resolve);
-    });
-    p.then( _ => {
-      domConsole.dispose();
-      domConsole = null;
-      $("#ivprog-term").hide();
-    })
+function waitToCloseConsole () {
+  domConsole.info("Aperte qualquer tecla para fechar...");
+  const p = new Promise((resolve, _) => {
+    domConsole.requestInput(resolve, true);
+  });
+  p.then( _ => {
+    domConsole.dispose();
+    domConsole = null;
+    $("#ivprog-term").hide();
   })
 }
 

+ 1 - 1
js/visualUI/ivprog_elements.js

@@ -1,4 +1,4 @@
-import { Types } from './../ast/types';
+import { Types } from './types';
 import WatchJS from 'melanke-watchjs';
 
 export const COMMAND_TYPES = Object.freeze({function:"function", comment:"comment", reader:"reader", writer:"writer", attribution:"attribution", iftrue:"iftrue",

+ 1 - 6
package-lock.json

@@ -3467,8 +3467,7 @@
         "console-control-strings": {
           "version": "1.1.0",
           "bundled": true,
-          "dev": true,
-          "optional": true
+          "dev": true
         },
         "core-util-is": {
           "version": "1.0.2",
@@ -3628,7 +3627,6 @@
           "version": "2.2.4",
           "bundled": true,
           "dev": true,
-          "optional": true,
           "requires": {
             "safe-buffer": "^5.1.1",
             "yallist": "^3.0.0"
@@ -3647,7 +3645,6 @@
           "version": "0.5.1",
           "bundled": true,
           "dev": true,
-          "optional": true,
           "requires": {
             "minimist": "0.0.8"
           }
@@ -3741,7 +3738,6 @@
           "version": "1.4.0",
           "bundled": true,
           "dev": true,
-          "optional": true,
           "requires": {
             "wrappy": "1"
           }
@@ -3863,7 +3859,6 @@
           "version": "1.0.2",
           "bundled": true,
           "dev": true,
-          "optional": true,
           "requires": {
             "code-point-at": "^1.0.0",
             "is-fullwidth-code-point": "^1.0.0",