11 extern FILE *fh, *fc, *fhs;
18 ** Generate to the .h file:
26 fprintf(fh, "#ifndef %s_defined\n", gtid(t));
27 fprintf(fh, "#define %s_defined\n", gtid(t));
28 fprintf(fh, "\n#include <stdio.h>\n"); /* for stderr */
29 fprintf(fh, "\n#ifndef PROTO\n");
30 fprintf(fh, "#ifdef __STDC__\n");
31 fprintf(fh, "#define PROTO(x) x\n");
32 fprintf(fh, "#else\n");
33 fprintf(fh, "#define PROTO(x) /**/\n");
34 fprintf(fh, "#endif\n");
35 fprintf(fh, "#endif\n\n");
36 fprintf(fh, "typedef enum {\n");
37 ge_typlist(gtdeflist(t));
38 fprintf(fh, "\n} T%s;\n\n", gtid(t));
40 ** Generate to the .hs file:
43 ** = U_constructor1 | U_constructor2 | ...
46 ** Generate to the .h file:
48 ** typedef struct { Ttypename tag; } *typename;
50 fprintf(fh, "typedef struct { T%s tag; } *%s;\n\n", gtid(t), gtid(t));
52 g_tagfun(gtid(t)); /* generate the tag-grabbing function */
54 /* Generate the struct definitions (to the .h file). */
55 gs_typlist(gtdeflist(t), gtid(t));
57 /* Generate a Haskell-equiv data type (to the .hs file) */
58 fprintf(fhs, "data U_%s = ", gtid(t));
59 hs_typlist(gtdeflist(t));
61 /* And a type with which to talk about the C-land parse tree */
62 /* fprintf(fhs, "data U__%s = U__%s Addr#\n", gtid(t), gtid(t));
63 fprintf(fhs, "instance _CCallable U__%s\n", gtid(t));
64 fprintf(fhs, "instance _CReturnable U__%s\n\n", gtid(t));
74 ge_typlist(gdeflist(t));
75 fprintf(fh, ",\n\t%s", gdid(gdef(t)));
78 fprintf(fh, "\t%s", gdid(t));
81 fprintf(stderr,"ge_typlist: funny abstract syntax.\n");
93 gs_typlist(gdeflist(t), tid);
100 fprintf(stderr,"gs_typlist: funny abstract syntax.\n");
111 hs_typlist(gdeflist(t));
119 fprintf(stderr,"hs_typlist: funny abstract syntax.\n");
129 fprintf(fh, "struct S%s {\n", gdid(t));
130 fprintf(fh, "\tT%s tag;\n", tid);
131 gs_itemlist(gditemlist(t));
132 fprintf(fh, "};\n\n");
139 fprintf(fhs, "U_%s ", gdid(t));
140 hs_itemlist(gditemlist(t));
151 gs_itemlist(gitemlist(t));
152 fprintf(fh, "\t%s X%s;\n",
153 gitemtypid(gitem(t)), gitemfunid(gitem(t)) );
156 fprintf(fh, "\t%s X%s;\n",
157 gitemtypid(t), gitemfunid(t));
160 fprintf(stderr,"gs_itemlist: funny abs. syntax: %d\n.", ttree(t));
173 hs_itemlist(gitemlist(t));
174 fprintf(fhs, "U_%s ", gitemtypid(gitem(t)));
177 fprintf(fhs, "U_%s ", gitemtypid(t));
180 fprintf(stderr,"hs_itemlist: funny abs. syntax: %d\n.", ttree(t));
189 fprintf(fh, "#ifdef __GNUC__\n");
191 fprintf(fh, "extern __inline__ T%s t%s(%s t)\n{\n\treturn(t -> tag);\n}\n",
192 typid, typid, typid);
194 fprintf(fh, "#else /* ! __GNUC__ */\n");
196 fprintf(fh, "extern T%s t%s PROTO((%s));\n", typid, typid, typid);
197 fprintf(fc, "\nT%s t%s(t)\n %s t;\n{\n\treturn(t -> tag);\n}\n\n",
198 typid, typid, typid);
200 fprintf(fh, "#endif /* ! __GNUC__ */\n\n");
202 /*******************************************************************/
211 g_consels(gdeflist(t), typid);
212 g_typconsel(gdef(t), typid);
215 g_typconsel(t, typid);
218 fprintf(stderr,"g_consel: funny abstract syntax.\n");
223 /***********************************************************************/
226 g_typconsel(t, typid)
230 fprintf(fc, "\n/************** %s ******************/\n\n", gdid(t));
232 gensels(typid, gdid(t), gditemlist(t));
239 tree t; /* of kind 'def'. */
241 fprintf(fh, "extern %s mk%s PROTO((", typid, gdid(t));
242 genmkprotodekl(gditemlist(t));
243 fprintf(fh, "));\n");
245 fprintf(fc, "%s mk%s(", typid, gdid(t));
246 genmkparamlist(gditemlist(t));
248 genmkparamdekl(gditemlist(t));
249 fprintf(fc, "{\n\tregister struct S%s *pp =\n", gdid(t));
250 fprintf(fc, "\t\t(struct S%s *) malloc(sizeof(struct S%s));\n",
252 fprintf(fc, "\tpp -> tag = %s;\n", gdid(t));
253 genmkfillin(gditemlist(t));
254 fprintf(fc, "\treturn((%s)pp);\n", typid);
266 genmkparamlist(gitemlist(t));
268 genmkparamlist(gitem(t));
271 fprintf(fc, "PP%s", gitemfunid(t));
274 fprintf(stderr,"genparamlist: funny abs syntax.\n");
281 tree t; /* of kind 'itemlist' or 'item' */
287 genmkparamdekl(gitemlist(t));
288 genmkparamdekl(gitem(t));
291 fprintf(fc, " %s PP%s;\n", gitemtypid(t), gitemfunid(t));
294 fprintf(stderr,"genmkparamdekl: funny abs syntax.\n");
301 tree t; /* of kind 'itemlist' or 'item' */
307 genmkprotodekl(gitemlist(t));
309 genmkprotodekl(gitem(t));
312 fprintf(fh, "%s", gitemtypid(t));
315 fprintf(stderr,"genmkprotodekl: funny abs syntax.\n");
328 genmkfillin(gitemlist(t));
329 genmkfillin(gitem(t));
332 fprintf(fc, "\tpp -> X%s = PP%s;\n",
333 gitemfunid(t), gitemfunid(t));
336 fprintf(stderr,"genmkfillin: funny abs syntax.\n");
342 gensels(typid, variantid, t)
351 gensels(typid, variantid, gitemlist(t));
352 gensels(typid, variantid, gitem(t));
355 fprintf(fh, "#ifdef __GNUC__\n");
357 fprintf(fh, "\nextern __inline__ %s *R%s(struct S%s *t)\n{\n",
358 gitemtypid(t), gitemfunid(t), variantid);
359 fprintf(fh, "#ifdef UGEN_DEBUG\n");
360 fprintf(fh, "\tif(t -> tag != %s)\n", variantid);
361 fprintf(fh, "\t\tfprintf(stderr,\"%s: illegal selection; was %%d\\n\", t -> tag);\n", gitemfunid(t));
362 fprintf(fh, "#endif /* UGEN_DEBUG */\n");
363 fprintf(fh, "\treturn(& t -> X%s);\n}\n", gitemfunid(t));
365 fprintf(fh, "#else /* ! __GNUC__ */\n");
368 "extern %s *R%s PROTO((struct S%s *));\n",
369 gitemtypid(t), gitemfunid(t), variantid);
371 fprintf(fc, "\n%s *R%s(t)\n struct S%s *t;\n{\n",
372 gitemtypid(t), gitemfunid(t), variantid);
373 fprintf(fc, "#ifdef UGEN_DEBUG\n");
374 fprintf(fc, "\tif(t -> tag != %s)\n", variantid);
375 fprintf(fc, "\t\tfprintf(stderr,\"%s: illegal selection; was %%d\\n\", t -> tag);\n", gitemfunid(t));
376 fprintf(fc, "#endif /* UGEN_DEBUG */\n");
377 fprintf(fc, "\treturn(& t -> X%s);\n}\n", gitemfunid(t));
379 fprintf(fh, "#endif /* ! __GNUC__ */\n\n");
382 "#define %s(xyzxyz) (*R%s((struct S%s *) (xyzxyz)))\n",
383 gitemfunid(t), gitemfunid(t), variantid);
386 fprintf(stderr,"gensels: funny abs syntax.\n");
392 /***********************************************************************/
395 gen_hs_reader(typid, deflist)
400 fprintf(fhs, "rdU_%s :: _Addr -> UgnM U_%s\n", typid, typid);
403 fprintf(fhs, "rdU_%s t\n = ioToUgnM (_ccall_ t%s t) `thenUgn` \\ tag@(I# _) ->\n", typid, typid);
404 fprintf(fhs, " if ");
405 gen_hs_rdalts(typid, deflist);
406 fprintf(fhs, " else\n\terror (\"rdU_%s: bad tag selection:\"++show tag++\"\\n\")\n", typid);
410 gen_hs_rdalts(typid, t)
416 gen_hs_rdalts(typid, gdeflist(t));
417 fprintf(fhs, " else if ");
418 gen_hs_rdalt (typid, gdef(t));
421 gen_hs_rdalt(typid, t);
424 fprintf(stderr,"gen_hs_rdalts: funny abstract syntax.\n");
430 gen_hs_rdalt(typid, t)
434 fprintf(fhs, "tag == ``%s'' then\n", gdid(t));
435 gen_hs_rdcomponents (typid, gdid(t), gditemlist(t));
436 fprintf(fhs, "\treturnUgn (U_%s ", gdid(t));
437 gen_hs_retcomponents(typid, gdid(t), gditemlist(t));
438 fprintf(fhs, ")\n"); /* end of alt */
442 gen_hs_rdcomponents(typid, variantid, t)
451 gen_hs_rdcomponents(typid, variantid, gitemlist(t));
452 gen_hs_rdcomponents(typid, variantid, gitem(t));
455 fprintf(fhs, "\tioToUgnM (_ccall_ %s t) `thenUgn` \\ x_%s ->\n",
456 gitemfunid(t), gitemfunid(t));
458 fprintf(fhs, "\trdU_%s x_%s `thenUgn` \\ y_%s ->\n",
459 gitemtypid(t), gitemfunid(t), gitemfunid(t));
461 /* fprintf(fhs, "\tif(t -> tag != %s)\n", variantid);
462 fprintf(fhs, "\t\tfprintf(stderr,\"%s: illegal selection; was %%d\\n\", t -> tag);\n", gitemfunid(t));
463 fprintf(fhs, "\treturn(& t -> X%s);\n}\n", gitemfunid(t));
467 fprintf(stderr,"gen_hs_rdcomponents: funny abs syntax.\n");
473 gen_hs_retcomponents(typid, variantid, t)
482 gen_hs_retcomponents(typid, variantid, gitemlist(t));
484 gen_hs_retcomponents(typid, variantid, gitem(t));
487 fprintf(fhs, "y_%s", gitemfunid(t));
491 fprintf(stderr,"gen_hs_retcomponents: funny abs syntax.\n");