Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //implements statement checks
- @Override
- public void exitAssignStatement(DecafParser.AssignStatementContext ctx) {
- String symbolName = ctx.location().ID().getText();
- Symbol symbol = this.currentScope.resolve(symbolName);
- String operator = ctx.assign_op().getText();
- Symbol expr = annotations.get(ctx.expr());
- if (symbol != null) {
- if (ctx.location().expr() != null && this.currentScope.resolve(symbolName) instanceof Location) {
- Integer locationIndex = null;
- try {
- locationIndex = Integer.parseInt(ctx.location().expr().getText());
- } catch (NumberFormatException e) {
- }
- Location location = (Location) this.currentScope.resolve(symbolName);
- if (location != null && locationIndex != null) {
- if (locationIndex > location.Size) {
- printError(location.ID + " called with incorrect index", ctx.getStart());
- printError("Expected index to be <= " + location.Size + " recieved " + locationIndex, ctx.getStart());
- }
- }
- }
- if (operator.equals("+=") || operator.equals("-=") || operator.equals("=")) {
- if (expr != null) {
- if (!expr.Type.equals("int") || !symbol.Type.equals("int")) {
- printError("Invalid types for operand " + operator, ctx.getStart());
- printError("Found " + expr.Type + " + " + symbol.Type + " expected int + int", ctx.getStart());
- }
- }
- } else //ensure we catch other types such as boolean = boolean
- {
- if (expr != null) {
- if (!expr.Type.equals(symbol.Type)) {
- printError("Invalid expression assignment type", ctx.getStart());
- printError("Recieved " + expr.Type + " Expected " + symbol.Type, ctx.getStart());
- }
- }
- }
- } else {
- printError(ctx.getText() + " must be declared before use", ctx.getStart());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement