use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class IsgamemodeCommandTest method runIsgamemode.
/**
* Run isgamemode.
*
* @param stack the stack
*
* @return true, if successful
*/
private static boolean runIsgamemode(final Stack stack) {
final PostfixMathCommandI pCommand = new IsgamemodeCommand();
boolean b;
try {
pCommand.run(stack);
b = true;
} catch (ParseException e) {
b = false;
}
return b;
}
use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class ClassLevelCommand method run.
/**
* Runs classlevel on the inStack. The parameter is popped
* off the {@code inStack}, and the variable's value is
* pushed back to the top of {@code inStack}.
* @param inStack the jep stack
* @throws ParseException
*/
//Uses JEP, which doesn't use generics
@SuppressWarnings("unchecked")
@Override
public void run(final Stack inStack) throws ParseException {
// check the stack
checkStack(inStack);
// get the parameter from the stack
int paramCount = curNumberOfParameters;
String applied = null;
String className = null;
if (paramCount > 2) {
throw new ParseException("Invalid number of parameters");
}
if (paramCount >= 2) {
String p2 = inStack.pop().toString();
if (p2.startsWith("APPLIEDAS=")) {
applied = p2.substring(10);
}
}
if (paramCount >= 1) {
String p1 = inStack.pop().toString();
if (p1.startsWith("APPLIEDAS=")) {
if (applied != null) {
throw new ParseException("Formula had two APPLIEDAS= entries");
}
applied = p1.substring(10);
} else {
//Should be a class name
className = p1;
}
}
/*
* If there was no parameter showing the class, and this is used in a
* CLASS file, then use the class name
*/
if (className == null) {
String src = variableSource;
if (src.startsWith("CLASS:")) {
className = src.substring(6);
}
}
if (className == null) {
throw new ParseException("Unable to determine class name");
}
PlayerCharacter pc = getPC();
String cl = className;
if (applied != null) {
if ("NONEPIC".equalsIgnoreCase(applied)) {
GameMode mode = SettingsHandler.getGame();
//Add 1 since game mode is inclusive, but BEFORELEVEL is not!
int limit = mode.getMaxNonEpicLevel() + 1;
if (limit == Integer.MAX_VALUE) {
throw new ParseException("Game Mode has no EPIC limit");
}
cl += ";BEFORELEVEL=" + limit;
} else {
throw new ParseException("Did not understand APPLIEDAS=" + applied);
}
}
Double result = new Double(pc.getClassLevelString(cl, false));
inStack.push(result);
}
use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class GetVarCommand method run.
/**
* Runs getvar on the inStack. The parameter is popped
* off the {@code inStack}, and the variable's value is
* pushed back to the top of {@code inStack}.
* @param inStack the jep stack
* @throws ParseException
*/
//Uses JEP, which doesn't use generics
@SuppressWarnings("unchecked")
@Override
public void run(final Stack inStack) throws ParseException {
// check the stack
checkStack(inStack);
// get the parameter from the stack
final Object param1;
Object param2 = null;
//
if (curNumberOfParameters == 1) {
param1 = inStack.pop();
} else if (curNumberOfParameters == 2) {
param2 = inStack.pop();
param1 = inStack.pop();
if (!(param2 instanceof Double)) {
throw new ParseException("Invalid parameter type");
}
} else {
throw new ParseException("Invalid parameter count");
}
if (param1 instanceof String) {
Float result = null;
if (parent instanceof PlayerCharacter) {
final PlayerCharacter character = (PlayerCharacter) parent;
result = getVariableForCharacter(character, param1);
} else if (parent instanceof Equipment) {
boolean bPrimary = true;
if (param2 != null) {
bPrimary = (((Double) param2).intValue() != 0);
}
result = ((Equipment) parent).getVariableValue((String) param1, "", bPrimary, null);
} else if (parent instanceof VariableProcessorPC) {
final VariableProcessorPC vpc = (VariableProcessorPC) parent;
// check to see if this is just a variable
result = vpc.lookupVariable((String) param1, variableSource, null);
if (result == null) {
result = vpc.getVariableValue(null, (String) param1, variableSource, 0);
}
} else if (parent instanceof VariableProcessorEq) {
VariableProcessorEq veq = (VariableProcessorEq) parent;
result = veq.getVariableValue(null, (String) param1, variableSource, 0);
} else if (parent == null) {
Logging.errorPrint("Ignored request for var " + String.valueOf(param1) + " with no parent.");
}
if (result == null) {
throw new ParseException("Error retreiving variable:" + param1);
}
inStack.push(result.doubleValue());
} else {
throw new ParseException("Invalid parameter type");
}
}
use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class IfCommand method run.
/**
* <p>
* Evaluates the three parameters. The first may be a subclass of
* Number, or a Boolean. The second and third can be any supported type.
* If the first argument is true, the second argument is returned;
* otherwise, the third argument is returned.
* </p>
*
* @param stack
* Stack of incoming arguments.
* @throws ParseException
*/
//Uses JEP, which doesn't use generics
@SuppressWarnings("unchecked")
@Override
public void run(final Stack stack) throws ParseException {
// Check if stack is null
if (null == stack) {
throw new ParseException("Stack argument null");
}
final boolean condition;
final Object param3 = stack.pop();
final Object param2 = stack.pop();
final Object param1 = stack.pop();
if (param1 instanceof Number) {
condition = (((Number) param1).doubleValue() != 0.0d);
} else if (param1 instanceof Boolean) {
condition = (Boolean) param1;
} else {
throw new ParseException("Invalid parameter type for Parameter 1");
}
// push the result on the inStack
stack.push(condition ? param2 : param3);
}
use of org.nfunk.jep.ParseException in project pcgen by PCGen.
the class MasterVarCommand method run.
/**
* Runs mastervar on the inStack. The parameter is popped
* off the {@code inStack}, and the variable's value is
* pushed back to the top of {@code inStack}.
* @param inStack the jep stack
* @throws ParseException
*/
//Uses JEP, which doesn't use generics
@SuppressWarnings("unchecked")
@Override
public void run(final Stack inStack) throws ParseException {
// check the stack
checkStack(inStack);
// get the parameter from the stack
final Object param1;
if (curNumberOfParameters == 1) {
param1 = inStack.pop();
} else {
throw new ParseException("Invalid parameter count");
}
if (param1 instanceof String) {
Float result = null;
PlayerCharacter pc = getPC();
if (pc == null) {
throw new ParseException("Invalid parent for function");
}
PlayerCharacter master = pc.getMasterPC();
if (master == null) {
throw new ParseException("Invalid: PC had no master");
}
result = master.getVariableValue((String) param1, variableSource);
if (result == null) {
throw new ParseException("Error retreiving variable:" + param1);
}
inStack.push(result.doubleValue());
} else {
throw new ParseException("Invalid parameter type");
}
}
Aggregations