package calculator.interpreter.ast.vars;
import net.sf.etl.parsers.LiteralUtils;
import calculator.interpreter.Environment;
import calculator.interpreter.EvalException;
import calculator.interpreter.ast.Expression;
/** The name expression (refers to some variable) */
public class Name extends Expression {
/** The name to resolve (identifier form), use {@link #name()} instead. */
public String literal;
/** The name to resolve (quoted form), use {@link #name()} instead. */
public String quoted;
/** The parsed name */
private String parsed;
/**
* @return the parsed name
*/
public String name() {
if (parsed == null) {
assert (literal != null) != (quoted != null) : "Exactly one form "
+ "should be specified: q=" + quoted + " l=" + literal;
if (quoted != null) {
try {
parsed = LiteralUtils.parseString(quoted);
} catch (Exception e) {
throw new EvalException(this, "The name is invalid format: " + quoted);
}
} else {
parsed = literal;
}
}
return parsed;
}
@Override
public Object eval(Environment env) {
return env.findDefined(location, name()).get(this);
}
}