Skip to content

Commit e992412

Browse files
authored
Float number literals (#54)
* Adds float literals
1 parent 965c55e commit e992412

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

src/template_compiler_expr.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ compile({trans_literal, SrcPos, {trans, _} = Tr}, #cs{runtime=Runtime} = CState,
5050
]),
5151
{Ws, template_compiler_utils:set_pos(SrcPos, Ast)};
5252
compile({number_literal, _SrcPos, Nr}, _CState, Ws) ->
53-
{Ws, erl_syntax:abstract(z_convert:to_integer(Nr))};
53+
Number = case catch z_convert:to_integer(Nr) of
54+
{'EXIT', {badarg, _}} -> z_convert:to_float(Nr);
55+
N -> N
56+
end,
57+
{Ws, erl_syntax:abstract(Number)};
5458
compile({atom_literal, _SrcPos, Atom}, _CState, Ws) ->
5559
{Ws, erl_syntax:abstract(template_compiler_utils:to_atom(Atom))};
5660
compile({find_value, LookupList}, CState, Ws) ->

src/template_compiler_scanner.erl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,13 @@ scan(<<"=", T/binary>>, Scanned, {SourceRef, Row, Column}, {_, Closer}) ->
424424
scan(<<":", T/binary>>, Scanned, {SourceRef, Row, Column}, {_, Closer}) ->
425425
scan(T, [{colon, {SourceRef, Row, Column}, <<":">>} | Scanned], {SourceRef, Row, Column + 1}, {in_code, Closer});
426426

427+
scan(<<".", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_number, Closer}) ->
428+
case first_char_type(T) of
429+
digit ->
430+
scan(T, append_char(Scanned, $.), {SourceRef, Row, Column + 1}, {in_number, Closer});
431+
_ ->
432+
scan(T, [{dot, {SourceRef, Row, Column}, <<".">>} | Scanned], {SourceRef, Row, Column + 1}, {in_code, Closer})
433+
end;
427434
scan(<<".", T/binary>>, Scanned, {SourceRef, Row, Column}, {_, Closer}) ->
428435
scan(T, [{dot, {SourceRef, Row, Column}, <<".">>} | Scanned], {SourceRef, Row, Column + 1}, {in_code, Closer});
429436

@@ -508,6 +515,8 @@ char_type($_) -> letter_underscore;
508515
char_type(C) when C >= $0, C =< $9 -> digit;
509516
char_type(_) -> undefined.
510517

518+
first_char_type(<<C/utf8, _/binary>>) -> char_type(C);
519+
first_char_type(<<>>) -> undefined.
511520

512521
% Find the 'endraw %}' tag
513522
find_endraw(<<C/utf8, Rest/binary>>, Closer, Row, Column) when C == 9; C == 32 ->

test/template_compiler_expr_SUITE.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ expr_literals(_Config) ->
9090
false
9191
undefined
9292
42
93+
1.61803
9394
<<104,101,108,108,111,32,119,111,114,108,100>>
9495
atom
9596
[a,b,c]
96-
#{<<97>> => 1,<<98>> => 2}">> = iolist_to_binary(Bin1),
97+
#{<<97>> => 1,<<98>> => 2,<<99>> => 3.14159}">> = iolist_to_binary(Bin1),
9798
ok.
9899

99100
expr_nested(_Config) ->

test/test-data/literals.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
{{ false | template_compiler_test:"w" }}
33
{{ undefined | template_compiler_test:"w" }}
44
{{ 42 | template_compiler_test:"w" }}
5+
{{ 1.61803 | template_compiler_test:"w" }}
56
{{ "hello world" | template_compiler_test:"w" }}
67
{{ `atom` | template_compiler_test:"w" }}
78
{{ [`a`, `b`, `c`] | template_compiler_test:"w" }}
8-
{{ %{ a : 1, b: 2 } | template_compiler_test:"w" }}
9+
{{ %{ a : 1, b: 2, c: 3.14159 } | template_compiler_test:"w" }}

0 commit comments

Comments
 (0)