strings.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import * as Commands from './../../ast/commands';
  2. import { Types } from './../../typeSystem/types';
  3. import { toInt } from "./../../typeSystem/parsers";
  4. import { Modes } from '../modes';
  5. import { StoreValue } from '../store/value/store_value';
  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.get().substring(start.get(), end.get());
  19. const temp = new StoreValue(Types.STRING, result);
  20. sto.insertStore("$", temp);
  21. sto.mode = Modes.RETURN;
  22. return Promise.resolve(sto);
  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 StoreValue(Types.INTEGER, toInt(str.value.length));
  36. sto.insertStore("$", temp);
  37. sto.mode = Modes.RETURN;
  38. return Promise.resolve(sto);
  39. }
  40. const block = new Commands.CommandBlock([], [new Commands.SysCall(lengthFun)]);
  41. const func = new Commands.Function('$length', Types.INTEGER,
  42. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  43. block);
  44. return func;
  45. }
  46. export function createUppercaseFun () {
  47. const uppercaseFun = (sto, _) => {
  48. const str = sto.applyStore("str");
  49. const temp = new StoreValue(Types.STRING, str.get().toUpperCase());
  50. sto.insertStore("$", temp);
  51. sto.mode = Modes.RETURN;
  52. return Promise.resolve(sto);
  53. }
  54. const block = new Commands.CommandBlock([], [new Commands.SysCall(uppercaseFun)]);
  55. const func = new Commands.Function('$uppercase', Types.STRING,
  56. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  57. block);
  58. return func;
  59. }
  60. export function createLowercaseFun () {
  61. const lowercaseFun = (sto, _) => {
  62. const str = sto.applyStore("str");
  63. const temp = new StoreValue(Types.STRING, str.get().toLowerCase());
  64. sto.insertStore("$", temp);
  65. sto.mode = Modes.RETURN;
  66. return Promise.resolve(sto);
  67. }
  68. const block = new Commands.CommandBlock([], [new Commands.SysCall(lowercaseFun)]);
  69. const func = new Commands.Function('$lowercase', Types.STRING,
  70. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  71. block);
  72. return func;
  73. }
  74. export function createrCharAtFun () {
  75. const charAtFun = (sto, _) => {
  76. const str = sto.applyStore("str");
  77. const idx = sto.applyStore("index");
  78. if (idx.get().toNumber() < 0 || idx.get().toNumber() >= str.get().length) {
  79. // TODO better error message
  80. return Promise.reject(new Error("invalid string position"));
  81. }
  82. const temp = new StoreValue(Types.STRING, str.get().charAt(idx.get().toNumber()));
  83. sto.insertStore("$", temp);
  84. sto.mode = Modes.RETURN;
  85. return Promise.resolve(sto);
  86. }
  87. const block = new Commands.CommandBlock([], [new Commands.SysCall(charAtFun)]);
  88. const func = new Commands.Function('$charAt', Types.STRING,
  89. [new Commands.FormalParameter(Types.STRING, 'str', false),
  90. new Commands.FormalParameter(Types.INTEGER, 'index', false)],
  91. block);
  92. return func;
  93. }