Commit db4af5ff authored by dcw's avatar dcw

removed tSTAR, started building up to the print stuff

parsing done, just needs decs updating now..
parent 010de9a3
......@@ -8,23 +8,14 @@
*
******* Description:
*
* This program builds C data declarations from a series
* of high-level data declarations.
* This program builds C data declarations, construction functions
* and print functions from a series of HOPE/Miranda style recursive
* data declarations. (with hints on printing)
*
* The C declarations produced are placed in pair of files
* The output produced is placed in pair of files
* ( x.c and x.h ) which together form a module provided the
* relevent data types.
*
******* History
*
* Version Who Date Comments
* ======= === ==== ========
*
* 1.0 dcw 31st Dec 1987 Created
* 1.1 dcw 1st Jan 1988 Recreated after screwup
* 1.2 dcw 2nd Jan 1988 Added lexfile stuff
*/
#include <dcw.h>
#include "struct.h"
......@@ -40,7 +31,7 @@ main( argc, argv ) int argc; char **argv;
int len;
char *basename;
ASSERT( argc==2 || argc==3, ("Usage: datadecs outfile [infile]\n") );
ASSERT( argc==2 || argc==3, ("Usage: datadec outfile [infile]\n") );
basename = argv[1];
len = strlen( basename );
......@@ -50,21 +41,21 @@ main( argc, argv ) int argc; char **argv;
sprintf( tempname, "%s.c", basename );
cfile = fopen( tempname, "w" );
ASSERT( cfile != NULL, ("datadecs: can't create '%s'\n",tempname) );
ASSERT( cfile != NULL, ("datadec: can't create '%s'\n",tempname) );
sprintf( tempname, "%s.h", basename );
hfile = fopen( tempname, "w" );
ASSERT( hfile != NULL, ("datadecs: can't create '%s'\n",tempname) );
ASSERT( hfile != NULL, ("datadec: can't create '%s'\n",tempname) );
if( argc == 3 ) {
lexfile = fopen( argv[2], "r" );
ASSERT( lexfile != NULL,
("datadecs: can't open '%s'\n",argv[2]) );
("datadec: can't open '%s'\n",argv[2]) );
} else {
lexfile = stdin;
}
ASSERT( parse_declns( &declns ),
("datadecs: can't parse input properly\n") );
("datadec: can't parse input properly\n") );
make_declns( declns, cfile, hfile, basename );
fclose( cfile );
fclose( hfile );
......
......@@ -10,14 +10,8 @@
*
******* Tokens:
*
* tERROR tEOF tSEMI tID tEQ tOR tOPENBR tCOMMA tCLOSEBR tSTAR
*
******* History
*
* Version Who Date Comments
* ======= === ==== ========
*
* 1.0 dcw 2nd Jan 1988 Created
* tERROR tEOF tSEMI tID tEQ tOR tOPENBR tCOMMA tCLOSEBR
* tSTR tNUM
*/
......@@ -29,17 +23,18 @@
#ifdef DEBUG
static char *tokenname[] = {
"tERROR", "tEOF", "tSEMI", "tID", "tEQ", "tOR", "tOPENBR",
"tCOMMA", "tCLOSEBR", "tSTAR"
"tCOMMA", "tCLOSEBR", "tSTR", "tNUM"
};
#endif
/* ----------------- Exported variables ---------------- */
#define MAXIDSIZE 32
#define MAXIDSIZE 100
int lineno = 1;
char lexidval[ MAXIDSIZE ];
int lexintval;
FILE *lexfile;
......@@ -82,32 +77,64 @@ TOKEN nexttok()
int c;
int pos;
if( havepushedtok ) {
if( havepushedtok )
{
havepushedtok = FALSE;
} else {
} else
{
white_space();
c = getc(lexfile);
if( c==EOF )
{
curtok = tEOF;
} else if( isalpha( c ) )
switch( c )
{
curtok = tID;
for( pos=0; isalpha(c) || isdigit(c); )
case EOF: curtok = tEOF; break;
case ';': curtok = tSEMI; break;
case '(': curtok = tOPENBR; break;
case ')': curtok = tCLOSEBR; break;
case ',': curtok = tCOMMA; break;
case '|': curtok = tOR; break;
case '=': curtok = tEQ; break;
case '"':
for( pos=0; (c=getc(lexfile)) != '\"'; )
{
if( pos<MAXIDSIZE-1 )
{
lexidval[pos++] = c;
}
}
while( pos<MAXIDSIZE )
{
lexidval[pos++] = '\0';
}
break;
default:
if( isalpha( c ) )
{
curtok = tID;
for( pos=0; isalpha(c) || isdigit(c); )
{
if( pos<MAXIDSIZE-1 )
lexidval[pos++] = c;
c=getc(lexfile);
}
ungetc( c, lexfile );
while( pos<MAXIDSIZE )
lexidval[pos++] = '\0';
} else if( isdigit( c ) )
{
int t;
curtok = tNUM;
for( t=0; isdigit(c); t = t*10 + (c-'0'))
{
c=getc(lexfile);
}
ungetc( c, lexfile );
lexintval = t;
} else
{
if( pos<MAXIDSIZE-1 ) lexidval[pos++] = c;
c=getc(lexfile);
}
ungetc( c, lexfile );
while( pos<MAXIDSIZE ) lexidval[pos++] = '\0';
} else if( c==';' ) curtok = tSEMI;
else if( c=='(' ) curtok = tOPENBR;
else if( c==')' ) curtok = tCLOSEBR;
else if( c==',' ) curtok = tCOMMA;
else if( c=='|' ) curtok = tOR;
else if( c=='=' ) curtok = tEQ;
else if( c=='*' ) curtok = tSTAR;
else curtok = tERROR;
curtok = tERROR;
}
}
}
#ifdef DEBUG
printf( "returning token %s\n", tokenname[ curtok ] );
......
......@@ -14,7 +14,7 @@
typedef enum {
tERROR, tEOF, tSEMI, tID, tEQ, tOR,
tOPENBR, tCOMMA, tCLOSEBR, tSTAR
tOPENBR, tCOMMA, tCLOSEBR, tSTR, tNUM
} TOKEN;
......@@ -22,6 +22,7 @@ typedef enum {
/* ----------------- Exported variables ---------------- */
extern char lexidval[];
extern int lexintval;
extern int lineno;
extern FILE *lexfile;
......
......@@ -13,22 +13,10 @@
* declns = ne-list( decln ) tEOF
* decln = tID tEQ shapes tSEMI
* shapes = ne-sep-list( shape, tOR )
* shape = tID [ tOPENBR params tCLOSEBR ]
* shape = tID [ tOPENBR params tCLOSEBR ] [ list( printitem ) ]
* params = ne-sep-list( param, tCOMMA )
* param = tID list( tSTAR ) tID
*
******* History
*
* Version Who Date Comments
* ======= === ==== ========
*
* 1.0 dcw 1st Jan 1988 Created
* 1.1 dcw 24th Apr 1989 Major rewrite.
* 1.2 dcw 14th Nov 1989 Fixed a silly buggy wug..
* (empty params => previous params
* retained).
* Added MUSTBE & expected to simplify
* logic.
* param = tID tID
* printitem = tSTR | tNUM
*/
......@@ -41,15 +29,19 @@
#ifdef HASPROTOS
static BOOL parse_decln( char ** , shapelist * );
static BOOL parse_shapes( shapelist * );
static BOOL parse_shape( char ** , paramlist * );
static BOOL parse_shape( char ** , paramlist *, printlist * );
static BOOL parse_params( paramlist * );
static BOOL parse_param( char ** , int * , char ** );
static BOOL parse_param( char ** , char ** );
static BOOL parse_printlist( printlist * );
static BOOL parse_printitem( printitem * );
#else
static BOOL parse_decln();
static BOOL parse_shapes();
static BOOL parse_shape();
static BOOL parse_params();
static BOOL parse_param();
static BOOL parse_printlist();
static BOOL parse_printitem();
#endif
......@@ -115,15 +107,16 @@ static BOOL parse_shapes( sp ) shapelist *sp;
}
/* shape = tID [ tOPENBR params tCLOSEBR ] */
/* shape = tID [ tOPENBR params tCLOSEBR ] [ printlist ] */
static BOOL parse_shape( tagname, pl ) char **tagname; paramlist *pl;
static BOOL parse_shape( tagname, pl, print ) char **tagname; paramlist *pl; printlist *print;
{
*pl = NULL;
*print = NULL;
MUSTBE( tID, "shape name" );
COPYOF( *tagname, lexidval );
*pl = NULL;
if( nexttok() != tOPENBR )
{
ungettok();
......@@ -134,6 +127,8 @@ static BOOL parse_shape( tagname, pl ) char **tagname; paramlist *pl;
MUSTBE( tCLOSEBR, "')'" );
if( ! parse_printlist( print ) ) return FALSE;
return TRUE;
}
......@@ -142,13 +137,12 @@ static BOOL parse_shape( tagname, pl ) char **tagname; paramlist *pl;
static BOOL parse_params( pp ) paramlist *pp;
{
int stars;
char *type;
char *name;
for(;;) {
if( !parse_param( &type, &stars, &name ) ) return FALSE;
*pp = build_paramlist( type, stars, name, NULL );
if( !parse_param( &type, &name ) ) return FALSE;
*pp = build_paramlist( type, name, NULL );
if( nexttok() != tCOMMA ) break;
pp = &( (*pp)->next );
}
......@@ -157,16 +151,49 @@ static BOOL parse_params( pp ) paramlist *pp;
}
/* param = tID list( tSTAR ) tID */
/* printlist = list( printitem ) */
/* NB: Never fails, cos items are one token long */
static BOOL parse_printlist( pp ) printlist *pp;
{
printitem item;
*pp = NULL;
while( parse_printitem( &item ) )
{
*pp = build_printlist( item, NULL );
pp = &( (*pp)->next );
}
return TRUE;
}
/* printitem = tSTR | tNUM */
static BOOL parse_param( type, stars, name ) char **type,**name; int *stars;
static BOOL parse_printitem( item ) printitem *item;
{
switch( nexttok() ) {
case tSTR:
*item = build_printitem_str( lexidval );
return TRUE;
case tNUM:
*item = build_printitem_num( lexintval );
return TRUE;
default:
ungettok();
return FALSE;
}
}
/* param = tID tID */
static BOOL parse_param( type, name ) char **type, **name;
{
MUSTBE( tID, "Field type" );
COPYOF( *type, lexidval );
for( *stars = 0; nexttok()==tSTAR; (*stars)++ );
ungettok();
MUSTBE( tID, "Field name" );
COPYOF( *name, lexidval );
......
......@@ -10,16 +10,8 @@
*
* This module provides the internal structures needed
* for the data declaration builder. It is itself the
* sort of output generated by the program.
*
******* History
*
* Version Who Date Comments
* ======= === ==== ========
*
* 1.0 dcw 31st Dec 1987 Created
* 1.1 dcw 1st Jan 1988 Recreated after screwup
* 1.2 dcw 24th Apr 1989 Had a look again.. lowercased the lot
* sort of output generated by the program (although a
* bit optimized!)
*/
......@@ -38,19 +30,6 @@ declnlist build_declnlist( name, s, next ) char *name; shapelist s; declnlist ne
}
void free_declnlist( d ) declnlist d;
{
declnlist temp;
for( ; d!=NULL; d=temp ) {
temp = d->next;
free( d->name );
free_shapelist( d->shapes );
free( d );
}
}
void print_declnlist( d ) declnlist d;
{
for( ; d!=NULL; d=d->next ) {
......@@ -61,83 +40,109 @@ void print_declnlist( d ) declnlist d;
}
shapelist build_shapelist( id, p, next ) char *id; paramlist p; shapelist next;
shapelist build_shapelist( id, p, pl, next ) char *id; paramlist p; printlist pl; shapelist next;
{
shapelist new = NEW( struct shapelist_str );
COPYOF( new->tagname, id );
new->params = p;
new->pl = pl;
new->next = next;
return new;
}
void free_shapelist( s ) shapelist s;
{
shapelist temp;
for( ; s!=NULL; s=temp ) {
temp = s->next;
free( s->tagname );
free_paramlist( s->params );
free( s );
}
}
void print_shapelist( s ) shapelist s;
{
if( s != NULL ) {
printf( "%s( ", s->tagname );
print_paramlist( s->params );
printf( " )" );
printf( " ) " );
print_printlist( s->pl );
for( s=s->next; s!=NULL; s=s->next ) {
printf( " or %s( ", s->tagname );
print_paramlist( s->params );
printf( " )" );
printf( " ) " );
print_printlist( s->pl );
}
}
}
paramlist build_paramlist( type, stars, id, next ) char *type, *id; int stars; paramlist next;
paramlist build_paramlist( type, id, next ) char *type, *id; paramlist next;
{
paramlist new = NEW( struct paramlist_str );
COPYOF( new->type, type );
COPYOF( new->name, id );
new->stars = stars;
new->next = next;
return new;
}
void free_paramlist( p ) paramlist p;
void print_paramlist( p ) paramlist p;
{
paramlist temp;
while( p!=NULL ) {
temp = p->next;
free( p->type );
free( p->name );
free( p );
p = temp;
if( p != NULL ) {
printf( "%s %s", p->type, p->name );
for( p=p->next; p!=NULL; p=p->next ) {
printf( ", %s %s", p->type, p->name );
}
}
}
void print_paramlist( p ) paramlist p;
printitem build_printitem_str( s ) char *s;
{
int i;
printitem new = NEW( struct printitem_str );
if( p != NULL ) {
printf( "%s ", p->type );
for( i= p->stars; i>0; i-- ) putchar( '*' );
printf( "%s", p->name );
for( p=p->next; p!=NULL; p=p->next ) {
printf( ", %s ", p->type );
for( i= p->stars; i>0; i-- ) putchar( '*' );
printf( "%s", p->name );
}
}
new->tag = printitem_is_str;
new->str = s;
return new;
}
printitem build_printitem_num( n ) int n;
{
printitem new = NEW( struct printitem_str );
new->tag = printitem_is_num;
new->num = n;
return new;
}
void print_printitem( i ) printitem i;
{
switch( i->tag )
{
case printitem_is_num:
printf( "%d ", i->num );
break;
case printitem_is_str:
printf( "\"%s\" ", i->str );
break;
default:
ABORT( ("datadec: print_printitem called with printitem tag = %d\n", i->tag ) );
}
}
printlist build_printlist( h, t ) printitem h; printlist t;
{
printlist new = NEW( struct printlist_str );
new->item = h;
new->next = t;
return new;
}
void print_printlist( p ) printlist p;
{
for( ; p!=NULL; p=p->next )
{
print_printitem( p->item );
}
}
......@@ -14,52 +14,76 @@
*/
typedef struct printitem_str *printitem;
#define printitem_is_str 0
#define printitem_is_num 1
struct printitem_str {
int tag;
int num;
char *str;
};
typedef struct printlist_str *printlist;
struct printlist_str {
printlist next;
printitem item;
};
typedef struct paramlist_str *paramlist;
struct paramlist_str {
paramlist next;
char *type;
int stars;
char *name;
paramlist next;
};
typedef struct shapelist_str *shapelist;
struct shapelist_str {
shapelist next;
char *tagname;
paramlist params;
shapelist next;
printlist pl;
};
typedef struct declnlist_str *declnlist;
struct declnlist_str {
declnlist next;
char *name;
shapelist shapes;
declnlist next;
};
#ifdef HASPROTOS
extern declnlist build_declnlist( char * , shapelist , declnlist );
extern void free_declnlist( declnlist );
extern void print_declnlist( declnlist );
extern shapelist build_shapelist( char * , paramlist , shapelist );
extern void free_shapelist( shapelist );
extern shapelist build_shapelist( char * , paramlist , printlist , shapelist );
extern void print_shapelist( shapelist );
extern paramlist build_paramlist( char * , int , char * , paramlist );
extern void free_paramlist( paramlist );
extern paramlist build_paramlist( char * , char * , paramlist );
extern void print_paramlist( paramlist );
extern printlist build_printlist( printitem , printlist );
extern void print_printlist( printlist );
extern printitem build_printitem_str( char * );
extern printitem build_printitem_num( int );
extern void print_printitem( printitem );
#else
extern declnlist build_declnlist();
extern void free_declnlist();
extern void print_declnlist();
extern shapelist build_shapelist();
extern void free_shapelist();
extern void print_shapelist();
extern paramlist build_paramlist();
extern void free_paramlist();
extern void print_paramlist();
extern printlist build_printlist();
extern void print_printlist();
extern printitem build_printitem_str();
extern printitem build_printitem_num();
extern void print_printitem();
#endif
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