Skip to main content
summaryrefslogtreecommitdiffstats
blob: 464d7e44df29896a47804104922beffe65f696de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

package org.eclipse.internal.xtend.expression.ast;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.xtend.expression.AnalysationIssue;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.ExpressionFacade;
import org.eclipse.xtend.typesystem.Type;

/**
 * @author Sven Efftinge (http://www.efftinge.de)
 * @author Arno Haase
 * @author Bernd Kolb
 */
public class SwitchExpression extends Expression {

	private Expression switchExpr = null;

	private Expression defaultExpr = null;

	private List<Case> cases = null;

	public SwitchExpression(final Expression switchExpr, final List<Case> cases, final Expression defaultExpr) {
		this.switchExpr = switchExpr;
		this.cases = cases;
		this.defaultExpr = defaultExpr;
	}

	@Override
	protected Object evaluateInternal(final ExecutionContext ctx) {
		Object switchVal = Boolean.TRUE;
		if (switchExpr != null) {
			switchVal = switchExpr.evaluate(ctx);
		}
		for (final Iterator<Case> iter = cases.iterator(); iter.hasNext();) {
			final Case c = iter.next();
			final Object caseCondVal = c.getCondition().evaluate(ctx);
			if ((switchVal != null && equals(switchVal, caseCondVal, ctx)) || switchVal == caseCondVal)
				return c.getThenPart().evaluate(ctx);
		}
		return defaultExpr.evaluate(ctx);
	}

	private boolean equals(Object switchVal, final Object caseCondVal, ExecutionContext ctx) {

		Map<String, Object> props = new HashMap<String, Object>();
		props.put("swtch", switchVal);
		props.put("cse", caseCondVal);
		return (Boolean) new ExpressionFacade(ctx).evaluate("swtch==cse", props);
	}

	@Override
	public Type analyzeInternal(final ExecutionContext ctx, final Set<AnalysationIssue> issues) {
		Type condType = ctx.getBooleanType();
		if (switchExpr != null) {
			condType = switchExpr.analyze(ctx, issues);
		}
		if (condType == null)
			return null;
		Type returnType = defaultExpr.analyze(ctx, issues);
		if (returnType == null)
			return null;
		for (final Iterator<Case> iter = cases.iterator(); iter.hasNext();) {
			final Case c = iter.next();
			final Type caseCondType = c.getCondition().analyze(ctx, issues);
			if (caseCondType != null) {
				if (!condType.isAssignableFrom(caseCondType)) {
					issues.add(new AnalysationIssue(AnalysationIssue.INCOMPATIBLE_TYPES, condType.getName()
							+ " expected!", c.getCondition()));
				}
			}
			final Type caseThenType = c.getThenPart().analyze(ctx, issues);
			if (caseThenType != null) {
				if (!returnType.isAssignableFrom(caseThenType)) {
					if (caseThenType.isAssignableFrom(returnType)) {
						returnType = caseThenType;
					}
					else {
						returnType = ctx.getObjectType();
					}
				}
			}
		}
		return returnType;
	}

	public List<Case> getCases() {
		return cases;
	}

	public Expression getDefaultExpr() {
		return defaultExpr;
	}

	public Expression getSwitchExpr() {
		return switchExpr;
	}

	@Override
	protected String toStringInternal() {
		return "switch " + switchExpr.toStringInternal();
	}
}

Back to the top