123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import { StoreObject } from './../store/storeObject';
- import * as Commands from './../../ast/commands';
- import { Modes } from '../modes';
- import {toInt, toString, toBool, toReal, convertToString} from './../../typeSystem/parsers';
- import { Types } from './../../typeSystem/types';
- import { ProcessorErrorFactory } from "./../error/processorErrorFactory";
- import { StoreObjectArrayAddressRef } from '../store/storeObjectArrayAddressRef';
- import { Location } from '../../memory/location';
- export function createOutputFun () {
- const writeFunction = function (store, _) {
- const val = store.applyStore('p1');
- this.output.sendOutput(convertToString(val.value, val.type));
- store.mode = Modes.RETURN;
- return Promise.resolve(store);
- }
- const block = new Commands.CommandBlock([], [new Commands.SysCall(writeFunction)]);
- const func = new Commands.Function('$write', Types.VOID,
- [new Commands.FormalParameter(Types.ALL, 'p1', false)],
- block);
- return func;
- }
- export function createInputFun () {
- const readFunction = function (store, _) {
- const request = new Promise((resolve, _) => {
- this.input.requestInput(resolve);
- });
- return request.then(text => {
- const typeToConvert = store.applyStore('p1').type;
- let type = null
- let result = null;
- try {
- if (typeToConvert.isCompatible(Types.INTEGER)) {
- result = toInt(text).trunc();
- type = Types.INTEGER;
- } else if (typeToConvert.isCompatible(Types.REAL)) {
- result = toReal(text)
- type = Types.REAL;
- } else if (typeToConvert.isCompatible(Types.BOOLEAN)) {
- result = toBool(text)
- type = Types.BOOLEAN;
- } else if (typeToConvert.isCompatible(Types.STRING)) {
- result = toString(text)
- type = Types.STRING;
- } else {
- return Promise.reject(new Error("!!!!Critical error: Unknown type in readFunction!!!!"));
- }
- } catch (_) {
- const stringInfo = typeToConvert.stringInfo()[0]
- const realObject = store.getStoreObject("p1");
- if (realObject instanceof StoreObjectArrayAddressRef) {
- const arrayInfo = realObject.address.getArrayObject().type.stringInfo()[0];
- const error = ProcessorErrorFactory.invalid_read_type_array(text, stringInfo.type, stringInfo.dim, realObject.address.refID, arrayInfo.type, arrayInfo.dim);
- return Promise.reject(error);
- }
- const error = ProcessorErrorFactory.invalid_read_type(text, stringInfo.type, stringInfo.dim, store.applyStore('p1').id);
- return Promise.reject(error);
- }
- this.loopTimers.splice(0, this.loopTimers.length)
- const stoObj = new StoreObject(type, Location.allocate(result));
- store.updateStore('p1', stoObj);
- store.mode = Modes.RETURN;
- return Promise.resolve(store);
- });
- }
- const block = new Commands.CommandBlock([], [new Commands.SysCall(readFunction)]);
- const func = new Commands.Function('$read', Types.VOID,
- [new Commands.FormalParameter(Types.ALL, 'p1', true)],
- block);
- return func;
- }
|