fix warnings and use fopen_s instead
This commit is contained in:
parent
69157aec8c
commit
5f717f4427
2
buffer.h
2
buffer.h
|
@ -14,7 +14,7 @@
|
|||
#define BUFFER_H_
|
||||
|
||||
#define MACOS_DEP
|
||||
/*#undef MACOS_DEP
|
||||
#undef MACOS_DEP
|
||||
/*#pragma warning(1:4001) *//*to enforce C89 type comments - to make //comments an warning */
|
||||
|
||||
/*#pragma warning(error:4001)*//* to enforce C89 comments - to make // comments an error */
|
||||
|
|
105
stable.c
105
stable.c
|
@ -28,11 +28,12 @@ STD st_create(int st_size){
|
|||
STD new_stable;
|
||||
|
||||
new_stable.plsBD = NULL;
|
||||
|
||||
if (st_size <= 0 || (new_stable.pstvr = (STVR*)malloc((size_t)st_size * sizeof(STVR)) == NULL))
|
||||
new_stable.pstvr = (STVR*)malloc((size_t)st_size * sizeof(STVR));
|
||||
if (st_size <= 0 || (new_stable.pstvr == NULL))
|
||||
new_stable.st_size = 0;
|
||||
|
||||
if ((new_stable.plsBD = b_create(st_size, 1, 'a')) == NULL){
|
||||
new_stable.plsBD = b_create(100, 1, 'a');
|
||||
if (new_stable.plsBD == NULL) {
|
||||
free(new_stable.plsBD);
|
||||
new_stable.st_size = 0;
|
||||
}
|
||||
|
@ -57,49 +58,49 @@ STD st_create(int st_size){
|
|||
-1 if an internal error occurs
|
||||
* Algorithm:
|
||||
*/
|
||||
int st_install(STD sym_table, char *lexeme, char type, int line){
|
||||
int st_install(STD s_table, char *lexeme, char type, int line){
|
||||
unsigned int offset, i;
|
||||
char f_realloc; /* Reallocation flag */
|
||||
|
||||
/* Cannot add new entry, table full */
|
||||
if (sym_table.st_offset >= sym_table.st_size)
|
||||
if (s_table.st_offset >= s_table.st_size)
|
||||
return -1;
|
||||
|
||||
/* Look for an existing entry in the symbol table */
|
||||
if ((offset = st_lookup(sym_table, lexeme)) > -1)
|
||||
if ((offset = st_lookup(s_table, lexeme)) > -1)
|
||||
return offset;
|
||||
|
||||
/* Set proper pointer values on symbol table */
|
||||
sym_table.pstvr[sym_table.st_offset].plex = b_setmark(sym_table.plsBD, b_size(sym_table.plsBD));
|
||||
sym_table.pstvr[sym_table.st_offset].o_line = line;
|
||||
s_table.pstvr[s_table.st_offset].plex = b_setmark(s_table.plsBD, b_size(s_table.plsBD));
|
||||
s_table.pstvr[s_table.st_offset].o_line = line;
|
||||
|
||||
/* Add lexeme to the symbol table's lexeme buffer */
|
||||
for (i = 0; i < strlen(lexeme); ++i){
|
||||
if (!b_addc(sym_table.plsBD, lexeme[i]))
|
||||
if (!b_addc(s_table.plsBD, lexeme[i]))
|
||||
return -1;
|
||||
|
||||
if (b_rflag(sym_table.plsBD) == SET_R_FLAG)
|
||||
if (b_rflag(s_table.plsBD) == SET_R_FLAG)
|
||||
f_realloc = SET_R_FLAG;
|
||||
}
|
||||
|
||||
if (!b_addc(sym_table.plsBD, '\0'))
|
||||
if (!b_addc(s_table.plsBD, '\0'))
|
||||
return -1;
|
||||
|
||||
/* Set the default mask before setting the rest of the masks */
|
||||
sym_table.pstvr[sym_table.st_offset].status_field = DFT_MASK;
|
||||
s_table.pstvr[s_table.st_offset].status_field = DFT_MASK;
|
||||
|
||||
switch (type){
|
||||
case 'I': /* Integer type */
|
||||
sym_table.pstvr[sym_table.st_offset].status_field |= INT_MASK;
|
||||
sym_table.pstvr[sym_table.st_offset].i_value.int_val = 0;
|
||||
s_table.pstvr[s_table.st_offset].status_field |= INT_MASK;
|
||||
s_table.pstvr[s_table.st_offset].i_value.int_val = 0;
|
||||
break;
|
||||
case 'F': /* Floating point type */
|
||||
sym_table.pstvr[sym_table.st_offset].status_field |= FLT_MASK;
|
||||
sym_table.pstvr[sym_table.st_offset].i_value.int_val = 0.0f;
|
||||
s_table.pstvr[s_table.st_offset].status_field |= FLT_MASK;
|
||||
s_table.pstvr[s_table.st_offset].i_value.fpl_val = 0.0f;
|
||||
break;
|
||||
case 'S': /* String type */
|
||||
sym_table.pstvr[sym_table.st_offset].status_field |= STR_MASK;
|
||||
sym_table.pstvr[sym_table.st_offset].i_value.str_offset = -1;
|
||||
s_table.pstvr[s_table.st_offset].status_field |= STR_MASK;
|
||||
s_table.pstvr[s_table.st_offset].i_value.str_offset = -1;
|
||||
break;
|
||||
default:
|
||||
return -1; /* Not supposed to fall into here */
|
||||
|
@ -113,7 +114,7 @@ int st_install(STD sym_table, char *lexeme, char type, int line){
|
|||
|
||||
/* Increment the symbol table offset */
|
||||
st_incoffset();
|
||||
return sym_table.st_offset;
|
||||
return s_table.st_offset;
|
||||
|
||||
}
|
||||
|
||||
|
@ -128,12 +129,12 @@ int st_install(STD sym_table, char *lexeme, char type, int line){
|
|||
int - the element offset index in the symbol table
|
||||
-1 if no element was found
|
||||
*/
|
||||
int st_lookup(STD sym_table, char *lexeme){
|
||||
int st_lookup(STD s_table, char *lexeme){
|
||||
int idx, i; /* idx: index locatio for symbol table, i: increment counter*/
|
||||
char *head;
|
||||
|
||||
head = b_setmark(sym_table.plsBD, 0); /*Get head of the lexeme storage */
|
||||
for (i = 0; i < sym_table.st_offset; ++i) {
|
||||
head = b_setmark(s_table.plsBD, 0); /*Get head of the lexeme storage */
|
||||
for (i = 0, idx = 0; i < s_table.st_offset; ++i) {
|
||||
if (strcmp(lexeme, head + idx) == 0)
|
||||
return i;
|
||||
else
|
||||
|
@ -153,27 +154,27 @@ int st_lookup(STD sym_table, char *lexeme){
|
|||
* Return value:
|
||||
1 ifchange successful, -1 if no change was made or internal error
|
||||
*/
|
||||
int st_change_type(STD sym_table, int vid_offset, char v_type) {
|
||||
int st_change_type(STD s_table, int vid_offset, char v_type) {
|
||||
|
||||
/* Do nothing if update flag has already been set */
|
||||
if ((sym_table.pstvr[vid_offset].status_field & U_MASK)) return -1;
|
||||
if ((s_table.pstvr[vid_offset].status_field & U_MASK)) return -1;
|
||||
|
||||
/* Reset to new type with default mask and update flag
|
||||
Note: Can separate the statements to set the update flag at the
|
||||
end, but this resets AND updates at once
|
||||
*/
|
||||
/*sym_table.pstvr[vid_offset].status_field = sym_table.pstvr[vid_offset].status_field & DFT_U_MASK;*/
|
||||
sym_table.pstvr[vid_offset].status_field &= DFT_U_MASK;
|
||||
s_table.pstvr[vid_offset].status_field &= DFT_U_MASK;
|
||||
|
||||
/*TODO: Ask if re-setting flags and "bailing out" is spec breaking, and
|
||||
if flags should only be set if v_type is valid */
|
||||
|
||||
switch (v_type){
|
||||
case 'I': /* Integer type */
|
||||
sym_table.pstvr[sym_table.st_offset].status_field |= INT_MASK;
|
||||
s_table.pstvr[s_table.st_offset].status_field |= INT_MASK;
|
||||
break;
|
||||
case 'F': /* Floating point type */
|
||||
sym_table.pstvr[sym_table.st_offset].status_field |= FLT_MASK;
|
||||
s_table.pstvr[s_table.st_offset].status_field |= FLT_MASK;
|
||||
break;
|
||||
case 'S': /* String type, do nothing as it cannot be changed */
|
||||
break;
|
||||
|
@ -194,8 +195,8 @@ int st_change_type(STD sym_table, int vid_offset, char v_type) {
|
|||
Value - the new value of the VID
|
||||
* Return value: the offset location of the VID
|
||||
*/
|
||||
int st_change_value(STD sym_table, int vid_offset, Value value){
|
||||
sym_table.pstvr[vid_offset].i_value = value;
|
||||
int st_change_value(STD s_table, int vid_offset, Value value){
|
||||
s_table.pstvr[vid_offset].i_value = value;
|
||||
return vid_offset;
|
||||
}
|
||||
|
||||
|
@ -210,9 +211,9 @@ int st_change_value(STD sym_table, int vid_offset, Value value){
|
|||
char - the type of the VID ('I','F', or 'S')
|
||||
-1 if there is an invalid value set
|
||||
*/
|
||||
char st_get_type(STD sym_table, int vid_offset){
|
||||
char st_get_type(STD s_table, int vid_offset){
|
||||
unsigned short mask;
|
||||
mask = sym_table.pstvr[vid_offset].status_field & CHK_MASK;
|
||||
mask = s_table.pstvr[vid_offset].status_field & CHK_MASK;
|
||||
switch (mask){
|
||||
case INT_MASK:
|
||||
return 'I';
|
||||
|
@ -236,8 +237,8 @@ char st_get_type(STD sym_table, int vid_offset){
|
|||
Value - the value of the VID
|
||||
Incorrect parameters will cause undefined behaviour
|
||||
*/
|
||||
Value st_get_value(STD sym_table, int vid_offset){
|
||||
return sym_table.pstvr[vid_offset].i_value;
|
||||
Value st_get_value(STD s_table, int vid_offset){
|
||||
return s_table.pstvr[vid_offset].i_value;
|
||||
}
|
||||
|
||||
/* Free memory used by the symbol table
|
||||
|
@ -247,12 +248,12 @@ Value st_get_value(STD sym_table, int vid_offset){
|
|||
* Parameters:
|
||||
STD - The symbol table
|
||||
*/
|
||||
void st_destroy(STD sym_table){
|
||||
if (sym_table.pstvr != NULL){
|
||||
free(sym_table.pstvr);
|
||||
sym_table.pstvr = NULL;
|
||||
void st_destroy(STD s_table){
|
||||
if (s_table.pstvr != NULL){
|
||||
free(s_table.pstvr);
|
||||
s_table.pstvr = NULL;
|
||||
}
|
||||
b_free(sym_table.plsBD);
|
||||
b_free(s_table.plsBD);
|
||||
}
|
||||
|
||||
/* Print the contents of the symbol table to standard output
|
||||
|
@ -264,14 +265,14 @@ void st_destroy(STD sym_table){
|
|||
* Return value:
|
||||
int - the number of items printed to stdout
|
||||
*/
|
||||
int st_print(STD sym_table){
|
||||
int st_print(STD s_table){
|
||||
int i;
|
||||
|
||||
if (sym_table.st_size <=0) return -1;
|
||||
if (s_table.st_size <=0) return -1;
|
||||
|
||||
printf("Symbol Table\n____________\nLine Number\tVariable Identifier");
|
||||
for(i = 0; i < sym_table.st_offset; ++i)
|
||||
printf("%2d\t%s\n", sym_table.pstvr[i].o_line, sym_table.pstvr[i].plex);
|
||||
for(i = 0; i < s_table.st_offset; ++i)
|
||||
printf("%2d\t%s\n", s_table.pstvr[i].o_line, s_table.pstvr[i].plex);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
@ -287,23 +288,23 @@ int st_print(STD sym_table){
|
|||
int - the number of items stored
|
||||
-1 if the file stream cannot be opened
|
||||
*/
|
||||
int st_store(STD sym_table){
|
||||
int st_store(STD s_table){
|
||||
FILE *out; /* The target file*/
|
||||
int i;
|
||||
|
||||
if((out = fopen(ST_FILE_NAME, "w+")) == NULL)
|
||||
if(fopen_s(&out, ST_FILE_NAME, "w+") != 0)
|
||||
return -1; /* Can't open file, stop. */
|
||||
|
||||
fprintf(out, "%d", sym_table.st_size);
|
||||
fprintf(out, "%d", s_table.st_size);
|
||||
|
||||
for(i = 0; i < sym_table.st_size; ++i){
|
||||
fprintf(out, " %4X", sym_table.pstvr[i].status_field); /* Status flag */
|
||||
fprintf(out, " %d", (int)strlen(sym_table.pstvr[i].plex)); /* Length of lexeme */
|
||||
fprintf(out, " %s", sym_table.pstvr[i].plex); /* The lexeme itself */
|
||||
fprintf(out, " %d", sym_table.pstvr[i].o_line); /* Line number */
|
||||
for(i = 0; i < s_table.st_size; ++i){
|
||||
fprintf(out, " %4X", s_table.pstvr[i].status_field); /* Status flag */
|
||||
fprintf(out, " %d", (int)strlen(s_table.pstvr[i].plex)); /* Length of lexeme */
|
||||
fprintf(out, " %s", s_table.pstvr[i].plex); /* The lexeme itself */
|
||||
fprintf(out, " %d", s_table.pstvr[i].o_line); /* Line number */
|
||||
|
||||
/* Initial value */
|
||||
char type = st_get_type(sym_table, i);
|
||||
char type = st_get_type(s_table, i);
|
||||
switch (type){
|
||||
case 'I':
|
||||
case 'F':
|
||||
|
@ -348,6 +349,6 @@ static void st_incoffset(void){
|
|||
* Return value:
|
||||
* Algorithm:
|
||||
*/
|
||||
int st_sort(STD sym_table, char s_order){
|
||||
int st_sort(STD s_table, char s_order){
|
||||
return 0; /* SYKE! */
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue