|
@@ -5,6 +5,8 @@ import { toReal, convertToString } from "./../../typeSystem/parsers";
|
|
import { IVProgParser } from '../../ast/ivprogParser';
|
|
import { IVProgParser } from '../../ast/ivprogParser';
|
|
import { RealLiteral, IntLiteral, BoolLiteral } from '../../ast/expressions';
|
|
import { RealLiteral, IntLiteral, BoolLiteral } from '../../ast/expressions';
|
|
import { Modes } from '../modes';
|
|
import { Modes } from '../modes';
|
|
|
|
+import { MultiType } from '../../typeSystem/multiType';
|
|
|
|
+import { ProcessorErrorFactory } from '../error/processorErrorFactory';
|
|
|
|
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
@@ -89,14 +91,16 @@ export function createIsBoolFun () {
|
|
export function createCastRealFun () {
|
|
export function createCastRealFun () {
|
|
const castRealFun = (sto, _) => {
|
|
const castRealFun = (sto, _) => {
|
|
const val = sto.applyStore("val");
|
|
const val = sto.applyStore("val");
|
|
|
|
+ let value = val.value;
|
|
switch (val.type.ord) {
|
|
switch (val.type.ord) {
|
|
case Types.INTEGER.ord: {
|
|
case Types.INTEGER.ord: {
|
|
- const temp = new StoreObject(Types.REAL, toReal(val.number));
|
|
|
|
|
|
+ value = value.toNumber();
|
|
|
|
+ const temp = new StoreObject(Types.REAL, toReal(value));
|
|
sto.mode = Modes.RETURN;
|
|
sto.mode = Modes.RETURN;
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
}
|
|
}
|
|
case Types.STRING.ord: {
|
|
case Types.STRING.ord: {
|
|
- const parser = IVProgParser.createParser(val.value);
|
|
|
|
|
|
+ const parser = IVProgParser.createParser(value);
|
|
try {
|
|
try {
|
|
const result = parser.parseTerm();
|
|
const result = parser.parseTerm();
|
|
if (result instanceof RealLiteral) {
|
|
if (result instanceof RealLiteral) {
|
|
@@ -104,16 +108,17 @@ export function createCastRealFun () {
|
|
sto.mode = Modes.RETURN;
|
|
sto.mode = Modes.RETURN;
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
}
|
|
}
|
|
- } catch (error) {
|
|
|
|
- return Promise.reject("cannot convert string to real");
|
|
|
|
- }
|
|
|
|
|
|
+ } catch (error) { }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ const typeStringInfoArray = Types.REAL.stringInfo();
|
|
|
|
+ const typeInfo = typeStringInfoArray[0];
|
|
|
|
+ return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
|
|
}
|
|
}
|
|
|
|
|
|
const block = new Commands.CommandBlock([], [new Commands.SysCall(castRealFun)]);
|
|
const block = new Commands.CommandBlock([], [new Commands.SysCall(castRealFun)]);
|
|
const func = new Commands.Function('$castReal', Types.REAL,
|
|
const func = new Commands.Function('$castReal', Types.REAL,
|
|
- [new Commands.FormalParameter(Types.ALL, 'val', false)],
|
|
|
|
|
|
+ [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.STRING]), 'val', false)],
|
|
block);
|
|
block);
|
|
return func;
|
|
return func;
|
|
}
|
|
}
|
|
@@ -121,14 +126,16 @@ export function createCastRealFun () {
|
|
export function createCastIntFun () {
|
|
export function createCastIntFun () {
|
|
const castIntFun = (sto, _) => {
|
|
const castIntFun = (sto, _) => {
|
|
const val = sto.applyStore("val");
|
|
const val = sto.applyStore("val");
|
|
|
|
+ let value = val.value;
|
|
switch (val.type.ord) {
|
|
switch (val.type.ord) {
|
|
case Types.REAL.ord: {
|
|
case Types.REAL.ord: {
|
|
- const temp = new StoreObject(Types.INTEGER, Math.floor(val.number));
|
|
|
|
|
|
+ value = value.toNumber();
|
|
|
|
+ const temp = new StoreObject(Types.INTEGER, Math.floor(value));
|
|
sto.mode = Modes.RETURN;
|
|
sto.mode = Modes.RETURN;
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
}
|
|
}
|
|
case Types.STRING.ord: {
|
|
case Types.STRING.ord: {
|
|
- const parser = IVProgParser.createParser(val.value);
|
|
|
|
|
|
+ const parser = IVProgParser.createParser(value);
|
|
try {
|
|
try {
|
|
const result = parser.parseTerm();
|
|
const result = parser.parseTerm();
|
|
if (result instanceof IntLiteral) {
|
|
if (result instanceof IntLiteral) {
|
|
@@ -136,16 +143,17 @@ export function createCastIntFun () {
|
|
sto.mode = Modes.RETURN;
|
|
sto.mode = Modes.RETURN;
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
}
|
|
}
|
|
- } catch (error) {
|
|
|
|
- return Promise.reject("cannot convert string to real");
|
|
|
|
- }
|
|
|
|
|
|
+ } catch (error) { }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ const typeStringInfoArray = Types.INTEGER.stringInfo();
|
|
|
|
+ const typeInfo = typeStringInfoArray[0];
|
|
|
|
+ return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
|
|
}
|
|
}
|
|
|
|
|
|
const block = new Commands.CommandBlock([], [new Commands.SysCall(castIntFun)]);
|
|
const block = new Commands.CommandBlock([], [new Commands.SysCall(castIntFun)]);
|
|
const func = new Commands.Function('$castInt', Types.INTEGER,
|
|
const func = new Commands.Function('$castInt', Types.INTEGER,
|
|
- [new Commands.FormalParameter(Types.ALL, 'val', false)],
|
|
|
|
|
|
+ [new Commands.FormalParameter(new MultiType([Types.REAL, Types.STRING]), 'val', false)],
|
|
block);
|
|
block);
|
|
return func;
|
|
return func;
|
|
}
|
|
}
|
|
@@ -153,7 +161,8 @@ export function createCastIntFun () {
|
|
export function createCastBoolFun () {
|
|
export function createCastBoolFun () {
|
|
const castBoolFun = (sto, _) => {
|
|
const castBoolFun = (sto, _) => {
|
|
const str = sto.applyStore("str");
|
|
const str = sto.applyStore("str");
|
|
- const parser = IVProgParser.createParser(str.value);
|
|
|
|
|
|
+ let value = str.value;
|
|
|
|
+ const parser = IVProgParser.createParser(value);
|
|
try {
|
|
try {
|
|
const val = parser.parseTerm();
|
|
const val = parser.parseTerm();
|
|
if (val instanceof BoolLiteral) {
|
|
if (val instanceof BoolLiteral) {
|
|
@@ -162,7 +171,9 @@ export function createCastBoolFun () {
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
return Promise.resolve(sto.updateStore("$", temp));
|
|
}
|
|
}
|
|
} catch (error) { }
|
|
} catch (error) { }
|
|
- return Promise.reject("cannot convert " + str.value + " to boolean");
|
|
|
|
|
|
+ const typeStringInfoArray = Types.BOOLEAN.stringInfo();
|
|
|
|
+ const typeInfo = typeStringInfoArray[0];
|
|
|
|
+ return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
|
|
}
|
|
}
|
|
|
|
|
|
const block = new Commands.CommandBlock([], [new Commands.SysCall(castBoolFun)]);
|
|
const block = new Commands.CommandBlock([], [new Commands.SysCall(castBoolFun)]);
|