1
0

lang.js 6.0 KB

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