lang.js 7.7 KB

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