Skip to content
Browse files

More operators.

* New: >=, <=, >, <

* Change: != instead of /= (Django compatibility)
  • Loading branch information...
1 parent b96dd0b commit e66e38a125f2f25fa11a3b3b5f34a7d3857e9335 @evanmiller evanmiller committed
View
12 src/erlydtl/erlydtl_parser.yrl
@@ -143,7 +143,9 @@ Terminals
text
trans_keyword
with_keyword
- '==' '/='
+ '==' '!='
+ '>=' '<='
+ '>' '<'
'(' ')'.
Rootsymbol
@@ -152,7 +154,7 @@ Rootsymbol
%% Operator precedences for the E non terminal
Left 100 or_keyword.
Left 110 and_keyword.
-Nonassoc 300 '==' '/='.
+Nonassoc 300 '==' '!=' '>=' '<=' '>' '<'.
Unary 600 Unot.
Elements -> '$empty' : [].
@@ -231,7 +233,11 @@ IfBraced -> open_tag if_keyword IfExpression close_tag : '$3'.
IfExpression -> Value in_keyword Value : {'expr', "in", '$1', '$3'}.
IfExpression -> Value not_keyword in_keyword Value : {'expr', "not", {'expr', "in", '$1', '$4'}}.
IfExpression -> Value '==' Value : {'expr', "eq", '$1', '$3'}.
-IfExpression -> Value '/=' Value : {'expr', "ne", '$1', '$3'}.
+IfExpression -> Value '!=' Value : {'expr', "ne", '$1', '$3'}.
+IfExpression -> Value '>=' Value : {'expr', "ge", '$1', '$3'}.
+IfExpression -> Value '<=' Value : {'expr', "le", '$1', '$3'}.
+IfExpression -> Value '>' Value : {'expr', "gt", '$1', '$3'}.
+IfExpression -> Value '<' Value : {'expr', "lt", '$1', '$3'}.
IfExpression -> '(' IfExpression ')' : '$2'.
IfExpression -> Unot : '$1'.
IfExpression -> IfExpression or_keyword IfExpression : {'expr', "or", '$1', '$3'}.
View
24 src/erlydtl/erlydtl_runtime.erl
@@ -119,6 +119,30 @@ is_true(V) ->
'ne'(Value1, Value2) ->
not are_equal(Value1, Value2).
+'le'(Value1, Value2) ->
+ not 'gt'(Value1, Value2).
+
+'ge'(Value1, Value2) ->
+ not 'lt'(Value1, Value2).
+
+'gt'(Value1, Value2) when is_list(Value1) ->
+ 'gt'(list_to_integer(Value1), Value2);
+'gt'(Value1, Value2) when is_list(Value2) ->
+ 'gt'(Value1, list_to_integer(Value2));
+'gt'(Value1, Value2) when Value1 > Value2 ->
+ true;
+'gt'(_, _) ->
+ false.
+
+'lt'(Value1, Value2) when is_list(Value1) ->
+ 'lt'(list_to_integer(Value1), Value2);
+'lt'(Value1, Value2) when is_list(Value2) ->
+ 'lt'(Value1, list_to_integer(Value2));
+'lt'(Value1, Value2) when Value1 < Value2 ->
+ true;
+'lt'(_, _) ->
+ false.
+
stringify_final(In) ->
stringify_final(In, []).
stringify_final([], Out) ->
View
10 src/erlydtl/erlydtl_scanner.erl
@@ -172,20 +172,14 @@ scan("%}" ++ T, Scanned, {Row, Column}, {_, "%}"}) ->
scan("==" ++ T, Scanned, {Row, Column}, {_, Closer}) ->
scan(T, [{'==', {Row, Column}, "=="} | Scanned], {Row, Column + 2}, {in_code, Closer});
-scan("/=" ++ T, Scanned, {Row, Column}, {_, Closer}) ->
- scan(T, [{'/=', {Row, Column}, "/="} | Scanned], {Row, Column + 2}, {in_code, Closer});
-
scan("!=" ++ T, Scanned, {Row, Column}, {_, Closer}) ->
- scan(T, [{'/=', {Row, Column}, "!="} | Scanned], {Row, Column + 2}, {in_code, Closer});
+ scan(T, [{'!=', {Row, Column}, "!="} | Scanned], {Row, Column + 2}, {in_code, Closer});
scan(">=" ++ T, Scanned, {Row, Column}, {_, Closer}) ->
scan(T, [{'>=', {Row, Column}, ">="} | Scanned], {Row, Column + 2}, {in_code, Closer});
-scan("=<" ++ T, Scanned, {Row, Column}, {_, Closer}) ->
- scan(T, [{'=<', {Row, Column}, "=<"} | Scanned], {Row, Column + 2}, {in_code, Closer});
-
scan("<=" ++ T, Scanned, {Row, Column}, {_, Closer}) ->
- scan(T, [{'=<', {Row, Column}, "<="} | Scanned], {Row, Column + 2}, {in_code, Closer});
+ scan(T, [{'<=', {Row, Column}, "<="} | Scanned], {Row, Column + 2}, {in_code, Closer});
scan("<" ++ T, Scanned, {Row, Column}, {_, Closer}) ->
scan(T, [{'<', {Row, Column}, "<"} | Scanned], {Row, Column + 1}, {in_code, Closer});
View
31 src/tests/erlydtl_unittests.erl
@@ -155,7 +155,7 @@ tests() ->
{"If false or false ",
<<"{% if var1 or var2 %}yay{% endif %}">>, [{var1, false}, {var2, false}], <<"">>}
]},
- {"if comparisons", [
+ {"if equality", [
{"If int equals number literal",
<<"{% if var1 == 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
{"If int equals number literal (false)",
@@ -165,14 +165,39 @@ tests() ->
{"If string equals string literal (false)",
<<"{% if var1 == \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"">>},
{"If int not equals number literal",
- <<"{% if var1 /= 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
+ <<"{% if var1 != 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
{"If string not equals string literal",
- <<"{% if var1 /= \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"yay">>},
+ <<"{% if var1 != \"2\" %}yay{% endif %}">>, [{var1, "3"}], <<"yay">>},
{"If filter result equals number literal",
<<"{% if var1|length == 2 %}yay{% endif %}">>, [{var1, ["fo", "bo"]}], <<"yay">>},
{"If filter result equals string literal",
<<"{% if var1|capfirst == \"Foo\" %}yay{% endif %}">>, [{var1, "foo"}], <<"yay">>}
]},
+ {"if size comparison", [
+ {"If int greater than number literal",
+ <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
+ {"If int greater than number literal (false)",
+ <<"{% if var1 > 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
+
+ {"If int greater than or equal to number literal",
+ <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 3}], <<"yay">>},
+ {"If int greater than or equal to number literal (2)",
+ <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
+ {"If int greater than or equal to number literal (false)",
+ <<"{% if var1 >= 2 %}yay{% endif %}">>, [{var1, 1}], <<"">>},
+
+ {"If int less than number literal",
+ <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
+ {"If int less than number literal (false)",
+ <<"{% if var1 < 2 %}yay{% endif %}">>, [{var1, 2}], <<"">>},
+
+ {"If int less than or equal to number literal",
+ <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 1}], <<"yay">>},
+ {"If int less than or equal to number literal",
+ <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 2}], <<"yay">>},
+ {"If int less than or equal to number literal (false)",
+ <<"{% if var1 <= 2 %}yay{% endif %}">>, [{var1, 3}], <<"">>}
+ ]},
{"if complex bool", [
{"If (true or false) and true",
<<"{% if (var1 or var2) and var3 %}yay{% endif %}">>,

0 comments on commit e66e38a

Please sign in to comment.
Something went wrong with that request. Please try again.