|
@@ -28,7 +28,7 @@ public class SnuviParser
|
|
protected final ISnuviLogger logger;
|
|
protected final ISnuviLogger logger;
|
|
private final ISnuviScheduler scheduler;
|
|
private final ISnuviScheduler scheduler;
|
|
private final HashMap<String, BiFunction<Object[], Script, Object>> functions;
|
|
private final HashMap<String, BiFunction<Object[], Script, Object>> functions;
|
|
- private boolean printStack;
|
|
|
|
|
|
+ public boolean printStack;
|
|
|
|
|
|
private int idCounter;
|
|
private int idCounter;
|
|
private final HashMap<Integer, Script> scripts;
|
|
private final HashMap<Integer, Script> scripts;
|
|
@@ -101,6 +101,27 @@ public class SnuviParser
|
|
registerFunction("event.isloaded", (args, sc) ->
|
|
registerFunction("event.isloaded", (args, sc) ->
|
|
sc.isLoadedEvent(args[0].toString()));
|
|
sc.isLoadedEvent(args[0].toString()));
|
|
|
|
|
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
|
+ // bit
|
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
|
+
|
|
|
|
+ registerFunction("bit.rightshift", (args, qd) ->
|
|
|
|
+ (double) (getInt(args[0]) >> getInt(args[1])));
|
|
|
|
+ registerFunction("bit.leftshift", (args, qd) ->
|
|
|
|
+ (double) (getInt(args[0]) << getInt(args[1])));
|
|
|
|
+ registerFunction("bit.and", (args, qd) ->
|
|
|
|
+ (double) (getInt(args[0]) & getInt(args[1])));
|
|
|
|
+ registerFunction("bit.or", (args, qd) ->
|
|
|
|
+ (double) (getInt(args[0]) | getInt(args[1])));
|
|
|
|
+ registerFunction("bit.xor", (args, qd) ->
|
|
|
|
+ (double) (getInt(args[0]) ^ getInt(args[1])));
|
|
|
|
+ registerFunction("bit.invert", (args, qd) ->
|
|
|
|
+ (double) (~getInt(args[0])));
|
|
|
|
+ registerFunction("bit.set", (args, qd) ->
|
|
|
|
+ (double) (getInt(args[0]) | (getInt(args[2]) << getInt(args[1]))));
|
|
|
|
+ registerFunction("bit.get", (args, qd) ->
|
|
|
|
+ (double) ((getInt(args[0]) & (1 << getInt(args[1])))) != 0);
|
|
|
|
+
|
|
// -------------------------------------------------------------------------------
|
|
// -------------------------------------------------------------------------------
|
|
// math
|
|
// math
|
|
// -------------------------------------------------------------------------------
|
|
// -------------------------------------------------------------------------------
|
|
@@ -159,7 +180,7 @@ public class SnuviParser
|
|
registerFunction("list.contains", (args, sc) ->
|
|
registerFunction("list.contains", (args, sc) ->
|
|
((List) args[0]).contains(args[1]));
|
|
((List) args[0]).contains(args[1]));
|
|
registerFunction("list.getsize", (args, sc) ->
|
|
registerFunction("list.getsize", (args, sc) ->
|
|
- ((List) args[0]).size());
|
|
|
|
|
|
+ (double) ((List) args[0]).size());
|
|
registerFunction("list.getindex", (args, sc) ->
|
|
registerFunction("list.getindex", (args, sc) ->
|
|
((List) args[0]).get(getInt(args[1])));
|
|
((List) args[0]).get(getInt(args[1])));
|
|
registerConsumer("list.setindex", (args, sc) ->
|
|
registerConsumer("list.setindex", (args, sc) ->
|
|
@@ -167,7 +188,7 @@ public class SnuviParser
|
|
registerConsumer("list.clear", (args, sc) ->
|
|
registerConsumer("list.clear", (args, sc) ->
|
|
((List) args[0]).clear());
|
|
((List) args[0]).clear());
|
|
registerFunction("list.getindexof", (args, sc) ->
|
|
registerFunction("list.getindexof", (args, sc) ->
|
|
- ((List) args[0]).indexOf(args[1]));
|
|
|
|
|
|
+ (double) ((List) args[0]).indexOf(args[1]));
|
|
registerConsumer("list.sort", (args, sc) ->
|
|
registerConsumer("list.sort", (args, sc) ->
|
|
sortList((List<Object>) args[0], sc));
|
|
sortList((List<Object>) args[0], sc));
|
|
registerConsumer("list.reverse", (args, sc) ->
|
|
registerConsumer("list.reverse", (args, sc) ->
|
|
@@ -189,7 +210,7 @@ public class SnuviParser
|
|
registerFunction("map.contains", (args, sc) ->
|
|
registerFunction("map.contains", (args, sc) ->
|
|
((HashMap) args[0]).containsKey(args[1]));
|
|
((HashMap) args[0]).containsKey(args[1]));
|
|
registerFunction("map.getsize", (args, sc) ->
|
|
registerFunction("map.getsize", (args, sc) ->
|
|
- ((HashMap) args[0]).size());
|
|
|
|
|
|
+ (double) ((HashMap) args[0]).size());
|
|
registerFunction("map.get", (args, sc) ->
|
|
registerFunction("map.get", (args, sc) ->
|
|
((HashMap) args[0]).get(args[1]));
|
|
((HashMap) args[0]).get(args[1]));
|
|
registerConsumer("map.clear", (args, sc) ->
|
|
registerConsumer("map.clear", (args, sc) ->
|
|
@@ -209,15 +230,15 @@ public class SnuviParser
|
|
registerFunction("set.contains", (args, sc) ->
|
|
registerFunction("set.contains", (args, sc) ->
|
|
((HashSet) args[0]).contains(args[1]));
|
|
((HashSet) args[0]).contains(args[1]));
|
|
registerFunction("set.getsize", (args, sc) ->
|
|
registerFunction("set.getsize", (args, sc) ->
|
|
- ((HashSet) args[0]).size());
|
|
|
|
|
|
+ (double) ((HashSet) args[0]).size());
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
// -------------------------------------------------------------------------------
|
|
// time
|
|
// time
|
|
// -------------------------------------------------------------------------------
|
|
// -------------------------------------------------------------------------------
|
|
registerFunction("time.get", (args, sc) ->
|
|
registerFunction("time.get", (args, sc) ->
|
|
- (double) System.currentTimeMillis());
|
|
|
|
|
|
+ getDouble(System.currentTimeMillis()));
|
|
registerFunction("time.nextday", (args, sc) ->
|
|
registerFunction("time.nextday", (args, sc) ->
|
|
- (double) getNextDay(args));
|
|
|
|
|
|
+ getDouble(getNextDay(args)));
|
|
registerFunction("time.getyear", (args, sc) ->
|
|
registerFunction("time.getyear", (args, sc) ->
|
|
(double) getYear(args));
|
|
(double) getYear(args));
|
|
registerFunction("time.getmonth", (args, sc) ->
|
|
registerFunction("time.getmonth", (args, sc) ->
|
|
@@ -230,7 +251,15 @@ public class SnuviParser
|
|
(double) getMinute(args));
|
|
(double) getMinute(args));
|
|
registerFunction("time.getsecond", (args, sc) ->
|
|
registerFunction("time.getsecond", (args, sc) ->
|
|
(double) getSecond(args));
|
|
(double) getSecond(args));
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
|
+ // text
|
|
|
|
+ // -------------------------------------------------------------------------------
|
|
|
|
+ registerFunction("text.number", (args, sc) ->
|
|
|
|
+ doubleToString(getDouble(args[0])));
|
|
|
|
+ registerFunction("text.class", (args, sc) ->
|
|
|
|
+ args[0].getClass().getSimpleName());
|
|
|
|
+
|
|
// -------------------------------------------------------------------------------
|
|
// -------------------------------------------------------------------------------
|
|
// commands without library
|
|
// commands without library
|
|
// -------------------------------------------------------------------------------
|
|
// -------------------------------------------------------------------------------
|
|
@@ -357,6 +386,11 @@ public class SnuviParser
|
|
return scripts.get(id);
|
|
return scripts.get(id);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public boolean termUnsafe(Script sc)
|
|
|
|
+ {
|
|
|
|
+ return scripts.remove(sc.getId()) != null;
|
|
|
|
+ }
|
|
|
|
+
|
|
public void termSafe(Script sc)
|
|
public void termSafe(Script sc)
|
|
{
|
|
{
|
|
if(sc == null)
|
|
if(sc == null)
|
|
@@ -410,17 +444,25 @@ public class SnuviParser
|
|
return startScript(new Script(this, idCounter, scriptName, code));
|
|
return startScript(new Script(this, idCounter, scriptName, code));
|
|
}
|
|
}
|
|
|
|
|
|
- public Script startScript(Class<? extends Script> c, String scriptName, String code, boolean b, Object... o)
|
|
|
|
|
|
+ public <T extends Script> T startScript(Class<T> c, String scriptName, String code, boolean b, Object... o)
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- return startScript(c.getDeclaredConstructor(
|
|
|
|
|
|
+ return (T) startScript(c.getDeclaredConstructor(
|
|
SnuviParser.class, int.class, String.class, String.class, boolean.class)
|
|
SnuviParser.class, int.class, String.class, String.class, boolean.class)
|
|
.newInstance(this, idCounter, scriptName, code, b), o);
|
|
.newInstance(this, idCounter, scriptName, code, b), o);
|
|
}
|
|
}
|
|
catch(IllegalAccessException | IllegalArgumentException | InstantiationException |
|
|
catch(IllegalAccessException | IllegalArgumentException | InstantiationException |
|
|
InvocationTargetException | NoSuchMethodException | SecurityException ex)
|
|
InvocationTargetException | NoSuchMethodException | SecurityException ex)
|
|
{
|
|
{
|
|
|
|
+ if(ex instanceof InvocationTargetException)
|
|
|
|
+ {
|
|
|
|
+ Throwable t = ((InvocationTargetException) ex).getCause();
|
|
|
|
+ if(t != null && t instanceof SnuviException)
|
|
|
|
+ {
|
|
|
|
+ logger.printException((SnuviException) t);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -437,33 +479,56 @@ public class SnuviParser
|
|
.forEach(sc ->
|
|
.forEach(sc ->
|
|
{
|
|
{
|
|
sc.setVar("event", name);
|
|
sc.setVar("event", name);
|
|
- before.accept(sc);
|
|
|
|
|
|
+ if(before != null)
|
|
|
|
+ {
|
|
|
|
+ before.accept(sc);
|
|
|
|
+ }
|
|
sc.runCode();
|
|
sc.runCode();
|
|
- after.accept(sc);
|
|
|
|
|
|
+ if(after != null)
|
|
|
|
+ {
|
|
|
|
+ after.accept(sc);
|
|
|
|
+ }
|
|
sc.eventVars.forEach(s -> sc.removeVar(s));
|
|
sc.eventVars.forEach(s -> sc.removeVar(s));
|
|
sc.eventVars.clear();
|
|
sc.eventVars.clear();
|
|
});
|
|
});
|
|
term();
|
|
term();
|
|
}
|
|
}
|
|
|
|
|
|
- public void callEvent(String name, Script sc, Consumer<Script> before, Consumer<Script> after)
|
|
|
|
|
|
+ public boolean callEvent(String name, Script sc, Consumer<Script> before, Consumer<Script> after)
|
|
{
|
|
{
|
|
if(sc.isLoadedEvent(name))
|
|
if(sc.isLoadedEvent(name))
|
|
{
|
|
{
|
|
sc.setVar("event", name);
|
|
sc.setVar("event", name);
|
|
- before.accept(sc);
|
|
|
|
|
|
+ if(before != null)
|
|
|
|
+ {
|
|
|
|
+ before.accept(sc);
|
|
|
|
+ }
|
|
sc.runCode();
|
|
sc.runCode();
|
|
- after.accept(sc);
|
|
|
|
|
|
+ if(after != null)
|
|
|
|
+ {
|
|
|
|
+ after.accept(sc);
|
|
|
|
+ }
|
|
sc.eventVars.forEach(s -> sc.removeVar(s));
|
|
sc.eventVars.forEach(s -> sc.removeVar(s));
|
|
sc.eventVars.clear();
|
|
sc.eventVars.clear();
|
|
term();
|
|
term();
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------------------
|
|
// functions
|
|
// functions
|
|
// -----------------------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
+ private String doubleToString(Double d)
|
|
|
|
+ {
|
|
|
|
+ if(d.doubleValue() == d.longValue())
|
|
|
|
+ {
|
|
|
|
+ return String.valueOf(d.longValue());
|
|
|
|
+ }
|
|
|
|
+ return d.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
private void ifFunction(Object[] args, Script sc)
|
|
private void ifFunction(Object[] args, Script sc)
|
|
{
|
|
{
|
|
if(Arrays.stream(args).anyMatch(s -> !((boolean) s)))
|
|
if(Arrays.stream(args).anyMatch(s -> !((boolean) s)))
|
|
@@ -547,6 +612,7 @@ public class SnuviParser
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
// this is needed for comparing different number types (e.g. Integer and Double)
|
|
// this is needed for comparing different number types (e.g. Integer and Double)
|
|
|
|
+ // allthough there should be no numbers except doubles...
|
|
else if(args[1] instanceof Number && args[0] instanceof Number)
|
|
else if(args[1] instanceof Number && args[0] instanceof Number)
|
|
{
|
|
{
|
|
return ((Number) args[0]).doubleValue() == ((Number) args[1]).doubleValue();
|
|
return ((Number) args[0]).doubleValue() == ((Number) args[1]).doubleValue();
|
|
@@ -558,7 +624,7 @@ public class SnuviParser
|
|
{
|
|
{
|
|
sc.setVar(args[0].toString(),
|
|
sc.setVar(args[0].toString(),
|
|
Arrays.stream(ScriptUtils.connect(args, 2).split(args[1].toString()))
|
|
Arrays.stream(ScriptUtils.connect(args, 2).split(args[1].toString()))
|
|
- .map(s -> Code.convertInput(s)).collect(Collectors.toList()));
|
|
|
|
|
|
+ .map(s -> Code.convertInput(s, false)).collect(Collectors.toList()));
|
|
}
|
|
}
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------------------
|
|
@@ -575,6 +641,11 @@ public class SnuviParser
|
|
return ((Number) o).longValue();
|
|
return ((Number) o).longValue();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private double getDouble(Object o)
|
|
|
|
+ {
|
|
|
|
+ return ((Number) o).doubleValue();
|
|
|
|
+ }
|
|
|
|
+
|
|
// -----------------------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------------------
|
|
// time handler
|
|
// time handler
|
|
// -----------------------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------------------
|