From 69ba754128ec504bc6cafaed40c98fe4f1c4e48c Mon Sep 17 00:00:00 2001 From: DualWu Date: Thu, 20 Aug 2020 09:49:08 +0800 Subject: [PATCH] map support --- src/pmod_pt.erl | 15 +++++++++++++++ tests/Makefile | 3 ++- tests/map_test.erl | 29 +++++++++++++++++++++++++++++ tests/test_pmod.erl | 5 +++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/map_test.erl diff --git a/src/pmod_pt.erl b/src/pmod_pt.erl index 67dad23..5704d16 100644 --- a/src/pmod_pt.erl +++ b/src/pmod_pt.erl @@ -318,6 +318,21 @@ expr({bc,Line,E0,Qs0},St) -> expr({tuple,Line,Es0},St) -> Es1 = expr_list(Es0,St), {tuple,Line,Es1}; +expr({map, Line, Es0}, St) -> + Es1 = expr_list(Es0, St), + {map, Line, Es1}; +expr({map, Line, OldMap0, Es0}, St) -> + OldMap1 = expr(OldMap0, St), + Es1 = expr_list(Es0, St), + {map, Line, OldMap1, Es1}; +expr({map_field_assoc, Line, Key0, Val0}, St) -> + Key1 = expr(Key0, St), + Val1 = expr(Val0, St), + {map_field_assoc, Line, Key1, Val1}; +expr({map_field_exact, Line, Key0, Val0}, St) -> + Key1 = expr(Key0, St), + Val1 = expr(Val0, St), + {map_field_exact, Line, Key1, Val1}; expr({record_index,_,_,_}=RI, _St) -> RI; expr({record,Line,Name,Is0},St) -> diff --git a/tests/Makefile b/tests/Makefile index 6e75d22..3117f55 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -6,7 +6,8 @@ MODULES = \ pmod_basic.beam \ my_lists.beam \ static_call.beam \ - fun_in_pmod.beam + fun_in_pmod.beam \ + map_test.beam EBIN = $(dir $(CURDIR))ebin diff --git a/tests/map_test.erl b/tests/map_test.erl new file mode 100644 index 0000000..9c95284 --- /dev/null +++ b/tests/map_test.erl @@ -0,0 +1,29 @@ +%% +%% %CopyrightBegin% +%% +%% Copyright Ericsson AB 2013. All Rights Reserved. +%% +%% The contents of this file are subject to the Erlang Public License, +%% Version 1.1, (the "License"); you may not use this file except in +%% compliance with the License. You should have received a copy of the +%% Erlang Public License along with this software. If not, it can be +%% retrieved online at http://www.erlang.org/. +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and limitations +%% under the License. +%% +%% %CopyrightEnd% +%% +-include_lib("pmod_transform/include/pmod.hrl"). + +-module(map_test, [M]). + +-export([add/2, update/2]). + +add(Key, Val) -> + new(M#{Key => Val}). + +update(Key, Val) -> + new(M#{Key := Val}). diff --git a/tests/test_pmod.erl b/tests/test_pmod.erl index 4289ed3..608d95a 100644 --- a/tests/test_pmod.erl +++ b/tests/test_pmod.erl @@ -129,3 +129,8 @@ record_index_test() -> record_index_match({#r.a,#r.b,#r.c}) -> ok. + +map_test() -> + M0 = map_test:new(#{}), + M1 = M0:add(a, 1), + M1:update(a, 2). \ No newline at end of file