Scanner: Use symbol table to store SVIDs and AVIDs

Token: Use vid_offset instead of old vid_lex[]
This commit is contained in:
Victor Fernandes 2017-03-24 22:07:42 -04:00
parent a213c022ef
commit 1bfef0f5fc
2 changed files with 27 additions and 20 deletions

View File

@ -33,6 +33,7 @@
#include "buffer.h" #include "buffer.h"
#include "token.h" #include "token.h"
#include "table.h" #include "table.h"
#include "stable.h"
#define DEBUG /* for conditional processing */ #define DEBUG /* for conditional processing */
#undef DEBUG #undef DEBUG
@ -45,7 +46,7 @@
extern Buffer * str_LTBL; /*String literal table */ extern Buffer * str_LTBL; /*String literal table */
int line; /* current line number of the source code */ int line; /* current line number of the source code */
extern int scerrnum; /* defined in platy_st.c - run-time error number */ extern int scerrnum; /* defined in platy_st.c - run-time error number */
extern STD sym_table; /* symbol table */
/* Local(file) global objects - variables */ /* Local(file) global objects - variables */
static Buffer *lex_buf; /*pointer to temporary lexeme buffer*/ static Buffer *lex_buf; /*pointer to temporary lexeme buffer*/
@ -370,8 +371,9 @@ int char_class(char c)
Return values: Token Return values: Token
*/ */
Token aa_func02(char lexeme[]) { Token aa_func02(char lexeme[]) {
unsigned int i, kw_idx; /* Variable to contain keyword table index */ unsigned int i, kw_idx, offset; /* Variable to contain keyword table index */
Token t; Token t;
char v_type;
char* temp_str; char* temp_str;
#ifdef DEBUG #ifdef DEBUG
@ -391,13 +393,7 @@ Token aa_func02(char lexeme[]) {
return aa_table[ES]("RUN TIME ERROR: "); return aa_table[ES]("RUN TIME ERROR: ");
} }
for (i = 0; i < (VID_LEN) && i < strlen(lexeme); i++) { strncpy(temp_str, lexeme, VID_LEN);
temp_str[i] = lexeme[i];
}
strncpy(t.attribute.vid_lex, temp_str, VID_LEN);
t.attribute.vid_lex[strlen(temp_str)] = '\0';
free(temp_str);
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':
@ -405,11 +401,21 @@ Token aa_func02(char lexeme[]) {
case 'd': case 'd':
case 'n': case 'n':
/* Integer */ /* Integer */
v_type = 'I';
break; break;
default: default:
/* Floating point*/ /* Floating point*/
v_type = 'F';
break; break;
} }
if ((offset = st_install(sym_table, temp_str, v_type, line)) == -1){
printf("Error: Install failed - Symbol Table is full.\n");
st_store(sym_table);
free(temp_str);
exit(1);
}
t.attribute.vid_offset = offset;
free(temp_str);
return t; return t;
} }
@ -424,24 +430,25 @@ Token aa_func02(char lexeme[]) {
*/ */
Token aa_func03(char lexeme[]) { Token aa_func03(char lexeme[]) {
Token t; Token t;
unsigned int i; unsigned int i, 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: ");
} }
for (i = 0; i < (VID_LEN) && i < strlen(lexeme); i++) { strncpy(temp_str, lexeme, VID_LEN);
temp_str[i] = lexeme[i];
}
temp_str[strlen(temp_str) - 1] = '#'; /* Add# to end of the SVID */ temp_str[strlen(temp_str) - 1] = '#'; /* Add# to end of the SVID */
temp_str[strlen(temp_str)] = '\0';
if ((offset = st_install(sym_table, temp_str, 'S', line)) == -1){
strncpy(t.attribute.vid_lex, temp_str, VID_LEN); printf("Error: Install failed - Symbol Table is full.\n");
t.attribute.vid_lex[strlen(temp_str)] = '\0'; st_store(sym_table);
free(temp_str);
exit(1);
}
t.code = SVID_T;
t.attribute.vid_offset = offset;
free(temp_str); free(temp_str);
t.code = SVID_T;
return t; return t;
} }

View File

@ -62,7 +62,7 @@ typedef union TokenAttribute{
short str_offset; /* sring literal offset from the beginning of */ short str_offset; /* sring literal offset from the beginning of */
/* the string literal buffer (str_LTBL->cb_head) */ /* the string literal buffer (str_LTBL->cb_head) */
float flt_value; /* floating-point literal attribute (value) */ float flt_value; /* floating-point literal attribute (value) */
char vid_lex[VID_LEN+1]; /* variable identifier token attribute */ int vid_offset; /* the vid_offset member replaces vid_lex */
char err_lex[ERR_LEN+1]; /* error token attribite */ char err_lex[ERR_LEN+1]; /* error token attribite */
} TA; } TA;