Skip to content

Commit

Permalink
Float number literals (#54)
Browse files Browse the repository at this point in the history
* Adds float literals
  • Loading branch information
mmzeeman authored Dec 3, 2024
1 parent 965c55e commit e992412
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/template_compiler_expr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ compile({trans_literal, SrcPos, {trans, _} = Tr}, #cs{runtime=Runtime} = CState,
]),
{Ws, template_compiler_utils:set_pos(SrcPos, Ast)};
compile({number_literal, _SrcPos, Nr}, _CState, Ws) ->
{Ws, erl_syntax:abstract(z_convert:to_integer(Nr))};
Number = case catch z_convert:to_integer(Nr) of
{'EXIT', {badarg, _}} -> z_convert:to_float(Nr);
N -> N
end,
{Ws, erl_syntax:abstract(Number)};
compile({atom_literal, _SrcPos, Atom}, _CState, Ws) ->
{Ws, erl_syntax:abstract(template_compiler_utils:to_atom(Atom))};
compile({find_value, LookupList}, CState, Ws) ->
Expand Down
9 changes: 9 additions & 0 deletions src/template_compiler_scanner.erl
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,13 @@ scan(<<"=", T/binary>>, Scanned, {SourceRef, Row, Column}, {_, Closer}) ->
scan(<<":", T/binary>>, Scanned, {SourceRef, Row, Column}, {_, Closer}) ->
scan(T, [{colon, {SourceRef, Row, Column}, <<":">>} | Scanned], {SourceRef, Row, Column + 1}, {in_code, Closer});

scan(<<".", T/binary>>, Scanned, {SourceRef, Row, Column}, {in_number, Closer}) ->
case first_char_type(T) of
digit ->
scan(T, append_char(Scanned, $.), {SourceRef, Row, Column + 1}, {in_number, Closer});
_ ->
scan(T, [{dot, {SourceRef, Row, Column}, <<".">>} | Scanned], {SourceRef, Row, Column + 1}, {in_code, Closer})
end;
scan(<<".", T/binary>>, Scanned, {SourceRef, Row, Column}, {_, Closer}) ->
scan(T, [{dot, {SourceRef, Row, Column}, <<".">>} | Scanned], {SourceRef, Row, Column + 1}, {in_code, Closer});

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

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

% Find the 'endraw %}' tag
find_endraw(<<C/utf8, Rest/binary>>, Closer, Row, Column) when C == 9; C == 32 ->
Expand Down
3 changes: 2 additions & 1 deletion test/template_compiler_expr_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ expr_literals(_Config) ->
false
undefined
42
1.61803
<<104,101,108,108,111,32,119,111,114,108,100>>
atom
[a,b,c]
#{<<97>> => 1,<<98>> => 2}">> = iolist_to_binary(Bin1),
#{<<97>> => 1,<<98>> => 2,<<99>> => 3.14159}">> = iolist_to_binary(Bin1),
ok.

expr_nested(_Config) ->
Expand Down
3 changes: 2 additions & 1 deletion test/test-data/literals.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{{ false | template_compiler_test:"w" }}
{{ undefined | template_compiler_test:"w" }}
{{ 42 | template_compiler_test:"w" }}
{{ 1.61803 | template_compiler_test:"w" }}
{{ "hello world" | template_compiler_test:"w" }}
{{ `atom` | template_compiler_test:"w" }}
{{ [`a`, `b`, `c`] | template_compiler_test:"w" }}
{{ %{ a : 1, b: 2 } | template_compiler_test:"w" }}
{{ %{ a : 1, b: 2, c: 3.14159 } | template_compiler_test:"w" }}

0 comments on commit e992412

Please sign in to comment.