FunctionRegistry.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package me.hammerle.snuviscript.code;
  2. import me.hammerle.snuviscript.inputprovider.InputProvider;
  3. import java.io.File;
  4. import java.nio.charset.StandardCharsets;
  5. import java.lang.reflect.Array;
  6. import java.nio.file.Files;
  7. import java.nio.file.Paths;
  8. import java.time.ZonedDateTime;
  9. import java.util.List;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Collections;
  13. import java.util.Calendar;
  14. import java.util.GregorianCalendar;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import java.util.HashSet;
  18. import java.util.Iterator;
  19. import java.util.Objects;
  20. import java.util.Set;
  21. import java.util.stream.Collectors;
  22. import me.hammerle.snuviscript.config.SnuviConfig;
  23. import me.hammerle.snuviscript.inputprovider.Variable;
  24. public class FunctionRegistry {
  25. private static final HashMap<String, Object> GLOBAL_VARS = new HashMap<>();
  26. private static final HashMap<String, NamedFunction> FUNCTIONS = new HashMap<>();
  27. public static class ArrayIterator implements Iterator<Object> {
  28. private final Object[] array;
  29. private int index = 0;
  30. public ArrayIterator(Object[] array) {
  31. this.array = array;
  32. }
  33. @Override
  34. public boolean hasNext() {
  35. return index < array.length;
  36. }
  37. @Override
  38. public Object next() {
  39. return array[index++];
  40. }
  41. }
  42. protected static void registerFunction(String name,
  43. ExceptionBiFunction<Script, InputProvider[], Object> f) {
  44. FUNCTIONS.put(name, new NamedFunction(name, f));
  45. }
  46. protected static void registerConsumer(String name,
  47. ExceptionBiConsumer<Script, InputProvider[]> f) {
  48. FUNCTIONS.put(name, new NamedFunction(name, (sc, in) -> {
  49. f.apply(sc, in);
  50. return Void.TYPE;
  51. }));
  52. }
  53. protected static void registerAlias(String original, String alias) {
  54. FUNCTIONS.put(alias, FUNCTIONS.get(original));
  55. }
  56. public static NamedFunction getFunction(String f) {
  57. final String function = f.toLowerCase();
  58. return FUNCTIONS.getOrDefault(function, new NamedFunction(function, (sc, in) -> {
  59. sc.handleFunction(function, in);
  60. return Void.TYPE;
  61. }));
  62. }
  63. static {
  64. register();
  65. }
  66. private static class ScheduledGoto implements Runnable {
  67. private String name;
  68. private Script sc;
  69. private String label;
  70. private int line;
  71. public ScheduledGoto(String name, Script sc, String label, int line) {
  72. this.name = name;
  73. this.sc = sc;
  74. this.label = label;
  75. this.line = line;
  76. }
  77. @Override
  78. public void run() {
  79. if(sc.shouldTerm()) {
  80. return;
  81. } else if(sc.isHolded()) {
  82. sc.getScriptManager().getScheduler().scheduleTask(name,
  83. new ScheduledGoto(name, sc, label, line), 2);
  84. return;
  85. }
  86. try {
  87. sc.gotoLabel(label, true, 1);
  88. sc.run();
  89. } catch(Exception ex) {
  90. sc.logException(ex, "sgoto", line);
  91. }
  92. if(sc.shouldTerm()) {
  93. sc.getScriptManager().removeScript(sc);
  94. }
  95. }
  96. }
  97. @SuppressWarnings({"unchecked", "rawtypes"})
  98. private static void register() {
  99. registerFunction("nothing", (sc, in) -> Void.TYPE);
  100. registerConsumer("modtimer", (sc, in) -> sc.addTimer(in[0].getLong(sc)));
  101. registerConsumer("error", (sc, in) -> sc.setStackTrace(in[0].getBoolean(sc)));
  102. registerConsumer("event.load", (sc, in) -> {
  103. String event = in[0].getString(sc);
  104. sc.loadEvent(event);
  105. sc.getScriptManager().loadEvent(event, sc);
  106. });
  107. registerConsumer("event.unload", (sc, in) -> {
  108. String event = in[0].getString(sc);
  109. sc.unloadEvent(in[0].getString(sc));
  110. sc.getScriptManager().unloadEvent(event, sc);
  111. });
  112. registerFunction("event.isloaded", (sc, in) -> sc.isEventLoaded(in[0].getString(sc)));
  113. registerFunction("script.get", (sc, in) -> {
  114. if(in.length == 0) {
  115. return sc;
  116. }
  117. String name = in[0].getString(sc);
  118. for(Script script : sc.getScriptManager().getScripts()) {
  119. if(script.getName().equals(name)) {
  120. return script;
  121. }
  122. }
  123. return null;
  124. });
  125. registerFunction("script.getfromid",
  126. (sc, in) -> sc.getScriptManager().getScript(in[0].getInt(sc)));
  127. registerFunction("script.getid", (sc, in) -> (double) ((Script) in[0].get(sc)).getId());
  128. registerFunction("script.getvar", (sc, in) -> {
  129. Script other = (Script) in[0].get(sc);
  130. Variable v = other.getVar(in[1].getString(sc));
  131. if(v == null) {
  132. return null;
  133. }
  134. return v.get(other);
  135. });
  136. registerConsumer("script.setvar", (sc, in) -> {
  137. Script other = (Script) in[0].get(sc);
  138. other.getVar(in[1].getString(sc)).set(other, in[2].get(sc));
  139. });
  140. registerFunction("script.getall", (sc, in) -> {
  141. String name = in[0].getString(sc);
  142. return sc.getScriptManager().getScripts().stream()
  143. .filter(script -> script.getName().equals(name)).collect(Collectors.toList());
  144. });
  145. registerConsumer("script.term", (sc, in) -> {
  146. Script other = (Script) in[0].get(sc);
  147. other.term();
  148. sc.getScriptManager().removeScript(other);
  149. });
  150. registerFunction(">>", (sc, in) -> (double) (in[0].getInt(sc) >> in[1].getInt(sc)));
  151. registerFunction("<<", (sc, in) -> (double) (in[0].getInt(sc) << in[1].getInt(sc)));
  152. registerFunction("&", (sc, in) -> (double) (in[0].getInt(sc) & in[1].getInt(sc)));
  153. registerFunction("|", (sc, in) -> (double) (in[0].getInt(sc) | in[1].getInt(sc)));
  154. registerFunction("^", (sc, in) -> (double) (in[0].getInt(sc) ^ in[1].getInt(sc)));
  155. registerFunction("~", (sc, in) -> (double) (~in[0].getInt(sc)));
  156. registerFunction("bit.set",
  157. (sc, in) -> (double) (in[0].getInt(sc) | (1 << (in[1].getInt(sc)))));
  158. registerFunction("bit.unset",
  159. (sc, in) -> (double) (in[0].getInt(sc) & (~(1 << (in[1].getInt(sc))))));
  160. registerFunction("bit.get",
  161. (sc, in) -> (in[0].getInt(sc) & (1 << (in[1].getInt(sc)))) != 0);
  162. registerFunction("%", (sc, in) -> (double) (in[0].getInt(sc) % in[1].getInt(sc)));
  163. registerAlias("%", "math.mod");
  164. registerFunction("math.abs", (sc, in) -> Math.abs(in[0].getDouble(sc)));
  165. registerFunction("math.pow",
  166. (sc, in) -> Math.pow(in[0].getDouble(sc), in[1].getDouble(sc)));
  167. registerFunction("math.root",
  168. (sc, in) -> Math.pow(in[0].getDouble(sc), 1.0 / in[1].getDouble(sc)));
  169. registerFunction("math.sqrt", (sc, in) -> Math.sqrt(in[0].getDouble(sc)));
  170. registerFunction("math.hypot",
  171. (sc, in) -> Math.hypot(in[0].getDouble(sc), in[1].getDouble(sc)));
  172. registerFunction("math.sin", (sc, in) -> Math.sin(in[0].getDouble(sc)));
  173. registerFunction("math.cos", (sc, in) -> Math.cos(in[0].getDouble(sc)));
  174. registerFunction("math.tan", (sc, in) -> Math.tan(in[0].getDouble(sc)));
  175. registerFunction("math.asin", (sc, in) -> Math.asin(in[0].getDouble(sc)));
  176. registerFunction("math.acos", (sc, in) -> Math.acos(in[0].getDouble(sc)));
  177. registerFunction("math.atan", (sc, in) -> Math.atan(in[0].getDouble(sc)));
  178. registerFunction("math.e", (sc, in) -> Math.E);
  179. registerFunction("math.pi", (sc, in) -> Math.PI);
  180. registerFunction("math.ln", (sc, in) -> Math.log(in[0].getDouble(sc)));
  181. registerFunction("math.log", (sc, in) -> Math.log10(in[0].getDouble(sc)));
  182. registerFunction("math.random",
  183. (sc, in) -> (double) SnuviUtils.randomInt(in[0].getInt(sc), in[1].getInt(sc)));
  184. registerFunction("math.round", (sc, in) -> (double) Math.round(in[0].getDouble(sc)));
  185. registerFunction("math.rounddown", (sc, in) -> Math.floor(in[0].getDouble(sc)));
  186. registerFunction("math.roundup", (sc, in) -> Math.ceil(in[0].getDouble(sc)));
  187. registerFunction("math.roundcomma", (sc, in) -> {
  188. double d = in[0].getDouble(sc);
  189. int factor = (int) Math.pow(10, in[1].getInt(sc));
  190. return (double) (((double) Math.round(d * factor)) / factor);
  191. });
  192. registerFunction("math.min",
  193. (sc, in) -> Math.min(in[0].getDouble(sc), in[1].getDouble(sc)));
  194. registerFunction("math.max",
  195. (sc, in) -> Math.max(in[0].getDouble(sc), in[1].getDouble(sc)));
  196. registerFunction("list.new", (sc, in) -> new ArrayList<>());
  197. registerFunction("list.exists", (sc, in) -> in[0].get(sc) instanceof List);
  198. registerFunction("list.add", (sc, in) -> ((List) in[0].get(sc)).add(in[1].get(sc)));
  199. registerConsumer("list.addall", (sc, in) -> {
  200. List list = ((List) in[0].get(sc));
  201. for(int i = 1; i < in.length; i++) {
  202. list.add(in[i].get(sc));
  203. }
  204. });
  205. registerFunction("list.remove", (sc, in) -> ((List) in[0].get(sc)).remove(in[1].get(sc)));
  206. registerFunction("list.removeindex",
  207. (sc, in) -> ((List) in[0].get(sc)).remove(in[1].getInt(sc)));
  208. registerFunction("list.contains",
  209. (sc, in) -> ((List) in[0].get(sc)).contains(in[1].get(sc)));
  210. registerFunction("list.getsize", (sc, in) -> (double) ((List) in[0].get(sc)).size());
  211. registerFunction("list.getindex", (sc, in) -> ((List) in[0].get(sc)).get(in[1].getInt(sc)));
  212. registerAlias("list.getindex", "list.get");
  213. registerFunction("list.setindex",
  214. (sc, in) -> ((List) in[0].get(sc)).set(in[1].getInt(sc), in[2].get(sc)));
  215. registerConsumer("list.clear", (sc, in) -> ((List) in[0].get(sc)).clear());
  216. registerFunction("list.getindexof",
  217. (sc, in) -> (double) ((List) in[0].get(sc)).indexOf(in[1].get(sc)));
  218. registerConsumer("list.sort", (sc, in) -> {
  219. Collections.sort(((List<Object>) in[0].get(sc)),
  220. (o1, o2) -> ((Comparable) o1).compareTo(o2));
  221. });
  222. registerConsumer("list.reverse",
  223. (sc, in) -> Collections.reverse((List<Object>) in[0].get(sc)));
  224. registerConsumer("list.shuffle",
  225. (sc, in) -> Collections.shuffle((List<Object>) in[0].get(sc)));
  226. registerFunction("list.iterator", (sc, in) -> ((List) in[0].get(sc)).iterator());
  227. registerFunction("array.new", (sc, in) -> {
  228. if(in.length == 0) {
  229. throw new ArrayIndexOutOfBoundsException("missing array dimension");
  230. }
  231. int[] dim = new int[in.length];
  232. for(int i = 0; i < in.length; i++) {
  233. dim[i] = in[i].getInt(sc);
  234. }
  235. return Array.newInstance(Object.class, dim);
  236. });
  237. registerFunction("array.getsize", (sc, in) -> (double) Array.getLength(in[0].get(sc)));
  238. registerAlias("array.getsize", "array.length");
  239. registerFunction("map.new", (sc, in) -> new HashMap<>());
  240. registerFunction("map.exists", (sc, in) -> in[0].get(sc) instanceof Map);
  241. registerFunction("map.add",
  242. (sc, in) -> ((Map) in[0].get(sc)).put(in[1].get(sc), in[2].get(sc)));
  243. registerFunction("map.remove", (sc, in) -> ((Map) in[0].get(sc)).remove(in[1].get(sc)));
  244. registerFunction("map.contains",
  245. (sc, in) -> ((Map) in[0].get(sc)).containsKey(in[1].get(sc)));
  246. registerFunction("map.getsize", (sc, in) -> (double) ((Map) in[0].get(sc)).size());
  247. registerFunction("map.get", (sc, in) -> ((Map) in[0].get(sc)).get(in[1].get(sc)));
  248. registerFunction("map.getordefault",
  249. (sc, in) -> ((Map) in[0].get(sc)).getOrDefault(in[1].get(sc), in[2].get(sc)));
  250. registerConsumer("map.clear", (sc, in) -> ((Map) in[0].get(sc)).clear());
  251. registerFunction("map.iterator", (sc, in) -> ((Map) in[0].get(sc)).entrySet().iterator());
  252. registerFunction("map.getkey", (sc, in) -> ((Map.Entry) in[0].get(sc)).getKey());
  253. registerFunction("map.getvalue", (sc, in) -> ((Map.Entry) in[0].get(sc)).getValue());
  254. registerFunction("map.setvalue",
  255. (sc, in) -> ((Map.Entry) in[0].get(sc)).setValue(in[1].get(sc)));
  256. registerFunction("set.new", (sc, in) -> new HashSet<>());
  257. registerFunction("set.exists", (sc, in) -> in[0].get(sc) instanceof Set);
  258. registerFunction("set.add", (sc, in) -> ((Set) in[0].get(sc)).add(in[1].get(sc)));
  259. registerConsumer("set.addall", (sc, in) -> {
  260. Set set = ((Set) in[0].get(sc));
  261. for(int i = 1; i < in.length; i++) {
  262. set.add(in[i].get(sc));
  263. }
  264. });
  265. registerFunction("set.remove", (sc, in) -> ((Set) in[0].get(sc)).remove(in[1].get(sc)));
  266. registerFunction("set.contains", (sc, in) -> ((Set) in[0].get(sc)).contains(in[1].get(sc)));
  267. registerFunction("set.getsize", (sc, in) -> (double) ((Set) in[0].get(sc)).size());
  268. registerConsumer("set.clear", (sc, in) -> ((Set) in[0].get(sc)).clear());
  269. registerFunction("set.iterator", (sc, in) -> ((Set) in[0].get(sc)).iterator());
  270. registerFunction("time.new", (sc, in) -> {
  271. GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
  272. cal.setTimeInMillis(in[0].getLong(sc));
  273. return cal;
  274. });
  275. registerFunction("time.getmillis", (sc, in) -> (double) System.currentTimeMillis());
  276. registerFunction("time.getnanos", (sc, in) -> (double) System.nanoTime());
  277. registerFunction("time.from",
  278. (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).getTimeInMillis());
  279. registerConsumer("time.nextday", (sc, in) -> {
  280. GregorianCalendar cal = (GregorianCalendar) in[0].get(sc);
  281. cal.add(Calendar.DAY_OF_YEAR, 1);
  282. cal.set(Calendar.HOUR_OF_DAY, 0);
  283. cal.set(Calendar.SECOND, 0);
  284. cal.set(Calendar.MINUTE, 0);
  285. cal.set(Calendar.MILLISECOND, 0);
  286. });
  287. registerFunction("time.getyear",
  288. (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.YEAR));
  289. registerFunction("time.getmonth",
  290. (sc, in) -> (double) (((GregorianCalendar) in[0].get(sc)).get(Calendar.MONTH) + 1));
  291. registerFunction("time.getday", (sc,
  292. in) -> (double) (((GregorianCalendar) in[0].get(sc)).get(Calendar.DAY_OF_MONTH)));
  293. registerFunction("time.gethour",
  294. (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.HOUR_OF_DAY));
  295. registerFunction("time.getminute",
  296. (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.MINUTE));
  297. registerFunction("time.getsecond",
  298. (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.SECOND));
  299. registerFunction("string.matches",
  300. (sc, in) -> in[0].getString(sc).matches(in[1].getString(sc)));
  301. registerFunction("string.number", (sc, in) -> SnuviUtils.toString(in[0].getDouble(sc)));
  302. registerFunction("string.class", (sc, in) -> in[0].get(sc).getClass().getSimpleName());
  303. registerFunction("string.tolowercase",
  304. (sc, in) -> SnuviUtils.connect(sc, in, 0).toLowerCase());
  305. registerFunction("string.touppercase",
  306. (sc, in) -> SnuviUtils.connect(sc, in, 0).toUpperCase());
  307. registerFunction("string.split",
  308. (sc, in) -> in[1].getString(sc).split(in[0].getString(sc)));
  309. registerFunction("string.concat", (sc, in) -> SnuviUtils.connect(sc, in, 0));
  310. registerFunction("string", (sc, in) -> String.valueOf(in[0].get(sc)));
  311. registerFunction("string.substring",
  312. (sc, in) -> in[0].getString(sc).substring(in[1].getInt(sc), in[2].getInt(sc)));
  313. registerFunction("string.length", (sc, in) -> (double) in[0].getString(sc).length());
  314. registerFunction("string.startswith",
  315. (sc, in) -> in[0].getString(sc).startsWith(in[1].getString(sc), in[2].getInt(sc)));
  316. registerFunction("string.endswith",
  317. (sc, in) -> in[0].getString(sc).endsWith(in[1].getString(sc)));
  318. registerFunction("string.contains",
  319. (sc, in) -> in[0].getString(sc).contains(in[1].getString(sc)));
  320. registerFunction("string.indexof", (sc,
  321. in) -> (double) in[0].getString(sc).indexOf(in[1].getString(sc), in[2].getInt(sc)));
  322. registerFunction("string.lastindexof", (sc, in) -> (double) in[0].getString(sc)
  323. .lastIndexOf(in[1].getString(sc), in[2].getInt(sc)));
  324. registerFunction("string.replace", (sc, in) -> in[0].getString(sc)
  325. .replaceAll(in[1].getString(sc), in[2].getString(sc)));
  326. registerFunction("string.trim", (sc, in) -> in[0].getString(sc).trim());
  327. registerFunction("string.charcode",
  328. (sc, in) -> (double) in[0].getString(sc).charAt(in[1].getInt(sc)));
  329. registerFunction("string.fromcode", (sc, in) -> String.valueOf((char) in[0].getInt(sc)));
  330. registerFunction("file.new", (sc, in) -> new File(in[0].getString(sc)));
  331. registerFunction("file.exists", (sc, in) -> ((File) in[0].get(sc)).exists());
  332. registerFunction("file.isfile", (sc, in) -> ((File) in[0].get(sc)).isFile());
  333. registerFunction("file.isdirectory", (sc, in) -> ((File) in[0].get(sc)).isDirectory());
  334. registerFunction("file.delete", (sc, in) -> ((File) in[0].get(sc)).delete());
  335. registerFunction("file.getname", (sc, in) -> ((File) in[0].get(sc)).getName());
  336. registerFunction("file.getlist",
  337. (sc, in) -> Arrays.asList(((File) in[0].get(sc)).listFiles()));
  338. registerFunction("file.read",
  339. (sc, in) -> Files.readAllLines(((File) in[0].get(sc)).toPath()));
  340. registerConsumer("file.write", (sc, in) -> {
  341. File f = (File) in[0].get(sc);
  342. if(f.getParentFile() != null) {
  343. f.getParentFile().mkdirs();
  344. }
  345. if(!f.exists()) {
  346. f.createNewFile();
  347. }
  348. Files.write(
  349. Paths.get(f.toURI()), ((List<Object>) in[1].get(sc)).stream()
  350. .map(o -> String.valueOf(o)).collect(Collectors.toList()),
  351. StandardCharsets.UTF_8);
  352. });
  353. registerFunction("config.new",
  354. (sc, in) -> new SnuviConfig(in[0].getString(sc), in[1].getString(sc)));
  355. registerFunction("config.exists", (sc, in) -> ((SnuviConfig) in[0].get(sc)).exists());
  356. registerFunction("config.save", (sc, in) -> ((SnuviConfig) in[0].get(sc)).save(sc));
  357. registerConsumer("config.load", (sc, in) -> ((SnuviConfig) in[0].get(sc)).load(sc));
  358. registerFunction("config.delete", (sc, in) -> ((SnuviConfig) in[0].get(sc)).delete());
  359. registerConsumer("config.set",
  360. (sc, in) -> ((SnuviConfig) in[0].get(sc)).set(in[1].getString(sc), in[2].get(sc)));
  361. registerFunction("config.getbool", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getBoolean(sc,
  362. in[1].getString(sc), in[2].getBoolean(sc)));
  363. registerFunction("config.getdouble", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getDouble(sc,
  364. in[1].getString(sc), in[2].getDouble(sc)));
  365. registerFunction("config.getstring", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getString(sc,
  366. in[1].getString(sc), in[2].getString(sc)));
  367. registerFunction("read.number", (sc, in) -> {
  368. try {
  369. return Double.parseDouble(in[0].getString(sc));
  370. } catch(NumberFormatException ex) {
  371. return null;
  372. }
  373. });
  374. registerFunction("+", (sc, in) -> in[0].getDouble(sc) + in[1].getDouble(sc));
  375. registerAlias("+", "add");
  376. registerFunction("-", (sc, in) -> in.length == 1 ? -in[0].getDouble(sc)
  377. : in[0].getDouble(sc) - in[1].getDouble(sc));
  378. registerAlias("-", "sub");
  379. registerFunction("*", (sc, in) -> in[0].getDouble(sc) * in[1].getDouble(sc));
  380. registerAlias("*", "mul");
  381. registerFunction("/", (sc, in) -> in[0].getDouble(sc) / in[1].getDouble(sc));
  382. registerAlias("/", "div");
  383. registerFunction("=", (sc, in) -> {
  384. Object o = in[1].get(sc);
  385. in[0].set(sc, o);
  386. return o;
  387. });
  388. registerFunction("+=", (sc, in) -> {
  389. Object o = in[0].getDouble(sc) + in[1].getDouble(sc);
  390. in[0].set(sc, o);
  391. return o;
  392. });
  393. registerFunction("p++", (sc, in) -> {
  394. double d = in[0].getDouble(sc);
  395. in[0].set(sc, d + 1.0);
  396. return d;
  397. });
  398. registerAlias("p++", "inc");
  399. registerFunction("++", (sc, in) -> {
  400. double d = in[0].getDouble(sc) + 1.0;
  401. in[0].set(sc, d);
  402. return d;
  403. });
  404. registerFunction("-=", (sc, in) -> {
  405. Object o = in[0].getDouble(sc) - in[1].getDouble(sc);
  406. in[0].set(sc, o);
  407. return o;
  408. });
  409. registerFunction("p--", (sc, in) -> {
  410. double d = in[0].getDouble(sc);
  411. in[0].set(sc, d - 1.0);
  412. return d;
  413. });
  414. registerAlias("p--", "dec");
  415. registerFunction("--", (sc, in) -> {
  416. double d = in[0].getDouble(sc) - 1.0;
  417. in[0].set(sc, d);
  418. return d;
  419. });
  420. registerFunction("*=", (sc, in) -> {
  421. Object o = in[0].getDouble(sc) * in[1].getDouble(sc);
  422. in[0].set(sc, o);
  423. return o;
  424. });
  425. registerFunction("/=", (sc, in) -> {
  426. Object o = in[0].getDouble(sc) / in[1].getDouble(sc);
  427. in[0].set(sc, o);
  428. return o;
  429. });
  430. registerFunction("%=", (sc, in) -> {
  431. Object o = (double) (in[0].getInt(sc) % in[1].getInt(sc));
  432. in[0].set(sc, o);
  433. return o;
  434. });
  435. registerFunction("<<=", (sc, in) -> {
  436. Object o = (double) (in[0].getInt(sc) << in[1].getInt(sc));
  437. in[0].set(sc, o);
  438. return o;
  439. });
  440. registerFunction(">>=", (sc, in) -> {
  441. Object o = (double) (in[0].getInt(sc) >> in[1].getInt(sc));
  442. in[0].set(sc, o);
  443. return o;
  444. });
  445. registerFunction("&=", (sc, in) -> {
  446. Object o = (double) (in[0].getInt(sc) & in[1].getInt(sc));
  447. in[0].set(sc, o);
  448. return o;
  449. });
  450. registerFunction("^=", (sc, in) -> {
  451. Object o = (double) (in[0].getInt(sc) ^ in[1].getInt(sc));
  452. in[0].set(sc, o);
  453. return o;
  454. });
  455. registerFunction("|=", (sc, in) -> {
  456. Object o = (double) (in[0].getInt(sc) | in[1].getInt(sc));
  457. in[0].set(sc, o);
  458. return o;
  459. });
  460. registerFunction("getvar", (sc, in) -> sc.getVar(in[0].getString(sc)).get(sc));
  461. registerConsumer("setvar",
  462. (sc, in) -> sc.getVar(in[0].getString(sc)).set(sc, in[1].get(sc)));
  463. registerConsumer("removevar", (sc, in) -> sc.getVar(in[0].getString(sc)).set(sc, null));
  464. registerConsumer("wait", (sc, in) -> sc.setWaiting());
  465. registerConsumer("goto", (sc, in) -> sc.gotoLabel(in[0].getString(sc), true));
  466. registerConsumer("ignoregoto", (sc, in) -> sc.gotoLabel(in[0].getString(sc), false));
  467. registerAlias("ignoregoto", "igoto");
  468. registerConsumer("sgoto", (sc, in) -> {
  469. int time = in[0].getInt(sc);
  470. if(time < 0) {
  471. throw new IllegalArgumentException("time units can't be negative");
  472. }
  473. String label = in[1].getString(sc);
  474. int line = sc.getLine();
  475. sc.getScriptManager().getScheduler().scheduleTask("sgoto",
  476. new ScheduledGoto("sgoto", sc, label, line), time);
  477. });
  478. registerConsumer("gosub", (sc, in) -> sc.goSub(in[0].getString(sc)));
  479. registerFunction("==", (sc, in) -> Objects.equals(in[0].get(sc), in[1].get(sc)));
  480. registerAlias("==", "equal");
  481. registerAlias("==", "equals");
  482. registerFunction("!=", (sc, in) -> !Objects.equals(in[0].get(sc), in[1].get(sc)));
  483. registerAlias("!=", "notequal");
  484. registerFunction("<",
  485. (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) < 0);
  486. registerAlias("<", "less");
  487. registerFunction(">",
  488. (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) > 0);
  489. registerAlias(">", "greater");
  490. registerFunction("<=",
  491. (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) <= 0);
  492. registerAlias("<=", "lessequal");
  493. registerFunction(">=",
  494. (sc, in) -> ((Comparable) in[0].get(sc)).compareTo(in[1].get(sc)) >= 0);
  495. registerAlias(">=", "greaterequal");
  496. registerFunction("!", (sc, in) -> !in[0].getBoolean(sc));
  497. registerAlias("!", "invert");
  498. registerFunction("&&", (sc, in) -> {
  499. for(InputProvider i : in) {
  500. if(!i.getBoolean(sc)) {
  501. return false;
  502. }
  503. }
  504. return true;
  505. });
  506. registerAlias("&&", "and");
  507. registerFunction("||", (sc, in) -> {
  508. for(InputProvider i : in) {
  509. if(i.getBoolean(sc)) {
  510. return true;
  511. }
  512. }
  513. return false;
  514. });
  515. registerAlias("||", "or");
  516. registerFunction("getscriptvar", (sc, in) -> GLOBAL_VARS.get(in[0].getString(sc)));
  517. registerFunction("setscriptvar",
  518. (sc, in) -> GLOBAL_VARS.put(in[0].getString(sc), in[1].get(sc)));
  519. registerFunction("delscriptvar", (sc, in) -> GLOBAL_VARS.remove(in[0].getString(sc)));
  520. registerConsumer("clearscriptvars", (sc, in) -> GLOBAL_VARS.clear());
  521. registerFunction("hasnext", (sc, in) -> ((Iterator) in[0].get(sc)).hasNext());
  522. registerFunction("next", (sc, in) -> ((Iterator) in[0].get(sc)).next());
  523. registerConsumer("remove", (sc, in) -> ((Iterator) in[0].get(sc)).remove());
  524. registerFunction("iterator", (sc, in) -> {
  525. Object o = in[0].get(sc);
  526. if(o instanceof Iterable) {
  527. return ((Iterable) o).iterator();
  528. }
  529. return new ArrayIterator((Object[]) in[0].get(sc));
  530. });
  531. registerConsumer("swap", (sc, in) -> {
  532. Object o = in[0].get(sc);
  533. in[0].set(sc, in[1].get(sc));
  534. in[1].set(sc, o);
  535. });
  536. registerConsumer("print", (sc, in) -> {
  537. sc.getScriptManager().getLogger().print(SnuviUtils.connect(sc, in, 0), null, "print",
  538. sc.getName(), sc, sc.getStackTrace());
  539. });
  540. registerConsumer("waitfor", (sc, in) -> {
  541. long l = in[0].getInt(sc);
  542. if(l < 0) {
  543. throw new IllegalArgumentException("time units can't be negative");
  544. }
  545. sc.setHolded(true);
  546. sc.setWaiting();
  547. sc.getScriptManager().getScheduler().scheduleTask("waitfor", () -> {
  548. if(sc.shouldTerm()) {
  549. return;
  550. }
  551. sc.setHolded(false);
  552. sc.run();
  553. if(sc.shouldTerm()) {
  554. sc.getScriptManager().removeScript(sc);
  555. }
  556. }, l);
  557. });
  558. registerConsumer("term", (sc, in) -> {
  559. sc.term();
  560. sc.getScriptManager().removeScript(sc);
  561. });
  562. registerFunction("isbool", (sc, in) -> (in[0].get(sc) instanceof Boolean));
  563. registerFunction("isdouble", (sc, in) -> (in[0].get(sc) instanceof Double));
  564. registerFunction("islong", (sc, in) -> {
  565. Object o = in[0].get(sc);
  566. if(o instanceof Double) {
  567. double d = (Double) o;
  568. return d == (long) d;
  569. }
  570. return false;
  571. });
  572. registerConsumer("assert", (sc, in) -> {
  573. if(!in[0].getBoolean(sc)) {
  574. throw new IllegalArgumentException("assertion failed");
  575. }
  576. });
  577. registerFunction("class", (sc, in) -> in[0].get(sc).getClass());
  578. registerFunction("usedmemory", (sc, in) -> {
  579. Runtime runtime = Runtime.getRuntime();
  580. double usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / 1048576;
  581. return usedMemory;
  582. });
  583. registerFunction("allocatedmemory",
  584. (sc, in) -> Runtime.getRuntime().totalMemory() / 1048576.0);
  585. }
  586. }