strings.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { StoreObject } from '../store/storeObject';
  2. import * as Commands from './../../ast/commands';
  3. import { Types } from './../../typeSystem/types';
  4. import { toInt } from "./../../typeSystem/parsers";
  5. import { Modes } from '../modes';
  6. /*
  7. * substring
  8. * length
  9. * uppercase
  10. * lowercase
  11. * charAt
  12. **/
  13. export function createSubstringFun () {
  14. const substringFun = (sto, _) => {
  15. const str = sto.applyStore("str");
  16. const start = sto.applyStore("start");
  17. const end = sto.applyStore("end");
  18. const result = str.value.substring(start.value, end.value);
  19. const temp = new StoreObject(Types.STRING, result);
  20. sto.mode = Modes.RETURN;
  21. return Promise.resolve(sto.updateStore("$", temp));
  22. };
  23. const block = new Commands.CommandBlock([], [new Commands.SysCall(substringFun)]);
  24. const func = new Commands.Function('$substring', Types.STRING,
  25. [new Commands.FormalParameter(Types.STRING, 'str', false),
  26. new Commands.FormalParameter(Types.INTEGER, 'start', false),
  27. new Commands.FormalParameter(Types.INTEGER, 'end', false)],
  28. block);
  29. return func;
  30. }
  31. export function createLengthFun () {
  32. const lengthFun = (sto, _) => {
  33. const str = sto.applyStore("str");
  34. const temp = new StoreObject(Types.INTEGER, toInt(str.value.length));
  35. sto.mode = Modes.RETURN;
  36. return Promise.resolve(sto.updateStore("$", temp));
  37. }
  38. const block = new Commands.CommandBlock([], [new Commands.SysCall(lengthFun)]);
  39. const func = new Commands.Function('$length', Types.INTEGER,
  40. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  41. block);
  42. return func;
  43. }
  44. export function createUppercaseFun () {
  45. const uppercaseFun = (sto, _) => {
  46. const str = sto.applyStore("str");
  47. const temp = new StoreObject(Types.STRING, str.value.toUpperCase());
  48. sto.mode = Modes.RETURN;
  49. return Promise.resolve(sto.updateStore("$", temp));
  50. }
  51. const block = new Commands.CommandBlock([], [new Commands.SysCall(uppercaseFun)]);
  52. const func = new Commands.Function('$uppercase', Types.STRING,
  53. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  54. block);
  55. return func;
  56. }
  57. export function createLowercaseFun () {
  58. const lowercaseFun = (sto, _) => {
  59. const str = sto.applyStore("str");
  60. const temp = new StoreObject(Types.STRING, str.value.toLowerCase());
  61. sto.mode = Modes.RETURN;
  62. return Promise.resolve(sto.updateStore("$", temp));
  63. }
  64. const block = new Commands.CommandBlock([], [new Commands.SysCall(lowercaseFun)]);
  65. const func = new Commands.Function('$lowercase', Types.STRING,
  66. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  67. block);
  68. return func;
  69. }
  70. export function createrCharAtFun () {
  71. const charAtFun = (sto, _) => {
  72. const str = sto.applyStore("str");
  73. const idx = sto.applyStore("index");
  74. if (idx.value.toNumber() < 0 || idx.value.toNumber() >= str.value.length) {
  75. return Promise.reject(new Error("invalid string position"));
  76. }
  77. const temp = new StoreObject(Types.STRING, str.value.charAt(idx.value.toNumber()));
  78. sto.mode = Modes.RETURN;
  79. return Promise.resolve(sto.updateStore("$", temp));
  80. }
  81. const block = new Commands.CommandBlock([], [new Commands.SysCall(charAtFun)]);
  82. const func = new Commands.Function('$charAt', Types.STRING,
  83. [new Commands.FormalParameter(Types.STRING, 'str', false),
  84. new Commands.FormalParameter(Types.INTEGER, 'index', false)],
  85. block);
  86. return func;
  87. }