strings.js 3.1 KB

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