PageRenderTime 223ms CodeModel.GetById 121ms app.highlight 7ms RepoModel.GetById 93ms app.codeStats 0ms

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

http://github.com/nddrylliog/ooc
Java | 95 lines | 72 code | 23 blank | 0 comment | 5 complexity | aa0099db962ca299463762d7e395cdb6 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;
 7import org.ooc.middle.hobgoblins.Resolver;
 8
 9public class RangeLiteral extends Literal {
10
11	protected Expression lower;
12	protected Expression upper;
13	protected static Type type = new Type("Range", Token.defaultToken);
14	
15	public RangeLiteral(Expression lower, Expression upper, Token startToken) {
16		super(startToken);
17		this.lower = lower;
18		this.upper = upper;
19	}
20	
21	public Expression getLower() {
22		return lower;
23	}
24	
25	public void setLower(Expression lower) {
26		this.lower = lower;
27	}
28	
29	public Expression getUpper() {
30		return upper;
31	}
32	
33	public void setUpper(Expression upper) {
34		this.upper = upper;
35	}
36
37	public Type getType() {
38		return type;
39	}
40	
41	public void accept(Visitor visitor) throws IOException {
42		visitor.visit(this);
43	}
44	
45	public boolean hasChildren() {
46		return true;
47	}
48	
49	public void acceptChildren(Visitor visitor) throws IOException {
50		lower.accept(visitor);
51		upper.accept(visitor);
52	}
53	
54	@Override
55	public boolean replace(Node oldie, Node kiddo) {
56		
57		if(oldie == lower) {
58			lower = (Expression) kiddo;
59			return true;
60		}
61		
62		if(oldie == upper) {
63			upper = (Expression) kiddo;
64			return true;
65		}
66		
67		return false;
68		
69	}
70
71	@Override
72	public boolean isResolved() {
73		return false;
74	}
75
76	@Override
77	public Response resolve(NodeList<Node> stack, Resolver res, boolean fatal) {
78		Node parent = stack.peek();
79		if(!(parent instanceof Foreach)) {
80			MemberCall newCall = new MemberCall(new VariableAccess("Range", startToken), "new", null, startToken);
81			newCall.getArguments().add(lower);
82			newCall.getArguments().add(upper);
83			parent.replace(this, newCall);
84			return Response.RESTART;
85		}
86			
87		return Response.OK;
88	}
89	
90	@Override
91	public String toString() {
92		return getClass().getSimpleName()+":"+lower+".."+upper;
93	}
94
95}