io.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import * as Commands from './../../ast/commands';
  2. import { Modes } from '../modes';
  3. import {toInt, toString, toBool, toReal, convertToString} 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(convertToString(val.get(), val.type));
  14. }
  15. store.mode = Modes.RETURN;
  16. return store;
  17. }
  18. const block = new Commands.CommandBlock([], [new Commands.SysCall(writeFunction)]);
  19. const func = new Commands.Function('$write', Types.VOID,
  20. [new Commands.FormalParameter(Types.ALL, 'p1', false, true)],
  21. block);
  22. return func;
  23. }
  24. export function createInputFun () {
  25. const readFunction = async function (store, _) {
  26. const text = await this.input.requestInput();
  27. const typeToConvert = store.applyStore('p1').type;
  28. let type = null
  29. let result = null;
  30. try {
  31. if (typeToConvert.isCompatible(Types.INTEGER)) {
  32. result = toInt(text.trim()).trunc();
  33. type = Types.INTEGER;
  34. } else if (typeToConvert.isCompatible(Types.REAL)) {
  35. result = toReal(text.trim())
  36. type = Types.REAL;
  37. } else if (typeToConvert.isCompatible(Types.BOOLEAN)) {
  38. result = toBool(text.trim())
  39. type = Types.BOOLEAN;
  40. } else if (typeToConvert.isCompatible(Types.STRING)) {
  41. result = toString(text)
  42. type = Types.STRING;
  43. } else {
  44. throw new Error("!!!!Critical error: Unknown type in readFunction!!!!");
  45. }
  46. } catch (_) {
  47. if(this.mode == Modes.ABORT) {
  48. store.mode = Modes.RETURN;
  49. return store;
  50. }
  51. const stringInfo = typeToConvert.stringInfo()[0]
  52. const realObject = store.getStoreObject("p1");
  53. if (realObject.getReferenceDimension() > 0) {
  54. const arrayInfo = realObject.type.stringInfo()[0];
  55. const dim = realObject.getReferenceDimension();
  56. throw ProcessorErrorFactory.invalid_read_type_array(text, stringInfo.type, stringInfo.dim,
  57. realObject.getRefObj(), arrayInfo.type, dim, this.function_call_stack.pop());
  58. }
  59. throw ProcessorErrorFactory.invalid_read_type(text, stringInfo.type, stringInfo.dim,
  60. realObject.getRefObj(), this.function_call_stack.pop());
  61. }
  62. const stoValue = new StoreValue(type, result);
  63. store.updateStore('p1', stoValue);
  64. store.mode = Modes.RETURN;
  65. return store;
  66. }
  67. const block = new Commands.CommandBlock([], [new Commands.SysCall(readFunction)]);
  68. const func = new Commands.Function('$read', Types.VOID,
  69. [new Commands.FormalParameter(Types.ALL, 'p1', true)],
  70. block);
  71. return func;
  72. }