Search in sources :

Example 11 with ParseException

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;
}
Also used : PostfixMathCommandI(org.nfunk.jep.function.PostfixMathCommandI) ParseException(org.nfunk.jep.ParseException)

Example 12 with ParseException

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);
}
Also used : GameMode(pcgen.core.GameMode) PlayerCharacter(pcgen.core.PlayerCharacter) ParseException(org.nfunk.jep.ParseException)

Example 13 with ParseException

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");
    }
}
Also used : VariableProcessorPC(pcgen.core.VariableProcessorPC) PlayerCharacter(pcgen.core.PlayerCharacter) Equipment(pcgen.core.Equipment) VariableProcessorEq(pcgen.core.VariableProcessorEq) ParseException(org.nfunk.jep.ParseException)

Example 14 with ParseException

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);
}
Also used : ParseException(org.nfunk.jep.ParseException)

Example 15 with ParseException

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");
    }
}
Also used : PlayerCharacter(pcgen.core.PlayerCharacter) ParseException(org.nfunk.jep.ParseException)

Aggregations

ParseException (org.nfunk.jep.ParseException)24 Matcher (java.util.regex.Matcher)12 Test (org.junit.Test)9 PlayerCharacter (pcgen.core.PlayerCharacter)7 Object (java.lang.Object)2 List (java.util.List)2 VariableProcessor (pcgen.core.VariableProcessor)2 JepCountType (pcgen.util.JepCountType)2 Stack (java.util.Stack)1 PostfixMathCommandI (org.nfunk.jep.function.PostfixMathCommandI)1 Equipment (pcgen.core.Equipment)1 GameMode (pcgen.core.GameMode)1 Skill (pcgen.core.Skill)1 VariableProcessorEq (pcgen.core.VariableProcessorEq)1 VariableProcessorPC (pcgen.core.VariableProcessorPC)1