strings.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. import { ProcessorErrorFactory } from '../error/processorErrorFactory';
  7. /*
  8. * substring
  9. * length
  10. * uppercase
  11. * lowercase
  12. * charAt
  13. **/
  14. export function createSubstringFun () {
  15. const substringFun = async (sto, _) => {
  16. const str = sto.applyStore("str");
  17. const start = sto.applyStore("start");
  18. const end = sto.applyStore("end");
  19. const result = str.get().substring(start.get(), end.get());
  20. const temp = new StoreValue(Types.STRING, result);
  21. sto.insertStore("$", temp);
  22. sto.mode = Modes.RETURN;
  23. return sto;
  24. };
  25. const block = new Commands.CommandBlock([], [new Commands.SysCall(substringFun)]);
  26. const func = new Commands.Function('$substring', Types.STRING,
  27. [new Commands.FormalParameter(Types.STRING, 'str', false),
  28. new Commands.FormalParameter(Types.INTEGER, 'start', false),
  29. new Commands.FormalParameter(Types.INTEGER, 'end', false)],
  30. block);
  31. return func;
  32. }
  33. export function createLengthFun () {
  34. const lengthFun = async (sto, _) => {
  35. const str = sto.applyStore("str");
  36. const temp = new StoreValue(Types.INTEGER, toInt(str.value.length));
  37. sto.insertStore("$", temp);
  38. sto.mode = Modes.RETURN;
  39. return sto;
  40. }
  41. const block = new Commands.CommandBlock([], [new Commands.SysCall(lengthFun)]);
  42. const func = new Commands.Function('$length', Types.INTEGER,
  43. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  44. block);
  45. return func;
  46. }
  47. export function createUppercaseFun () {
  48. const uppercaseFun = async (sto, _) => {
  49. const str = sto.applyStore("str");
  50. const temp = new StoreValue(Types.STRING, str.get().toUpperCase());
  51. sto.insertStore("$", temp);
  52. sto.mode = Modes.RETURN;
  53. return sto;
  54. }
  55. const block = new Commands.CommandBlock([], [new Commands.SysCall(uppercaseFun)]);
  56. const func = new Commands.Function('$uppercase', Types.STRING,
  57. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  58. block);
  59. return func;
  60. }
  61. export function createLowercaseFun () {
  62. const lowercaseFun = async (sto, _) => {
  63. const str = sto.applyStore("str");
  64. const temp = new StoreValue(Types.STRING, str.get().toLowerCase());
  65. sto.insertStore("$", temp);
  66. sto.mode = Modes.RETURN;
  67. return sto;
  68. }
  69. const block = new Commands.CommandBlock([], [new Commands.SysCall(lowercaseFun)]);
  70. const func = new Commands.Function('$lowercase', Types.STRING,
  71. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  72. block);
  73. return func;
  74. }
  75. export function createrCharAtFun () {
  76. const charAtFun = async function (sto, _) {
  77. const str = sto.applyStore("str");
  78. const idx = sto.applyStore("index");
  79. if (idx.get().toNumber() < 0 || idx.get().toNumber() >= str.get().length) {
  80. throw ProcessorErrorFactory.invalid_string_index(idx.get().toNumber(), str.get(),
  81. this.function_call_stack.pop());
  82. }
  83. const temp = new StoreValue(Types.CHAR, str.get().charAt(idx.get().toNumber()));
  84. sto.insertStore("$", temp);
  85. sto.mode = Modes.RETURN;
  86. return sto;
  87. }
  88. const block = new Commands.CommandBlock([], [new Commands.SysCall(charAtFun)]);
  89. const func = new Commands.Function('$charAt', Types.CHAR,
  90. [new Commands.FormalParameter(Types.STRING, 'str', false),
  91. new Commands.FormalParameter(Types.INTEGER, 'index', false)],
  92. block);
  93. return func;
  94. }