io.js 2.8 KB

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