[m-users.] Initialising a map with static data
Fabrice Nicol
fabrnicol at gmail.com
Tue Jan 25 11:27:54 AEDT 2022
Below is an alternative, which has pros and cons.
A bit more concise, but you have to align the data carefully to avoid
input errors.
Or, if you have lots of data, you can write a database in a file, and
parse (use the GH csv library for example).
%%
:-module map_test.
:-interface.
:-import_module io.
:-import_module map.
:-import_module string.
:-pred create_maps(map(string, string), map(string, string)).
:-mode create_maps(out, out) is det.
:-pred main(io::di, io::uo) is det.
:-implementation.
:-import_module list.
%% create
create_maps(Types, Vars) :-
K1 = ["i", "psz", " v", "pv",
"c", "sc", "uc", "pc",
"ppc", "cpc", "cppc"],
V1 = ["int", "char*", "void", "void*",
"char", "signed char", "unsigned char", "char*",
"char**", "const char*", "const char**"],
K2 = ["argc", "argv"],
V2 = ["int", "ppc"],
det_insert_from_corresponding_lists(K1, V1, map.init, Types),
det_insert_from_corresponding_lists(K2, V2, map.init, Vars).
%% test
main(!IO) :- create_maps(Types, Vars),
write_string(Types^det_elem("cpc"), !IO),
nl(!IO),
write_string(Vars^det_elem("argc"), !IO),
nl(!IO).
:-end_module
%%
$ ./map_test
const char*
int
%%
Fabrice
> Is there a more concise way of creating a map full of static data? The
> best I have obtained is this:
>
> default_typemap = Out :-
> map.from_assoc_list([
> ("i" - "int"),
> ("psz" - "char*"),
> ("v" - "void"),
> ("pv" - "void*"),
> ("c" - "char"),
> ("sc" - "signed char"),
> ("uc" - "unsigned char"),
> ("pc" - "char*"),
> ("ppc" - "char**"),
> ("cpc" - "const char*"),
> ("cppc" - "const char**")
> ],
> Types),
> map.from_assoc_list([
> ("argc" - "int"),
> ("argv" - "ppc")
> ],
> Vars),
> :
> : .. more code..
> :
>
> Each backend coder that is a for typed language will be providing an
> initial lookup of both variables and types so it can infer necessary
> typing at code render time.
>
> Thanks,
> Sean
>
>
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20220125/02dc24fe/attachment-0001.html>
More information about the users
mailing list