parseFromVisual.js 3.4 KB

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