io.js 2.9 KB

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