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 @@
static char *tokenname[] = {
"tERROR", "tEOF", "tSEMI", "tID", "tEQ", "tOR", "tOPENBR",
"tCOMMA", "tCLOSEBR", "tSTR", "tNUM", "tEXPORT", "tGLOBAL",
"tBEGIN", "tOPENCURLY", "tCLOSECURLY", "tTYPE"
"tBEGIN", "tOPENCURLY", "tCLOSECURLY", "tTYPE", "tMINUS",
};
#endif
......@@ -109,6 +109,7 @@ TOKEN nexttok( void )
case ')': curtok = tCLOSEBR; break;
case ',': curtok = tCOMMA; break;
case '|': curtok = tOR; break;
case '-': curtok = tMINUS; break;
case '=': curtok = tEQ; break;
case '"':
for( pos=0; (c=getc(lexfile)) != '\"'; )
......
......@@ -15,7 +15,8 @@
typedef enum {
tERROR, tEOF, tSEMI, tID, tEQ, tOR,
tOPENBR, tCOMMA, tCLOSEBR, tSTR, tNUM,
tEXPORT, tGLOBAL, tBEGIN, tOPENCURLY, tCLOSECURLY, tTYPE
tEXPORT, tGLOBAL, tBEGIN, tOPENCURLY,
tCLOSECURLY, tTYPE, tMINUS,
} TOKEN;
......
......@@ -20,7 +20,7 @@
* shapes = sep-list+( shape, tOR )
* shape = tID [ tOPENBR params tCLOSEBR ] printlist
* params = sep-list+( param, tCOMMA )
* param = tID tID
* param = [tMINUS] tID tID
* printlist = list( tSTR | tNUM )
*/
......@@ -45,7 +45,8 @@ static BOOL parse_shape( char ** tagname, paramlist * pl, printlist * print );
static BOOL parse_params( paramlist * pp );
static BOOL parse_printlist( printlist * pp );
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;}
......@@ -219,10 +220,11 @@ static BOOL parse_params( paramlist *pp )
{
char *type;
char *name;
BOOL dontfree;
for(;;) {
if( !parse_param( &type, &name ) ) return FALSE;
*pp = build_paramlist( type, name, (paramlist) NULL );
if( !parse_param( &dontfree, &type, &name ) ) return FALSE;
*pp = build_paramlist( dontfree, type, name, (paramlist) NULL );
if( nexttok() != tCOMMA ) break;
pp = &( (*pp)->next );
}
......@@ -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" );
COPYOF( *type, lexidval );
/* apply standard "never free rules" */
if( ! *dontfree )
{
*dontfree = neverfreetype( *type );
}
MUSTBE( tID, "Field name expected" );
COPYOF( *name, lexidval );
//printf( "debug: param(%s, %s): dontfree %d\n", *type, *name, *dontfree );
return TRUE;
}
......@@ -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);
COPYOF(new->type, type);
COPYOF(new->name, id);
new->dontfree = dontfree;
new->next = next;
return new;
......@@ -105,10 +106,13 @@ void print_paramlist( paramlist p )
{
if (p != NULL)
{
if( p->dontfree ) putchar('-');
printf("%s %s", p->type, p->name);
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 {
paramlist next;
char *type;
char *name;
BOOL dontfree; /* exclude from freeing - eg shared ptr */
};
......@@ -86,7 +87,7 @@ extern void print_declnlist( declnlist d );
extern shapelist build_shapelist( char * id, paramlist p, printlist pl, shapelist next );
extern void print_shape( 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 printitem build_printitem_str( char * s );
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