Commit d78291bf authored by dcw's avatar dcw
Browse files

added protos for optimize, count_empty_non etc.

then added the indentation system,
ie. numtabs, indent(), outdent(), line().
seems to work so far.
still empty unions generated though.
parent a3c84233
......@@ -14,6 +14,9 @@ static void cons_fns( FILE * , declnlist );
static void print_fns( FILE * , declnlist );
static void print_fn_shape( FILE * , char * , shapelist );
static void print_param( FILE *, char * , char * , char * );
static void count_empty_non( shapelist , int *, int * );
static void optimize( declnlist );
static void line( char *, long, long, long, long );
#else
static void init();
static void data_decls();
......@@ -25,9 +28,33 @@ static void cons_fns();
static void print_fns();
static void print_fn_shape();
static void print_param();
static void count_empty_non();
static void optimize();
static void line();
#endif
static int numtabs = 0;
static FILE *outfile;
#define indent() numtabs++
#define outdent() numtabs--
#define usefile( f ) outfile = f
/*VARARGS*/
static void line( fmt, a, b, c, d ) char *fmt; long a, b, c, d;
{
int i;
for( i=numtabs; i; i-- ) fputc( '\t', outfile );
fprintf( outfile, fmt, a, b, c, d );
fputc( '\n', outfile );
}
static void init( h, c, basename ) FILE *c, *h; char *basename;
{
fprintf( h, "/*\n" );
......@@ -45,6 +72,8 @@ static void init( h, c, basename ) FILE *c, *h; char *basename;
fprintf( c, "#include <stdio.h>\n" );
fprintf( c, "#include \"%s.h\"\n\n\n", basename );
numtabs = 0;
}
......@@ -72,23 +101,35 @@ static void data_decls( f, dl ) FILE *f; declnlist dl;
fprintf( f, "#define %s_%s_tag %d\n",
name, s->tagname, n++ );
}
fprintf( f, "\nstruct %s_str {\n", name );
fprintf( f, "\tint\ttag;\n" );
fprintf( f, "\tunion {\n" );
fputc( '\n', f );
usefile( f );
numtabs = 0;
line( "struct %s_str {", name );
indent();
line( "int\ttag;" );
line( "union {" );
indent();
for( s = d->shapes; s; s=s->next )
{
if( s->params )
{
fprintf( f, "\t\tstruct {\n" );
line( "struct {" );
indent();
for( p = s->params; p; p=p->next )
{
fprintf( f, "\t\t\t%s\t%s;\n",
line( "%s\t%s;",
p->type, p->name );
}
fprintf( f, "\t\t} %s;\n", s->tagname );
outdent();
line( "} %s;", s->tagname );
}
}
fprintf( f, "\t} u;\n};\n\n" );
outdent();
line( "} u;" );
outdent();
line( "};\n" );
}
}
......@@ -172,14 +213,22 @@ static void cons_fn_body( f, name, s ) FILE *f; char *name; shapelist s;
paramlist p;
tag = s->tagname;
fprintf( f, "{\n\t%s new;\n\n", name );
fprintf( f, "\tnew = NEW( struct %s_str );\n", name );
fprintf( f, "\tnew->tag = %s_%s_tag;\n", name, tag );
numtabs = 0;
usefile( f );
line( "{" );
indent();
line( "%s new;\n", name );
line( "new = NEW( struct %s_str );", name );
line( "new->tag = %s_%s_tag;", name, tag );
for( p=s->params; p; p=p->next )
{
fprintf( f, "\tnew->u.%s.%s = %s;\n", tag, p->name, p->name );
line( "new->u.%s.%s = %s;", tag, p->name, p->name );
}
fprintf( f, "\treturn new;\n}\n" );
line( "return new;" );
outdent();
line( "}" );
}
......@@ -201,44 +250,45 @@ static void cons_fns( f, d ) FILE *f; declnlist d;
}
static void print_param( f, shape, ftype, fname ) FILE *f; char *shape, *ftype, *fname;
static void print_param( shape, ftype, fname ) char *shape, *ftype, *fname;
{
char pname[200];
sprintf( pname, "p->u.%s.%s", shape, fname );
if( streq( ftype, "int" ) )
{
fprintf( f, "\t\tfprintf( f, \"%%d\", %s );\n", pname );
line( "fprintf( f, \"%%d\", %s );", pname );
} else if( streq( ftype, "char" ) )
{
fprintf( f, "\t\tfprintf( f, \"%%c\", %s );\n", pname );
line( "fprintf( f, \"%%c\", %s );", pname );
} else if( streq( ftype, "BOOL" ) )
{
fprintf( f, "\t\tfprintf( f, \"%%s\", %s?\"true\":\"false\" );\n",
line( "fprintf( f, \"%%s\", %s?\"true\":\"false\" );",
pname );
} else if( streq( ftype, "string" ) )
{
fprintf( f, "\t\tfprintf( f, \"%%s\", %s );\n", pname );
line( "fprintf( f, \"%%s\", %s );", pname );
} else
{
fprintf( f, "\t\tprint_%s( f, %s );\n", ftype, pname );
line( "print_%s( f, %s );", ftype, pname );
}
}
static void print_fn_shape( f, typename, s ) FILE *f; char *typename; shapelist s;
static void print_fn_shape( typename, s ) char *typename; shapelist s;
{
printlist pl;
paramlist p;
int n;
fprintf( f, "\tcase %s_%s_tag:\n", typename, s->tagname );
line( "case %s_%s_tag:", typename, s->tagname );
indent();
if( s->pl == NULL )
{
for( p = s->params; p; p = p->next )
{
print_param( f, s->tagname, p->type, p->name );
print_param( s->tagname, p->type, p->name );
}
} else
{
......@@ -246,7 +296,7 @@ static void print_fn_shape( f, typename, s ) FILE *f; char *typename; shapelist
{
if( pl->item->tag == printitem_is_str )
{
fprintf( f, "\t\tfprintf( f, \"%s\" );\n",
line( "fprintf( f, \"%s\" );",
pl->item->str );
} else
{
......@@ -256,11 +306,12 @@ static void print_fn_shape( f, typename, s ) FILE *f; char *typename; shapelist
ASSERT( p != NULL,
("datadec: bad printitem in shape %s of type %s\n",s->tagname,typename));
}
print_param( f, s->tagname, p->type, p->name );
print_param( s->tagname, p->type, p->name );
}
}
}
fprintf( f, "\t\tbreak;\n" );
line( "break;" );
outdent();
}
......@@ -268,21 +319,30 @@ static void print_fns( f, d ) FILE *f ; declnlist d;
{
shapelist s;
usefile( f );
numtabs = 0;
for( ; d; d = d->next )
{
fprintf( f, "void print_%s( f, p ) FILE *f; %s p;\n",
d->name, d->name );
fprintf( f, "{\n\tswitch( p->tag )\n\t{\n" );
line( "void print_%s( f, p ) FILE *f; %s p;",d->name, d->name );
line( "{" );
indent();
line( "switch( p->tag )" );
line( "{" );
for( s = d->shapes; s; s = s->next )
{
print_fn_shape( f, d->name, s );
print_fn_shape( d->name, s );
}
fprintf( f, "\tdefault:\n" );
fprintf( f, "\t\tfprintf( stderr," );
fprintf( f,
" \"print_%s: impossible tag %%d\\n\", p->tag );\n",
d->name );
fprintf( f, "\t\texit(1);\n\t}\n}\n\n" );
line( "default:" );
indent();
line( "fprintf( stderr," );
line( " \"print_%s: impossible tag %%d\\n\", p->tag );",
d->name );
line( "exit(1);" );
outdent();
line( "}" );
outdent();
line( "}\n" );
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment