lang.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { StoreObject } from '../store/storeObject';
  2. import * as Commands from './../../ast/commands';
  3. import { Types } from './../../typeSystem/types';
  4. import { toReal, convertToString } from "./../../typeSystem/parsers";
  5. import { IVProgParser } from '../../ast/ivprogParser';
  6. import { RealLiteral, IntLiteral, BoolLiteral } from '../../ast/expressions';
  7. import { Modes } from '../modes';
  8. import { MultiType } from '../../typeSystem/multiType';
  9. import { ProcessorErrorFactory } from '../error/processorErrorFactory';
  10. /**
  11. *
  12. * is_real
  13. * is_int
  14. * is_bool
  15. * cast_real
  16. * cast_int
  17. * cast_bool
  18. * cast_string
  19. */
  20. export function createIsRealFun () {
  21. const isRealFun = (sto, _) => {
  22. const str = sto.applyStore("str");
  23. const parser = IVProgParser.createParser(str.value);
  24. let result = false;
  25. try {
  26. const val = parser.parseTerm();
  27. if (val instanceof RealLiteral) {
  28. result = true;
  29. }
  30. } catch (error) { }
  31. const temp = new StoreObject(Types.BOOLEAN, result);
  32. sto.mode = Modes.RETURN;
  33. return Promise.resolve(sto.updateStore("$", temp));
  34. }
  35. const block = new Commands.CommandBlock([], [new Commands.SysCall(isRealFun)]);
  36. const func = new Commands.Function('$isReal', Types.BOOLEAN,
  37. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  38. block);
  39. return func;
  40. }
  41. export function createIsIntFun () {
  42. const isIntFun = (sto, _) => {
  43. const str = sto.applyStore("str");
  44. const parser = IVProgParser.createParser(str.value);
  45. let result = false;
  46. try {
  47. const val = parser.parseTerm();
  48. if (val instanceof IntLiteral) {
  49. result = true;
  50. }
  51. } catch (error) { }
  52. const temp = new StoreObject(Types.BOOLEAN, result);
  53. sto.mode = Modes.RETURN;
  54. return Promise.resolve(sto.updateStore("$", temp));
  55. }
  56. const block = new Commands.CommandBlock([], [new Commands.SysCall(isIntFun)]);
  57. const func = new Commands.Function('$isInt', Types.BOOLEAN,
  58. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  59. block);
  60. return func;
  61. }
  62. export function createIsBoolFun () {
  63. const isBoolFun = (sto, _) => {
  64. const str = sto.applyStore("str");
  65. const parser = IVProgParser.createParser(str.value);
  66. let result = false;
  67. try {
  68. const val = parser.parseTerm();
  69. if (val instanceof BoolLiteral) {
  70. result = true;
  71. }
  72. } catch (error) { }
  73. const temp = new StoreObject(Types.BOOLEAN, result);
  74. sto.mode = Modes.RETURN;
  75. return Promise.resolve(sto.updateStore("$", temp));
  76. }
  77. const block = new Commands.CommandBlock([], [new Commands.SysCall(isBoolFun)]);
  78. const func = new Commands.Function('$isBool', Types.BOOLEAN,
  79. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  80. block);
  81. return func;
  82. }
  83. export function createCastRealFun () {
  84. const castRealFun = (sto, _) => {
  85. const val = sto.applyStore("val");
  86. let value = val.value;
  87. switch (val.type.ord) {
  88. case Types.INTEGER.ord: {
  89. value = value.toNumber();
  90. const temp = new StoreObject(Types.REAL, toReal(value));
  91. sto.mode = Modes.RETURN;
  92. return Promise.resolve(sto.updateStore("$", temp));
  93. }
  94. case Types.STRING.ord: {
  95. const parser = IVProgParser.createParser(value);
  96. try {
  97. const result = parser.parseTerm();
  98. if (result instanceof RealLiteral) {
  99. const temp = new StoreObject(Types.REAL, result.value);
  100. sto.mode = Modes.RETURN;
  101. return Promise.resolve(sto.updateStore("$", temp));
  102. }
  103. } catch (error) { }
  104. }
  105. }
  106. const typeStringInfoArray = Types.REAL.stringInfo();
  107. const typeInfo = typeStringInfoArray[0];
  108. return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
  109. }
  110. const block = new Commands.CommandBlock([], [new Commands.SysCall(castRealFun)]);
  111. const func = new Commands.Function('$castReal', Types.REAL,
  112. [new Commands.FormalParameter(new MultiType([Types.INTEGER, Types.STRING]), 'val', false)],
  113. block);
  114. return func;
  115. }
  116. export function createCastIntFun () {
  117. const castIntFun = (sto, _) => {
  118. const val = sto.applyStore("val");
  119. let value = val.value;
  120. switch (val.type.ord) {
  121. case Types.REAL.ord: {
  122. value = value.toNumber();
  123. const temp = new StoreObject(Types.INTEGER, Math.floor(value));
  124. sto.mode = Modes.RETURN;
  125. return Promise.resolve(sto.updateStore("$", temp));
  126. }
  127. case Types.STRING.ord: {
  128. const parser = IVProgParser.createParser(value);
  129. try {
  130. const result = parser.parseTerm();
  131. if (result instanceof IntLiteral) {
  132. const temp = new StoreObject(Types.INTEGER, result.value);
  133. sto.mode = Modes.RETURN;
  134. return Promise.resolve(sto.updateStore("$", temp));
  135. }
  136. } catch (error) { }
  137. }
  138. }
  139. const typeStringInfoArray = Types.INTEGER.stringInfo();
  140. const typeInfo = typeStringInfoArray[0];
  141. return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
  142. }
  143. const block = new Commands.CommandBlock([], [new Commands.SysCall(castIntFun)]);
  144. const func = new Commands.Function('$castInt', Types.INTEGER,
  145. [new Commands.FormalParameter(new MultiType([Types.REAL, Types.STRING]), 'val', false)],
  146. block);
  147. return func;
  148. }
  149. export function createCastBoolFun () {
  150. const castBoolFun = (sto, _) => {
  151. const str = sto.applyStore("str");
  152. let value = str.value;
  153. const parser = IVProgParser.createParser(value);
  154. try {
  155. const val = parser.parseTerm();
  156. if (val instanceof BoolLiteral) {
  157. const temp = new StoreObject(Types.BOOLEAN, val.value);
  158. sto.mode = Modes.RETURN;
  159. return Promise.resolve(sto.updateStore("$", temp));
  160. }
  161. } catch (error) { }
  162. const typeStringInfoArray = Types.BOOLEAN.stringInfo();
  163. const typeInfo = typeStringInfoArray[0];
  164. return Promise.reject(ProcessorErrorFactory.invalid_type_conversion(value, typeInfo.type, typeInfo.dim));
  165. }
  166. const block = new Commands.CommandBlock([], [new Commands.SysCall(castBoolFun)]);
  167. const func = new Commands.Function('$castBool', Types.BOOLEAN,
  168. [new Commands.FormalParameter(Types.STRING, 'str', false)],
  169. block);
  170. return func;
  171. }
  172. export function createCastStringFun () {
  173. const castStringFun = function (store, _) {
  174. const val = store.applyStore('str');
  175. let result = convertToString(val)
  176. const temp = new StoreObject(Types.STRING, result);
  177. sto.mode = Modes.RETURN;
  178. return Promise.resolve(sto.updateStore("$", temp));
  179. }
  180. const block = new Commands.CommandBlock([], [new Commands.SysCall(castStringFun)]);
  181. const func = new Commands.Function('$castString', Types.STRING,
  182. [new Commands.FormalParameter(Types.ALL, 'str', false)],
  183. block);
  184. return func;
  185. }