compatibilityTable.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { Types } from './../ast/types';
  2. import { Operators } from './../ast/operators';
  3. const InfixCompatibilityTable = Object.freeze([
  4. /*Add*/[Types.INTEGER, Types.REAL, Types.STRING],
  5. /*Sub*/[Types.INTEGER, Types.REAL],
  6. /*Mult*/[Types.INTEGER, Types.REAL],
  7. /*Div*/[Types.INTEGER, Types.REAL],
  8. /*Mod*/[Types.INTEGER],
  9. /*Gt*/[Types.INTEGER, Types.REAL],
  10. /*Ge*/[Types.INTEGER, Types.REAL],
  11. /*Lt*/[Types.INTEGER, Types.REAL],
  12. /*Le*/[Types.INTEGER, Types.REAL],
  13. /*Eq*/[Types.INTEGER, Types.REAL, Types.STRING, Types.BOOLEAN],
  14. /*Neq*/[Types.INTEGER, Types.REAL, Types.STRING, Types.BOOLEAN],
  15. /*And*/[Types.BOOLEAN],
  16. /*Or*/[Types.BOOLEAN],
  17. /*Not*/null,
  18. ]);
  19. const UnaryCompatibilityTable = Object.freeze([
  20. /*Add*/[Types.INTEGER, Types.REAL],
  21. /*Sub*/[Types.INTEGER, Types.REAL],
  22. /*Mult*/null,
  23. /*Div*/null,
  24. /*Mod*/null,
  25. /*Gt*/null,
  26. /*Ge*/null,
  27. /*Lt*/null,
  28. /*Le*/null,
  29. /*Eq*/null,
  30. /*Neq*/null,
  31. /*And*/null,
  32. /*Or*/null,
  33. /*Not*/[Types.BOOLEAN],
  34. ]);
  35. export function canApplyUnaryOp (op, a) {
  36. const list = UnaryCompatibilityTable[op];
  37. if (!!!list) {
  38. return false;
  39. }
  40. const type = list.find(t => t === a.type);
  41. if (!!!type) {
  42. return false;
  43. }
  44. return true
  45. }
  46. export function canApplyInfixOp (op, a, b) {
  47. const list = InfixCompatibilityTable[op];
  48. if (!!!list) {
  49. return false;
  50. }
  51. const type = list.find(t => t === a.type);
  52. if (!!!type) {
  53. return false;
  54. }
  55. return type === b.type;
  56. }