arrays.js 1.9 KB

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