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 012public final class WhenClause<T extends GoloElement> extends GoloElement { 013 private ExpressionStatement condition; 014 private T action; 015 016 WhenClause(ExpressionStatement condition, T action) { 017 this.condition = condition; 018 makeParentOf(condition); 019 setAction(action); 020 } 021 022 public ExpressionStatement condition() { return this.condition; } 023 024 public T action() { return this.action; } 025 026 public void setAction(T a) { 027 this.action = a; 028 makeParentOf(a); 029 } 030 031 @Override 032 public String toString() { 033 return String.format("when %s then %s", condition, action); 034 } 035 036 @Override 037 protected void replaceElement(GoloElement original, GoloElement newElement) { 038 if (condition.equals(original)) { 039 if (!(newElement instanceof ExpressionStatement)) { 040 throw cantConvert("ExpressionStatement", newElement); 041 } 042 this.condition = (ExpressionStatement) newElement; 043 makeParentOf(this.condition); 044 } else if (action.equals(original)) { 045 @SuppressWarnings("unchecked") 046 T element = (T) newElement; 047 setAction(element); 048 } else { 049 throw doesNotContain(original); 050 } 051 } 052 053 @Override 054 public void accept(GoloIrVisitor visitor) { 055 visitor.visitWhenClause(this); 056 } 057 058 @Override 059 public void walk(GoloIrVisitor visitor) { 060 this.condition.accept(visitor); 061 this.action.accept(visitor); 062 } 063} 064 065