1
0

arrays.js 2.0 KB

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