definedFunctions.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as Commands from './../ast/commands';
  2. import {Types} from './../ast/types';
  3. import { LanguageService } from '../services/languageService';
  4. function createOutputFun () {
  5. const block = new Commands.CommandBlock([], [new Commands.SysCall('$write')]);
  6. const func = new Commands.Function('$write', Types.VOID,
  7. [new Commands.FormalParameter(Types.ALL, 'p1', 0, false)],
  8. block);
  9. return func;
  10. }
  11. function createInputFun () {
  12. const block = new Commands.CommandBlock([], [new Commands.SysCall('$read')]);
  13. const func = new Commands.Function('$read', Types.VOID,
  14. [new Commands.FormalParameter(Types.ALL, 'p1', 0, true)],
  15. block);
  16. return func;
  17. }
  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. const funcsObject = {
  29. $read: createInputFun(),
  30. $write: createOutputFun()
  31. }
  32. export const LanguageDefinedFunction = Object.freeze({
  33. getMainFunctionName: () => LanguageService.getCurrentLangFuncs().main_function,
  34. getInternalName: (localName) => valueToKey(localName, LanguageService.getCurrentLangFuncs()),
  35. getFunction: (internalName) => funcsObject[internalName],
  36. });