FunctionLoader.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. package me.hammerle.snuviscript.code;
  2. import java.io.File;
  3. import java.io.IOException;
  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.function.BiFunction;
  22. import java.util.stream.Collectors;
  23. import me.hammerle.snuviscript.array.DynamicArray;
  24. import me.hammerle.snuviscript.config.SnuviConfig;
  25. import me.hammerle.snuviscript.exceptions.AssertionException;
  26. import me.hammerle.snuviscript.exceptions.FileIOException;
  27. import me.hammerle.snuviscript.variable.ArrayVariable;
  28. import me.hammerle.snuviscript.variable.Variable;
  29. public class FunctionLoader
  30. {
  31. private static final HashMap<String, BasicFunction> FUNCTIONS = new HashMap<>();
  32. protected static void registerFunction(String name, String fname, BiFunction<Script, InputProvider[], Object> f)
  33. {
  34. FUNCTIONS.put(name, new BasicFunction(fname, f));
  35. }
  36. protected static void registerFunction(String name, BiFunction<Script, InputProvider[], Object> f)
  37. {
  38. registerFunction(name, name, f);
  39. }
  40. protected static void registerAlias(String original, String alias)
  41. {
  42. FUNCTIONS.put(alias, FUNCTIONS.get(original));
  43. }
  44. protected static BasicFunction getFunction(String f)
  45. {
  46. final String function = f.toLowerCase();
  47. return FUNCTIONS.getOrDefault(function, new BasicFunction(function, (sc, in) ->
  48. {
  49. Script sub = sc.subScripts.get(function);
  50. if(sub == null)
  51. {
  52. throw new NullPointerException("function " + function + " does not exist");
  53. }
  54. // push storage for local vars
  55. HashMap<String, Variable> vars = new HashMap<>();
  56. if(in.length != sub.subScriptInput.length)
  57. {
  58. throw new NullPointerException("invalid number of parameters at function '" + function + "'");
  59. }
  60. // generate local vars
  61. String s;
  62. Variable v;
  63. for(int i = 0; i < in.length; i++)
  64. {
  65. s = sub.subScriptInput[i];
  66. if(in[i].isArray(sc))
  67. {
  68. v = new ArrayVariable(s);
  69. v.set(sc, in[i].getArray(sc));
  70. }
  71. else
  72. {
  73. v = new Variable(s);
  74. v.set(sc, in[i].get(sc));
  75. }
  76. vars.put(s, v);
  77. }
  78. sub.localVars.push(vars);
  79. // saving line for return
  80. int line = sub.currentLine;
  81. // set starting line for current run
  82. sub.currentLine = 0;
  83. // run subscript and save return value
  84. Object r = sub.run();
  85. // return back to previous line
  86. sub.currentLine = line;
  87. // pop storage for local vars
  88. sub.localVars.pop();
  89. return r;
  90. }));
  91. }
  92. static
  93. {
  94. // ---------------------------------------------------------------------
  95. // system stuff
  96. // ---------------------------------------------------------------------
  97. registerFunction("nothing", (sc, in) -> Void.TYPE);
  98. registerFunction("error", (sc, in) ->
  99. {
  100. sc.printStackTrace = !sc.printStackTrace;
  101. return Void.TYPE;
  102. });
  103. registerFunction("", (sc, in) -> in[0].get(sc));
  104. // ---------------------------------------------------------------------
  105. // event
  106. // ---------------------------------------------------------------------
  107. registerFunction("event.load", (sc, in) ->
  108. {
  109. sc.events.add(in[0].getString(sc));
  110. return Void.TYPE;
  111. });
  112. registerFunction("event.unload", (sc, in) ->
  113. {
  114. sc.events.remove(in[0].getString(sc));
  115. return Void.TYPE;
  116. });
  117. registerFunction("event.isloaded", (sc, in) -> sc.isEventLoaded(in[0].getString(sc)));
  118. // ---------------------------------------------------------------------
  119. // bit
  120. // ---------------------------------------------------------------------
  121. registerFunction(">>", (sc, in) -> (double) (in[0].getInt(sc) >> in[1].getInt(sc)));
  122. registerFunction("<<", (sc, in) -> (double) (in[0].getInt(sc) << in[1].getInt(sc)));
  123. registerFunction("&", (sc, in) -> (double) (in[0].getInt(sc) & in[1].getInt(sc)));
  124. registerFunction("|", (sc, in) -> (double) (in[0].getInt(sc) | in[1].getInt(sc)));
  125. registerFunction("^", (sc, in) -> (double) (in[0].getInt(sc) ^ in[1].getInt(sc)));
  126. registerFunction("~", (sc, in) -> (double) (~in[0].getInt(sc)));
  127. registerFunction("bit.set", (sc, in) -> (double) (in[0].getInt(sc) | (1 << (in[1].getInt(sc)))));
  128. registerFunction("bit.unset", (sc, in) -> (double) (in[0].getInt(sc) & (~(1 << (in[1].getInt(sc))))));
  129. registerFunction("bit.get", (sc, in) -> (in[0].getInt(sc) & (1 << (in[1].getInt(sc)))) != 0);
  130. // ---------------------------------------------------------------------
  131. // math
  132. // ---------------------------------------------------------------------
  133. registerFunction("%", (sc, in) -> (double) (in[0].getInt(sc) % in[1].getInt(sc)));
  134. registerAlias("%", "math.mod");
  135. registerFunction("math.abs", (sc, in) -> Math.abs(in[0].getDouble(sc)));
  136. registerFunction("math.pow", (sc, in) -> Math.pow(in[0].getDouble(sc), in[1].getDouble(sc)));
  137. registerFunction("math.root", (sc, in) -> Math.pow(in[0].getDouble(sc), 1.0 / in[1].getDouble(sc)));
  138. registerFunction("math.sqrt", (sc, in) -> Math.sqrt(in[0].getDouble(sc)));
  139. registerFunction("math.hypot", (sc, in) -> Math.hypot(in[0].getDouble(sc), in[1].getDouble(sc)));
  140. registerFunction("math.sin", (sc, in) -> Math.sin(in[0].getDouble(sc)));
  141. registerFunction("math.cos", (sc, in) -> Math.cos(in[0].getDouble(sc)));
  142. registerFunction("math.tan", (sc, in) -> Math.tan(in[0].getDouble(sc)));
  143. registerFunction("math.sin", (sc, in) -> Math.sin(in[0].getDouble(sc)));
  144. registerFunction("math.acos", (sc, in) -> Math.acos(in[0].getDouble(sc)));
  145. registerFunction("math.atan", (sc, in) -> Math.atan(in[0].getDouble(sc)));
  146. registerFunction("math.asin", (sc, in) -> Math.asin(in[0].getDouble(sc)));
  147. registerFunction("math.e", (sc, in) -> Math.E);
  148. registerFunction("math.pi", (sc, in) -> Math.PI);
  149. registerFunction("math.ln", (sc, in) -> Math.log(in[0].getDouble(sc)));
  150. registerFunction("math.log", (sc, in) -> Math.log10(in[0].getDouble(sc)));
  151. registerFunction("math.random", (sc, in) -> (double) SnuviUtils.randomInt(in[0].getInt(sc), in[1].getInt(sc)));
  152. registerFunction("math.round", (sc, in) -> (double) Math.round(in[0].getDouble(sc)));
  153. registerFunction("math.rounddown", (sc, in) -> Math.floor(in[0].getDouble(sc)));
  154. registerFunction("math.roundup", (sc, in) -> Math.ceil(in[0].getDouble(sc)));
  155. registerFunction("math.roundcomma", (sc, in) ->
  156. {
  157. double d = in[0].getDouble(sc);
  158. int factor = (int) Math.pow(10, in[1].getInt(sc));
  159. return (double) (((int) (d * factor)) / factor);
  160. });
  161. // ---------------------------------------------------------------------
  162. // lists
  163. // ---------------------------------------------------------------------
  164. registerFunction("list.new", (sc, in) ->
  165. {
  166. if(in.length == 0)
  167. {
  168. return new ArrayList<>();
  169. }
  170. in[0].set(sc, new ArrayList<>());
  171. return Void.TYPE;
  172. });
  173. registerFunction("list.exists", (sc, in) -> in[0].get(sc) instanceof List);
  174. registerFunction("list.add", (sc, in) -> ((List) in[0].get(sc)).add(in[1].get(sc)));
  175. registerFunction("list.addall", (sc, in) ->
  176. {
  177. List list = ((List) in[0].get(sc));
  178. for(int i = 1; i < in.length; i++)
  179. {
  180. list.add(in[i].get(sc));
  181. }
  182. return Void.TYPE;
  183. });
  184. registerFunction("list.remove", (sc, in) -> ((List) in[0].get(sc)).remove(in[1].get(sc)));
  185. registerFunction("list.removeindex", (sc, in) -> ((List) in[0].get(sc)).remove(in[1].getInt(sc)));
  186. registerFunction("list.contains", (sc, in) -> ((List) in[0].get(sc)).contains(in[1].get(sc)));
  187. registerFunction("list.getsize", (sc, in) -> (double) ((List) in[0].get(sc)).size());
  188. registerFunction("list.getindex", (sc, in) -> ((List) in[0].get(sc)).get(in[1].getInt(sc)));
  189. registerAlias("list.getindex", "list.get");
  190. registerFunction("list.setindex", (sc, in) -> ((List) in[0].get(sc)).set(in[1].getInt(sc), in[2].get(sc)));
  191. registerFunction("list.clear", (sc, in) ->
  192. {
  193. ((List) in[0].get(sc)).clear();
  194. return Void.TYPE;
  195. });
  196. registerFunction("list.getindexof", (sc, in) -> (double) ((List) in[0].get(sc)).indexOf(in[1].get(sc)));
  197. registerFunction("list.sort", (sc, in) ->
  198. {
  199. Collections.sort(((List<Object>) in[0].get(sc)), (Object o1, Object o2) -> ((Comparable) o1).compareTo(o2));
  200. return Void.TYPE;
  201. });
  202. registerFunction("list.reverse", (sc, in) ->
  203. {
  204. Collections.reverse((List<Object>) in[0].get(sc));
  205. return Void.TYPE;
  206. });
  207. registerFunction("list.shuffle", (sc, in) ->
  208. {
  209. Collections.shuffle((List<Object>) in[0].get(sc));
  210. return Void.TYPE;
  211. });
  212. // ---------------------------------------------------------------------
  213. // arrays
  214. // ---------------------------------------------------------------------
  215. registerFunction("array.new", (sc, in) ->
  216. {
  217. for(InputProvider input : in)
  218. {
  219. ((DynamicArray) input).init(sc);
  220. }
  221. return Void.TYPE;
  222. });
  223. registerFunction("array.getsize", (sc, in) -> (double) Array.getLength(in[0].getArray(sc)));
  224. /*
  225. registerFunction("array.swap", (sc, in) ->
  226. {
  227. Object[] o = (Object[]) args[0];
  228. int first = ScriptUtils.getInt(args[1]);
  229. int sec = ScriptUtils.getInt(args[2]);
  230. Object helper = o[first];
  231. o[first] = o[sec];
  232. o[sec] = helper;
  233. });
  234. registerFunction("array.sort", (sc, in) ->
  235. {
  236. if(args.length <= 1)
  237. {
  238. Arrays.sort((Object[]) args[0]);
  239. }
  240. else
  241. {
  242. Arrays.sort((Object[]) args[0], ScriptUtils.getInt(args[1]), ScriptUtils.getInt(args[2]));
  243. }
  244. });
  245. registerFunction("array.copy", (sc, in) ->
  246. {
  247. int first = ScriptUtils.getInt(args[2]);
  248. System.arraycopy((Object[]) args[0], first, (Object[]) args[1],
  249. ScriptUtils.getInt(args[4]), ScriptUtils.getInt(args[3]) - first + 1);
  250. });
  251. registerFunction("array.rsort", (sc, in) ->
  252. {
  253. if(args.length <= 1)
  254. {
  255. Arrays.sort((Object[]) args[0], (Object o, Object o1) -> -((Comparable) o).compareTo(o));
  256. }
  257. else
  258. {
  259. Arrays.sort((Object[]) args[0], ScriptUtils.getInt(args[1]),
  260. ScriptUtils.getInt(args[2]), (Object o, Object o1) -> -((Comparable) o).compareTo(o));
  261. }
  262. });
  263. registerFunction("array.fill", (sc, in) ->
  264. {
  265. if(args.length <= 2)
  266. {
  267. Arrays.fill((Object[]) args[0], args[1]);
  268. }
  269. else
  270. {
  271. Arrays.fill((Object[]) args[0], ScriptUtils.getInt(args[2]), ScriptUtils.getInt(args[3]), args[1]);
  272. }
  273. });*/
  274. // ---------------------------------------------------------------------
  275. // maps
  276. // ---------------------------------------------------------------------
  277. registerFunction("map.new", (sc, in) ->
  278. {
  279. if(in.length == 0)
  280. {
  281. return new HashMap<>();
  282. }
  283. in[0].set(sc, new HashMap<>());
  284. return Void.TYPE;
  285. });
  286. registerFunction("map.exists", (sc, in) -> in[0].get(sc) instanceof Map);
  287. registerFunction("map.add", (sc, in) -> ((Map) in[0].get(sc)).put(in[1].get(sc), in[2].get(sc)));
  288. registerFunction("map.remove", (sc, in) -> ((Map) in[0].get(sc)).remove(in[1].get(sc)));
  289. registerFunction("map.contains", (sc, in) -> ((Map) in[0].get(sc)).containsKey(in[1].get(sc)));
  290. registerFunction("map.getsize", (sc, in) -> (double) ((Map) in[0].get(sc)).size());
  291. registerFunction("map.get", (sc, in) -> ((Map) in[0].get(sc)).get(in[1].get(sc)));
  292. registerFunction("map.getordefault", (sc, in) -> ((Map) in[0].get(sc)).getOrDefault(in[1].get(sc), in[2].get(sc)));
  293. registerFunction("map.clear", (sc, in) ->
  294. {
  295. ((Map) in[0].get(sc)).clear();
  296. return Void.TYPE;
  297. });
  298. registerFunction("map.keys", (sc, in) ->
  299. {
  300. in[0].set(sc, ((Map) in[1].get(sc)).keySet().stream().collect(Collectors.toList()));
  301. return Void.TYPE;
  302. });
  303. registerFunction("map.values", (sc, in) ->
  304. {
  305. in[0].set(sc, ((Map) in[1].get(sc)).values().stream().collect(Collectors.toList()));
  306. return Void.TYPE;
  307. });
  308. // ---------------------------------------------------------------------
  309. // sets
  310. // ---------------------------------------------------------------------
  311. registerFunction("set.new", (sc, in) ->
  312. {
  313. if(in.length == 0)
  314. {
  315. return new HashSet<>();
  316. }
  317. in[0].set(sc, new HashSet<>());
  318. return Void.TYPE;
  319. });
  320. registerFunction("set.exists", (sc, in) -> in[0].get(sc) instanceof Set);
  321. registerFunction("set.add", (sc, in) -> ((Set) in[0].get(sc)).add(in[1].get(sc)));
  322. registerFunction("set.addall", (sc, in) ->
  323. {
  324. Set set = ((Set) in[0].get(sc));
  325. for(int i = 1; i < in.length; i++)
  326. {
  327. set.add(in[i].get(sc));
  328. }
  329. return Void.TYPE;
  330. });
  331. registerFunction("set.remove", (sc, in) -> ((Set) in[0].get(sc)).remove(in[1].get(sc)));
  332. registerFunction("set.contains", (sc, in) -> ((Set) in[0].get(sc)).contains(in[1].get(sc)));
  333. registerFunction("set.getsize", (sc, in) -> (double) ((Set) in[0].get(sc)).size());
  334. registerFunction("set.tolist", (sc, in) ->
  335. {
  336. in[0].set(sc, ((Set) in[1].get(sc)).stream().collect(Collectors.toList()));
  337. return Void.TYPE;
  338. });
  339. // ---------------------------------------------------------------------
  340. // time
  341. // ---------------------------------------------------------------------
  342. registerFunction("time.new", (sc, in) ->
  343. {
  344. GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
  345. cal.setTimeInMillis(in[1].getLong(sc));
  346. in[0].set(sc, cal);
  347. return Void.TYPE;
  348. });
  349. registerFunction("time.getmillis", (sc, in) -> (double) System.currentTimeMillis());
  350. registerFunction("time.getnanos", (sc, in) -> (double) System.nanoTime());
  351. registerFunction("time.from", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).getTimeInMillis());
  352. registerFunction("time.nextday", (sc, in) ->
  353. {
  354. GregorianCalendar cal = (GregorianCalendar) in[0].get(sc);
  355. cal.add(Calendar.DAY_OF_YEAR, 1);
  356. cal.set(Calendar.HOUR, 0);
  357. cal.set(Calendar.SECOND, 0);
  358. cal.set(Calendar.MINUTE, 0);
  359. cal.set(Calendar.MILLISECOND, 0);
  360. return Void.TYPE;
  361. });
  362. registerFunction("time.getyear", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.YEAR));
  363. registerFunction("time.getmonth", (sc, in) -> (double) (((GregorianCalendar) in[0].get(sc)).get(Calendar.MONTH) + 1));
  364. registerFunction("time.getday", (sc, in) -> (double) (((GregorianCalendar) in[0].get(sc)).get(Calendar.DAY_OF_MONTH)));
  365. registerFunction("time.gethour", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.HOUR_OF_DAY));
  366. registerFunction("time.getminute", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.MINUTE));
  367. registerFunction("time.getsecond", (sc, in) -> (double) ((GregorianCalendar) in[0].get(sc)).get(Calendar.SECOND));
  368. // ---------------------------------------------------------------------
  369. // text
  370. // ---------------------------------------------------------------------
  371. registerFunction("text.matches", (sc, in) -> in[0].getString(sc).matches(in[1].getString(sc)));
  372. registerFunction("text.number", (sc, in) -> SnuviUtils.toString(in[0].getDouble(sc)));
  373. registerFunction("text.class", (sc, in) -> in[0].get(sc).getClass().getSimpleName());
  374. registerFunction("text.tolowercase", (sc, in) -> SnuviUtils.connect(sc, in, 0).toLowerCase());
  375. registerAlias("text.tolowercase", "tolowercase");
  376. registerFunction("text.touppercase", (sc, in) -> SnuviUtils.connect(sc, in, 0).toUpperCase());
  377. registerAlias("text.touppercase", "touppercase");
  378. registerFunction("text.split", (sc, in) ->
  379. {
  380. String[] parts = SnuviUtils.connect(sc, in, 2).split(in[1].getString(sc));
  381. ArrayList<Object> list = new ArrayList<>();
  382. for(String part : parts)
  383. {
  384. list.add(Compiler.convert(part));
  385. }
  386. in[0].set(sc, list);
  387. return Void.TYPE;
  388. });
  389. registerAlias("text.split", "split");
  390. registerFunction("text.concatlist", (sc, in) ->
  391. {
  392. StringBuilder sb = new StringBuilder();
  393. List<Object> list = (List<Object>) in[0].get(sc);
  394. String splitter = in[1].getString(sc);
  395. Iterator<Object> iter = list.iterator();
  396. int from = in[2].getInt(sc);
  397. int to = Math.min(in[3].getInt(sc), list.size() - 1);
  398. to -= from;
  399. while(iter.hasNext() && from > 0)
  400. {
  401. iter.next();
  402. from--;
  403. }
  404. while(iter.hasNext() && to > 0)
  405. {
  406. sb.append(iter.next());
  407. sb.append(splitter);
  408. to--;
  409. }
  410. if(iter.hasNext() && to == 0)
  411. {
  412. sb.append(iter.next());
  413. }
  414. return sb.toString();
  415. });
  416. registerAlias("text.concatlist", "concatlist");
  417. registerFunction("text.concat", (sc, in) -> SnuviUtils.connect(sc, in, 0));
  418. registerAlias("text.concat", "concat");
  419. registerFunction("text", (sc, in) -> String.valueOf(in[0].get(sc)));
  420. registerFunction("text.substring", (sc, in) -> in[0].getString(sc).substring(in[1].getInt(sc), in[2].getInt(sc)));
  421. registerFunction("text.length", (sc, in) -> (double) in[0].getString(sc).length());
  422. registerFunction("text.startswith", (sc, in) -> in[0].getString(sc).startsWith(in[1].getString(sc), in[2].getInt(sc)));
  423. registerFunction("text.endswith", (sc, in) -> in[0].getString(sc).endsWith(in[1].getString(sc)));
  424. registerFunction("text.contains", (sc, in) -> in[0].getString(sc).contains(in[1].getString(sc)));
  425. registerFunction("text.indexof", (sc, in) -> in[0].getString(sc).indexOf(in[1].getString(sc), in[2].getInt(sc)));
  426. registerFunction("text.lastindexof", (sc, in) -> in[0].getString(sc).lastIndexOf(in[1].getString(sc), in[2].getInt(sc)));
  427. registerFunction("text.replace", (sc, in) -> in[0].getString(sc).replace(in[1].getString(sc), in[2].getString(sc)));
  428. registerFunction("text.trim", (sc, in) -> in[0].getString(sc).trim());
  429. registerFunction("text.charat", (sc, in) -> String.valueOf(in[0].getString(sc).charAt(in[1].getInt(sc))));
  430. registerFunction("text.charcode", (sc, in) -> (double) in[0].getString(sc).charAt(in[1].getInt(sc)));
  431. registerFunction("text.fromcode", (sc, in) -> String.valueOf((char) in[0].getInt(sc)));
  432. // -------------------------------------------------------------------------------
  433. // files
  434. // -------------------------------------------------------------------------------
  435. registerFunction("file.new", (sc, in) ->
  436. {
  437. in[0].set(sc, new File(in[1].getString(sc)));
  438. return Void.TYPE;
  439. });
  440. registerFunction("file.exists", (sc, in) -> ((File) in[0].get(sc)).exists());
  441. registerFunction("file.delete", (sc, in) -> ((File) in[0].get(sc)).delete());
  442. registerFunction("file.getname", (sc, in) -> ((File) in[0].get(sc)).getName());
  443. registerFunction("file.getlist", (sc, in) ->
  444. {
  445. in[0].set(sc, Arrays.asList(((File) in[0].get(sc)).listFiles()));
  446. return Void.TYPE;
  447. });
  448. registerFunction("file.read", (sc, in) ->
  449. {
  450. try
  451. {
  452. in[0].set(sc, Files.readAllLines(((File) in[1].get(sc)).toPath()));
  453. }
  454. catch(IOException ex)
  455. {
  456. throw new FileIOException(ex.getMessage());
  457. }
  458. return Void.TYPE;
  459. });
  460. registerFunction("file.write", (sc, in) ->
  461. {
  462. try
  463. {
  464. File f = (File) in[0].get(sc);
  465. if(f.getParentFile() != null)
  466. {
  467. f.getParentFile().mkdirs();
  468. }
  469. if(!f.exists())
  470. {
  471. try
  472. {
  473. f.createNewFile();
  474. }
  475. catch(IOException ex)
  476. {
  477. throw new FileIOException(ex.getMessage());
  478. }
  479. }
  480. Files.write(Paths.get(f.toURI()), ((List<Object>) in[1].get(sc))
  481. .stream().map(o -> String.valueOf(o)).collect(Collectors.toList()), StandardCharsets.UTF_8);
  482. }
  483. catch(UnsupportedOperationException | SecurityException | IOException ex)
  484. {
  485. throw new FileIOException(ex.getMessage());
  486. }
  487. return Void.TYPE;
  488. });
  489. // ---------------------------------------------------------------------
  490. // config
  491. // ---------------------------------------------------------------------
  492. registerFunction("config.new", (sc, in) ->
  493. {
  494. in[0].set(sc, new SnuviConfig(sc, in[1].getString(sc), in[2].getString(sc)));
  495. return Void.TYPE;
  496. });
  497. registerFunction("config.exists", (sc, in) -> ((SnuviConfig) in[0].get(sc)).exists());
  498. registerFunction("config.save", (sc, in) -> ((SnuviConfig) in[0].get(sc)).save());
  499. registerFunction("config.load", (sc, in) ->
  500. {
  501. ((SnuviConfig) in[0].get(sc)).load();
  502. return Void.TYPE;
  503. });
  504. registerFunction("config.delete", (sc, in) -> ((SnuviConfig) in[0].get(sc)).delete());
  505. registerFunction("config.set", (sc, in) ->
  506. {
  507. ((SnuviConfig) in[0].get(sc)).set(in[1].getString(sc), in[2].get(sc));
  508. return Void.TYPE;
  509. });
  510. registerFunction("config.getbool", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getBoolean(in[1].getString(sc), in[2].getBoolean(sc)));
  511. registerFunction("config.getdouble", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getDouble(in[1].getString(sc), in[2].getDouble(sc)));
  512. registerFunction("config.getstring", (sc, in) -> ((SnuviConfig) in[0].get(sc)).getString(in[1].getString(sc), in[2].getString(sc)));
  513. // ---------------------------------------------------------------------
  514. // commands without library
  515. // ---------------------------------------------------------------------
  516. // elementary calculating
  517. registerFunction("+", (sc, in) -> in[0].getDouble(sc) + in[1].getDouble(sc));
  518. registerAlias("+", "add");
  519. registerFunction("-", (sc, in) -> in[0].getDouble(sc) - in[1].getDouble(sc));
  520. registerAlias("-", "sub");
  521. registerFunction("*", (sc, in) -> in[0].getDouble(sc) * in[1].getDouble(sc));
  522. registerAlias("*", "mul");
  523. registerFunction("/", (sc, in) -> in[0].getDouble(sc) / in[1].getDouble(sc));
  524. registerAlias("/", "div");
  525. // var setter
  526. registerFunction("=", (sc, in) ->
  527. {
  528. in[0].set(sc, in[1].get(sc));
  529. return Void.TYPE;
  530. });
  531. registerFunction("+=", (sc, in) ->
  532. {
  533. in[0].set(sc, in[0].getDouble(sc) + in[1].getDouble(sc));
  534. return Void.TYPE;
  535. });
  536. registerFunction("++", (sc, in) ->
  537. {
  538. double d = in[0].getDouble(sc);
  539. in[0].set(sc, d + 1.0);
  540. return d;
  541. });
  542. registerAlias("++", "inc");
  543. registerFunction("p+", (sc, in) ->
  544. {
  545. double d = in[0].getDouble(sc) + 1.0;
  546. in[0].set(sc, d);
  547. return d;
  548. });
  549. registerFunction("-=", (sc, in) ->
  550. {
  551. in[0].set(sc, in[0].getDouble(sc) - in[1].getDouble(sc));
  552. return Void.TYPE;
  553. });
  554. registerFunction("--", (sc, in) ->
  555. {
  556. double d = in[0].getDouble(sc);
  557. in[0].set(sc, d - 1.0);
  558. return d;
  559. });
  560. registerAlias("--", "dec");
  561. registerFunction("p-", (sc, in) ->
  562. {
  563. double d = in[0].getDouble(sc) - 1.0;
  564. in[0].set(sc, d);
  565. return d;
  566. });
  567. registerFunction("*=", (sc, in) ->
  568. {
  569. in[0].set(sc, in[0].getDouble(sc) * in[1].getDouble(sc));
  570. return Void.TYPE;
  571. });
  572. registerFunction("/=", (sc, in) ->
  573. {
  574. in[0].set(sc, in[0].getDouble(sc) / in[1].getDouble(sc));
  575. return Void.TYPE;
  576. });
  577. registerFunction("%=", (sc, in) ->
  578. {
  579. in[0].set(sc, (double) (in[0].getInt(sc) % in[1].getInt(sc)));
  580. return Void.TYPE;
  581. });
  582. registerFunction("<<=", (sc, in) ->
  583. {
  584. in[0].set(sc, (double) (in[0].getInt(sc) << in[1].getInt(sc)));
  585. return Void.TYPE;
  586. });
  587. registerFunction(">>=", (sc, in) ->
  588. {
  589. in[0].set(sc, (double) (in[0].getInt(sc) >> in[1].getInt(sc)));
  590. return Void.TYPE;
  591. });
  592. registerFunction("&=", (sc, in) ->
  593. {
  594. in[0].set(sc, (double) (in[0].getInt(sc) & in[1].getInt(sc)));
  595. return Void.TYPE;
  596. });
  597. registerFunction("^=", (sc, in) ->
  598. {
  599. in[0].set(sc, (double) (in[0].getInt(sc) ^ in[1].getInt(sc)));
  600. return Void.TYPE;
  601. });
  602. registerFunction("|=", (sc, in) ->
  603. {
  604. in[0].set(sc, (double) (in[0].getInt(sc) | in[1].getInt(sc)));
  605. return Void.TYPE;
  606. });
  607. // var stuff
  608. registerFunction("getvar", (sc, in) -> sc.getVar(in[0].getString(sc)).get(sc));
  609. registerFunction("setvar", (sc, in) ->
  610. {
  611. sc.getVar(in[0].getString(sc)).set(sc, in[1].get(sc));
  612. return Void.TYPE;
  613. });
  614. registerFunction("removevar", (sc, in) ->
  615. {
  616. sc.getVar(in[0].getString(sc)).set(sc, null);
  617. return Void.TYPE;
  618. });
  619. registerFunction("wait", (sc, in) ->
  620. {
  621. sc.isWaiting = true;
  622. return Void.TYPE;
  623. });
  624. // try - catch
  625. registerFunction("try", (sc, in) ->
  626. {
  627. sc.catchLine = sc.currentLine + in[0].getInt(sc);
  628. return Void.TYPE;
  629. });
  630. registerFunction("catch", (sc, in) ->
  631. {
  632. if(sc.catchLine != -1)
  633. {
  634. sc.currentLine += in[0].getInt(sc);
  635. }
  636. return Void.TYPE;
  637. });
  638. // branching
  639. registerFunction("goto", (sc, in) ->
  640. {
  641. sc.currentLine = sc.labels.get(in[0].getString(sc));
  642. return Void.TYPE;
  643. });
  644. registerFunction("ignoregoto", (sc, in) ->
  645. {
  646. Integer i = sc.labels.get(in[0].getString(sc));
  647. if(i != null)
  648. {
  649. sc.currentLine = i;
  650. }
  651. return Void.TYPE;
  652. });
  653. registerFunction("sgoto", (sc, in) ->
  654. {
  655. if(sc.subScript)
  656. {
  657. throw new IllegalStateException("sgoto is not allowed in functions");
  658. }
  659. int time = in[0].getInt(sc);
  660. if(time < 0)
  661. {
  662. throw new IllegalArgumentException("time units can't be negative");
  663. }
  664. int label = sc.labels.get(in[1].getString(sc));
  665. sc.scheduler.scheduleTask(() ->
  666. {
  667. if(!sc.isValid || sc.isHolded)
  668. {
  669. return;
  670. }
  671. sc.currentLine = label + 1;
  672. sc.run();
  673. }, time);
  674. return Void.TYPE;
  675. });
  676. registerFunction("gosub", (sc, in) ->
  677. {
  678. sc.returnStack.push(sc.currentLine);
  679. sc.currentLine = sc.labels.get(in[0].getString(sc));
  680. return Void.TYPE;
  681. });
  682. registerFunction("return", (sc, in) ->
  683. {
  684. if(sc.returnStack.isEmpty())
  685. {
  686. sc.end();
  687. sc.returnValue = in.length > 0 ? in[0].get(sc) : null;
  688. }
  689. else
  690. {
  691. sc.currentLine = sc.returnStack.pop();
  692. }
  693. return Void.TYPE;
  694. });
  695. registerFunction("if", (sc, in) ->
  696. {
  697. sc.ifState = in[0].getBoolean(sc);
  698. if(!sc.ifState)
  699. {
  700. sc.currentLine += in[1].getInt(sc);
  701. }
  702. return Void.TYPE;
  703. });
  704. registerFunction("endif", (sc, in) ->
  705. {
  706. sc.ifState = true;
  707. return Void.TYPE;
  708. });
  709. registerFunction("elseif", (sc, in) ->
  710. {
  711. if(sc.ifState)
  712. {
  713. sc.currentLine += in[1].getInt(sc);
  714. }
  715. else
  716. {
  717. sc.ifState = in[0].getBoolean(sc);
  718. if(!sc.ifState)
  719. {
  720. sc.currentLine += in[1].getInt(sc);
  721. }
  722. }
  723. return Void.TYPE;
  724. });
  725. registerFunction("else", (sc, in) ->
  726. {
  727. if(sc.ifState)
  728. {
  729. sc.currentLine += in[0].getInt(sc);
  730. }
  731. sc.ifState = true;
  732. return Void.TYPE;
  733. });
  734. registerFunction("while", (sc, in) ->
  735. {
  736. if(!in[0].getBoolean(sc))
  737. {
  738. sc.currentLine += in[1].getInt(sc);
  739. }
  740. return Void.TYPE;
  741. });
  742. registerFunction("wend", (sc, in) ->
  743. {
  744. sc.currentLine += in[0].getInt(sc);
  745. return Void.TYPE;
  746. });
  747. registerFunction("for", (sc, in) ->
  748. {
  749. // for(var, start, end, step)
  750. double start = in[1].getDouble(sc);
  751. in[0].set(sc, start);
  752. if(start > in[2].getDouble(sc))
  753. {
  754. sc.currentLine += in[4].getInt(sc);
  755. }
  756. return Void.TYPE;
  757. });
  758. registerFunction("next", (sc, in) ->
  759. {
  760. int line = sc.currentLine + in[0].getInt(sc);
  761. InputProvider[] f = sc.code[line].getParameters();
  762. // for(var, start, end, step)
  763. double current = f[0].getDouble(sc) + f[3].getDouble(sc);
  764. f[0].set(sc, current);
  765. if(current <= f[2].getDouble(sc))
  766. {
  767. sc.currentLine = line;
  768. }
  769. return Void.TYPE;
  770. });
  771. registerFunction("continue", (sc, in) ->
  772. {
  773. sc.currentLine += in[0].getInt(sc);
  774. return Void.TYPE;
  775. });
  776. registerFunction("break", (sc, in) ->
  777. {
  778. sc.currentLine += in[0].getInt(sc);
  779. return Void.TYPE;
  780. });
  781. // comparing
  782. registerFunction("==", (sc, in) -> Objects.equals(in[0].get(sc), in[1].get(sc)));
  783. registerAlias("==", "equal");
  784. registerAlias("==", "equals");
  785. registerFunction("!=", (sc, in) -> !Objects.equals(in[0].get(sc), in[1].get(sc)));
  786. registerAlias("!=", "notequal");
  787. registerFunction("<", (sc, in) -> in[0].getDouble(sc) < in[1].getDouble(sc));
  788. registerAlias("<", "less");
  789. registerFunction(">", (sc, in) -> in[0].getDouble(sc) > in[1].getDouble(sc));
  790. registerAlias(">", "greater");
  791. registerFunction("<=", (sc, in) -> in[0].getDouble(sc) <= in[1].getDouble(sc));
  792. registerAlias("<=", "lessequal");
  793. registerFunction(">=", (sc, in) -> in[0].getDouble(sc) >= in[1].getDouble(sc));
  794. registerAlias(">=", "greaterequal");
  795. registerFunction("!", (sc, in) -> !in[0].getBoolean(sc));
  796. registerAlias("!", "invert");
  797. // logical stuff
  798. registerFunction("&&", (sc, in) ->
  799. {
  800. for(InputProvider i : in)
  801. {
  802. if(!i.getBoolean(sc))
  803. {
  804. return false;
  805. }
  806. }
  807. return true;
  808. });
  809. registerAlias("&&", "and");
  810. registerFunction("||", (sc, in) ->
  811. {
  812. for(InputProvider i : in)
  813. {
  814. if(i.getBoolean(sc))
  815. {
  816. return true;
  817. }
  818. }
  819. return false;
  820. });
  821. registerAlias( "||", "or");
  822. // non grouped stuff
  823. registerFunction("swap", (sc, in) ->
  824. {
  825. Object o = in[0].get(sc);
  826. in[0].set(sc, in[1].get(sc));
  827. in[1].set(sc, o);
  828. return Void.TYPE;
  829. });
  830. registerFunction("print", (sc, in) ->
  831. {
  832. System.out.println(SnuviUtils.connect(sc, in, 0));
  833. return Void.TYPE;
  834. });
  835. registerFunction("waitfor", (sc, in) ->
  836. {
  837. if(sc.subScript)
  838. {
  839. throw new IllegalStateException("waitfor is not allowed in functions");
  840. }
  841. long l = in[0].getInt(sc);
  842. if(l < 0)
  843. {
  844. throw new IllegalArgumentException("time units can't be negative");
  845. }
  846. sc.isHolded = true;
  847. sc.scheduler.scheduleTask(() ->
  848. {
  849. // activate this again on NullPointerException
  850. // if(sc == null || !sc.isValid)
  851. if(sc.isValid)
  852. {
  853. sc.isHolded = false;
  854. sc.run();
  855. }
  856. }, l);
  857. sc.isWaiting = true;
  858. return Void.TYPE;
  859. });
  860. registerFunction("term", (sc, in) ->
  861. {
  862. sc.parser.termSafe(sc);
  863. return Void.TYPE;
  864. });
  865. registerFunction("islong", (sc, in) ->
  866. {
  867. Object o = in[0].get(sc);
  868. if(o instanceof Double)
  869. {
  870. double d = (Double) o;
  871. return d == (long) d;
  872. }
  873. return false;
  874. });
  875. registerFunction("assert", (sc, in) ->
  876. {
  877. if(!in[0].getBoolean(sc))
  878. {
  879. throw new AssertionException("assertion failed");
  880. }
  881. return Void.TYPE;
  882. });
  883. registerFunction("class", (sc, in) -> in[0].get(sc).getClass());
  884. }
  885. }