Skip to content

Commit 7caa566

Browse files
committed
Merge pull request #1545 from migmatore/impl_unary_plus
Fix +/1 (unary plus) not implemented #1512 These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 19e9d1a + b5fbc87 commit 7caa566

File tree

7 files changed

+62
-0
lines changed

7 files changed

+62
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Added support for list insertion in 'ets:insert/2'.
2121
- Support to OTP-28
2222
- Added support for `ets:update_counter/3` and `ets:update_counter/4`.
23+
- Added `erlang:+/1`
2324

2425
### Fixed
2526
- ESP32: improved sntp sync speed from a cold boot.

src/libAtomVM/bif.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,17 @@ term bif_erlang_add_2(Context *ctx, uint32_t fail_label, int live, term arg1, te
497497
}
498498
}
499499

500+
term bif_erlang_plus_1(Context *ctx, uint32_t fail_label, int live, term arg1)
501+
{
502+
UNUSED(live);
503+
504+
if (LIKELY(term_is_number(arg1))) {
505+
return arg1;
506+
} else {
507+
RAISE_ERROR_BIF(fail_label, BADARITH_ATOM);
508+
}
509+
}
510+
500511
static term sub_overflow_helper(Context *ctx, uint32_t fail_label, uint32_t live, term arg1, term arg2)
501512
{
502513
avm_int_t val1 = term_to_int(arg1);

src/libAtomVM/bif.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ term bif_erlang_map_size_1(Context *ctx, uint32_t fail_label, int live, term arg
7070
term bif_erlang_map_get_2(Context *ctx, uint32_t fail_label, term arg1, term arg2);
7171

7272
term bif_erlang_add_2(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2);
73+
term bif_erlang_plus_1(Context *ctx, uint32_t fail_label, int live, term arg1);
7374
term bif_erlang_sub_2(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2);
7475
term bif_erlang_mul_2(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2);
7576
term bif_erlang_div_2(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2);

src/libAtomVM/bifs.gperf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ erlang:*/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlan
7272
erlang:div/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlang_div_2}
7373
erlang:rem/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlang_rem_2}
7474
erlang:-/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_neg_1}
75+
erlang:+/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_plus_1}
7576
erlang:abs/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_abs_1}
7677
erlang:ceil/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_ceil_1}
7778
erlang:floor/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_floor_1}

tests/erlang_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ compile_erlang(int28mulneg2)
292292
compile_erlang(negdiv)
293293
compile_erlang(absovf)
294294
compile_erlang(negovf)
295+
compile_erlang(unary_plus)
295296

296297
compile_erlang(plusone3)
297298
compile_erlang(plusone4)
@@ -771,6 +772,7 @@ add_custom_target(erlang_test_modules DEPENDS
771772
negdiv.beam
772773
absovf.beam
773774
negovf.beam
775+
unary_plus.beam
774776

775777
plusone3.beam
776778
plusone4.beam

tests/erlang_tests/unary_plus.erl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2025 migmatore <kazakvova201@gmail.com>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(unary_plus).
22+
23+
-export([start/0, unary_plus_int/1, unary_plus_float/1, unary_plus_str/1]).
24+
25+
start() ->
26+
ok = ?MODULE:unary_plus_int(1621436),
27+
ok = ?MODULE:unary_plus_float(1135.12523),
28+
ok = ?MODULE:unary_plus_str("string"),
29+
0.
30+
31+
unary_plus_int(A) ->
32+
+A,
33+
ok.
34+
35+
unary_plus_float(A) ->
36+
+A,
37+
ok.
38+
39+
unary_plus_str(A) ->
40+
try +A of
41+
ok -> error
42+
catch
43+
error:badarith -> ok;
44+
_:_ -> error
45+
end.

tests/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ struct Test tests[] = {
334334
TEST_CASE_EXPECTED(negdiv, 134217728),
335335
TEST_CASE_EXPECTED(absovf, 134217728),
336336
TEST_CASE_EXPECTED(negovf, 134217728),
337+
TEST_CASE(unary_plus),
337338

338339
TEST_CASE_EXPECTED(plusone3, 134217726),
339340
TEST_CASE_EXPECTED(plusone4, 134217728),

0 commit comments

Comments
 (0)