lang.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { StoreObject } from '../store/storeObject';
  2. import * as Commands from './../../ast/commands';
  3. import { Types, toReal } from './../../ast/types';
  4. import { LanguageService } from '../../services/languageService';
  5. import { IVProgParser } from '../../ast/ivprogParser';
  6. import { RealLiteral, IntLiteral, BoolLiteral } from '../../ast/expressions';
  7. /**
  8. *
  9. * is_real
  10. * is_int
  11. * is_bool
  12. * cast_real
  13. * cast_int
  14. * cast_bool
  15. * cast_string
  16. */
  17. export function createIsRealFun () {
  18. const isRealFun = (sto, _) => {
  19. const str = sto.applyStore("str");
  20. const lexer = LanguageService.getCurrentLexer();
  21. const parser = new IVProgParser(str.value, lexer);
  22. let result = false;
  23. try {
  24. const val = parser.parseTerm();
  25. if (val instanceof RealLiteral) {
  26. result = true;
  27. }
  28. } catch (error) { }
  29. const temp = new StoreObject(Types.BOOLEAN, result);
  30. return Promise.resolve(sto.updateStore("$", temp));
  31. }
  32. const block = new Commands.CommandBlock([], [new Commands.SysCall(isRealFun)]);
  33. const func = new Commands.Function('$isReal', Types.BOOLEAN,
  34. [new Commands.FormalParameter(Types.STRING, 'str', 0, false)],
  35. block);
  36. return func;
  37. }
  38. export function createIsIntFun () {
  39. const isIntFun = (sto, _) => {
  40. const str = sto.applyStore("str");
  41. const lexer = LanguageService.getCurrentLexer();
  42. const parser = new IVProgParser(str.value, lexer);
  43. let result = false;
  44. try {
  45. const val = parser.parseTerm();
  46. if (val instanceof IntLiteral) {
  47. result = true;
  48. }
  49. } catch (error) { }
  50. const temp = new StoreObject(Types.BOOLEAN, result);
  51. return Promise.resolve(sto.updateStore("$", temp));
  52. }
  53. const block = new Commands.CommandBlock([], [new Commands.SysCall(isIntFun)]);
  54. const func = new Commands.Function('$isInt', Types.BOOLEAN,
  55. [new Commands.FormalParameter(Types.STRING, 'str', 0, false)],
  56. block);
  57. return func;
  58. }
  59. export function createIsBoolFun () {
  60. const isBoolFun = (sto, _) => {
  61. const str = sto.applyStore("str");
  62. const lexer = LanguageService.getCurrentLexer();
  63. const parser = new IVProgParser(str.value, lexer);
  64. let result = false;
  65. try {
  66. const val = parser.parseTerm();
  67. if (val instanceof BoolLiteral) {
  68. result = true;
  69. }
  70. } catch (error) { }
  71. const temp = new StoreObject(Types.BOOLEAN, result);
  72. return Promise.resolve(sto.updateStore("$", temp));
  73. }
  74. const block = new Commands.CommandBlock([], [new Commands.SysCall(isBoolFun)]);
  75. const func = new Commands.Function('$isBool', Types.BOOLEAN,
  76. [new Commands.FormalParameter(Types.STRING, 'str', 0, false)],
  77. block);
  78. return func;
  79. }
  80. export function createCastRealFun () {
  81. const castRealFun = (sto, _) => {
  82. const val = sto.applyStore("val");
  83. switch (val.type.ord) {
  84. case Types.INTEGER.ord: {
  85. const temp = new StoreObject(Types.REAL, toReal(val.value));
  86. return Promise.resolve(sto.updateStore("$", temp));
  87. }
  88. case Types.STRING.ord: {
  89. const lexer = LanguageService.getCurrentLexer();
  90. const parser = new IVProgParser(val.value, lexer);
  91. try {
  92. const result = parser.parseTerm();
  93. if (result instanceof RealLiteral) {
  94. const temp = new StoreObject(Types.REAL, result.value);
  95. return Promise.resolve(sto.updateStore("$", temp));
  96. }
  97. } catch (error) {
  98. return Promise.reject("cannot convert string to real");
  99. }
  100. }
  101. }
  102. }
  103. const block = new Commands.CommandBlock([], [new Commands.SysCall(castRealFun)]);
  104. const func = new Commands.Function('$castReal', Types.REAL,
  105. [new Commands.FormalParameter(Types.ALL, 'val', 0, false)],
  106. block);
  107. return func;
  108. }