sort of works, needs to handle reallocation issue

This commit is contained in:
Victor Fernandes 2017-04-07 16:13:35 -04:00
parent 94d90611e1
commit fcb082a9de
4 changed files with 42 additions and 40 deletions

View File

@ -201,10 +201,10 @@ void print_token(Token t){
printf("SEOF_T\n" ); printf("SEOF_T\n" );
break; break;
case AVID_T: case AVID_T:
printf("AVID_T\t\t%s\n",t.attribute.vid_lex); printf("AVID_T\t\t%d\n",t.attribute.vid_offset);
break; break;
case SVID_T: case SVID_T:
printf("SVID_T\t\t%s\n",t.attribute.vid_lex); printf("SVID_T\t\t%d\n",t.attribute.vid_offset);
break; break;
case FPL_T: case FPL_T:
printf("FPL_T\t\t%f\n",t.attribute.flt_value); printf("FPL_T\t\t%f\n",t.attribute.flt_value);

View File

@ -23,7 +23,6 @@
#include "buffer.h" #include "buffer.h"
#include "token.h" #include "token.h"
#include "stable.h" /*Do not remove this line. SiR */ #include "stable.h" /*Do not remove this line. SiR */
#include "stable.h" /*Do not remove this line. SiR */
/* constant definitions */ /* constant definitions */
/* Input buffer parameters */ /* Input buffer parameters */

View File

@ -32,8 +32,8 @@
/* project header files */ /* project header files */
#include "buffer.h" #include "buffer.h"
#include "token.h" #include "token.h"
#include "table.h"
#include "stable.h" #include "stable.h"
#include "table.h"
#define DEBUG /* for conditional processing */ #define DEBUG /* for conditional processing */
#undef DEBUG #undef DEBUG
@ -375,7 +375,7 @@ Token aa_func02(char lexeme[]) {
unsigned kw_idx, offset; /* Variable to contain keyword table index */ unsigned kw_idx, offset; /* Variable to contain keyword table index */
Token t; Token t;
char v_type; char v_type;
char* temp_str; /*char* temp_str;*/
#ifdef DEBUG #ifdef DEBUG
printf("Lexeme: '%s'\n", lexeme); printf("Lexeme: '%s'\n", lexeme);
@ -390,11 +390,11 @@ Token aa_func02(char lexeme[]) {
/* Not a keyword? Must be AVID*/ /* Not a keyword? Must be AVID*/
t.code = AVID_T; t.code = AVID_T;
if ((temp_str = (char*)calloc(VID_LEN + 1, sizeof(char))) == NULL) { /*if ((temp_str = (char*)calloc(VID_LEN + 1, sizeof(char))) == NULL) {
return aa_table[ES]("RUN TIME ERROR: "); return aa_table[ES]("RUN TIME ERROR: ");
} }
strncpy(temp_str, lexeme, VID_LEN); strncpy(temp_str, lexeme, VID_LEN);*/
switch (lexeme[0]) { /* Read first character of lexeme for implicit type (not used yet?)*/ switch (lexeme[0]) { /* Read first character of lexeme for implicit type (not used yet?)*/
case 'i': case 'i':
@ -409,14 +409,14 @@ Token aa_func02(char lexeme[]) {
v_type = 'F'; v_type = 'F';
break; break;
} }
if ((offset = st_install(sym_table, temp_str, v_type, line)) == -1){ if ((offset = st_install(sym_table, lexeme, v_type, line)) == -1){
printf("Error: Install failed - Symbol Table is full.\n"); printf("Error: Install failed - Symbol Table is full.\n");
st_store(sym_table); st_store(sym_table);
free(temp_str); free(lexeme);
exit(1); exit(1);
} }
t.attribute.vid_offset = offset; t.attribute.vid_offset = offset;
free(temp_str); /*free(temp_str);*/
return t; return t;
} }
@ -431,24 +431,24 @@ Token aa_func02(char lexeme[]) {
*/ */
Token aa_func03(char lexeme[]) { Token aa_func03(char lexeme[]) {
Token t; Token t;
unsigned offset; unsigned offset;/*
char* temp_str; char* temp_str;
if ((temp_str = (char*)calloc(VID_LEN + 2, sizeof(char))) == NULL) { if ((temp_str = (char*)calloc(VID_LEN + 2, sizeof(char))) == NULL) {
return aa_table[ES]("RUN TIME ERROR: "); return aa_table[ES]("RUN TIME ERROR: ");
} }*/
strncpy(temp_str, lexeme, VID_LEN); /*strncpy(temp_str, lexeme, VID_LEN);*/
temp_str[strlen(temp_str) - 1] = '#'; /* Add# to end of the SVID */ lexeme[strlen(lexeme) - 1] = '#'; /* Add # to end of the SVID */
if ((offset = st_install(sym_table, temp_str, 'S', line)) == -1){ if ((offset = st_install(sym_table, lexeme, 'S', line)) == -1){
printf("Error: Install failed - Symbol Table is full.\n"); printf("Error: Install failed - Symbol Table is full.\n");
st_store(sym_table); st_store(sym_table);
free(temp_str); free(lexeme);
exit(1); exit(1);
} }
t.code = SVID_T; t.code = SVID_T;
t.attribute.vid_offset = offset; t.attribute.vid_offset = offset;
free(temp_str); /*free(temp_str);*/
return t; return t;
} }
@ -557,11 +557,12 @@ Token aa_func12(char lexeme[]) {
*/ */
Token aa_func13(char lexeme[]) { Token aa_func13(char lexeme[]) {
Token t; Token t;
unsigned int i; unsigned int i = strlen(lexeme);
t.code = ERR_T; t.code = ERR_T;
/*
for (i = 0; i < (ERR_LEN) && i < strlen(lexeme); i++) for (i = 0; i < (ERR_LEN) && i < strlen(lexeme); i++)
t.attribute.err_lex[i] = lexeme[i]; t.attribute.err_lex[i] = lexeme[i];
*/
if (strlen(lexeme) > ERR_LEN) { if (strlen(lexeme) > ERR_LEN) {
t.attribute.err_lex[i - 1] = '.'; t.attribute.err_lex[i - 1] = '.';
t.attribute.err_lex[i - 2] = '.'; t.attribute.err_lex[i - 2] = '.';

View File

@ -29,16 +29,16 @@ STD st_create(int st_size){
new_stable.plsBD = NULL; new_stable.plsBD = NULL;
new_stable.pstvr = (STVR*)malloc((size_t)st_size * sizeof(STVR)); new_stable.pstvr = (STVR*)malloc((size_t)st_size * sizeof(STVR));
if (st_size <= 0 || (new_stable.pstvr == NULL)) if (st_size <= 0 || (new_stable.pstvr == NULL)) {
new_stable.st_size = 0; new_stable.st_size = 0;
}
new_stable.plsBD = b_create(100, 1, 'a'); new_stable.plsBD = b_create(100, 1, 'a');
if (new_stable.plsBD == NULL) { if (new_stable.plsBD == NULL) {
free(new_stable.plsBD); free(new_stable.plsBD);
new_stable.st_size = 0; new_stable.st_size = 0;
} }
new_stable.st_size = 0; new_stable.st_size = st_size;
new_stable.st_offset = 0; new_stable.st_offset = 0;
return new_stable; return new_stable;
@ -66,22 +66,23 @@ int st_install(STD s_table, char *lexeme, char type, int line){
return -1; return -1;
/* Look for an existing entry in the symbol table */ /* Look for an existing entry in the symbol table */
if ((offset = st_lookup(s_table, lexeme)) > -1) offset = st_lookup(s_table, lexeme);
if (offset > -1)
return offset; return offset;
/* Set proper pointer values on symbol table */
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 */ /* Add lexeme to the symbol table's lexeme buffer */
for (i = 0; i < strlen(lexeme); ++i){ for (i = 0; i < strlen(lexeme); ++i){
if (!b_addc(s_table.plsBD, lexeme[i])) if (!b_addc(s_table.plsBD, lexeme[i]))
return -1; return -1;
} }
if (!b_addc(s_table.plsBD, '\0')) if (!b_addc(s_table.plsBD, '\0'))
return -1; return -1;
/* Set proper pointer values on symbol table */
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;
/* Set the default mask before setting the rest of the masks */ /* Set the default mask before setting the rest of the masks */
s_table.pstvr[s_table.st_offset].status_field = DFT_MASK; s_table.pstvr[s_table.st_offset].status_field = DFT_MASK;
@ -102,11 +103,11 @@ int st_install(STD s_table, char *lexeme, char type, int line){
return -1; /* Not supposed to fall into here */ return -1; /* Not supposed to fall into here */
} }
/* TODO: Handle reallocation flag behaviour /* TODO: Handle reallocation flag behaviour*/
if (b_rflag(s_table.plsBD) == SET_R_FLAG){ if (b_rflag(s_table.plsBD) == SET_R_FLAG){
printf("memory got tossed around\n");
} }
*/
/* Increment the symbol table offset */ /* Increment the symbol table offset */
st_incoffset(); st_incoffset();
@ -126,16 +127,17 @@ int st_install(STD s_table, char *lexeme, char type, int line){
-1 if no element was found -1 if no element was found
*/ */
int st_lookup(STD s_table, char *lexeme){ int st_lookup(STD s_table, char *lexeme){
int idx, i; /* idx: index locatio for symbol table, i: increment counter*/ int idx, i; /* idx: index location for symbol table, i: increment counter*/
char *head; char *store;
head = b_setmark(s_table.plsBD, 0); /*Get head of the lexeme storage */ store = b_setmark(s_table.plsBD, 0); /* Get head of the lexeme storage */
for (i = 0, idx = 0; i < s_table.st_offset; ++i) { for (i = 0, idx = 0; i < s_table.st_offset; ++i) {
if (strcmp(lexeme, head + idx) == 0) if (strncmp(lexeme, store + idx, strlen(lexeme)) == 0)
return i; return i;
else else
idx+= (short)(strlen(head + idx) + 1); idx+= (short)(strlen(store + idx) + 1);
} }
printf("found nothing\n");
return -1; /* Found nothing */ return -1; /* Found nothing */
} }
@ -148,7 +150,7 @@ int st_lookup(STD s_table, char *lexeme){
int - the offset location of the VID int - the offset location of the VID
char - the new type of the VID char - the new type of the VID
* Return value: * Return value:
1 ifchange successful, -1 if no change was made or internal error 1 if change successful, -1 if no change was made or internal error
*/ */
int st_change_type(STD s_table, int vid_offset, char v_type) { int st_change_type(STD s_table, int vid_offset, char v_type) {
@ -266,9 +268,9 @@ int st_print(STD s_table){
if (s_table.st_size <=0) return -1; if (s_table.st_size <=0) return -1;
printf("Symbol Table\n____________\nLine Number\tVariable Identifier"); printf("Symbol Table\n____________\nLine Number\tVariable Identifier\n");
for(i = 0; i < s_table.st_offset; ++i) 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); printf("%2d\t\t%s\n", s_table.pstvr[i].o_line, s_table.pstvr[i].plex);
return i; return i;
} }
@ -294,7 +296,7 @@ int st_store(STD s_table){
* this works, too. * this works, too.
*/ */
#ifdef _WIN32 #ifdef _WIN32
if(fopen_s(&out, ST_FILE_NAME, "w+") != 0) if (fopen_s(&out, ST_FILE_NAME, "w+") != 0 || out == NULL)
#else #else
if ((out = fopen(ST_FILE_NAME, "w+")) == NULL) if ((out = fopen(ST_FILE_NAME, "w+")) == NULL)
#endif #endif