From 7f48fa65f314f0105276689a0d708929106fe392 Mon Sep 17 00:00:00 2001 From: Marcos Magueta Date: Sat, 24 Oct 2026 16:09:32 -0300 Subject: rnt sending and receiving on erlang --- subsystem/karuta/rnt.erl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 subsystem/karuta/rnt.erl (limited to 'subsystem') diff --git a/subsystem/karuta/rnt.erl b/subsystem/karuta/rnt.erl new file mode 100644 index 0000000..c94a01c --- /dev/null +++ b/subsystem/karuta/rnt.erl @@ -0,0 +1,27 @@ +%% Tuples of an RNT relation, as Translator.ml writes and reads them: +%% a tuple is {Type, #{Name => Value}}, its type, names, bitstrings +%% and its integers no wider than an OCaml int. +-module(rnt). +-export([recv/1, send/1]). + +-define(MIN_INT, -(1 bsl 62)). +-define(MAX_INT, (1 bsl 62) - 1). + +%% A tuple from the bytes RNT sent +recv(Bytes) -> holdable(binary_to_term(Bytes, [safe])). + +%% The bytes RNT reads a tuple from +send(Tuple) -> term_to_binary(holdable(Tuple)). + +%% The tuple, if RNT can hold it +holdable({Type, Attributes} = Tuple) when is_binary(Type), is_map(Attributes) -> + case lists:all(fun attribute/1, maps:to_list(Attributes)) of + true -> Tuple; + false -> error(badarg, [Tuple]) + end; +holdable(Tuple) -> error(badarg, [Tuple]). + +attribute({Name, Value}) when is_binary(Name), is_binary(Value) -> true; +attribute({Name, Value}) when is_binary(Name), is_integer(Value) -> + Value >= ?MIN_INT andalso Value =< ?MAX_INT; +attribute(_) -> false. -- cgit v1.2.3