1
0

definedFunctions.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { LanguageService } from '../services/languageService';
  2. import {createInputFun, createOutputFun} from './lib/io';
  3. import {createLengthFun, createLowercaseFun,
  4. createrCharAtFun, createSubstringFun,
  5. createUppercaseFun} from './lib/strings';
  6. import {createMatrixColumnsFun, createMatrixLinesFun,
  7. createNumElementsFun} from './lib/arrays';
  8. import {createCastBoolFun, createCastIntFun,
  9. createCastRealFun, createCastStringFun,
  10. createIsBoolFun, createIsIntFun,
  11. createIsRealFun} from './lib/lang';
  12. import {createAbsFun, createCosFun,
  13. createInvertFun, createLogFun,
  14. createMaxFun, createMinFun,
  15. createNegateFun, createPowFun,
  16. createSinFun, createSqrtFun,
  17. createTanFun, createRandFun} from './lib/math';
  18. function valueToKey (value, object) {
  19. for (const key in object) {
  20. if(object.hasOwnProperty(key)){
  21. if (object[key] === value) {
  22. return key;
  23. }
  24. }
  25. }
  26. return null;
  27. }
  28. function concatObjects (...objs) {
  29. let result = {};
  30. for (let i = 0; i < objs.length; i++) {
  31. const obj = objs[i];
  32. for(const key in obj) {
  33. if(obj.hasOwnProperty(key)) {
  34. result[key] = obj[key];
  35. }
  36. }
  37. }
  38. return result;
  39. }
  40. const libsObject = {
  41. $mathLib: {
  42. $sin: createSinFun(),
  43. $cos: createCosFun(),
  44. $tan: createTanFun(),
  45. $sqrt: createSqrtFun(),
  46. $pow: createPowFun(),
  47. $log: createLogFun(),
  48. $abs: createAbsFun(),
  49. $negate: createNegateFun(),
  50. $invert: createInvertFun(),
  51. $max: createMaxFun(),
  52. $min: createMinFun(),
  53. $rand: createRandFun()
  54. },
  55. $ioLib: {
  56. $read: createInputFun(),
  57. $write: createOutputFun()
  58. },
  59. $strLib: {
  60. $substring: createSubstringFun(),
  61. $length: createLengthFun(),
  62. $uppercase: createUppercaseFun(),
  63. $lowercase: createLowercaseFun(),
  64. $charAt: createrCharAtFun(),
  65. },
  66. $arrayLib: {
  67. $numElements: createNumElementsFun(),
  68. $matrixLines: createMatrixLinesFun(),
  69. $matrixColumns: createMatrixColumnsFun()
  70. },
  71. $langLib: {
  72. $isReal: createIsRealFun(),
  73. $isInt: createIsIntFun(),
  74. $isBool: createIsBoolFun(),
  75. $castReal: createCastRealFun(),
  76. $castInt: createCastIntFun(),
  77. $castBool: createCastBoolFun(),
  78. $castString: createCastStringFun()
  79. }
  80. }
  81. const funcsObject = concatObjects(libsObject.$ioLib, libsObject.$langLib,
  82. libsObject.$strLib, libsObject.$arrayLib);
  83. export const LanguageDefinedFunction = Object.freeze({
  84. getMainFunctionName: () => LanguageService.getCurrentLangFuncs().main_function,
  85. getInternalName: (localName) => {
  86. if (localName.indexOf(".") !== -1) {
  87. const names = localName.split(".");
  88. const lib = valueToKey(names[0], LanguageService.getCurrentLangLibs());
  89. const internalName = valueToKey(names[1], LanguageService.getCurrentLangFuncs());
  90. if (lib === null || internalName === null) {
  91. return null;
  92. }
  93. return lib + "." + internalName;
  94. }
  95. const funcName = valueToKey(localName, LanguageService.getCurrentLangFuncs());
  96. if(funcName !== null) {
  97. if(funcsObject[funcName]) {
  98. return funcName;
  99. }
  100. }
  101. return null;
  102. },
  103. getFunction: (internalName) => {
  104. if (internalName.indexOf(".") !== -1) {
  105. const names = internalName.split(".");
  106. const libName = names[0];
  107. const funName = names[1];
  108. return libsObject[libName][funName];
  109. }
  110. return funcsObject[internalName];
  111. },
  112. getLocalName: (internalName) => {
  113. if (internalName.indexOf(".") !== -1) {
  114. const names = internalName.split(".");
  115. const libName = LanguageService.getCurrentLangLibs()[names[0]];
  116. const funName = LanguageService.getCurrentLangFuncs()[names[1]];
  117. return `${libName}.${funName}`;
  118. } else if (LanguageService.getCurrentLangFuncs()[internalName]) {
  119. return LanguageService.getCurrentLangFuncs()[internalName];
  120. } else {
  121. return internalName
  122. }
  123. }
  124. });