lang.js 7.0 KB

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