parseFromVisual.js 3.1 KB

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