diff --git a/src/main/java/com/jagrosh/jagtag/libraries/Functional.java b/src/main/java/com/jagrosh/jagtag/libraries/Functional.java index f7f251b..704ee18 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")) @@ -63,12 +63,36 @@ public static Collection getMethods() { return in[1]; return in[2]; }, "|then:", "|else:"), - + + // performs a conditional and outputs true/false + new Method("bool", (env, in) -> { + if(in[0].equalsIgnoreCase("true")) + return "true"; + if(in[0].equalsIgnoreCase("false")) + return "false"; + return Boolean.toString(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) -> { return evaluateMath(in[0]); }), - + // returns the absolute value of the provided number new Method("abs", (env,in) -> { try { @@ -79,7 +103,7 @@ public static Collection getMethods() { } catch(NumberFormatException e){} return in[0]; }), - + // rounds a number down new Method("floor", (env,in) -> { try { @@ -88,7 +112,7 @@ public static Collection getMethods() { return in[0]; } }), - + // rounds a number up new Method("ceil", (env,in) -> { try { @@ -97,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 { @@ -106,7 +130,7 @@ public static Collection getMethods() { return in[0]; } }), - + // takes the sine of radians new Method("sin", (env,in) -> { try { @@ -115,7 +139,7 @@ public static Collection getMethods() { return in[0]; } }), - + // takes the cosine of radians new Method("cos", (env,in) -> { try { @@ -124,7 +148,7 @@ public static Collection getMethods() { return in[0]; } }), - + // takes the tangent of radians new Method("tan", (env,in) -> { try { @@ -133,7 +157,7 @@ public static Collection getMethods() { return in[0]; } }), - + // converts a number to a different base new Method("base", (env, in) -> { try { @@ -144,7 +168,7 @@ public static Collection getMethods() { },true) ); } - + private static String evaluateMath(String statement) { int index = statement.lastIndexOf("|+|"); @@ -201,7 +225,7 @@ private static String evaluateMath(String statement) } return statement; } - + private static boolean evaluateStatement(String statement) { int index = statement.indexOf("|=|"); @@ -217,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); @@ -233,7 +257,7 @@ private static boolean evaluateStatement(String statement) return (i1