Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1.     //implements statement checks
  2.     @Override
  3.     public void exitAssignStatement(DecafParser.AssignStatementContext ctx) {
  4.         String symbolName = ctx.location().ID().getText();
  5.         Symbol symbol = this.currentScope.resolve(symbolName);
  6.         String operator = ctx.assign_op().getText();
  7.         Symbol expr = annotations.get(ctx.expr());
  8.  
  9.         if (symbol != null) {
  10.             if (ctx.location().expr() != null && this.currentScope.resolve(symbolName) instanceof Location) {
  11.                 Integer locationIndex = null;
  12.  
  13.                 try {
  14.                     locationIndex = Integer.parseInt(ctx.location().expr().getText());
  15.                 } catch (NumberFormatException e) {
  16.                 }
  17.  
  18.                 Location location = (Location) this.currentScope.resolve(symbolName);
  19.  
  20.                 if (location != null && locationIndex != null) {
  21.                     if (locationIndex > location.Size) {
  22.                         printError(location.ID + " called with incorrect index", ctx.getStart());
  23.                         printError("Expected index to be <= " + location.Size + " recieved " + locationIndex, ctx.getStart());
  24.                     }
  25.                 }
  26.             }
  27.             if (operator.equals("+=") || operator.equals("-=") || operator.equals("=")) {
  28.                 if (expr != null) {
  29.                     if (!expr.Type.equals("int") || !symbol.Type.equals("int")) {
  30.                         printError("Invalid types for operand " + operator, ctx.getStart());
  31.                         printError("Found " + expr.Type + " + " + symbol.Type + " expected int + int", ctx.getStart());
  32.                     }
  33.                 }
  34.             } else //ensure we catch other types such as boolean = boolean
  35.             {
  36.                 if (expr != null) {
  37.                     if (!expr.Type.equals(symbol.Type)) {
  38.                         printError("Invalid expression assignment type", ctx.getStart());
  39.                         printError("Recieved " + expr.Type + " Expected " + symbol.Type, ctx.getStart());
  40.                     }
  41.                 }
  42.             }
  43.         } else {
  44.             printError(ctx.getText() + " must be declared before use", ctx.getStart());
  45.         }
  46.  
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement