io.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. 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 stoObj = null;
  29. try {
  30. if (typeToConvert.isCompatible(Types.INTEGER)) {
  31. const val = toInt(text);
  32. stoObj = new StoreObject(Types.INTEGER, val.trunc());
  33. } else if (typeToConvert.isCompatible(Types.REAL)) {
  34. stoObj = new StoreObject(Types.REAL, toReal(text));
  35. } else if (typeToConvert.isCompatible(Types.BOOLEAN)) {
  36. stoObj = new StoreObject(Types.BOOLEAN, toBool(text));
  37. } else if (typeToConvert.isCompatible(Types.STRING)) {
  38. stoObj = new StoreObject(Types.STRING, toString(text));
  39. } else {
  40. return Promise.reject(new Error("!!!!Critical error: Unknown type in readFunction!!!!"));
  41. }
  42. } catch (_) {
  43. const stringInfo = typeToConvert.stringInfo()[0]
  44. const realObject = store.getStoreObject("p1");
  45. if (realObject instanceof StoreObjectArrayAddressRef) {
  46. const arrayInfo = realObject.address.getArrayObject().type.stringInfo()[0];
  47. const error = ProcessorErrorFactory.invalid_read_type_array(text, stringInfo.type, stringInfo.dim, realObject.address.refID, arrayInfo.type, arrayInfo.dim);
  48. return Promise.reject(error);
  49. }
  50. const error = ProcessorErrorFactory.invalid_read_type(text, stringInfo.type, stringInfo.dim, store.applyStore('p1').id);
  51. return Promise.reject(error);
  52. }
  53. this.loopTimers.splice(0,this.loopTimers.length)
  54. store.updateStore('p1', stoObj);
  55. store.mode = Modes.RETURN;
  56. return Promise.resolve(store);
  57. });
  58. }
  59. const block = new Commands.CommandBlock([], [new Commands.SysCall(readFunction)]);
  60. const func = new Commands.Function('$read', Types.VOID,
  61. [new Commands.FormalParameter(Types.ALL, 'p1', true)],
  62. block);
  63. return func;
  64. }