io.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { StoreObjectArrayAddressRef } from '../store/storeObjectArrayAddressRef';
  7. import { StoreValue } from '../store/value/store_value';
  8. export function createOutputFun () {
  9. const writeFunction = function (store, _) {
  10. const val = store.applyStore('p1');
  11. this.output.sendOutput(convertToString(val.value, val.type));
  12. store.mode = Modes.RETURN;
  13. return Promise.resolve(store);
  14. }
  15. const block = new Commands.CommandBlock([], [new Commands.SysCall(writeFunction)]);
  16. const func = new Commands.Function('$write', Types.VOID,
  17. [new Commands.FormalParameter(Types.ALL, 'p1', false)],
  18. block);
  19. return func;
  20. }
  21. export function createInputFun () {
  22. const readFunction = function (store, _) {
  23. const request = new Promise((resolve, _) => {
  24. this.input.requestInput(resolve);
  25. });
  26. return request.then(text => {
  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).trunc();
  33. type = Types.INTEGER;
  34. } else if (typeToConvert.isCompatible(Types.REAL)) {
  35. result = toReal(text)
  36. type = Types.REAL;
  37. } else if (typeToConvert.isCompatible(Types.BOOLEAN)) {
  38. result = toBool(text)
  39. type = Types.BOOLEAN;
  40. } else if (typeToConvert.isCompatible(Types.STRING)) {
  41. result = toString(text)
  42. type = Types.STRING;
  43. } else {
  44. return Promise.reject(new Error("!!!!Critical error: Unknown type in readFunction!!!!"));
  45. }
  46. } catch (_) {
  47. const stringInfo = typeToConvert.stringInfo()[0]
  48. const realObject = store.getStoreObject("p1");
  49. if (realObject instanceof StoreObjectArrayAddressRef) {
  50. const arrayInfo = realObject.address.getArrayObject().type.stringInfo()[0];
  51. const error = ProcessorErrorFactory.invalid_read_type_array(text, stringInfo.type, stringInfo.dim, realObject.address.refID, arrayInfo.type, arrayInfo.dim);
  52. return Promise.reject(error);
  53. }
  54. const error = ProcessorErrorFactory.invalid_read_type(text, stringInfo.type, stringInfo.dim, realObject.getRefObj());
  55. return Promise.reject(error);
  56. }
  57. this.loopTimers.splice(0, this.loopTimers.length)
  58. const stoValue = new StoreValue(type, result);
  59. store.updateStore('p1', stoValue);
  60. store.mode = Modes.RETURN;
  61. return Promise.resolve(store);
  62. });
  63. }
  64. const block = new Commands.CommandBlock([], [new Commands.SysCall(readFunction)]);
  65. const func = new Commands.Function('$read', Types.VOID,
  66. [new Commands.FormalParameter(Types.ALL, 'p1', true)],
  67. block);
  68. return func;
  69. }