parseFromVisual.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { IVProgParser } from "../ast/ivprogParser";
  2. import * as Expressions from "../ast/expressions";
  3. import { Types } from "../typeSystem/types";
  4. import { convertBoolToString } from "../typeSystem/parsers";
  5. const TYPES = {
  6. VARIABLE: "var",
  7. CONST: "const",
  8. FUNCTION: "function",
  9. RELATIONAL: "relational",
  10. LOGIC: "logic",
  11. ARITHMETIC: "arithmetic",
  12. };
  13. function translateOp (type, op) {
  14. switch (type) {
  15. case TYPES.ARITHMETIC:
  16. return op.value;
  17. case TYPES.RELATIONAL:
  18. return op.value;
  19. case TYPES.LOGIC: {
  20. if (op.ord === 11) {
  21. return "and";
  22. } else if (op.ord === 12) {
  23. return "or";
  24. } else {
  25. return "not";
  26. }
  27. }
  28. }
  29. }
  30. function getOpType (op) {
  31. switch (op.ord) {
  32. case 0:
  33. case 1:
  34. case 2:
  35. case 3:
  36. case 4:
  37. return TYPES.ARITHMETIC;
  38. case 5:
  39. case 6:
  40. case 7:
  41. case 8:
  42. case 9:
  43. case 10:
  44. return TYPES.RELATIONAL;
  45. default:
  46. return TYPES.LOGIC;
  47. }
  48. }
  49. function expressionWalker (expression) {
  50. let result;
  51. if (expression instanceof Expressions.VariableLiteral) {
  52. result = [
  53. { instance: "expression", type: TYPES.VARIABLE, value: expression.id },
  54. ];
  55. } else if (expression instanceof Expressions.FunctionCall) {
  56. const funcObj = {
  57. instance: "expression",
  58. type: TYPES.FUNCTION,
  59. value: expression.id,
  60. };
  61. const paramsList = expression.actualParameters.map((e) =>
  62. expressionWalker(e)
  63. );
  64. //const params = Array.prototype.concat.apply([], paramsList);
  65. funcObj.params = paramsList;
  66. result = [funcObj];
  67. } else if (expression instanceof Expressions.UnaryApp) {
  68. const left = expressionWalker(expression.left);
  69. const opType = getOpType(expression.op);
  70. const opValue = translateOp(opType, expression.op);
  71. result = [{ instance: "operator", type: opType, value: opValue }, ...left];
  72. } else if (expression instanceof Expressions.InfixApp) {
  73. const left = expressionWalker(expression.left);
  74. const right = expressionWalker(expression.right);
  75. const opType = getOpType(expression.op);
  76. const opValue = translateOp(opType, expression.op);
  77. result = [
  78. ...left,
  79. { instance: "operator", type: opType, value: opValue },
  80. ...right,
  81. ];
  82. } else if (expression instanceof Expressions.ArrayAccess) {
  83. const line = expressionWalker(expression.line);
  84. let arrayClass = "vector";
  85. let column = null;
  86. if (expression.column) {
  87. arrayClass = "matrix";
  88. column = expressionWalker(expression.column);
  89. }
  90. result = [
  91. {
  92. instance: "expression",
  93. type: TYPES.VARIABLE,
  94. class: arrayClass,
  95. column: column,
  96. line: line,
  97. value: expression.id,
  98. },
  99. ];
  100. } else if (expression instanceof Expressions.BoolLiteral) {
  101. const value = expression.value;
  102. result = [
  103. {
  104. instance: "expression",
  105. class: "simple",
  106. type: TYPES.CONST,
  107. value: convertBoolToString(value),
  108. },
  109. ];
  110. } else {
  111. let value = expression.value;
  112. if (expression.value.toNumber) {
  113. if (
  114. Types.REAL.isCompatible(expression.type) &&
  115. expression.value.decimalPlaces() == 0
  116. ) {
  117. value = expression.value.toFixed(2);
  118. } else {
  119. value = expression.value.toNumber();
  120. }
  121. }
  122. result = [
  123. {
  124. instance: "expression",
  125. class: "simple",
  126. type: TYPES.CONST,
  127. value: value,
  128. },
  129. ];
  130. }
  131. if (expression.parenthesis) return ["(", ...result, ")"];
  132. else return result;
  133. }
  134. export function parseExpression (text) {
  135. const parser = IVProgParser.createParser(text);
  136. const expressionAST = parser.parseExpressionOR();
  137. return expressionWalker(expressionAST);
  138. }