From 620c1af6aa643e45f4368b7c8301be4ec16b0d11 Mon Sep 17 00:00:00 2001 From: hizamakura Date: Sat, 22 Feb 2020 19:59:15 +0800 Subject: [PATCH 1/3] Implement an eval method --- .../jagrosh/jagtag/libraries/Functional.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/com/jagrosh/jagtag/libraries/Functional.java b/src/main/java/com/jagrosh/jagtag/libraries/Functional.java index f7f251b..4f5b05f 100644 --- a/src/main/java/com/jagrosh/jagtag/libraries/Functional.java +++ b/src/main/java/com/jagrosh/jagtag/libraries/Functional.java @@ -63,6 +63,30 @@ public static Collection getMethods() { return in[1]; return in[2]; }, "|then:", "|else:"), + + // performs a conditional and outputs true/false + new Method("eval", (env, in) -> { + if(in[0].equalsIgnoreCase("true")) + return "true"; + if(in[0].equalsIgnoreCase("false")) + return "false"; + return evaluateStatement(in[0]); + }); + + // performs a switch conditional + new Method("switch", (env,in) -> { + String[] cases = in[1].split("\\|"); + + if(cases.length % 2 != 0) + return in[in.length - 1]; + + for (int i = 0; i < cases.length; i += 2) { + if(in[0].equals(cases[i])) + return cases[i + 1]; + } + + return in[in.length - 1]; + }, "|cases:", "|default:"), // performs basic mathematical function new Method("math", (env,in) -> { From c8972619fee99e88a27bd1b33fc82ea2f13b2d58 Mon Sep 17 00:00:00 2001 From: loliprotector Date: Sat, 22 Feb 2020 20:52:53 +0800 Subject: [PATCH 2/3] Fix issues with commit - toString() boolean properly - Use a , not ; --- .../jagrosh/jagtag/libraries/Functional.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/jagrosh/jagtag/libraries/Functional.java b/src/main/java/com/jagrosh/jagtag/libraries/Functional.java index 4f5b05f..072c5a3 100644 --- a/src/main/java/com/jagrosh/jagtag/libraries/Functional.java +++ b/src/main/java/com/jagrosh/jagtag/libraries/Functional.java @@ -23,19 +23,19 @@ /** * Functional Library * Methods for conditionals and mathematics - * + * * @author John Grosh (jagrosh) */ public class Functional { - + public static Collection getMethods() { return Arrays.asList( // equivalent to a comment; gets removed at runtime new Method("note", (env,in) -> ""), - + // chooses randomly between options new Method("choose", (env,in) -> {return in.length==0 ? "" : in[(int)(Math.random()*in.length)];}, true), - + // picks a random number in the provided range new Method("range", (env,in) -> { try{ @@ -52,7 +52,7 @@ public static Collection getMethods() { return in[0]+"|"+in[1]; } }, "|"), - + // performs a conditional new Method("if", (env,in) -> { if(in[0].equalsIgnoreCase("true")) @@ -70,8 +70,8 @@ public static Collection getMethods() { return "true"; if(in[0].equalsIgnoreCase("false")) return "false"; - return evaluateStatement(in[0]); - }); + return Boolean.toString(evaluateStatement(in[0])); + }), // performs a switch conditional new Method("switch", (env,in) -> { @@ -87,12 +87,12 @@ public static Collection getMethods() { return in[in.length - 1]; }, "|cases:", "|default:"), - + // performs basic mathematical function new Method("math", (env,in) -> { return evaluateMath(in[0]); }), - + // returns the absolute value of the provided number new Method("abs", (env,in) -> { try { @@ -103,7 +103,7 @@ public static Collection getMethods() { } catch(NumberFormatException e){} return in[0]; }), - + // rounds a number down new Method("floor", (env,in) -> { try { @@ -112,7 +112,7 @@ public static Collection getMethods() { return in[0]; } }), - + // rounds a number up new Method("ceil", (env,in) -> { try { @@ -121,7 +121,7 @@ public static Collection getMethods() { return in[0]; } }), - + // rounds a number up if the decimal part is .5 or greater, down otherwise new Method("round", (env,in) -> { try { @@ -130,7 +130,7 @@ public static Collection getMethods() { return in[0]; } }), - + // takes the sine of radians new Method("sin", (env,in) -> { try { @@ -139,7 +139,7 @@ public static Collection getMethods() { return in[0]; } }), - + // takes the cosine of radians new Method("cos", (env,in) -> { try { @@ -148,7 +148,7 @@ public static Collection getMethods() { return in[0]; } }), - + // takes the tangent of radians new Method("tan", (env,in) -> { try { @@ -157,7 +157,7 @@ public static Collection getMethods() { return in[0]; } }), - + // converts a number to a different base new Method("base", (env, in) -> { try { @@ -168,7 +168,7 @@ public static Collection getMethods() { },true) ); } - + private static String evaluateMath(String statement) { int index = statement.lastIndexOf("|+|"); @@ -225,7 +225,7 @@ private static String evaluateMath(String statement) } return statement; } - + private static boolean evaluateStatement(String statement) { int index = statement.indexOf("|=|"); @@ -241,7 +241,7 @@ private static boolean evaluateStatement(String statement) return false; String s1 = statement.substring(0, index).trim(); String s2 = statement.substring(index+3).trim(); - + try{ double i1 = Double.parseDouble(s1); double i2 = Double.parseDouble(s2); @@ -257,7 +257,7 @@ private static boolean evaluateStatement(String statement) return (i1 Date: Thu, 27 Feb 2020 11:15:22 +0800 Subject: [PATCH 3/3] Rename eval to bool --- src/main/java/com/jagrosh/jagtag/libraries/Functional.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/jagrosh/jagtag/libraries/Functional.java b/src/main/java/com/jagrosh/jagtag/libraries/Functional.java index 072c5a3..704ee18 100644 --- a/src/main/java/com/jagrosh/jagtag/libraries/Functional.java +++ b/src/main/java/com/jagrosh/jagtag/libraries/Functional.java @@ -65,7 +65,7 @@ public static Collection getMethods() { }, "|then:", "|else:"), // performs a conditional and outputs true/false - new Method("eval", (env, in) -> { + new Method("bool", (env, in) -> { if(in[0].equalsIgnoreCase("true")) return "true"; if(in[0].equalsIgnoreCase("false"))