<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Sean, <br>
</p>
<p>your preferred style of input looks json-like. In this case, as
there is no language-internal construct to directly parse this,
you should perhaps take a look at Julien's json library
(<a class="moz-txt-link-freetext" href="https://github.com/juliensf/mercury-json">https://github.com/juliensf/mercury-json</a>). This means that your
data should be imported from file and not hard-coded. Beyond a
certain load, this is good practice anyhow in almost all languages
I know of, with standard Prolog a possible and controversial
exception.</p>
<p>Fabrice<br>
</p>
<div class="moz-cite-prefix">Le 25/01/2022 à 09:18, Sean Charles
(emacstheviking) a écrit :<br>
</div>
<blockquote type="cite"
cite="mid:9FFCE782-7A8D-4A57-B4E0-2C88570E244F@gmail.com">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
I actually did try that way but I didn’t like it, I wanted
something that more accurately —read— as it was to be if that
makes sense… also I guess it was influnced by the fact it’s being
ported from SWI| Prolog code I wrote before I realised Mercury was
The Way.
<div class=""> </div>
<div class="">The actual SWI code is this, and I wanted it to be
as visually close as possible henc eusing pair() worked for me
but I wondered if there was an alternative.</div>
<div class=""><br class="">
</div>
<div class="">Thanks Fabrice.</div>
<div class="">Sean</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">type_dict(<br class="">
_{ names:<br class="">
_{<br class="">
main: "i", % TODO: invert: i: [main, argc] ?!?!?<br
class="">
argc: "i",<br class="">
argv: "ppc"<br class="">
%i: [i,j,k,n,x,y,z],<br class="">
% c: [c, chr] %TODO: SHOULD I DO THIS???<br class="">
% TODO: Provide a means to UNDO some defaults eg
specify<br class="">
% an empty replacement as a signal to remove key<br
class="">
},<br class="">
types:<br class="">
_{<br class="">
v: "void",<br class="">
pv: "void*",<br class="">
<br class="">
c: "char",<br class="">
sc: "signed char",<br class="">
uc: "unsigned char",<br class="">
pc: "char*",<br class="">
ppc: "char**",<br class="">
cpc: "const char*",<br class="">
cppc: "const char**",<br class="">
<br class="">
sh: "short",<br class="">
shi: "short int",<br class="">
ssh: "signed short",<br class="">
sshi: "signed short int",<br class="">
<br class="">
us: "unsigned short",<br class="">
ushi: "unsigned short int",<br class="">
<br class="">
i: "int",<br class="">
s: "signed",<br class="">
si: "signed int",<br class="">
<br class="">
u: "unsigned",<br class="">
ui: "unsigned int",<br class="">
<br class="">
l: "long",<br class="">
li: "long int",<br class="">
sl: "signed long",<br class="">
sli: "signed long int",<br class="">
<br class="">
ul: "unsigned long",<br class="">
uli: "unsigned long int",<br class="">
<br class="">
ll: "long long",<br class="">
lli: "long long int",<br class="">
sll: "signed long long",<br class="">
slli: "signed long long int",<br class="">
<br class="">
ull: "unsigned long long",<br class="">
ulli: "unsigned longlong int",<br class="">
<br class="">
f: "float",<br class="">
d: "double",<br class="">
ld: "long double",<br class="">
<br class="">
b: "_Bool", % TODO: "auto-include" stdbool.h ?!?!?<br
class="">
'?': "_Bool",<br class="">
<br class="">
% some extra ones that I find I use all the time<br
class="">
<br class="">
fi: "FILE",<br class="">
pfi: "FILE*"<br class="">
},<br class="">
defaults:<br class="">
_{<br class="">
variable: "i",<br class="">
function: "v"<br class="">
},<br class="">
%% maps supported types to printf() format specifier for
EMIT<br class="">
printf:<br class="">
_{<br class="">
'void*': "%p",<br class="">
'char': "%c",<br class="">
'signed char': "%c",<br class="">
'unsigned char': "%uc",<br class="">
'char*': "%s",<br class="">
'char**': "%p",<br class="">
'const char*': "%s",<br class="">
'const char**': "%p",<br class="">
'short': "%i",<br class="">
'short int': "%i",<br class="">
'signed short': "%i",<br class="">
'signed short int': "%ui",<br class="">
'unsigned short': "%ui",<br class="">
'unsigned short int': "%ui",<br class="">
'int': "%i",<br class="">
'signed': "%i",<br class="">
'signed int': "%i",<br class="">
'unsigned': "%ui",<br class="">
'unsigned int': "%ui",<br class="">
'long': "%li",<br class="">
'long int': "%li",<br class="">
'signed long': "%li",<br class="">
'signed long int': "%li",<br class="">
'unsigned long': "%ul",<br class="">
'unsigned long int': "%ul",<br class="">
'long long': "%lli",<br class="">
'long long int': "%lli",<br class="">
'signed long long': "%lli",<br class="">
'signed long long int': "%lli",<br class="">
'unsigned long long': "%llu",<br class="">
'unsigned long long int': "%llu",<br class="">
'float': "%g",<br class="">
'double': "%g",<br class="">
'long double': "%Lg",<br class="">
'_Bool': "%i",<br class="">
'FILE*': "%p"<br class="">
}<br class="">
}<br class="">
).<br class="">
<br class="">
<br class="">
<br class="">
<br class="">
<br class="">
</div>
<div class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On 25 Jan 2022, at 00:27, Fabrice Nicol <<a
href="mailto:fabrnicol@gmail.com"
class="moz-txt-link-freetext" moz-do-not-send="true">fabrnicol@gmail.com</a>>
wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" class="">
<div class="">
<p class="">Below is an alternative, which has pros and
cons. <br class="">
</p>
<p class="">A bit more concise, but you have to align
the data carefully to avoid input errors.</p>
<p class="">Or, if you have lots of data, you can write
a database in a file, and parse (use the GH csv
library for example).<br class="">
</p>
<p class="">%%<br class="">
</p>
<p class="">:-module map_test.<br class="">
:-interface.<br class="">
:-import_module io.<br class="">
:-import_module map.<br class="">
:-import_module string.<br class="">
:-pred create_maps(map(string, string), map(string,
string)).<br class="">
:-mode create_maps(out, out) is det.<br class="">
<br class="">
:-pred main(io::di, io::uo) is det.<br class="">
<br class="">
:-implementation.<br class="">
</p>
<p class="">:-import_module list.<br class="">
<br class="">
%% create<br class="">
<br class="">
create_maps(Types, Vars) :-<br class="">
K1 = ["i", "psz", "
v", "pv",<br class="">
"c", "sc",
"uc", "pc",<br class="">
"ppc", "cpc", "cppc"],<br
class="">
V1 = ["int", "char*", "void",
"void*",<br class="">
"char", "signed char", "unsigned char",
"char*",<br class="">
"char**", "const char*", "const char**"],<br
class="">
K2 = ["argc", "argv"],<br class="">
V2 = ["int", "ppc"],<br class="">
det_insert_from_corresponding_lists(K1, V1,
map.init, Types),<br class="">
det_insert_from_corresponding_lists(K2, V2,
map.init, Vars).<br class="">
<br class="">
%% test<br class="">
<br class="">
main(!IO) :- create_maps(Types, Vars),<br class="">
write_string(Types^det_elem("cpc"), !IO),<br
class="">
nl(!IO),<br class="">
write_string(Vars^det_elem("argc"), !IO),<br
class="">
nl(!IO).</p>
<p class="">:-end_module<br class="">
</p>
<p class="">%%</p>
<p class="">$ ./map_test</p>
<p class="">const char*<br class="">
int</p>
<p class="">%%<br class="">
</p>
<p class="">Fabrice</p>
<p class=""><br class="">
</p>
<blockquote type="cite"
cite="mid:0A280606-7937-41AA-B4BB-9B2540322678@gmail.com"
class="">
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" class="">
Is there a more concise way of creating a map full of
static data? The best I have obtained is this:
<div class=""><br class="">
</div>
<div class=""><font class="" face="Courier New">default_typemap
= Out :-<br class="">
map.from_assoc_list([<br class="">
("i" - "int"),<br class="">
("psz" - "char*"),<br class="">
("v" - "void"),<br class="">
("pv" - "void*"),<br class="">
("c" - "char"),<br class="">
("sc" - "signed char"),<br class="">
("uc" - "unsigned char"),<br class="">
("pc" - "char*"),<br class="">
("ppc" - "char**"),<br class="">
("cpc" - "const char*"),<br class="">
("cppc" - "const char**")<br class="">
],<br class="">
Types),<br class="">
map.from_assoc_list([<br class="">
("argc" - "int"),<br class="">
("argv" - "ppc")<br class="">
],<br class="">
Vars),</font></div>
<div class=""><font class="" face="Courier New">:</font></div>
<div class=""><font class="" face="Courier New">: ..
more code..</font></div>
<div class=""><font class="" face="Courier New">:</font></div>
<div class=""><br class="">
</div>
<div class="">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.</div>
<div class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class="">Sean</div>
<div class=""><br class="">
</div>
<br class="">
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
users mailing list
<a class="moz-txt-link-abbreviated moz-txt-link-freetext" href="mailto:users@lists.mercurylang.org" moz-do-not-send="true">users@lists.mercurylang.org</a>
<a class="moz-txt-link-freetext" href="https://lists.mercurylang.org/listinfo/users" moz-do-not-send="true">https://lists.mercurylang.org/listinfo/users</a>
</pre>
</blockquote>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</blockquote>
</body>
</html>