summaryrefslogtreecommitdiff
path: root/Shenanigans/src/org/archphantom/shenanigans/elements/expressions/Variable.java
blob: 4251286e3dd7f4ae979cbbc8c864806bb7a0b404 (plain)
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
package org.archphantom.shenanigans.elements.expressions;
import org.archphantom.shenanigans.elements.Program;
import org.archphantom.shenanigans.elements.values.Namespace;
import org.archphantom.shenanigans.elements.values.Value;
import org.archphantom.shenanigans.elements.variables.VarTable;

public class Variable extends Expression {
	private String identifier;
	
	public Variable (String identifier) {
		this.identifier = identifier;
	}
	
	public Value evaluate (VarTable vars, Program program) {
		if (identifier.contains(".")) {
			String nspacecall = identifier.substring(0, identifier.indexOf('.'));
			String varname = identifier.substring(identifier.indexOf('.')+1);
			Namespace ns;
			if ((ns = program.getNamespace(nspacecall)) != null) {
				return ns.getVar(varname, vars, program);
			} else {
				throw new RuntimeException("No such variable: "+identifier);
			}
		} else {
			Value val;
			if ((val = vars.get(identifier)) != null) {
				return val;
			} else {
				throw new RuntimeException("No such variable: "+identifier);
			}
		}
	}
	
}