Guest User

Untitled

a guest
Jul 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. import utils.CompileTimeErrors;
  2. import semanticlib.SemType;
  3. import semanticlib.SymbolTable;
  4. aspect TypeAnalysis {
  5.  
  6. void ASTNode.typeCheck(CompileTimeErrors errs) {
  7. for (int i = 0; i < getNumChild(); i++) {
  8. getChild(i).typeCheck(errs);
  9. }
  10. }
  11.  
  12. public void Start.typeCheck(CompileTimeErrors errs) {
  13. for (int i = 0; i < getNumChild(); i++) {
  14. getChild(i).typeCheck(errs);
  15. }
  16. }
  17.  
  18. SemType Type.type() {
  19. if (getTYPE().equals(SemType.INTEGER.toString()))
  20. return SemType.INTEGER;
  21. else if (getTYPE().equals(SemType.BOOLEAN.toString()))
  22. return SemType.BOOLEAN;
  23. else if (getTYPE().equals(SemType.VOID.toString()))
  24. return SemType.VOID;
  25. else
  26. return SemType.UNKNOWN;
  27. }
  28.  
  29. SemType Expr.type() {
  30. return SemType.UNKNOWN;
  31. }
  32.  
  33. SemType BoolExpr.type() {
  34. return SemType.BOOLEAN;
  35. }
  36.  
  37. SemType IntExpr.type() {
  38. return SemType.INTEGER;
  39. }
  40.  
  41. SemType MethodExpr.type() {
  42. return getIdUse().type;
  43. }
  44.  
  45. void ReturnStmt.typeCheck(CompileTimeErrors errs) {
  46. ASTNode parent = getParent();
  47. while (!(parent instanceof DeclMethod) && parent != null) {
  48. parent = parent.getParent();
  49. }
  50. if (parent instanceof DeclMethod) {
  51. DeclMethod dm = (DeclMethod)parent;
  52.  
  53. if (getIdUse().getID().equals("writeint")) {
  54. if (hasExpr) {
  55. errs.add(this, "Return-type is not the same as declared by method (" +
  56. dm.getType().getTYPE() + ")" +
  57. " at: " + startLineCol());
  58. }
  59. } else if (getIdUse().getID().equals("writeln")) {
  60. if (hasExpr) {
  61. errs.add(this, "Return-type is not the same as declared by method (" +
  62. dm.getType().getTYPE() + ")" +
  63. " at: " + startLineCol());
  64. }
  65. } else if (getIdUse().getID().equals("readint")) {
  66. if (!hasExpr || !dm.getType().getTYPE().equals(SemType.INTEGER)) {
  67. errs.add(this, "Return-type is not the same as declared by method (" +
  68. dm.getType().getTYPE() + ")" +
  69. " at: " + startLineCol());
  70. }
  71. } else if (!hasExpr()) {
  72. if (!dm.getType().getTYPE().equals(SemType.VOID.toString()))
  73. errs.add(this, "Return-type is not the same as declared by method (" +
  74. dm.getType().getTYPE() + ")" +
  75. " at: " + startLineCol());
  76. } else if (!dm.getType().getTYPE().equals(getExpr().type().toString())) {
  77. errs.add(this, "Return-type is not the same as declared by method (" +
  78. dm.getType().getTYPE() + " vs. " + getExpr().type().toString() + ")" +
  79. " at: " + startLineCol());
  80. }
  81. } else {
  82. errs.add(this, "No DeclMethod-parent" +
  83. " at: " + startLineCol());
  84. }
  85. }
  86.  
  87. void Assignment.typeCheck(CompileTimeErrors errs) {
  88. SemType type = getIdUse().type;
  89. if (getExpr().type() != type) {
  90. errs.add(this, "Not right types in assignments: " + type + " vs. " + getExpr().type() +
  91. " at: " + startLineCol());
  92. }
  93. }
  94.  
  95. void VarIntFactor.typeCheck(CompileTimeErrors errs) {
  96. SemType type = getIdUse().type;
  97. if (type != SemType.INTEGER) {
  98. errs.add(this, "Not right types in assignments: " +
  99. type + " vs. " + SemType.INTEGER +
  100. " at: " + startLineCol());
  101. }
  102. }
  103.  
  104. void UseMethod.typeCheck(CompileTimeErrors errs) {
  105. if (getIdUse().getID().equals("writeint")) {
  106. if (!hasExprList() || getExprList().getExprs().getNumChild() != 1) {
  107. errs.add(this, "Incompatible parameter-list for " + getIdUse().getID() + " at: " + startLineCol());
  108. } else if (getExprList().getExpr(0).type() != SemType.INTEGER) {
  109. errs.add(this, "Wrong type of parameter, " +
  110. getExprList().getExpr(0).type() +
  111. " instead of integer, at: " + startLineCol());
  112. }
  113. } else if (getIdUse().getID().equals("writeln")) {
  114. if (hasExprList()) {
  115. errs.add(this, "Incompatible parameter-list for " + getIdUse().getID() + " at: " + startLineCol());
  116. }
  117. } else if (getIdUse().getID().equals("readint")) {
  118. if (hasExprList()) {
  119. errs.add(this, "Incompatible parameter-list for " + getIdUse().getID() + " at: " + startLineCol());
  120. }
  121. } else {
  122. DeclMethod dm = (DeclMethod)getIdUse().decl;
  123. if (dm != null && hasExprList() != dm.hasParams()) {
  124. errs.add(this, "Incompatible parameter-list for " + getIdUse().getID() + " at: " + startLineCol());
  125. }else if (getExprList().getExprs().getNumChild() != dm.getParams().size()) { //get size of DeclMethod
  126. errs.add(this, "Incorrect number of parameters for " + getIdUse().getID() + " at: " + startLineCol());
  127. } else{
  128. for(int i = 0; i < getExprList().size(); i++){ //check types of all parameters
  129. if(getExprList().getExpr(0).type() != dm.getParams.getNumChild(i).type()){
  130. errs.add(this, "Wrong type of parameter, " +
  131. getExprList().getExpr(0).type() +
  132. " instead of" + dm.getParams.getNumChild(i).type() + ", at: " + startLineCol());
  133. }
  134. }
  135. }
  136. }
  137. }
  138.  
  139. void MethodExpr.typeCheck(CompileTimeErrors errs) {//chack that the method is used correctly in expressions
  140.  
  141. }
  142. }
Add Comment
Please, Sign In to add comment