io.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { StoreObject } from './../store/storeObject';
  2. import * as Commands from './../../ast/commands';
  3. import {toInt, toString, toBool, toReal, convertToString} from './../../typeSystem/parsers';
  4. import { Types } from './../../typeSystem/types';
  5. export function createOutputFun () {
  6. const writeFunction = function (store, _) {
  7. const val = store.applyStore('p1');
  8. this.output.sendOutput(convertToString(val.value, val.type));
  9. return Promise.resolve(store);
  10. }
  11. const block = new Commands.CommandBlock([], [new Commands.SysCall(writeFunction)]);
  12. const func = new Commands.Function('$write', Types.VOID,
  13. [new Commands.FormalParameter(Types.ALL, 'p1', false)],
  14. block);
  15. return func;
  16. }
  17. export function createInputFun () {
  18. const readFunction = function (store, _) {
  19. const request = new Promise((resolve, _) => {
  20. this.input.requestInput(resolve);
  21. });
  22. return request.then(text => {
  23. const typeToConvert = store.applyStore('p1').type;
  24. let stoObj = null;
  25. if (typeToConvert.isCompatible(Types.INTEGER)) {
  26. const val = toInt(text);
  27. stoObj = new StoreObject(Types.INTEGER, val);
  28. } else if (typeToConvert.isCompatible(Types.REAL)) {
  29. stoObj = new StoreObject(Types.REAL, toReal(text));
  30. } else if (typeToConvert.isCompatible(Types.BOOLEAN)) {
  31. stoObj = new StoreObject(Types.BOOLEAN, toBool(text));
  32. } else if (typeToConvert.isCompatible(Types.STRING)) {
  33. stoObj = new StoreObject(Types.STRING, toString(text));
  34. }
  35. this.loopTimers.splice(0,this.loopTimers.length)
  36. store.updateStore('p1', stoObj);
  37. return Promise.resolve(store);
  38. });
  39. }
  40. const block = new Commands.CommandBlock([], [new Commands.SysCall(readFunction)]);
  41. const func = new Commands.Function('$read', Types.VOID,
  42. [new Commands.FormalParameter(Types.ALL, 'p1', true)],
  43. block);
  44. return func;
  45. }