Commit f25a7a0c authored by Duncan White's avatar Duncan White

first stage of adding free/dontfree support to datadec: added token '-' (to...

first stage of adding free/dontfree support to datadec: added token '-' (to the lexer), made optional in a parameter declaration (in the parser), made a place to store "BOOL dontfree" (in the struct module) and rules in the parser to glue it together.  also added an array of predefined types-that-are-never-freed and used that (as well as the "-") to set dontfree.
parent b7ac97c7
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
static char *tokenname[] = { static char *tokenname[] = {
"tERROR", "tEOF", "tSEMI", "tID", "tEQ", "tOR", "tOPENBR", "tERROR", "tEOF", "tSEMI", "tID", "tEQ", "tOR", "tOPENBR",
"tCOMMA", "tCLOSEBR", "tSTR", "tNUM", "tEXPORT", "tGLOBAL", "tCOMMA", "tCLOSEBR", "tSTR", "tNUM", "tEXPORT", "tGLOBAL",
"tBEGIN", "tOPENCURLY", "tCLOSECURLY", "tTYPE" "tBEGIN", "tOPENCURLY", "tCLOSECURLY", "tTYPE", "tMINUS",
}; };
#endif #endif
...@@ -109,6 +109,7 @@ TOKEN nexttok( void ) ...@@ -109,6 +109,7 @@ TOKEN nexttok( void )
case ')': curtok = tCLOSEBR; break; case ')': curtok = tCLOSEBR; break;
case ',': curtok = tCOMMA; break; case ',': curtok = tCOMMA; break;
case '|': curtok = tOR; break; case '|': curtok = tOR; break;
case '-': curtok = tMINUS; break;
case '=': curtok = tEQ; break; case '=': curtok = tEQ; break;
case '"': case '"':
for( pos=0; (c=getc(lexfile)) != '\"'; ) for( pos=0; (c=getc(lexfile)) != '\"'; )
......
...@@ -15,7 +15,8 @@ ...@@ -15,7 +15,8 @@
typedef enum { typedef enum {
tERROR, tEOF, tSEMI, tID, tEQ, tOR, tERROR, tEOF, tSEMI, tID, tEQ, tOR,
tOPENBR, tCOMMA, tCLOSEBR, tSTR, tNUM, tOPENBR, tCOMMA, tCLOSEBR, tSTR, tNUM,
tEXPORT, tGLOBAL, tBEGIN, tOPENCURLY, tCLOSECURLY, tTYPE tEXPORT, tGLOBAL, tBEGIN, tOPENCURLY,
tCLOSECURLY, tTYPE, tMINUS,
} TOKEN; } TOKEN;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* shapes = sep-list+( shape, tOR ) * shapes = sep-list+( shape, tOR )
* shape = tID [ tOPENBR params tCLOSEBR ] printlist * shape = tID [ tOPENBR params tCLOSEBR ] printlist
* params = sep-list+( param, tCOMMA ) * params = sep-list+( param, tCOMMA )
* param = tID tID * param = [tMINUS] tID tID
* printlist = list( tSTR | tNUM ) * printlist = list( tSTR | tNUM )
*/ */
...@@ -45,7 +45,8 @@ static BOOL parse_shape( char ** tagname, paramlist * pl, printlist * print ); ...@@ -45,7 +45,8 @@ static BOOL parse_shape( char ** tagname, paramlist * pl, printlist * print );
static BOOL parse_params( paramlist * pp ); static BOOL parse_params( paramlist * pp );
static BOOL parse_printlist( printlist * pp ); static BOOL parse_printlist( printlist * pp );
static BOOL parse_printitem( printitem * item ); static BOOL parse_printitem( printitem * item );
static BOOL parse_param( char ** type, char ** name ); static BOOL neverfreetype( char * typename );
static BOOL parse_param( BOOL * dontfree, char ** type, char ** name );
#define MUSTBE(t,mesg) if( nexttok() != (t) ) {error(mesg); return FALSE;} #define MUSTBE(t,mesg) if( nexttok() != (t) ) {error(mesg); return FALSE;}
...@@ -219,10 +220,11 @@ static BOOL parse_params( paramlist *pp ) ...@@ -219,10 +220,11 @@ static BOOL parse_params( paramlist *pp )
{ {
char *type; char *type;
char *name; char *name;
BOOL dontfree;
for(;;) { for(;;) {
if( !parse_param( &type, &name ) ) return FALSE; if( !parse_param( &dontfree, &type, &name ) ) return FALSE;
*pp = build_paramlist( type, name, (paramlist) NULL ); *pp = build_paramlist( dontfree, type, name, (paramlist) NULL );
if( nexttok() != tCOMMA ) break; if( nexttok() != tCOMMA ) break;
pp = &( (*pp)->next ); pp = &( (*pp)->next );
} }
...@@ -270,15 +272,51 @@ static BOOL parse_printitem( printitem *item ) ...@@ -270,15 +272,51 @@ static BOOL parse_printitem( printitem *item )
} }
/* param = tID tID */ /* never free the following types - sorted list */
static char *neverfree[] = {
"BOOL",
"char",
"int",
};
static BOOL parse_param( char **type, char **name ) #define NEVERFREE_NEL (sizeof(neverfree)/sizeof(char *))
static BOOL neverfreetype( char *typename )
{ {
int i;
for( i=0; i<NEVERFREE_NEL; i++ )
{
if( strcmp( neverfree[i], typename ) == 0 )
{
return TRUE;
}
}
return FALSE;
}
/* param = [tMINUS] tID tID */
static BOOL parse_param( BOOL *dontfree, char **type, char **name )
{
*dontfree = nexttok() == tMINUS;
if( ! *dontfree )
{
ungettok();
}
MUSTBE( tID, "Field type expected" ); MUSTBE( tID, "Field type expected" );
COPYOF( *type, lexidval ); COPYOF( *type, lexidval );
/* apply standard "never free rules" */
if( ! *dontfree )
{
*dontfree = neverfreetype( *type );
}
MUSTBE( tID, "Field name expected" ); MUSTBE( tID, "Field name expected" );
COPYOF( *name, lexidval ); COPYOF( *name, lexidval );
//printf( "debug: param(%s, %s): dontfree %d\n", *type, *name, *dontfree );
return TRUE; return TRUE;
} }
...@@ -89,12 +89,13 @@ void print_shapelist( shapelist s ) ...@@ -89,12 +89,13 @@ void print_shapelist( shapelist s )
} }
paramlist build_paramlist( char *type, char *id, paramlist next ) paramlist build_paramlist( BOOL dontfree, char *type, char *id, paramlist next )
{ {
paramlist new = NEW(paramlist); paramlist new = NEW(paramlist);
COPYOF(new->type, type); COPYOF(new->type, type);
COPYOF(new->name, id); COPYOF(new->name, id);
new->dontfree = dontfree;
new->next = next; new->next = next;
return new; return new;
...@@ -105,10 +106,13 @@ void print_paramlist( paramlist p ) ...@@ -105,10 +106,13 @@ void print_paramlist( paramlist p )
{ {
if (p != NULL) if (p != NULL)
{ {
if( p->dontfree ) putchar('-');
printf("%s %s", p->type, p->name); printf("%s %s", p->type, p->name);
for (p = p->next; p; p = p->next) for (p = p->next; p; p = p->next)
{ {
printf(", %s %s", p->type, p->name); fputs(", ",stdout );
if( p->dontfree ) putchar('-');
printf( "%s %s", p->type, p->name);
} }
} }
} }
......
...@@ -57,6 +57,7 @@ struct paramlist { ...@@ -57,6 +57,7 @@ struct paramlist {
paramlist next; paramlist next;
char *type; char *type;
char *name; char *name;
BOOL dontfree; /* exclude from freeing - eg shared ptr */
}; };
...@@ -86,7 +87,7 @@ extern void print_declnlist( declnlist d ); ...@@ -86,7 +87,7 @@ extern void print_declnlist( declnlist d );
extern shapelist build_shapelist( char * id, paramlist p, printlist pl, shapelist next ); extern shapelist build_shapelist( char * id, paramlist p, printlist pl, shapelist next );
extern void print_shape( shapelist s ); extern void print_shape( shapelist s );
extern void print_shapelist( shapelist s ); extern void print_shapelist( shapelist s );
extern paramlist build_paramlist( char * type, char * id, paramlist next ); extern paramlist build_paramlist( BOOL dontfree, char * type, char * id, paramlist next );
extern void print_paramlist( paramlist p ); extern void print_paramlist( paramlist p );
extern printitem build_printitem_str( char * s ); extern printitem build_printitem_str( char * s );
extern printitem build_printitem_num( int n ); extern printitem build_printitem_num( int 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