Commit 3b97bee6 authored by dcw's avatar dcw

rearranged order of procedures into better top down order

parent 100008ae
......@@ -12,34 +12,34 @@ stat %
#ifdef HASPROTOS
static void line( char * , long , long , long , long );
static void declfields( paramlist );
static void h_declns( char * , char * , declnlist );
static void c_declns( char * , char * , declnlist );
static void data_decls( declnlist );
static void one_proto( char * , shapelist , BOOL );
static void declfields( paramlist );
static void protos( declnlist , BOOL );
static void one_proto( char * , shapelist , BOOL );
static void cons_fns( declnlist );
static void cons_fn_hdr( char * , shapelist );
static void cons_fn_body( declnlist , char * , shapelist );
static void cons_fns( declnlist );
static void print_param( char * , paramlist , BOOL );
static void print_all_params( declnlist , shapelist );
static void print_fn_shape( declnlist , shapelist );
static void print_fns( declnlist );
static void h_declns( char * , char * , declnlist );
static void c_declns( char * , char * , declnlist );
static void print_fn_shape( declnlist , shapelist );
static void print_all_params( declnlist , shapelist );
static void print_param( char * , paramlist , BOOL );
#else
static void line();
static void declfields();
static void h_declns();
static void c_declns();
static void data_decls();
static void one_proto();
static void declfields();
static void protos();
static void one_proto();
static void cons_fns();
static void cons_fn_hdr();
static void cons_fn_body();
static void cons_fns();
static void print_param();
static void print_all_params();
static void print_fn_shape();
static void print_fns();
static void h_declns();
static void c_declns();
static void print_fn_shape();
static void print_all_params();
static void print_param();
#endif
......@@ -67,15 +67,142 @@ static void line( fmt, a, b, c, d ) char *fmt; long a, b, c, d;
}
static void declfields( p ) paramlist p;
void make_declns( exports, globals, d, basename ) declnlist d; char *exports, *globals, *basename;
{
for( ; p != NULL; p=p->next )
printf( "datadec: Making data declarations in %s.[ch]\n", basename );
h_declns( basename, exports, d );
c_declns( basename, globals, d );
}
static void h_declns( basename, exports, d ) char *basename, *exports; declnlist d;
{
char tempname[256];
char *exportptr;
FILE *hfile;
declnlist dl;
sprintf( tempname, "%s.h", basename );
hfile = fopen( tempname, "w" );
ASSERT( hfile != NULL, ("datadec: can't create '%s'\n",tempname) );
usefile( hfile );
line( "/*" );
line( " * Automatically Generated by DataDec" );
line( " */\n" );
line( "typedef char *string;\n" );
line( "typedef char BOOL;" );
line( "#define TRUE 1" );
line( "#define FALSE 0\n" );
line( "#define NEW(t) ((t)malloc(sizeof(struct t)))\n\n" );
if( *exports != '\0' )
{
line( "%s\t%s;", p->type, p->name );
int i;
line( "/* Contents of EXPORT section */" );
for( exportptr=exports; *exportptr; exportptr++ )
{
if( *exportptr == '@' && exportptr[1] == '@'
&& exportptr[2] == '\n' )
{
exportptr += 2;
break;
}
outchar( *exportptr );
}
nl();
nl();
}
data_decls( d );
for( dl = d; dl != NULL; dl=dl->next )
{
if( dl->UseNull )
{
line( "#define %s_%s() ((%s)NULL)",
dl->name, dl->shapes->name, dl->name );
}
}
line( "#ifdef HASPROTOS" );
protos( d, TRUE );
line( "#else" );
protos( d, FALSE );
line( "#endif" );
if( *exportptr != '\0' )
{
nl();
line( "/* Remaining contents of EXPORT section */" );
line( exportptr );
nl();
}
fclose( hfile );
}
static void c_declns( basename, globals, d ) char *basename, *globals; declnlist d;
{
char tempname[256];
FILE *cfile;
char *globalptr;
sprintf( tempname, "%s.c", basename );
cfile = fopen( tempname, "w" );
ASSERT( cfile != NULL, ("datadec: can't create '%s'\n",tempname) );
usefile( cfile );
line( "/*" );
line( " * Automatically Generated by DataDec" );
line( " */\n" );
line( "#include <stdio.h>" );
line( "#include <malloc.h>" );
line( "#include \"%s.h\"\n\n", basename );
if( *globals != '\0' )
{
line( "/* Contents of GLOBAL section */" );
for( globalptr=globals; *globalptr; globalptr++ )
{
if( *globalptr == '@' && globalptr[1] == '@'
&& globalptr[2] == '\n' )
{
globalptr += 2;
break;
}
outchar( *globalptr );
}
nl();
nl();
}
cons_fns( d );
print_fns( d );
if( *globalptr != '\0' )
{
nl();
nl();
line( "/* Remaining contents of GLOBAL section */" );
line( globalptr );
nl();
}
fclose( cfile );
}
/* --------------------------- The types ---------------------------------- */
static void data_decls( decs ) declnlist decs;
{
declnlist d;
......@@ -155,6 +282,47 @@ static void data_decls( decs ) declnlist decs;
}
static void declfields( p ) paramlist p;
{
for( ; p != NULL; p=p->next )
{
line( "%s\t%s;", p->type, p->name );
}
}
/* ------------------------ Constructor functions ------------------------- */
static void protos( d, prot ) declnlist d; BOOL prot;
{
shapelist s;
shapelist shapes;
for( ; d != NULL; d = d->next )
{
if( d->Struct )
{
shapes = d->shapes;
if( d->UseNull )
{
shapes = shapes->next;
}
for( s = shapes; s != NULL; s = s->next )
{
one_proto( d->name, s, prot );
}
}
fprintf( outfile, "extern void print_%s(", d->name );
if( prot )
{
fprintf( outfile, " %s ", d->name );
}
fputs( ");\n", outfile );
}
}
static void one_proto( name, s, prot ) char *name; shapelist s; BOOL prot;
{
paramlist p;
......@@ -181,31 +349,29 @@ static void one_proto( name, s, prot ) char *name; shapelist s; BOOL prot;
}
static void protos( d, prot ) declnlist d; BOOL prot;
static void cons_fns( d ) declnlist d;
{
shapelist s;
shapelist shapes;
for( ; d != NULL; d = d->next )
for( ; d != NULL; d=d->next )
{
if( d->Struct )
{
/* We need constructor functions */
shapes = d->shapes;
if( d->UseNull )
{
/* Null cons func already made */
shapes = shapes->next;
}
for( s = shapes; s != NULL; s = s->next )
for( s = shapes; s != NULL; s=s->next )
{
one_proto( d->name, s, prot );
cons_fn_hdr( d->name, s );
cons_fn_body( d, d->name, s );
fprintf( outfile, "\n\n" );
}
}
fprintf( outfile, "extern void print_%s(", d->name );
if( prot )
{
fprintf( outfile, " %s ", d->name );
}
fputs( ");\n", outfile );
}
}
......@@ -262,116 +428,7 @@ static void cons_fn_body( d, name, s ) declnlist d; char *name; shapelist s;
}
static void cons_fns( d ) declnlist d;
{
shapelist s;
shapelist shapes;
for( ; d != NULL; d=d->next )
{
if( d->Struct )
{
/* We need constructor functions */
shapes = d->shapes;
if( d->UseNull )
{
/* Null cons func already made */
shapes = shapes->next;
}
for( s = shapes; s != NULL; s=s->next )
{
cons_fn_hdr( d->name, s );
cons_fn_body( d, d->name, s );
fprintf( outfile, "\n\n" );
}
}
}
}
static void print_param( shape, p, Union ) char *shape; paramlist p; BOOL Union;
{
char pname[200];
if( Union )
{
sprintf( pname, "p->u.%s.%s", shape, p->name );
} else
{
sprintf( pname, "p->%s", p->name );
}
if( streq( p->type, "int" ) )
{
line( "fprintf( f, \"%%d\", %s );", pname );
} else if( streq( p->type, "char" ) )
{
line( "fputc( %s, f );", pname );
} else if( streq( p->type, "BOOL" ) )
{
line( "fputs( %s?\"true\":\"false\", f );", pname );
} else if( streq( p->type, "string" ) )
{
line( "fputs( %s, f );", pname );
} else
{
line( "print_%s( f, %s );", p->type, pname );
}
}
static void print_all_params( d, s ) declnlist d; shapelist s;
{
printlist pl;
paramlist p;
int n;
if( s->pl == NULL )
{
line( "fputs( \"%s\", f );", s->name );
if( s->params )
{
line( "fputc( '(', f );" );
}
for( p = s->params; p != NULL; p = p->next )
{
print_param( s->name, p, d->Union );
}
if( s->params )
{
line( "fputc( ')', f );" );
}
} else
{
for( pl = s->pl; pl != NULL; pl = pl->next )
{
if( pl->item->tag == printitem_is_str )
{
line( "fputs( \"%s\", f );",
pl->item->str );
} else
{
n = pl->item->num;
for( p = s->params; --n; p = p->next )
{
ASSERT( p != NULL,
("datadec: bad printitem in shape %s of type %s\n",s->name,d->name));
}
print_param( s->name, p, d->Union );
}
}
}
}
static void print_fn_shape( d, s ) declnlist d; shapelist s;
{
line( "case %s_%s_tag:", d->name, s->name );
indent();
print_all_params( d, s );
line( "break;" );
outdent();
}
/* -------------------------- Print functions ----------------------------- */
static void print_fns( d ) declnlist d;
......@@ -440,134 +497,86 @@ static void print_fns( d ) declnlist d;
}
void make_declns( exports, globals, d, basename ) declnlist d; char *exports, *globals, *basename;
static void print_fn_shape( d, s ) declnlist d; shapelist s;
{
printf( "datadec: Making data declarations in %s.[ch]\n", basename );
h_declns( basename, exports, d );
c_declns( basename, globals, d );
line( "case %s_%s_tag:", d->name, s->name );
indent();
print_all_params( d, s );
line( "break;" );
outdent();
}
static void h_declns( basename, exports, d ) char *basename, *exports; declnlist d;
static void print_all_params( d, s ) declnlist d; shapelist s;
{
char tempname[256];
char *exportptr;
FILE *hfile;
declnlist dl;
sprintf( tempname, "%s.h", basename );
hfile = fopen( tempname, "w" );
ASSERT( hfile != NULL, ("datadec: can't create '%s'\n",tempname) );
usefile( hfile );
line( "/*" );
line( " * Automatically Generated by DataDec" );
line( " */\n" );
line( "typedef char *string;\n" );
line( "typedef char BOOL;" );
line( "#define TRUE 1" );
line( "#define FALSE 0\n" );
line( "#define NEW(t) ((t)malloc(sizeof(struct t)))\n\n" );
printlist pl;
paramlist p;
int n;
if( *exports != '\0' )
if( s->pl == NULL )
{
int i;
line( "/* Contents of EXPORT section */" );
for( exportptr=exports; *exportptr; exportptr++ )
line( "fputs( \"%s\", f );", s->name );
if( s->params )
{
if( *exportptr == '@' && exportptr[1] == '@'
&& exportptr[2] == '\n' )
{
exportptr += 2;
break;
}
outchar( *exportptr );
line( "fputc( '(', f );" );
}
nl();
nl();
}
data_decls( d );
for( dl = d; dl != NULL; dl=dl->next )
{
if( dl->UseNull )
for( p = s->params; p != NULL; p = p->next )
{
line( "#define %s_%s() ((%s)NULL)",
dl->name, dl->shapes->name, dl->name );
print_param( s->name, p, d->Union );
}
}
line( "#ifdef HASPROTOS" );
protos( d, TRUE );
line( "#else" );
protos( d, FALSE );
line( "#endif" );
if( *exportptr != '\0' )
{
nl();
line( "/* Remaining contents of EXPORT section */" );
line( exportptr );
nl();
}
fclose( hfile );
}
static void c_declns( basename, globals, d ) char *basename, *globals; declnlist d;
{
char tempname[256];
FILE *cfile;
char *globalptr;
sprintf( tempname, "%s.c", basename );
cfile = fopen( tempname, "w" );
ASSERT( cfile != NULL, ("datadec: can't create '%s'\n",tempname) );
usefile( cfile );
line( "/*" );
line( " * Automatically Generated by DataDec" );
line( " */\n" );
line( "#include <stdio.h>" );
line( "#include <malloc.h>" );
line( "#include \"%s.h\"\n\n", basename );
if( *globals != '\0' )
if( s->params )
{
line( "fputc( ')', f );" );
}
} else
{
line( "/* Contents of GLOBAL section */" );
for( globalptr=globals; *globalptr; globalptr++ )
for( pl = s->pl; pl != NULL; pl = pl->next )
{
if( *globalptr == '@' && globalptr[1] == '@'
&& globalptr[2] == '\n' )
if( pl->item->tag == printitem_is_str )
{
globalptr += 2;
break;
line( "fputs( \"%s\", f );",
pl->item->str );
} else
{
n = pl->item->num;
for( p = s->params; --n; p = p->next )
{
ASSERT( p != NULL,
("datadec: bad printitem in shape %s of type %s\n",s->name,d->name));
}
print_param( s->name, p, d->Union );
}
outchar( *globalptr );
}
nl();
nl();
}
}
cons_fns( d );
print_fns( d );
static void print_param( shape, p, Union ) char *shape; paramlist p; BOOL Union;
{
char pname[200];
if( *globalptr != '\0' )
if( Union )
{
nl();
nl();
line( "/* Remaining contents of GLOBAL section */" );
line( globalptr );
nl();
sprintf( pname, "p->u.%s.%s", shape, p->name );
} else
{
sprintf( pname, "p->%s", p->name );
}
fclose( cfile );
if( streq( p->type, "int" ) )
{
line( "fprintf( f, \"%%d\", %s );", pname );
} else if( streq( p->type, "char" ) )
{
line( "fputc( %s, f );", pname );
} else if( streq( p->type, "BOOL" ) )
{
line( "fputs( %s?\"true\":\"false\", f );", pname );
} else if( streq( p->type, "string" ) )
{
line( "fputs( %s, f );", pname );
} else
{
line( "print_%s( f, %s );", p->type, pname );
}
}
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