lang.js 5.9 KB

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