PageRenderTime 391ms CodeModel.GetById 202ms app.highlight 5ms RepoModel.GetById 176ms app.codeStats 7ms

/src/org/ooc/frontend/model/Ternary.java

http://github.com/nddrylliog/ooc
Java | 78 lines | 58 code | 15 blank | 5 comment | 6 complexity | c58e6546eef6a3e0282f9ef2d89159a3 MD5 | raw file
 1package org.ooc.frontend.model;
 2
 3import java.io.IOException;
 4
 5import org.ooc.frontend.Visitor;
 6import org.ooc.frontend.model.tokens.Token;
 7
 8/**
 9 * condition ? valueIfTrue else valueIfFalse 
10 * 
11 * @author Amos Wenger
12 */
13public class Ternary extends Expression {
14
15	public Ternary(Token startToken, Expression condition, Expression valueIfTrue, Expression valueIfFalse) {
16		super(startToken);
17		this.condition = condition;
18		this.valueIfTrue = valueIfTrue;
19		this.valueIfFalse = valueIfFalse;
20	}
21
22	private Expression condition;
23	private Expression valueIfTrue;
24	private Expression valueIfFalse;
25	
26	@Override
27	public boolean replace(Node oldie, Node kiddo) {
28		if(oldie == condition) {
29			condition = (Expression) kiddo;
30			return true;
31		}
32		if(oldie == valueIfTrue) {
33			valueIfTrue = (Expression) kiddo;
34			return true;
35		}
36		if(oldie == valueIfFalse) {
37			valueIfFalse = (Expression) kiddo;
38			return true;
39		}
40		return false;
41	}
42
43	public Type getType() {
44		return valueIfTrue.getType();
45	}
46
47	public void accept(Visitor visitor) throws IOException {
48		visitor.visit(this);
49	}
50
51	public void acceptChildren(Visitor visitor) throws IOException {
52		condition.accept(visitor);
53		valueIfTrue.accept(visitor);
54		valueIfFalse.accept(visitor);
55	}
56
57	public boolean hasChildren() {
58		return true;
59	}
60	
61	public Expression getCondition() {
62		return condition;
63	}
64	
65	public Expression getValueIfTrue() {
66		return valueIfTrue;
67	}
68	
69	public Expression getValueIfFalse() {
70		return valueIfFalse;
71	}
72	
73	@Override
74	public String toString() {
75		return condition+" ? "+valueIfTrue+" : "+valueIfFalse;
76	}
77
78}