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 java.util.List; 013import java.util.LinkedList; 014import org.eclipse.golo.compiler.parser.GoloASTNode; 015 016public final class CollectionLiteral extends ExpressionStatement { 017 018 public static enum Type { 019 array, list, set, map, tuple, vector, range 020 } 021 022 private final Type type; 023 private final List<ExpressionStatement> expressions = new LinkedList<>(); 024 025 CollectionLiteral(Type type) { 026 super(); 027 this.type = type; 028 } 029 030 @Override 031 public CollectionLiteral ofAST(GoloASTNode node) { 032 super.ofAST(node); 033 return this; 034 } 035 036 public CollectionLiteral add(Object expression) { 037 ExpressionStatement expr = ExpressionStatement.of(expression); 038 this.expressions.add(expr); 039 makeParentOf(expr); 040 return this; 041 } 042 043 public Type getType() { 044 return type; 045 } 046 047 public List<ExpressionStatement> getExpressions() { 048 return expressions; 049 } 050 051 @Override 052 public String toString() { 053 return this.type.toString() + this.expressions.toString(); 054 } 055 056 @Override 057 public void accept(GoloIrVisitor visitor) { 058 visitor.visitCollectionLiteral(this); 059 } 060 061 @Override 062 public void walk(GoloIrVisitor visitor) { 063 for (ExpressionStatement expression : expressions) { 064 expression.accept(visitor); 065 } 066 } 067 068 @Override 069 protected void replaceElement(GoloElement original, GoloElement newElement) { 070 if (expressions.contains(original) && newElement instanceof ExpressionStatement) { 071 expressions.set(expressions.indexOf(original), (ExpressionStatement) newElement); 072 } else { 073 throw cantReplace(original, newElement); 074 } 075 } 076}