io.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { StoreObject } from './../store/storeObject';
  2. import * as Commands from './../../ast/commands';
  3. import { Modes } from '../modes';
  4. import {toInt, toString, toBool, toReal, convertToString} from './../../typeSystem/parsers';
  5. import { Types } from './../../typeSystem/types';
  6. import { ProcessorErrorFactory } from "./../error/processorErrorFactory";
  7. import { StoreObjectArrayAddressRef } from '../store/storeObjectArrayAddressRef';
  8. import { Location } from '../../memory/location';
  9. export function createOutputFun () {
  10. const writeFunction = function (store, _) {
  11. const val = store.applyStore('p1');
  12. this.output.sendOutput(convertToString(val.value, val.type));
  13. store.mode = Modes.RETURN;
  14. return Promise.resolve(store);
  15. }
  16. const block = new Commands.CommandBlock([], [new Commands.SysCall(writeFunction)]);
  17. const func = new Commands.Function('$write', Types.VOID,
  18. [new Commands.FormalParameter(Types.ALL, 'p1', false)],
  19. block);
  20. return func;
  21. }
  22. export function createInputFun () {
  23. const readFunction = function (store, _) {
  24. const request = new Promise((resolve, _) => {
  25. this.input.requestInput(resolve);
  26. });
  27. return request.then(text => {
  28. const typeToConvert = store.applyStore('p1').type;
  29. let type = null
  30. let result = null;
  31. try {
  32. if (typeToConvert.isCompatible(Types.INTEGER)) {
  33. result = toInt(text).trunc();
  34. type = Types.INTEGER;
  35. } else if (typeToConvert.isCompatible(Types.REAL)) {
  36. result = toReal(text)
  37. type = Types.REAL;
  38. } else if (typeToConvert.isCompatible(Types.BOOLEAN)) {
  39. result = toBool(text)
  40. type = Types.BOOLEAN;
  41. } else if (typeToConvert.isCompatible(Types.STRING)) {
  42. result = toString(text)
  43. type = Types.STRING;
  44. } else {
  45. return Promise.reject(new Error("!!!!Critical error: Unknown type in readFunction!!!!"));
  46. }
  47. } catch (_) {
  48. const stringInfo = typeToConvert.stringInfo()[0]
  49. const realObject = store.getStoreObject("p1");
  50. if (realObject instanceof StoreObjectArrayAddressRef) {
  51. const arrayInfo = realObject.address.getArrayObject().type.stringInfo()[0];
  52. const error = ProcessorErrorFactory.invalid_read_type_array(text, stringInfo.type, stringInfo.dim, realObject.address.refID, arrayInfo.type, arrayInfo.dim);
  53. return Promise.reject(error);
  54. }
  55. const error = ProcessorErrorFactory.invalid_read_type(text, stringInfo.type, stringInfo.dim, store.applyStore('p1').id);
  56. return Promise.reject(error);
  57. }
  58. this.loopTimers.splice(0, this.loopTimers.length)
  59. const stoObj = new StoreObject(type, Location.allocate(result));
  60. store.updateStore('p1', stoObj);
  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. }