lang.js 6.0 KB

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