Search in sources :

Example 1 with IVariableList

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

the class Procedure method execute.

/**
 * Executes this procedure, with the arguments that were passed in
 *
 * @param args
 * @param env
 * @param t
 * @return
 */
public Construct execute(List<Construct> args, Environment env, Target t) {
    env.getEnv(GlobalEnv.class).SetVarList(new IVariableList());
    // This is what will become our @arguments var
    CArray arguments = new CArray(Target.UNKNOWN);
    for (String key : originals.keySet()) {
        Construct c = originals.get(key);
        env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(Auto.TYPE, key, c, Target.UNKNOWN));
        arguments.push(c, t);
    }
    // new Script(null, null);
    Script fakeScript = Script.GenerateScript(tree, env.getEnv(GlobalEnv.class).GetLabel());
    for (int i = 0; i < args.size(); i++) {
        Construct c = args.get(i);
        arguments.set(i, c, t);
        if (varIndex.size() > i) {
            String varname = varIndex.get(i).getVariableName();
            if (c instanceof CNull || InstanceofUtil.isInstanceof(c, varIndex.get(i).getDefinedType()) || varIndex.get(i).getDefinedType().equals(Auto.TYPE)) {
                env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(varIndex.get(i).getDefinedType(), varname, c, c.getTarget()));
            } else {
                throw new CRECastException("Procedure \"" + name + "\" expects a value of type " + varIndex.get(i).getDefinedType().val() + " in argument " + (i + 1) + ", but" + " a value of type " + c.typeof() + " was found instead.", c.getTarget());
            }
        }
    }
    env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(CArray.TYPE, "@arguments", arguments, Target.UNKNOWN));
    StackTraceManager stManager = env.getEnv(GlobalEnv.class).GetStackTraceManager();
    stManager.addStackTraceElement(new ConfigRuntimeException.StackTraceElement("proc " + name, getTarget()));
    try {
        if (tree.getData() instanceof CFunction && "sconcat".equals(tree.getData().val())) {
            // compiler proper.
            for (ParseTree child : tree.getChildren()) {
                fakeScript.eval(child, env);
            }
        } else {
            fakeScript.eval(tree, env);
        }
    } catch (FunctionReturnException e) {
        // Normal exit
        stManager.popStackTraceElement();
        Construct ret = e.getReturn();
        if (!InstanceofUtil.isInstanceof(ret, returnType)) {
            throw new CRECastException("Expected procedure \"" + name + "\" to return a value of type " + returnType.val() + " but a value of type " + ret.typeof() + " was returned instead", ret.getTarget());
        }
        return ret;
    } catch (LoopManipulationException ex) {
        // Not exactly normal, but pop anyways
        stManager.popStackTraceElement();
        // a compile error.
        throw ConfigRuntimeException.CreateUncatchableException("Loop manipulation operations (e.g. break() or continue()) cannot" + " bubble up past procedures.", t);
    } catch (ConfigRuntimeException e) {
        if (e instanceof AbstractCREException) {
            ((AbstractCREException) e).freezeStackTraceElements(stManager);
        }
        stManager.popStackTraceElement();
        throw e;
    } catch (Throwable th) {
        // Not sure. Pop, but rethrow
        stManager.popStackTraceElement();
        throw th;
    }
    // Normal exit, but no return.
    stManager.popStackTraceElement();
    // If we got here, then there was no return value. This is fine, but only for returnType void or auto.
    if (!(returnType.equals(Auto.TYPE) || returnType.equals(CVoid.TYPE))) {
        throw new CRECastException("Expecting procedure \"" + name + "\" to return a value of type " + returnType.val() + "," + " but no value was returned.", tree.getTarget());
    }
    return CVoid.VOID;
}
Also used : AbstractCREException(com.laytonsmith.core.exceptions.CRE.AbstractCREException) CRECastException(com.laytonsmith.core.exceptions.CRE.CRECastException) IVariable(com.laytonsmith.core.constructs.IVariable) IVariableList(com.laytonsmith.core.constructs.IVariableList) CArray(com.laytonsmith.core.constructs.CArray) CFunction(com.laytonsmith.core.constructs.CFunction) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) StackTraceManager(com.laytonsmith.core.exceptions.StackTraceManager) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) LoopManipulationException(com.laytonsmith.core.exceptions.LoopManipulationException) CNull(com.laytonsmith.core.constructs.CNull)

Example 2 with IVariableList

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

the class MathTest method setUp.

@Before
public void setUp() throws Exception {
    StaticTest.InstallFakeServerFrontend();
    fakePlayer = GetOnlinePlayer();
    fakeServer = GetFakeServer();
    varList = new IVariableList();
    varList.set(new IVariable(Auto.TYPE, "var", C.onstruct(1), Target.UNKNOWN));
    varList.set(new IVariable(Auto.TYPE, "var2", C.onstruct(2.5), Target.UNKNOWN));
    env = Static.GenerateStandaloneEnvironment();
    env.getEnv(GlobalEnv.class).SetVarList(varList);
    env.getEnv(CommandHelperEnvironment.class).SetPlayer(fakePlayer);
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) IVariableList(com.laytonsmith.core.constructs.IVariableList) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) Before(org.junit.Before)

Example 3 with IVariableList

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

the class CompositeFunction method exec.

@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
    ParseTree tree;
    if (!cachedScripts.containsKey(this.getClass())) {
        try {
            String script = script();
            tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, true)).getChildAt(0);
        } catch (ConfigCompileException | ConfigCompileGroupException ex) {
            // This is really bad.
            throw new Error(ex);
        }
        if (cacheCompile()) {
            cachedScripts.put(this.getClass(), tree);
        }
    } else {
        tree = cachedScripts.get(this.getClass());
    }
    GlobalEnv env = environment.getEnv(GlobalEnv.class);
    IVariableList oldVariables = env.GetVarList();
    IVariableList newVariables = new IVariableList();
    newVariables.set(new IVariable(CClassType.get("array"), "@arguments", new CArray(t, args.length, args), t));
    env.SetVarList(newVariables);
    Construct ret = CVoid.VOID;
    try {
        env.GetScript().eval(tree, environment);
    } catch (FunctionReturnException ex) {
        ret = ex.getReturn();
    }
    env.SetVarList(oldVariables);
    return ret;
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) IVariableList(com.laytonsmith.core.constructs.IVariableList) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Aggregations

IVariable (com.laytonsmith.core.constructs.IVariable)3 IVariableList (com.laytonsmith.core.constructs.IVariableList)3 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)3 CArray (com.laytonsmith.core.constructs.CArray)2 Construct (com.laytonsmith.core.constructs.Construct)2 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)2 ParseTree (com.laytonsmith.core.ParseTree)1 CFunction (com.laytonsmith.core.constructs.CFunction)1 CNull (com.laytonsmith.core.constructs.CNull)1 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)1 AbstractCREException (com.laytonsmith.core.exceptions.CRE.AbstractCREException)1 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)1 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)1 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)1 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)1 LoopManipulationException (com.laytonsmith.core.exceptions.LoopManipulationException)1 StackTraceManager (com.laytonsmith.core.exceptions.StackTraceManager)1 Before (org.junit.Before)1