Skip to content

Commit 7faa258

Browse files
committed
Add Elixir List.Chars protocol.
https://github.com/elixir-lang/elixir/blob/v1.17.2/lib/elixir/lib/list/chars.ex Split into several source files because of how our CMakeLists.txt is built Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 4f851d3 commit 7faa258

File tree

8 files changed

+187
-0
lines changed

8 files changed

+187
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ also non string parameters (e.g. `Enum.join([1, 2], ",")`
2222
- Support for `lists:last/1` and `lists:mapfoldl/3`
2323
- Add support to Elixir for `Process.send/2` `Process.send_after/3/4` and `Process.cancel_timer/1`
2424
- Add support for `handle_continue` callback in `gen_server`
25+
- Support for Elixir `List.Chars` protocol
2526

2627
### Changed
2728

libs/exavmlib/lib/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ set(ELIXIR_MODULES
7575
Collectable.Map
7676
Collectable.MapSet
7777

78+
List.Chars
79+
List.Chars.Atom
80+
List.Chars.BitString
81+
List.Chars.Float
82+
List.Chars.Integer
83+
List.Chars.List
84+
7885
String.Chars
7986
String.Chars.Atom
8087
String.Chars.BitString

libs/exavmlib/lib/List.Chars.Atom.ex

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# This file is part of elixir-lang.
3+
#
4+
# Copyright 2013-2023 Elixir Contributors
5+
# https://github.com/elixir-lang/elixir/commits/v1.17.2/lib/elixir/lib/list/chars.ex
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# SPDX-License-Identifier: Apache-2.0
20+
#
21+
22+
defimpl List.Chars, for: Atom do
23+
def to_charlist(nil), do: ~c""
24+
25+
def to_charlist(atom), do: Atom.to_charlist(atom)
26+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# This file is part of elixir-lang.
3+
#
4+
# Copyright 2013-2023 Elixir Contributors
5+
# https://github.com/elixir-lang/elixir/commits/v1.17.2/lib/elixir/lib/list/chars.ex
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# SPDX-License-Identifier: Apache-2.0
20+
#
21+
22+
defimpl List.Chars, for: BitString do
23+
@doc """
24+
Returns the given binary `term` converted to a charlist.
25+
"""
26+
def to_charlist(term) when is_binary(term) do
27+
String.to_charlist(term)
28+
end
29+
30+
def to_charlist(term) do
31+
raise Protocol.UndefinedError,
32+
protocol: @protocol,
33+
value: term,
34+
description: "cannot convert a bitstring to a charlist"
35+
end
36+
end

libs/exavmlib/lib/List.Chars.Float.ex

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# This file is part of elixir-lang.
3+
#
4+
# Copyright 2013-2023 Elixir Contributors
5+
# https://github.com/elixir-lang/elixir/commits/v1.17.2/lib/elixir/lib/list/chars.ex
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# SPDX-License-Identifier: Apache-2.0
20+
#
21+
22+
defimpl List.Chars, for: Float do
23+
def to_charlist(term) do
24+
# TODO: :short option not yet supported right now, so :decimals+:compact should be replaced
25+
:erlang.float_to_list(term, [{:decimals, 17}, :compact])
26+
end
27+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# This file is part of elixir-lang.
3+
#
4+
# Copyright 2013-2023 Elixir Contributors
5+
# https://github.com/elixir-lang/elixir/commits/v1.17.2/lib/elixir/lib/list/chars.ex
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# SPDX-License-Identifier: Apache-2.0
20+
#
21+
22+
defimpl List.Chars, for: Integer do
23+
def to_charlist(term) do
24+
Integer.to_charlist(term)
25+
end
26+
end

libs/exavmlib/lib/List.Chars.List.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# This file is part of elixir-lang.
3+
#
4+
# Copyright 2013-2023 Elixir Contributors
5+
# https://github.com/elixir-lang/elixir/commits/v1.17.2/lib/elixir/lib/list/chars.ex
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# SPDX-License-Identifier: Apache-2.0
20+
#
21+
22+
defimpl List.Chars, for: List do
23+
# Note that same inlining is used for the rewrite rule.
24+
def to_charlist(list), do: list
25+
end

libs/exavmlib/lib/List.Chars.ex

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#
2+
# This file is part of elixir-lang.
3+
#
4+
# Copyright 2013-2023 Elixir Contributors
5+
# https://github.com/elixir-lang/elixir/commits/v1.17.2/lib/elixir/lib/list/chars.ex
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
# SPDX-License-Identifier: Apache-2.0
20+
#
21+
22+
defprotocol List.Chars do
23+
@moduledoc ~S"""
24+
The `List.Chars` protocol is responsible for
25+
converting a structure to a charlist (only if applicable).
26+
27+
The only function that must be implemented is
28+
`to_charlist/1` which does the conversion.
29+
30+
The `to_charlist/1` function automatically imported
31+
by `Kernel` invokes this protocol.
32+
"""
33+
34+
@doc """
35+
Converts `term` to a charlist.
36+
"""
37+
@spec to_charlist(t) :: charlist
38+
def to_charlist(term)
39+
end

0 commit comments

Comments
 (0)