arrays.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. import { CompoundType } from '../../typeSystem/compoundType';
  6. import { Modes } from '../modes';
  7. /**
  8. * num_elements
  9. * matrix_lines
  10. * matrix_columns
  11. */
  12. export function createNumElementsFun () {
  13. const numElementsFun = (sto, _) => {
  14. const vector = sto.applyStore("vector");
  15. const temp = new StoreObject(Types.INTEGER, toInt(vector.lines));
  16. sto.mode = Modes.RETURN;
  17. return Promise.resolve(sto.updateStore("$", temp));
  18. }
  19. const block = new Commands.CommandBlock([], [new Commands.SysCall(numElementsFun)]);
  20. const func = new Commands.Function('$numElements', Types.INTEGER,
  21. [new Commands.FormalParameter(new CompoundType(Types.ALL, 1), 'vector', false)],
  22. block);
  23. return func;
  24. }
  25. export function createMatrixLinesFun () {
  26. const matrixLinesFun = (sto, _) => {
  27. const matrix = sto.applyStore("matrix");
  28. const temp = new StoreObject(Types.INTEGER, toInt(matrix.lines));
  29. sto.mode = Modes.RETURN;
  30. return Promise.resolve(sto.updateStore("$", temp));
  31. }
  32. const block = new Commands.CommandBlock([], [new Commands.SysCall(matrixLinesFun)]);
  33. const func = new Commands.Function('$matrixLines', Types.INTEGER,
  34. [new Commands.FormalParameter(new CompoundType(Types.ALL, 2), 'matrix', false)],
  35. block);
  36. return func;
  37. }
  38. export function createMatrixColumnsFun () {
  39. const matrixColumnsFun = (sto, _) => {
  40. const matrix = sto.applyStore("matrix");
  41. const temp = new StoreObject(Types.INTEGER, toInt(matrix.columns));
  42. sto.mode = Modes.RETURN;
  43. return Promise.resolve(sto.updateStore("$", temp));
  44. }
  45. const block = new Commands.CommandBlock([], [new Commands.SysCall(matrixColumnsFun)]);
  46. const func = new Commands.Function('$matrixColumns', Types.INTEGER,
  47. [new Commands.FormalParameter(new CompoundType(Types.ALL, 2), 'matrix', false)],
  48. block);
  49. return func;
  50. }