io.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import * as Commands from "./../../ast/commands";
  2. import { Modes } from "../modes";
  3. import * as Parsers from "./../../typeSystem/parsers";
  4. import { Types } from "./../../typeSystem/types";
  5. import { ProcessorErrorFactory } from "./../error/processorErrorFactory";
  6. import { StoreValue } from "../store/value/store_value";
  7. export function createOutputFun () {
  8. const writeFunction = async function (store, _) {
  9. const totalSV = store.applyStore("p1.0");
  10. const total = totalSV.get();
  11. for (let i = 1; i < total; i += 1) {
  12. const val = store.applyStore(`p1.${i}`);
  13. this.output.sendOutput(Parsers.convertToString(val.get(), val.type));
  14. }
  15. store.mode = Modes.RETURN;
  16. return store;
  17. };
  18. const block = new Commands.CommandBlock(
  19. [],
  20. [new Commands.SysCall(writeFunction)]
  21. );
  22. const func = new Commands.Function(
  23. "$write",
  24. Types.VOID,
  25. [new Commands.FormalParameter(Types.ALL, "p1", false, true)],
  26. block
  27. );
  28. return func;
  29. }
  30. export function createInputFun () {
  31. const readFunction = async function (store, _) {
  32. const text = await this.input.requestInput();
  33. const typeToConvert = store.applyStore("p1").type;
  34. let type = null;
  35. let result = null;
  36. try {
  37. if (typeToConvert.isCompatible(Types.INTEGER)) {
  38. result = Parsers.toInt(text.trim()).trunc();
  39. type = Types.INTEGER;
  40. } else if (typeToConvert.isCompatible(Types.REAL)) {
  41. result = Parsers.toReal(text.trim());
  42. type = Types.REAL;
  43. } else if (typeToConvert.isCompatible(Types.BOOLEAN)) {
  44. result = Parsers.toBool(text.trim());
  45. type = Types.BOOLEAN;
  46. } else if (typeToConvert.isCompatible(Types.STRING)) {
  47. result = Parsers.toString(text);
  48. type = Types.STRING;
  49. } else if (typeToConvert.isCompatible(Types.CHAR)) {
  50. result = Parsers.toChar(text);
  51. type = Types.CHAR;
  52. } else {
  53. throw new Error("!!!!Critical error: Unknown type in readFunction!!!!");
  54. }
  55. } catch (_) {
  56. if (this.mode == Modes.ABORT) {
  57. store.mode = Modes.RETURN;
  58. return store;
  59. }
  60. const stringInfo = typeToConvert.stringInfo()[0];
  61. const realObject = store.getStoreObject("p1");
  62. if (realObject.getReferenceDimension() > 0) {
  63. const arrayInfo = realObject.type.stringInfo()[0];
  64. const dim = realObject.getReferenceDimension();
  65. throw ProcessorErrorFactory.invalid_read_type_array(
  66. text,
  67. stringInfo.type,
  68. stringInfo.dim,
  69. realObject.getRefObj(),
  70. arrayInfo.type,
  71. dim,
  72. this.function_call_stack.pop()
  73. );
  74. }
  75. throw ProcessorErrorFactory.invalid_read_type(
  76. text,
  77. stringInfo.type,
  78. stringInfo.dim,
  79. realObject.getRefObj(),
  80. this.function_call_stack.pop()
  81. );
  82. }
  83. const stoValue = new StoreValue(type, result);
  84. store.updateStore("p1", stoValue);
  85. store.mode = Modes.RETURN;
  86. return store;
  87. };
  88. const block = new Commands.CommandBlock(
  89. [],
  90. [new Commands.SysCall(readFunction)]
  91. );
  92. const func = new Commands.Function(
  93. "$read",
  94. Types.VOID,
  95. [new Commands.FormalParameter(Types.ALL, "p1", true)],
  96. block
  97. );
  98. return func;
  99. }