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;
}
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);
}
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;
}
Aggregations