arrays.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import * as Commands from './../../ast/commands';
  2. import { Types } from './../../typeSystem/types';
  3. import { toInt } from "./../../typeSystem/parsers";
  4. import { ArrayType } from '../../typeSystem/array_type';
  5. import { Modes } from '../modes';
  6. import { StoreValue } from '../store/value/store_value';
  7. /**
  8. * num_elements
  9. * matrix_lines
  10. * matrix_columns
  11. */
  12. export function createNumElementsFun () {
  13. const numElementsFun = async (sto, _) => {
  14. const vector = sto.applyStore("vector");
  15. const temp = new StoreValue(Types.INTEGER, toInt(vector.lines));
  16. sto.mode = Modes.RETURN;
  17. sto.insertStore("$", temp);
  18. return sto;
  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 ArrayType(Types.ALL, 1), 'vector', false)],
  23. block);
  24. return func;
  25. }
  26. export function createMatrixLinesFun () {
  27. const matrixLinesFun = async (sto, _) => {
  28. const matrix = sto.applyStore("matrix");
  29. const temp = new StoreValue(Types.INTEGER, toInt(matrix.lines));
  30. sto.mode = Modes.RETURN;
  31. sto.insertStore("$", temp);
  32. return sto;
  33. }
  34. const block = new Commands.CommandBlock([], [new Commands.SysCall(matrixLinesFun)]);
  35. const func = new Commands.Function('$matrixLines', Types.INTEGER,
  36. [new Commands.FormalParameter(new ArrayType(Types.ALL, 2), 'matrix', false)],
  37. block);
  38. return func;
  39. }
  40. export function createMatrixColumnsFun () {
  41. const matrixColumnsFun = async (sto, _) => {
  42. const matrix = sto.applyStore("matrix");
  43. const temp = new StoreValue(Types.INTEGER, toInt(matrix.columns));
  44. sto.mode = Modes.RETURN;
  45. sto.insertStore("$", temp);
  46. return sto;
  47. }
  48. const block = new Commands.CommandBlock([], [new Commands.SysCall(matrixColumnsFun)]);
  49. const func = new Commands.Function('$matrixColumns', Types.INTEGER,
  50. [new Commands.FormalParameter(new ArrayType(Types.ALL, 2), 'matrix', false)],
  51. block);
  52. return func;
  53. }