io.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if (realObject.getReferenceDimension() > 0) {
  49. const arrayInfo = realObject.type.stringInfo()[0];
  50. const dim = realObject.getReferenceDimension();
  51. const error = ProcessorErrorFactory.invalid_read_type_array(text, stringInfo.type, stringInfo.dim, realObject.getRefObj(), arrayInfo.type, dim, this.function_call_stack.pop());
  52. return Promise.reject(error);
  53. }
  54. const error = ProcessorErrorFactory.invalid_read_type(text, stringInfo.type, stringInfo.dim, realObject.getRefObj(), this.function_call_stack.pop());
  55. return Promise.reject(error);
  56. }
  57. const stoValue = new StoreValue(type, result);
  58. store.updateStore('p1', stoValue);
  59. store.mode = Modes.RETURN;
  60. return Promise.resolve(store);
  61. });
  62. }
  63. const block = new Commands.CommandBlock([], [new Commands.SysCall(readFunction)]);
  64. const func = new Commands.Function('$read', Types.VOID,
  65. [new Commands.FormalParameter(Types.ALL, 'p1', true)],
  66. block);
  67. return func;
  68. }