strings.js 3.2 KB

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