Explorar el Código

Refactor LocalizedStrings to include operations and type translations

Fix void fuctions not updating store mode to return
Lucas de Souza hace 5 años
padre
commit
b0a7a0f960

+ 3 - 1
js/assessment/ivprogAssessment.js

@@ -3,10 +3,12 @@ import { SemanticAnalyser } from "./../processor/semantic/semanticAnalyser";
 import { IVProgProcessor } from "./../processor/ivprogProcessor";
 import { InputTest } from "./../util/inputTest";
 import { OutputTest } from "./../util/outputTest";
-import { LocalizedStrings } from "../services/localizedStringsService";
+import * as LocalizedStringsService from "../services/localizedStringsService";
 import { Decimal } from 'decimal.js';
 import { Config } from "../util/config";
 
+const LocalizedStrings = LocalizedStringsService.getInstance();
+
 export class IVProgAssessment {
 
   constructor (textCode, testCases, domConsole) {

+ 3 - 1
js/ast/error/syntaxErrorFactory.js

@@ -1,6 +1,8 @@
-import { LocalizedStrings } from './../../services/localizedStringsService';
+import * as LocalizedStringsService from './../../services/localizedStringsService';
 import { SyntaxError } from './syntaxError';
 
+const LocalizedStrings = LocalizedStringsService.getInstance();
+
 export const SyntaxErrorFactory = Object.freeze({
   extra_lines: () => new SyntaxError(LocalizedStrings.getError("extra_lines")),
   token_missing_one: (expected, token) => {

+ 2 - 1
js/main.js

@@ -1,9 +1,10 @@
 import { runner } from './runner';
 import { initVisualUI } from './visualUI/functions';
-import { LocalizedStrings} from './services/localizedStringsService';
+import * as LocalizedStringsService from './services/localizedStringsService';
 import { i18nHelper } from "./services/i18nHelper";
 
 const i18n = i18nHelper.i18n
+const LocalizedStrings = LocalizedStringsService.getInstance();
 
 export {
   runner,

+ 16 - 39
js/processor/error/processorErrorFactory.js

@@ -1,31 +1,8 @@
 import { RuntimeError } from './runtimeError';
 import { SemanticError } from './semanticError';
-import { LocalizedStrings } from './../../services/localizedStringsService';
-import { Operators } from '../../ast/operators';
+import * as  LocalizedStringsService from './../../services/localizedStringsService';
 
-function translateType (type, dim) {
-  switch (dim) {
-    case 0:
-      return LocalizedStrings.getUI(type);
-    default:
-      const transType = LocalizedStrings.getUI(type);
-      if(dim === 1)
-        return LocalizedStrings.getUI("vector_string", [transType])
-      else
-        return LocalizedStrings.getUI("matrix_string", [transType])
-  }
-}
-
-function translateOp (op) {
-  switch(op.ord) {
-    case Operators.AND.ord:
-    case Operators.OR.ord:
-    case Operators.NOT.ord:
-      return LocalizedStrings.getUI(op.value);
-    default:
-      return op.value;
-  }
-}
+const LocalizedStrings = LocalizedStringsService.getInstance();
 
 export const ProcessorErrorFactory  = Object.freeze({
   symbol_not_found_full: (id, sourceInfo) => {
@@ -80,26 +57,26 @@ export const ProcessorErrorFactory  = Object.freeze({
   },
   incompatible_types_full: (type, dim, sourceInfo) => {
     if(sourceInfo) {
-      const context = [translateType(type, dim), sourceInfo.line, sourceInfo.column];
+      const context = [LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
       return new SemanticError(LocalizedStrings.getError("incompatible_types_full", context));
     } else {
       return ProcessorErrorFactory.incompatible_types(type, dim);
     }
   },
   incompatible_types: (type, dim) => {
-    const context = [translateType(type, dim)];
+    const context = [LocalizedStrings.translateType(type, dim)];
     return new SemanticError(LocalizedStrings.getError("incompatible_types", context));
   },
   incompatible_types_array_full: (exp, type, dim, sourceInfo) => {
     if(sourceInfo) {
-      const context = [exp, translateType(type, dim), sourceInfo.line, sourceInfo.column];
+      const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
       return new SemanticError(LocalizedStrings.getError("incompatible_types_array_full", context));
     } else {
       return ProcessorErrorFactory.incompatible_types_array(exp, type, dim);
     }
   },
   incompatible_types_array: (exp, type, dim) => {
-    const context = [exp, translateType(type, dim)];
+    const context = [exp, LocalizedStrings.translateType(type, dim)];
     return new SemanticError(LocalizedStrings.getError("incompatible_types_array", context));
   },
   loop_condition_type_full: (exp, sourceInfo) => {
@@ -158,14 +135,14 @@ export const ProcessorErrorFactory  = Object.freeze({
   },
   invalid_case_type_full: (exp, type, dim, sourceInfo) => {
     if(sourceInfo) {
-      const context = [exp, translateType(type, dim), sourceInfo.line, sourceInfo.column];
+      const context = [exp, LocalizedStrings.translateType(type, dim), sourceInfo.line, sourceInfo.column];
       return new SemanticError(LocalizedStrings.getError("invalid_case_type_full", context));
     } else {
       return ProcessorErrorFactory.invalid_case_type(exp, type, dim);
     }
   },
   invalid_case_type: (exp, type, dim) => {
-    const context = [exp, translateType(type, dim)];
+    const context = [exp, LocalizedStrings.translateType(type, dim)];
     return new SemanticError(LocalizedStrings.getError("invalid_case_type", context));
   },
   void_in_expression_full: (id, sourceInfo) => {
@@ -258,26 +235,26 @@ export const ProcessorErrorFactory  = Object.freeze({
   },
   invalid_void_return_full: (id, type, dim, sourceInfo) => {
     if(sourceInfo) {
-      const context = [sourceInfo.line, id, translateType(type, dim)];
+      const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
       return new SemanticError(LocalizedStrings.getError("invalid_void_return_full", context));
     } else {
       return ProcessorErrorFactory.invalid_void_return(id, type, dim);
     }
   },
   invalid_void_return: (id, type, dim) => {
-    const context = [id, translateType(type, dim)];
+    const context = [id, LocalizedStrings.translateType(type, dim)];
     return new SemanticError(LocalizedStrings.getError("invalid_void_return_full", context));
   },
   invalid_return_type_full: (id, type, dim, sourceInfo) => {
     if(sourceInfo) {
-      const context = [sourceInfo.line, id, translateType(type, dim)];
+      const context = [sourceInfo.line, id, LocalizedStrings.translateType(type, dim)];
       return new SemanticError(LocalizedStrings.getError("invalid_return_type_full", context));
     } else {
       return ProcessorErrorFactory.invalid_return_type(id, type, dim);
     }
   },
   invalid_return_type: (id, type, dim) => {
-    const context = [id, translateType(type, dim)];
+    const context = [id, LocalizedStrings.translateType(type, dim)];
     return new SemanticError(LocalizedStrings.getError("invalid_return_type", context));
   },
   invalid_parameters_size_full: (id, expected, actual, sourceInfo) => {
@@ -365,26 +342,26 @@ export const ProcessorErrorFactory  = Object.freeze({
   },
   invalid_unary_op_full: (opName, type, dim, sourceInfo) => {
     if(sourceInfo) {
-      const context = [sourceInfo.line, translateOp(opName), translateType(type, dim)];
+      const context = [sourceInfo.line, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
       return new RuntimeError(LocalizedStrings.getError("invalid_unary_op_full", context));
     } else {
       return ProcessorErrorFactory.invalid_unary_op(opName, type, dim);
     }
   },
   invalid_unary_op: (opName, type, dim) => {
-    const context = [translateOp(opName), translateType(type, dim)];
+    const context = [LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(type, dim)];
     return new RuntimeError(LocalizedStrings.getError("invalid_unary_op", context));
   },
   invalid_infix_op_full: (opName, typeLeft, dimLeft, typeRight, dimRight,  sourceInfo) => {
     if(sourceInfo) {
-      const context = [sourceInfo.line, translateOp(opName), translateType(typeLeft, dimLeft), translateType(typeRight, dimRight)];
+      const context = [sourceInfo.line, LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
       return new RuntimeError(LocalizedStrings.getError("invalid_infix_op_full", context));
     } else {
       return ProcessorErrorFactory.invalid_infix_op(opName, typeLeft, dimLeft, typeRight, dimRight);
     }
   },
   invalid_infix_op: (opName, typeLeft, dimLeft, typeRight, dimRight) => {
-    const context = [translateOp(opName), translateType(typeLeft, dimLeft), translateType(typeRight, dimRight)];
+    const context = [LocalizedStrings.translateOp(opName), LocalizedStrings.translateType(typeLeft, dimLeft), LocalizedStrings.translateType(typeRight, dimRight)];
     return new RuntimeError(LocalizedStrings.getError("invalid_infix_op", context));
   },
   array_dimension_not_positive_full: (sourceInfo) => {

+ 1 - 1
js/processor/ivprogProcessor.js

@@ -209,7 +209,6 @@ export class IVProgProcessor {
   }
 
   executeCommand (store, cmd) {
-
     if(this.forceKill) {
       return Promise.reject("FORCED_KILL!");
     } else if (store.mode === Modes.PAUSE) {
@@ -456,6 +455,7 @@ export class IVProgProcessor {
       return $value.then(vl => {
 
         if(vl === null && funcType.isCompatible(Types.VOID)) {
+          store.mode = Modes.RETURN;
           return Promise.resolve(store);
         }
 

+ 43 - 2
js/services/localizedStringsService.js

@@ -1,4 +1,45 @@
 import { LanguageService } from "./languageService";
-import line_i18n from 'line-i18n'
+import line_i18n from 'line-i18n';
 import Langs from './../../i18n';
-export const LocalizedStrings = Object.freeze(new line_i18n.LocalizedStrings(LanguageService, Langs, true));
+
+class IVProgLocalizedStrings extends line_i18n.LocalizedStrings {
+
+  constructor(langService, langsJsons, shouldListenToChange =  false) {
+    super(langService, langsJsons, shouldListenToChange);
+  }
+
+  translateType (type, dim) {
+    switch (dim) {
+      case 0:
+        return this.getUI(type);
+      default:
+        const transType = this.getUI(type);
+        if(dim === 1)
+          return this.getUI("vector_string", [transType])
+        else
+          return this.getUI("matrix_string", [transType])
+    }
+  }
+  
+  translateOp (op) {
+    switch(op.ord) {
+      case Operators.AND.ord:
+      case Operators.OR.ord:
+      case Operators.NOT.ord:
+        return this.getUI(op.value);
+      default:
+        return op.value;
+    }
+  }
+}
+
+export const LocalizedStrings = Object.freeze(new IVProgLocalizedStrings(LanguageService, Langs, true));
+
+let _instance = null;
+
+export function getInstance () {
+  if(_instance == null) {
+    _instance = new IVProgLocalizedStrings(LanguageService, Langs);
+  }
+  return _instance;
+}