/src/org/ooc/frontend/model/OpDecl.java

http://github.com/nddrylliog/ooc · Java · 197 lines · 176 code · 21 blank · 0 comment · 3 complexity · 59423fd23761611b1ef19ba0d95e1134 MD5 · raw file

  1. package org.ooc.frontend.model;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import org.ooc.frontend.Visitor;
  5. import org.ooc.frontend.model.tokens.Token;
  6. public class OpDecl extends Declaration {
  7. public enum OpType {
  8. ADD,
  9. ADD_ASS,
  10. SUB,
  11. SUB_ASS,
  12. MUL,
  13. MUL_ASS,
  14. DIV,
  15. DIV_ASS,
  16. ASS,
  17. NOT,
  18. MOD,
  19. L_OR,
  20. L_AND,
  21. B_XOR,
  22. B_XOR_ASS,
  23. B_OR,
  24. B_OR_ASS,
  25. B_AND,
  26. B_AND_ASS,
  27. LSHIFT,
  28. B_LSHIFT_ASS,
  29. RSHIFT,
  30. B_RSHIFT_ASS,
  31. IDX,
  32. IDX_ASS,
  33. GT,
  34. GTE,
  35. LT,
  36. LTE,
  37. EQ,
  38. NE,
  39. AS,
  40. B_NEG;
  41. public String toPrettyString() {
  42. switch(this) {
  43. case ADD:
  44. return "+";
  45. case DIV:
  46. return "/";
  47. case IDX_ASS:
  48. return "[]=";
  49. case IDX:
  50. return "[]";
  51. case MUL:
  52. return "*";
  53. case SUB:
  54. return "-";
  55. case B_AND:
  56. return "&";
  57. case B_OR:
  58. return "|";
  59. case B_XOR:
  60. return "^";
  61. case L_AND:
  62. return "&&";
  63. case L_OR:
  64. return "||";
  65. case MOD:
  66. return "%";
  67. case EQ:
  68. return "==";
  69. case GT:
  70. return ">";
  71. case GTE:
  72. return ">=";
  73. case LT:
  74. return "<";
  75. case LTE:
  76. return "<=";
  77. case NE:
  78. return "!=";
  79. case NOT:
  80. return "!";
  81. case ASS:
  82. return "=";
  83. case ADD_ASS:
  84. return "+=";
  85. case DIV_ASS:
  86. return "/=";
  87. case MUL_ASS:
  88. return "*=";
  89. case SUB_ASS:
  90. return "-=";
  91. case B_LSHIFT_ASS:
  92. return "<<=";
  93. case B_RSHIFT_ASS:
  94. return ">>=";
  95. case B_XOR_ASS:
  96. return "^=";
  97. case LSHIFT:
  98. return ">>";
  99. case RSHIFT:
  100. return "<<";
  101. case B_OR_ASS:
  102. return "|=";
  103. case B_AND_ASS:
  104. return "&=";
  105. case AS:
  106. return "as";
  107. case B_NEG:
  108. return "~";
  109. }
  110. return "unknown";
  111. }
  112. public boolean isNumeric() {
  113. switch(this) {
  114. case ADD: case SUB: case MUL: case DIV:
  115. case ADD_ASS: case SUB_ASS: case MUL_ASS: case DIV_ASS:
  116. case B_AND: case B_OR: case B_XOR: case B_NEG:
  117. return true;
  118. default:
  119. return false;
  120. }
  121. }
  122. }
  123. protected OpType opType;
  124. protected FunctionDecl func;
  125. public OpDecl(OpType opType, FunctionDecl func, Token startToken, Module module) {
  126. super("Operator "+opType, startToken, module);
  127. this.opType = opType;
  128. this.func = func;
  129. String name = "__OP_"+opType.toString();
  130. Iterator<Argument> iter = func.getArguments().iterator();
  131. while(iter.hasNext()) {
  132. name += "_" + iter.next().getType().getMangledName();
  133. }
  134. if(!func.getReturnType().isVoid()) {
  135. name += "__" + func.getReturnType().getMangledName();
  136. }
  137. func.setName(name);
  138. }
  139. @Override
  140. public boolean replace(Node oldie, Node kiddo) {
  141. if(oldie == func) {
  142. func = (FunctionDecl) kiddo;
  143. return true;
  144. }
  145. return false;
  146. }
  147. public Type getType() {
  148. return new Type("Operator", Token.defaultToken);
  149. }
  150. public OpType getOpType() {
  151. return opType;
  152. }
  153. public FunctionDecl getFunc() {
  154. return func;
  155. }
  156. public void accept(Visitor visitor) throws IOException {
  157. visitor.visit(this);
  158. }
  159. public void acceptChildren(Visitor visitor) throws IOException {
  160. func.accept(visitor);
  161. }
  162. public boolean hasChildren() {
  163. return true;
  164. }
  165. public String getOpString() {
  166. return opType.toPrettyString();
  167. }
  168. @Override
  169. public String toString() {
  170. return "operator "+getOpString()+" "+func.getArgsRepr()+" -> "+func.getReturnType();
  171. }
  172. @Override
  173. public TypeDecl getTypeDecl() {
  174. throw new Error("getting type decl of an "+getClass().getSimpleName()+", wtf?");
  175. }
  176. }