Skip to content
Snippets Groups Projects
Commit 48d76596 authored by dcw's avatar dcw
Browse files

major change: fields now unionised.

also: TRUE and FALSE were wrong way round (oops)
and I have removed ABORT.. explicit error mesgs on stderr now.
parent 83176e62
No related branches found
No related tags found
No related merge requests found
...@@ -35,8 +35,8 @@ static void init( h, c, basename ) FILE *c, *h; char *basename; ...@@ -35,8 +35,8 @@ static void init( h, c, basename ) FILE *c, *h; char *basename;
fprintf( h, " */\n\n"); fprintf( h, " */\n\n");
fprintf( h, "typedef char *string;\n\n" ); fprintf( h, "typedef char *string;\n\n" );
fprintf( h, "typedef char BOOL;\n" ); fprintf( h, "typedef char BOOL;\n" );
fprintf( h, "#define TRUE 0\n" ); fprintf( h, "#define TRUE 1\n" );
fprintf( h, "#define FALSE 1\n\n" ); fprintf( h, "#define FALSE 0\n\n" );
fprintf( c, "/*\n" ); fprintf( c, "/*\n" );
fprintf( c, "\tAutomatically Generated by DataDec\n" ); fprintf( c, "\tAutomatically Generated by DataDec\n" );
...@@ -45,8 +45,7 @@ static void init( h, c, basename ) FILE *c, *h; char *basename; ...@@ -45,8 +45,7 @@ static void init( h, c, basename ) FILE *c, *h; char *basename;
fprintf( c, "#include <stdio.h>\n" ); fprintf( c, "#include <stdio.h>\n" );
fprintf( c, "#include \"%s.h\"\n\n\n", basename ); fprintf( c, "#include \"%s.h\"\n\n\n", basename );
fprintf( c, "#define NEW(t) ((t *)malloc(sizeof(t)))\n\n" ); fprintf( c, "#define NEW(t) ((t *)malloc(sizeof(t)))\n\n\n" );
fprintf( c, "#define ABORT(m) {printf m; exit(1);}\n\n\n" );
} }
...@@ -77,16 +76,21 @@ static void data_decls( f, dl ) FILE *f; declnlist dl; ...@@ -77,16 +76,21 @@ static void data_decls( f, dl ) FILE *f; declnlist dl;
} }
fprintf( f, "\nstruct %s_str {\n", name ); fprintf( f, "\nstruct %s_str {\n", name );
fprintf( f, "\tint\ttag;\n" ); fprintf( f, "\tint\ttag;\n" );
fprintf( f, "\tunion {\n" );
for( s = d->shapes; s; s=s->next ) for( s = d->shapes; s; s=s->next )
{ {
tag = s->tagname; if( s->params )
for( p = s->params; p; p=p->next )
{ {
fprintf( f, "\t%s\t%s_%s;\n", fprintf( f, "\t\tstruct {\n" );
p->type, tag, p->name ); for( p = s->params; p; p=p->next )
{
fprintf( f, "\t\t\t%s\t%s;\n",
p->type, p->name );
}
fprintf( f, "\t\t} %s;\n", s->tagname );
} }
} }
fprintf( f, "};\n\n" ); fprintf( f, "\t} u;\n};\n\n" );
} }
} }
...@@ -175,7 +179,7 @@ static void cons_fn_body( f, name, s ) FILE *f; char *name; shapelist s; ...@@ -175,7 +179,7 @@ static void cons_fn_body( f, name, s ) FILE *f; char *name; shapelist s;
fprintf( f, "\tnew->tag = %s_%s_tag;\n", name, tag ); fprintf( f, "\tnew->tag = %s_%s_tag;\n", name, tag );
for( p=s->params; p; p=p->next ) for( p=s->params; p; p=p->next )
{ {
fprintf( f, "\tnew->%s_%s = %s;\n", tag, p->name, p->name ); fprintf( f, "\tnew->u.%s.%s = %s;\n", tag, p->name, p->name );
} }
fprintf( f, "\treturn new;\n}\n" ); fprintf( f, "\treturn new;\n}\n" );
} }
...@@ -203,7 +207,7 @@ static void print_param( f, shape, ftype, fname ) FILE *f; char *shape, *ftype, ...@@ -203,7 +207,7 @@ static void print_param( f, shape, ftype, fname ) FILE *f; char *shape, *ftype,
{ {
char pname[200]; char pname[200];
sprintf( pname, "p->%s_%s", shape, fname ); sprintf( pname, "p->u.%s.%s", shape, fname );
if( streq( ftype, "int" ) ) if( streq( ftype, "int" ) )
{ {
fprintf( f, "\t\tfprintf( f, \"%%d\", %s );\n", pname ); fprintf( f, "\t\tfprintf( f, \"%%d\", %s );\n", pname );
...@@ -276,10 +280,11 @@ static void print_fns( f, d ) FILE *f ; declnlist d; ...@@ -276,10 +280,11 @@ static void print_fns( f, d ) FILE *f ; declnlist d;
print_fn_shape( f, d->name, s ); print_fn_shape( f, d->name, s );
} }
fprintf( f, "\tdefault:\n" ); fprintf( f, "\tdefault:\n" );
fprintf( f, "\t\tfprintf( stderr," );
fprintf( f, fprintf( f,
"\tABORT((\"print_%s: impossible tag %%d\\n\",p->tag));\n", " \"print_%s: impossible tag %%d\\n\", p->tag );\n",
d->name ); d->name );
fprintf( f, "\t}\n}\n\n" ); fprintf( f, "\t\texit(1);\n\t}\n}\n\n" );
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment