trying to fix memory
This commit is contained in:
parent
992897c4ff
commit
990900ae9a
|
@ -267,7 +267,7 @@ Token malar_next_token(Buffer * sc_buf)
|
||||||
|
|
||||||
b_retract_to_mark(sc_buf);
|
b_retract_to_mark(sc_buf);
|
||||||
|
|
||||||
lex_buf = b_create(1, 1, 'a');
|
lex_buf = b_create(20, 8, 'a');
|
||||||
|
|
||||||
/* Copy the scanned lexeme into lexical buffer */
|
/* Copy the scanned lexeme into lexical buffer */
|
||||||
for (; lexstart < lexend; ++lexstart) {
|
for (; lexstart < lexend; ++lexstart) {
|
||||||
|
|
35
stable.c
35
stable.c
|
@ -9,7 +9,7 @@
|
||||||
*/
|
*/
|
||||||
#include "stable.h"
|
#include "stable.h"
|
||||||
|
|
||||||
#define PLSBD_SZ 256
|
#define PLSBD_SZ 40
|
||||||
#define PLSBD_INC 8
|
#define PLSBD_INC 8
|
||||||
|
|
||||||
#define DEBUG
|
#define DEBUG
|
||||||
|
@ -65,15 +65,16 @@ STD st_create(int st_size){
|
||||||
* Algorithm:
|
* Algorithm:
|
||||||
*/
|
*/
|
||||||
int st_install(STD s_table, char *lexeme, char type, int line) {
|
int st_install(STD s_table, char *lexeme, char type, int line) {
|
||||||
unsigned int offset, i, j, lex_len;
|
int offset = -1, i, j, lex_len, bd_offset, lex_offset = 0, flag = 0;
|
||||||
short bd_offset, flag;
|
|
||||||
|
|
||||||
/* Cannot add new entry, table full */
|
/* Cannot add new entry, table full */
|
||||||
if (s_table.st_offset >= s_table.st_size)
|
if (s_table.st_offset >= s_table.st_size)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Look for an existing entry in the symbol table */
|
/* Look for an existing entry in the symbol table */
|
||||||
|
if (s_table.st_offset > 0)
|
||||||
offset = st_lookup(s_table, lexeme);
|
offset = st_lookup(s_table, lexeme);
|
||||||
|
|
||||||
if (offset > -1)
|
if (offset > -1)
|
||||||
return offset;
|
return offset;
|
||||||
|
|
||||||
|
@ -89,7 +90,7 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
||||||
if (flag == 1) {
|
if (flag == 1) {
|
||||||
bd_offset = 0;
|
bd_offset = 0;
|
||||||
for (j = 0; j <= s_table.st_offset; ++j) {
|
for (j = 0; j <= s_table.st_offset; ++j) {
|
||||||
s_table.pstvr[j].plex = b_setmark(s_table.plsBD, bd_offset + s_table.pstvr[j].i_value.str_offset);
|
s_table.pstvr[j].plex = b_setmark(s_table.plsBD, (short)(bd_offset + s_table.pstvr[j].i_value.str_offset));
|
||||||
if (j < s_table.st_offset)
|
if (j < s_table.st_offset)
|
||||||
bd_offset += s_table.pstvr[j + 1].i_value.str_offset + 1; /*Add one because the offset doesn't include '\0' */
|
bd_offset += s_table.pstvr[j + 1].i_value.str_offset + 1; /*Add one because the offset doesn't include '\0' */
|
||||||
}
|
}
|
||||||
|
@ -97,9 +98,12 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
||||||
|
|
||||||
|
|
||||||
/* Set proper pointer values on symbol table */
|
/* 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));
|
/* Get buffer offset location of the lexeme stored */
|
||||||
s_table.pstvr[s_table.st_offset].o_line = line;
|
lex_offset = b_size(s_table.plsBD) - (strlen(lexeme) + 1);
|
||||||
|
|
||||||
|
s_table.pstvr[s_table.st_offset].plex = b_setmark(s_table.plsBD, (short) lex_offset);
|
||||||
|
s_table.pstvr[s_table.st_offset].o_line = line;
|
||||||
|
s_table.pstvr[s_table.st_offset].i_value.str_offset = lex_offset;
|
||||||
|
|
||||||
/* 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;
|
||||||
|
@ -108,6 +112,7 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
||||||
case 'I': /* Integer type */
|
case 'I': /* Integer type */
|
||||||
s_table.pstvr[s_table.st_offset].status_field |= INT_MASK;
|
s_table.pstvr[s_table.st_offset].status_field |= INT_MASK;
|
||||||
s_table.pstvr[s_table.st_offset].i_value.int_val = 0;
|
s_table.pstvr[s_table.st_offset].i_value.int_val = 0;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'F': /* Floating point type */
|
case 'F': /* Floating point type */
|
||||||
s_table.pstvr[s_table.st_offset].status_field |= FLT_MASK;
|
s_table.pstvr[s_table.st_offset].status_field |= FLT_MASK;
|
||||||
|
@ -115,13 +120,13 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
||||||
break;
|
break;
|
||||||
case 'S': /* String type */
|
case 'S': /* String type */
|
||||||
s_table.pstvr[s_table.st_offset].status_field |= STR_MASK;
|
s_table.pstvr[s_table.st_offset].status_field |= STR_MASK;
|
||||||
s_table.pstvr[s_table.st_offset].i_value.str_offset = -1;
|
s_table.pstvr[s_table.st_offset].i_value.int_val = -1;
|
||||||
|
s_table.pstvr[s_table.st_offset].i_value.fpl_val = -1.0f;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return -1; /* Not supposed to fall into here */
|
return -1; /* Not supposed to fall into here */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Increment the symbol table offset */
|
/* Increment the symbol table offset */
|
||||||
st_incoffset();
|
st_incoffset();
|
||||||
return s_table.st_offset;
|
return s_table.st_offset;
|
||||||
|
@ -141,13 +146,17 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
||||||
*/
|
*/
|
||||||
int st_lookup(STD s_table, char *lexeme) {
|
int st_lookup(STD s_table, char *lexeme) {
|
||||||
int i; /* idx: index location for symbol table, i: increment counter*/
|
int i; /* idx: index location for symbol table, i: increment counter*/
|
||||||
|
#ifdef DEBUG
|
||||||
for (i = s_table.st_offset; i >= 0; --i) {
|
printf("Looking up %s...", lexeme);
|
||||||
if (strcmp(lexeme, s_table.pstvr[i].plex) == 0)
|
#endif
|
||||||
|
for (i = s_table.st_offset - 1; i >= 0; --i) {
|
||||||
|
if (strcmp(lexeme, s_table.pstvr[i].plex) == 0) {
|
||||||
|
printf("YES\n");
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("found nothing\n");
|
printf("NO\n");
|
||||||
#endif
|
#endif
|
||||||
return -1; /* Found nothing */
|
return -1; /* Found nothing */
|
||||||
}
|
}
|
||||||
|
@ -280,7 +289,7 @@ 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\n");
|
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 - 1 ; ++i)
|
||||||
printf("%2d\t\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;
|
||||||
|
|
Loading…
Reference in New Issue