Merge branch 'master' into 1.17
This commit is contained in:
commit
769e38021c
9 files changed
+61
-21
No files matched your search
@@ -2,6 +2,7 @@ package carpet.script;
|
||||
|
||||
import carpet.CarpetServer;
|
||||
import carpet.script.api.Auxiliary;
|
||||
import carpet.script.argument.FunctionArgument;
|
||||
import carpet.script.bundled.Module;
|
||||
import carpet.script.command.CommandArgument;
|
||||
import carpet.script.exception.CarpetExpressionException;
|
||||
@@ -161,6 +162,26 @@ public class CarpetScriptHost extends ScriptHost
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean readCommands()
|
||||
{
|
||||
Value commands = appConfig.get(StringValue.of("commands"));
|
||||
if (commands == null) return true;
|
||||
if (!(commands instanceof MapValue))
|
||||
throw new InternalExpressionException("'commands' element in config should be a map");
|
||||
for (Map.Entry<Value, Value> commandsData : ((MapValue)commands).getMap().entrySet())
|
||||
{
|
||||
String path = commandsData.getKey().getString();
|
||||
FunctionArgument<Value> funSpec = FunctionArgument.fromCommandSpec(this, commandsData.getValue());
|
||||
//if (fun)
|
||||
|
||||
//... WIP
|
||||
|
||||
//Map<String, Value> specData = ((MapValue) spec).getMap().entrySet().stream().collect(Collectors.toMap(e -> e.getKey().getString(), Map.Entry::getValue));
|
||||
//appArgTypes.put(argument, CommandArgument.buildFromConfig(argument, specData));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Module getModuleOrLibraryByName(String name)
|
||||
{
|
||||
|
||||
@@ -642,12 +642,12 @@ public class Auxiliary {
|
||||
throw new InternalExpressionException("'schedule' should have at least 2 arguments, delay and call name");
|
||||
long delay = NumericValue.asNumber(lv.get(0).evalValue(c)).getLong();
|
||||
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, true);
|
||||
FunctionArgument<LazyValue> functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, true);
|
||||
|
||||
CarpetServer.scriptServer.events.scheduleCall(
|
||||
(CarpetContext) c,
|
||||
functionArgument.function,
|
||||
functionArgument.resolveArgs(c, t),
|
||||
FunctionValue.resolveArgs(functionArgument.args, c, t),
|
||||
delay
|
||||
);
|
||||
return (c_, t_) -> Value.TRUE;
|
||||
@@ -853,7 +853,7 @@ public class Auxiliary {
|
||||
if (lv.size() < 2)
|
||||
throw new InternalExpressionException("'handle_event' requires at least two arguments, event name, and a callback");
|
||||
String event = lv.get(0).evalValue(c).getString();
|
||||
FunctionArgument callback = FunctionArgument.findIn(c, expression.module, lv, 1, true, false);
|
||||
FunctionArgument<LazyValue> callback = FunctionArgument.findIn(c, expression.module, lv, 1, true, false);
|
||||
CarpetScriptHost host = ((CarpetScriptHost)c.host);
|
||||
Value success;
|
||||
if (callback.function == null)
|
||||
|
||||
@@ -276,7 +276,7 @@ public class Entities {
|
||||
: Collections.singletonList(entityValue.getString());
|
||||
Set<EntityType<? extends Entity>> types = new HashSet<>();
|
||||
descriptors.forEach(s -> types.addAll(EntityValue.getEntityDescriptor(s).typeList));
|
||||
FunctionArgument funArg = FunctionArgument.findIn(c, expression.module, lv, 1, true, false);
|
||||
FunctionArgument<LazyValue> funArg = FunctionArgument.findIn(c, expression.module, lv, 1, true, false);
|
||||
CarpetEventServer events = ((CarpetScriptHost)c.host).getScriptServer().events;
|
||||
if (funArg.function == null)
|
||||
{
|
||||
@@ -300,7 +300,7 @@ public class Entities {
|
||||
throw new InternalExpressionException("First argument to entity_event should be an entity");
|
||||
String what = lv.get(1).evalValue(c).getString();
|
||||
|
||||
FunctionArgument funArg = FunctionArgument.findIn(c, expression.module, lv, 2, true, false);
|
||||
FunctionArgument<LazyValue> funArg = FunctionArgument.findIn(c, expression.module, lv, 2, true, false);
|
||||
|
||||
((EntityValue) v).setEvent((CarpetContext)c, what, funArg.function, FunctionValue.resolveArgs(funArg.args, c, t));
|
||||
|
||||
|
||||
@@ -2,21 +2,23 @@ package carpet.script.argument;
|
||||
|
||||
import carpet.script.Context;
|
||||
import carpet.script.LazyValue;
|
||||
import carpet.script.ScriptHost;
|
||||
import carpet.script.bundled.Module;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.value.FunctionValue;
|
||||
import carpet.script.value.ListValue;
|
||||
import carpet.script.value.Value;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class FunctionArgument extends Argument
|
||||
public class FunctionArgument<T> extends Argument
|
||||
{
|
||||
public FunctionValue function;
|
||||
public List<LazyValue> args;
|
||||
public List<T> args;
|
||||
|
||||
private FunctionArgument(FunctionValue function, int offset, List<LazyValue> args)
|
||||
private FunctionArgument(FunctionValue function, int offset, List<T> args)
|
||||
{
|
||||
super(offset);
|
||||
this.function = function;
|
||||
@@ -34,7 +36,7 @@ public class FunctionArgument extends Argument
|
||||
* if the number of supplied arguments is right
|
||||
* @return argument data
|
||||
*/
|
||||
public static FunctionArgument findIn(
|
||||
public static FunctionArgument<LazyValue> findIn(
|
||||
Context c,
|
||||
Module module,
|
||||
List<LazyValue> params,
|
||||
@@ -45,7 +47,7 @@ public class FunctionArgument extends Argument
|
||||
Value functionValue = params.get(offset).evalValue(c);
|
||||
if (functionValue.isNull())
|
||||
{
|
||||
if (allowNone) return new FunctionArgument(null, offset+1, Collections.emptyList());
|
||||
if (allowNone) return new FunctionArgument<>(null, offset+1, Collections.emptyList());
|
||||
throw new InternalExpressionException("function argument cannot be null");
|
||||
}
|
||||
if (!(functionValue instanceof FunctionValue))
|
||||
@@ -65,13 +67,29 @@ public class FunctionArgument extends Argument
|
||||
}
|
||||
List<LazyValue> lvargs = new ArrayList<>();
|
||||
for (int i = offset+1, mx = params.size(); i < mx; i++) lvargs.add(params.get(i));
|
||||
return new FunctionArgument(fun, offset+1+argsize, lvargs);
|
||||
return new FunctionArgument<>(fun, offset + 1 + argsize, lvargs);
|
||||
}
|
||||
|
||||
public List<Value> resolveArgs(Context context, Integer type)
|
||||
public static FunctionArgument<Value> fromCommandSpec(ScriptHost host, Value funSpec)
|
||||
{
|
||||
List<Value> res = new ArrayList<>(args.size());
|
||||
for (LazyValue arg : args) res.add(arg.evalValue(context, type));
|
||||
return res;
|
||||
FunctionValue function;
|
||||
List<Value> args = Collections.emptyList();
|
||||
if (!(funSpec instanceof ListValue))
|
||||
funSpec = ListValue.of(funSpec);
|
||||
List<Value> params = ((ListValue) funSpec).getItems();
|
||||
if (params.isEmpty()) throw new InternalExpressionException("Function has empty spec");
|
||||
Value first = params.get(0);
|
||||
if (first instanceof FunctionValue)
|
||||
{
|
||||
function = (FunctionValue)first;
|
||||
}
|
||||
else
|
||||
{
|
||||
String name = funSpec.getString();
|
||||
function = host.getAssertFunction(host.main, name);
|
||||
}
|
||||
if (params.size() > 1) args = params.subList(1,params.size());
|
||||
|
||||
return new FunctionArgument<>(function, 0, args);
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ public class Functions {
|
||||
//lv.remove(lv.size()-1); // aint gonna cut it // maybe it will because of the eager eval changes
|
||||
if (t != Context.SIGNATURE) // just call the function
|
||||
{
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true);
|
||||
FunctionArgument<LazyValue> functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true);
|
||||
FunctionValue fun = functionArgument.function;
|
||||
Value retval = fun.callInContext(expr, c, t, fun.getExpression(), fun.getToken(), functionArgument.args).evalValue(c);
|
||||
return (cc, tt) -> retval; ///!!!! dono might need to store expr and token in statics? (e? t?)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package carpet.script.language;
|
||||
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.LazyValue;
|
||||
import carpet.script.argument.FunctionArgument;
|
||||
import carpet.script.exception.ExitStatement;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
@@ -17,7 +18,7 @@ public class Threading
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true);
|
||||
FunctionArgument<LazyValue> functionArgument = FunctionArgument.findIn(c, expression.module, lv, 0, false, true);
|
||||
ThreadValue thread = new ThreadValue(Value.NULL, functionArgument.function, expr, tok, c, FunctionValue.resolveArgs(functionArgument.args, c, t));
|
||||
Thread.yield();
|
||||
return (cc, tt) -> thread;
|
||||
@@ -28,7 +29,7 @@ public class Threading
|
||||
if (lv.size() < 2)
|
||||
throw new InternalExpressionException("'task' requires at least function to call as a parameter");
|
||||
Value queue = lv.get(0).evalValue(c);
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, true);
|
||||
FunctionArgument<LazyValue> functionArgument = FunctionArgument.findIn(c, expression.module, lv, 1, false, true);
|
||||
ThreadValue thread = new ThreadValue(queue, functionArgument.function, expr, tok, c, FunctionValue.resolveArgs(functionArgument.args, c, t));
|
||||
Thread.yield();
|
||||
return (cc, tt) -> thread;
|
||||
|
||||
Reference in new issue
Block a user