arrays.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { StoreObject } from '../store/storeObject';
  2. import * as Commands from './../../ast/commands';
  3. import { Types, toInt } from './../../ast/types';
  4. /**
  5. * num_elements
  6. * matrix_lines
  7. * matrix_columns
  8. */
  9. export function createNumElementsFun () {
  10. const numElementsFun = (sto, _) => {
  11. const vector = sto.applyStore("vector");
  12. const temp = new StoreObject(Types.INTEGER, toInt(vector.lines));
  13. return Promise.resolve(sto.updateStore("$", temp));
  14. }
  15. const block = new Commands.CommandBlock([], [new Commands.SysCall(numElementsFun)]);
  16. const func = new Commands.Function('$numElements', Types.INTEGER,
  17. [new Commands.FormalParameter(Types.ALL, 'vector', 1, false)],
  18. block);
  19. return func;
  20. }
  21. export function createMatrixLinesFun () {
  22. const matrixLinesFun = (sto, _) => {
  23. const matrix = sto.applyStore("matrix");
  24. const temp = new StoreObject(Types.INTEGER, toInt(matrix.lines));
  25. return Promise.resolve(sto.updateStore("$", temp));
  26. }
  27. const block = new Commands.CommandBlock([], [new Commands.SysCall(matrixLinesFun)]);
  28. const func = new Commands.Function('$matrixLines', Types.INTEGER,
  29. [new Commands.FormalParameter(Types.ALL, 'matrix', 2, false)],
  30. block);
  31. return func;
  32. }
  33. export function createMatrixColumnsFun () {
  34. const matrixColumnsFun = (sto, _) => {
  35. const matrix = sto.applyStore("matrix");
  36. const temp = new StoreObject(Types.INTEGER, toInt(matrix.columns));
  37. return Promise.resolve(sto.updateStore("$", temp));
  38. }
  39. const block = new Commands.CommandBlock([], [new Commands.SysCall(matrixColumnsFun)]);
  40. const func = new Commands.Function('$matrixColumns', Types.INTEGER,
  41. [new Commands.FormalParameter(Types.ALL, 'matrix', 2, false)],
  42. block);
  43. return func;
  44. }