strings.js 3.4 KB

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