001/* 002 * Copyright (c) 2012-2017 Institut National des Sciences Appliquées de Lyon (INSA-Lyon) 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 */ 009 010package org.eclipse.golo.compiler.ir; 011 012import org.eclipse.golo.compiler.parser.GoloASTNode; 013 014public final class AssignmentStatement extends GoloStatement { 015 private LocalReference localReference; 016 private ExpressionStatement expressionStatement; 017 private boolean declaring = false; 018 019 AssignmentStatement() { super(); } 020 021 /** 022 * @inheritDoc 023 */ 024 @Override 025 public AssignmentStatement ofAST(GoloASTNode node) { 026 super.ofAST(node); 027 return this; 028 } 029 030 public boolean isDeclaring() { 031 return declaring; 032 } 033 034 public AssignmentStatement declaring() { 035 return declaring(true); 036 } 037 038 public AssignmentStatement declaring(boolean isDeclaring) { 039 this.declaring = isDeclaring; 040 return this; 041 } 042 043 public LocalReference getLocalReference() { 044 return localReference; 045 } 046 047 /** 048 * Defines the reference to assign to. 049 */ 050 public AssignmentStatement to(Object ref) { 051 localReference = (LocalReference) ref; 052 makeParentOf(localReference); 053 return this; 054 } 055 056 public ExpressionStatement getExpressionStatement() { 057 return expressionStatement; 058 } 059 060 /** 061 * Defines the value to be assigned. 062 */ 063 public AssignmentStatement as(Object expr) { 064 expressionStatement = (ExpressionStatement) expr; 065 makeParentOf(expressionStatement); 066 return this; 067 } 068 069 @Override 070 public String toString() { 071 return String.format("%s = %s", localReference, expressionStatement); 072 } 073 074 /** 075 * @inheritDoc 076 */ 077 @Override 078 public void accept(GoloIrVisitor visitor) { 079 visitor.visitAssignmentStatement(this); 080 } 081 082 /** 083 * @inheritDoc 084 */ 085 @Override 086 public void walk(GoloIrVisitor visitor) { 087 expressionStatement.accept(visitor); 088 } 089 090 /** 091 * @inheritDoc 092 */ 093 @Override 094 protected void replaceElement(GoloElement original, GoloElement newElement) { 095 if (original.equals(expressionStatement) && newElement instanceof ExpressionStatement) { 096 as(newElement); 097 } else { 098 throw cantReplace(original, newElement); 099 } 100 } 101}