io.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.trim()).trunc();
  32. type = Types.INTEGER;
  33. } else if (typeToConvert.isCompatible(Types.REAL)) {
  34. result = toReal(text.trim())
  35. type = Types.REAL;
  36. } else if (typeToConvert.isCompatible(Types.BOOLEAN)) {
  37. result = toBool(text.trim())
  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. if(this.mode == Modes.ABORT) {
  47. store.mode = Modes.RETURN;
  48. return Promise.resolve(store);
  49. }
  50. const stringInfo = typeToConvert.stringInfo()[0]
  51. const realObject = store.getStoreObject("p1");
  52. if (realObject.getReferenceDimension() > 0) {
  53. const arrayInfo = realObject.type.stringInfo()[0];
  54. const dim = realObject.getReferenceDimension();
  55. const error = ProcessorErrorFactory.invalid_read_type_array(text, stringInfo.type, stringInfo.dim, realObject.getRefObj(), arrayInfo.type, dim, this.function_call_stack.pop());
  56. return Promise.reject(error);
  57. }
  58. const error = ProcessorErrorFactory.invalid_read_type(text, stringInfo.type, stringInfo.dim, realObject.getRefObj(), this.function_call_stack.pop());
  59. return Promise.reject(error);
  60. }
  61. const stoValue = new StoreValue(type, result);
  62. store.updateStore('p1', stoValue);
  63. store.mode = Modes.RETURN;
  64. return Promise.resolve(store);
  65. });
  66. }
  67. const block = new Commands.CommandBlock([], [new Commands.SysCall(readFunction)]);
  68. const func = new Commands.Function('$read', Types.VOID,
  69. [new Commands.FormalParameter(Types.ALL, 'p1', true)],
  70. block);
  71. return func;
  72. }