1
0

definedFunctions.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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} 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. },
  54. $ioLib: {
  55. $read: createInputFun(),
  56. $write: createOutputFun()
  57. },
  58. $strLib: {
  59. $substring: createSubstringFun(),
  60. $length: createLengthFun(),
  61. $uppercase: createUppercaseFun(),
  62. $lowercase: createLowercaseFun(),
  63. $charAt: createrCharAtFun(),
  64. },
  65. $arrayLib: {
  66. $numElements: createNumElementsFun(),
  67. $matrixLines: createMatrixLinesFun(),
  68. $matrixColumns: createMatrixColumnsFun()
  69. },
  70. $langLib: {
  71. $isReal: createIsRealFun(),
  72. $isInt: createIsIntFun(),
  73. $isBool: createIsBoolFun(),
  74. $castReal: createCastRealFun(),
  75. $castInt: createCastIntFun(),
  76. $castBool: createCastBoolFun(),
  77. $castString: createCastStringFun()
  78. }
  79. }
  80. const funcsObject = concatObjects(libsObject.$ioLib, libsObject.$langLib,
  81. libsObject.$strLib, libsObject.$arrayLib);
  82. export const LanguageDefinedFunction = Object.freeze({
  83. getMainFunctionName: () => LanguageService.getCurrentLangFuncs().main_function,
  84. getInternalName: (localName) => {
  85. if (localName.indexOf(".") !== -1) {
  86. const names = localName.split(".");
  87. const lib = valueToKey(names[0], LanguageService.getCurrentLangLibs());
  88. const internalName = valueToKey(names[1], LanguageService.getCurrentLangFuncs());
  89. if (lib === null || internalName === null) {
  90. return null;
  91. }
  92. return lib + "." + internalName;
  93. }
  94. const funcName = valueToKey(localName, LanguageService.getCurrentLangFuncs());
  95. if(funcName !== null) {
  96. if(funcsObject[funcName]) {
  97. return funcName;
  98. }
  99. }
  100. return null;
  101. },
  102. getFunction: (internalName) => {
  103. if (internalName.indexOf(".") !== -1) {
  104. const names = internalName.split(".");
  105. const libName = names[0];
  106. const funName = names[1];
  107. return libsObject[libName][funName];
  108. }
  109. return funcsObject[internalName];
  110. },
  111. });