diff --git a/src/main/java/com/googlecode/aviator/runtime/function/AbstractFunction.java b/src/main/java/com/googlecode/aviator/runtime/function/AbstractFunction.java index 1a2adbcb..5a84a0dc 100644 --- a/src/main/java/com/googlecode/aviator/runtime/function/AbstractFunction.java +++ b/src/main/java/com/googlecode/aviator/runtime/function/AbstractFunction.java @@ -14,20 +14,25 @@ * */ public abstract class AbstractFunction extends AviatorObject implements AviatorFunction { - public AviatorObject throwArity(int n) { - String name = this.getName(); + public AviatorObject throwArity(final int n) { + String name = getName(); throw new IllegalArgumentException("Wrong number of args (" + n + ") passed to: " + name); } + @Override + public String desc(final Map env) { + return "<" + getAviatorType() + ", " + getName() + ">"; + + } @Override - public AviatorObject call(Map env) { - return this.throwArity(0); + public AviatorObject call(final Map env) { + return throwArity(0); } @Override - public int compare(AviatorObject other, Map env) { + public int compare(final AviatorObject other, final Map env) { throw new UnsupportedOperationException("Lambda function can't be compared."); } @@ -38,188 +43,210 @@ public AviatorType getAviatorType() { } @Override - public Object getValue(Map env) { + public Object getValue(final Map env) { return this; } @Override - public AviatorObject call(Map env, AviatorObject arg1) { - return this.throwArity(1); + public AviatorObject call(final Map env, final AviatorObject arg1) { + return throwArity(1); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2) { - return this.throwArity(2); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2) { + return throwArity(2); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3) { - return this.throwArity(3); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3) { + return throwArity(3); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4) { - return this.throwArity(4); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4) { + return throwArity(4); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5) { - return this.throwArity(5); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5) { + return throwArity(5); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6) { - return this.throwArity(6); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6) { + return throwArity(6); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7) { - return this.throwArity(7); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7) { + return throwArity(7); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8) { - return this.throwArity(8); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8) { + return throwArity(8); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9) { - return this.throwArity(9); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9) { + return throwArity(9); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10) { - return this.throwArity(10); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10) { + return throwArity(10); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11) { - return this.throwArity(11); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11) { + return throwArity(11); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12) { - return this.throwArity(12); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12) { + return throwArity(12); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13) { - return this.throwArity(13); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13) { + return throwArity(13); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14) { - return this.throwArity(14); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13, + final AviatorObject arg14) { + return throwArity(14); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14, - AviatorObject arg15) { - return this.throwArity(15); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13, + final AviatorObject arg14, final AviatorObject arg15) { + return throwArity(15); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14, - AviatorObject arg15, AviatorObject arg16) { - return this.throwArity(16); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13, + final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16) { + return throwArity(16); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14, - AviatorObject arg15, AviatorObject arg16, AviatorObject arg17) { - return this.throwArity(17); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13, + final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16, + final AviatorObject arg17) { + return throwArity(17); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14, - AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18) { - return this.throwArity(18); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13, + final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16, + final AviatorObject arg17, final AviatorObject arg18) { + return throwArity(18); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14, - AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18, - AviatorObject arg19) { - return this.throwArity(19); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13, + final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16, + final AviatorObject arg17, final AviatorObject arg18, final AviatorObject arg19) { + return throwArity(19); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14, - AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18, - AviatorObject arg19, AviatorObject arg20) { - return this.throwArity(20); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13, + final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16, + final AviatorObject arg17, final AviatorObject arg18, final AviatorObject arg19, + final AviatorObject arg20) { + return throwArity(20); } @Override - public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, - AviatorObject arg3, AviatorObject arg4, AviatorObject arg5, AviatorObject arg6, - AviatorObject arg7, AviatorObject arg8, AviatorObject arg9, AviatorObject arg10, - AviatorObject arg11, AviatorObject arg12, AviatorObject arg13, AviatorObject arg14, - AviatorObject arg15, AviatorObject arg16, AviatorObject arg17, AviatorObject arg18, - AviatorObject arg19, AviatorObject arg20, AviatorObject... args) { - return this.throwArity(21); + public AviatorObject call(final Map env, final AviatorObject arg1, + final AviatorObject arg2, final AviatorObject arg3, final AviatorObject arg4, + final AviatorObject arg5, final AviatorObject arg6, final AviatorObject arg7, + final AviatorObject arg8, final AviatorObject arg9, final AviatorObject arg10, + final AviatorObject arg11, final AviatorObject arg12, final AviatorObject arg13, + final AviatorObject arg14, final AviatorObject arg15, final AviatorObject arg16, + final AviatorObject arg17, final AviatorObject arg18, final AviatorObject arg19, + final AviatorObject arg20, final AviatorObject... args) { + return throwArity(21); } } diff --git a/src/main/java/com/googlecode/aviator/runtime/type/AviatorObject.java b/src/main/java/com/googlecode/aviator/runtime/type/AviatorObject.java index ce7a0218..215f8c4c 100644 --- a/src/main/java/com/googlecode/aviator/runtime/type/AviatorObject.java +++ b/src/main/java/com/googlecode/aviator/runtime/type/AviatorObject.java @@ -70,7 +70,12 @@ public AviatorObject not(final Map env) { } public String desc(final Map env) { - return "<" + getAviatorType() + ", " + getValue(env) + ">"; + Object val = getValue(env); + if (val != this) { + return "<" + getAviatorType() + ", " + getValue(env) + ">"; + } else { + return "<" + getAviatorType() + ", this>"; + } } public abstract Object getValue(Map env); diff --git a/src/test/java/com/googlecode/aviator/test/function/GrammarUnitTest.java b/src/test/java/com/googlecode/aviator/test/function/GrammarUnitTest.java index 239b6b0b..4bab314f 100644 --- a/src/test/java/com/googlecode/aviator/test/function/GrammarUnitTest.java +++ b/src/test/java/com/googlecode/aviator/test/function/GrammarUnitTest.java @@ -56,6 +56,15 @@ public void testIssue77() { AviatorEvaluator.setOption(Options.ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL, false); } + @Test + public void testFunctionToString() { + AviatorEvaluatorInstance instance = AviatorEvaluator.newInstance(); + String expression = "(lambda (x) -> lambda(y) -> lambda(z) -> x + y + z end end end)(appName)"; + HashMap env = new HashMap<>(); + String s = instance.execute(expression, env, true).toString(); + assertTrue(s.startsWith(" env = new HashMap<>();