Search in sources :

Example 41 with Construct

use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.

the class Script method seval.

/**
 * Runs eval on the code tree, and if it returns an ival, resolves it.
 *
 * @param c
 * @param env
 * @return
 */
public Construct seval(ParseTree c, final Environment env) {
    Construct ret = eval(c, env);
    while (ret instanceof IVariable) {
        IVariable cur = (IVariable) ret;
        ret = env.getEnv(GlobalEnv.class).GetVarList().get(cur.getVariableName(), cur.getTarget()).ival();
    }
    return ret;
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) Construct(com.laytonsmith.core.constructs.Construct)

Example 42 with Construct

use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.

the class Script method checkAmbiguous.

public void checkAmbiguous(List<Script> scripts) throws ConfigCompileException {
    List<Construct> thisCommand = this.cleft;
    for (Script script : scripts) {
        List<Construct> thatCommand = script.cleft;
        if (thatCommand == null) {
            // It hasn't been compiled yet.
            return;
        }
        if (this.cleft == script.cleft) {
            // Of course this command is going to match its own signature.
            continue;
        }
        matchScope: {
            for (int k = 0; k < thisCommand.size(); k++) {
                Construct c1 = thisCommand.get(k);
                if (k < thatCommand.size()) {
                    Construct c2 = thatCommand.get(k);
                    // the same argument position.
                    if (c1.getCType() == c2.getCType() && (c1.getCType() == ConstructType.STRING || c1.getCType() == ConstructType.COMMAND)) {
                        if (c1.nval() != c2.nval() && (c1.nval() == null || !c1.nval().equals(c2.nval()))) {
                            break matchScope;
                        }
                    }
                } else {
                    // after the last Construct in thatCommand.
                    if (!(c1 instanceof Variable) || (c1 instanceof Variable && !((Variable) c1).isOptional())) {
                        break matchScope;
                    } else {
                        // There is no need to loop over later Constructs, the commands are ambigous.
                        break;
                    }
                }
            }
            if (thatCommand.size() > thisCommand.size()) {
                // thisCommand is shorter than thatCommand.
                // Commands are not ambigous if thatCommand contains a non-variable or a non-optional variable
                // after the last Construct in thisCommand.
                Construct c2 = thatCommand.get(thisCommand.size());
                if (!(c2 instanceof Variable) || (c2 instanceof Variable && !((Variable) c2).isOptional())) {
                    break matchScope;
                }
            }
            // The signature of thisCommand and thatCommand are ambigous. Throw a compile exception.
            String commandThis = "";
            for (Construct c : thisCommand) {
                commandThis += c.val() + " ";
            }
            String commandThat = "";
            for (Construct c : thatCommand) {
                commandThat += c.val() + " ";
            }
            script.compilerError = true;
            this.compilerError = true;
            throw new ConfigCompileException("The command " + commandThis.trim() + " is ambiguous because it " + "matches the signature of " + commandThat.trim() + " defined at " + thatCommand.get(0).getTarget(), thisCommand.get(0).getTarget());
        }
    }
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) Construct(com.laytonsmith.core.constructs.Construct) CString(com.laytonsmith.core.constructs.CString) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint)

Aggregations

Construct (com.laytonsmith.core.constructs.Construct)42 CString (com.laytonsmith.core.constructs.CString)25 CArray (com.laytonsmith.core.constructs.CArray)23 IVariable (com.laytonsmith.core.constructs.IVariable)14 CInt (com.laytonsmith.core.constructs.CInt)11 CNull (com.laytonsmith.core.constructs.CNull)11 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)10 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)9 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Variable (com.laytonsmith.core.constructs.Variable)8 CDouble (com.laytonsmith.core.constructs.CDouble)7 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)7 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)7 CBoolean (com.laytonsmith.core.constructs.CBoolean)6 Target (com.laytonsmith.core.constructs.Target)6 Environment (com.laytonsmith.core.environments.Environment)6 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)6 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)6