#include "dstruct.h"
/* make_var: build a new variable;
* It uses the type passed as second argument
* and add an entry in a list of variables.
*/
char *
make_var (char *name, char *type)
{
int name_len = strlen (name), type_len = strlen (type);
variable *current_var;
variable *new_var;
variable *previous_var;
/* let's create a new node */
new_var = (variable *) malloc (sizeof (variable));
if (new_var == NULL)
{
fprintf (stderr, "Error in variables list\n");
exit (1);
}
/* initialize new node if creation had success */
new_var->type = (char *) malloc (type_len + 1);
strncpy (new_var->type, type, type_len);
new_var->type[type_len] = '\0';
new_var->name = (char *) malloc (name_len + 1);
strncpy (new_var->name, name, name_len);
new_var->name[name_len] = '\0';
new_var->next = NULL;
/* preparing for list traversing */
previous_var = NULL;
current_var = var_list;
/*
* Navigate the list to find the right place
* for the newly created node
*/
/* fprintf(stderr, "debug: prima del while di make_var\n"); */
while ((current_var != NULL) &&
(strncmp (name, current_var->name, 256) > 0))
{
/* fprintf(stderr, "debug: current_var->name = %s\n", current_var->name);
* fprintf(stderr, "debug: name = %s\n", name);
* fprintf(stderr, "debug: nel while di make_var\n");
*/
previous_var = current_var;
current_var = current_var->next;
/* if a variable with the current name is in the list
* we free some mem and eexit
*/
if ((current_var != NULL) &&
(strncmp (name, current_var->name, 256) == 0))
{
/* fprintf(stderr, "debug: variabile \"%s\" gia\' nella lista\n",
* current_var->name);
*/
free (new_var);
return name;
}
}
/* insert the node in the right place */
if (previous_var != NULL)
{
new_var->next = previous_var->next;
previous_var->next = new_var;
}
else
{
/* new node has to be placed at list head
* just a control to see if a variable with current name
* is in the list
*/
if ((var_list != NULL) && (strncmp (name, var_list->name, 256) == 0))
{
free (new_var);
return name;
}
new_var->next = var_list;
var_list = new_var;
}
return name;
}
/* print the body of c-file generated by the parser */
void
print_body (FILE * outfile, FILE * tmp_file)
{
char line[1024];
while (fgets (line, 1024, tmp_file) != NULL)
fputs (line, outfile);
}
/* print declatarion infos collected during parsing */
void
print_variables (FILE * outfile)
{
variable *current_var;
current_var = var_list;
/* print list elements
* fprintf(stderr, "debug: stampo le variabili\n");
*/
while (current_var != NULL)
{
fprintf (outfile, "%s %s;\n", current_var->type, current_var->name);
current_var = current_var->next;
}
printf ("\n");
}
char *
get_type(char *var_name)
{
char *tmp_type;
int type_name_len;
variable *current_var;
current_var = var_list;
/* print list elements
* fprintf(stderr, "debug: stampo le variabili\n");
*/
while (current_var != NULL)
{
if (strncmp(current_var->name, var_name, strlen(var_name)) == 0)
{
type_name_len = strlen(current_var->type);
tmp_type = malloc( type_name_len+1 );
strncpy( tmp_type, current_var->type, type_name_len );
tmp_type[type_name_len] = '\0';
return tmp_type;
}
current_var = current_var->next;
}
return NULL;
}
syntax highlighted by Code2HTML, v. 0.9.1