Merge branch 'master' of git.xeserv.us:victorvalenca/platypus
This commit is contained in:
commit
5bd748b2e6
|
@ -267,7 +267,7 @@ Token malar_next_token(Buffer * 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 */
|
||||
for (; lexstart < lexend; ++lexstart) {
|
||||
|
|
106
stable.c
106
stable.c
|
@ -9,13 +9,13 @@
|
|||
*/
|
||||
#include "stable.h"
|
||||
|
||||
#define PLSBD_SZ 256
|
||||
#define PLSBD_INC 8
|
||||
#define PLSBD_SZ 128
|
||||
#define PLSBD_INC 64
|
||||
|
||||
#define DEBUG
|
||||
/*#undef DEBUG*/
|
||||
#undef DEBUG*/
|
||||
|
||||
/* Forward function declarations */
|
||||
/* Forward function declarations */
|
||||
static void st_setsize(void);
|
||||
static void st_incoffset(void);
|
||||
extern STD sym_table;
|
||||
|
@ -30,7 +30,7 @@ extern STD sym_table;
|
|||
STD - The symbol table
|
||||
* Algorithm:
|
||||
*/
|
||||
STD st_create(int st_size){
|
||||
STD st_create(int st_size) {
|
||||
STD new_stable;
|
||||
|
||||
new_stable.plsBD = NULL;
|
||||
|
@ -64,50 +64,53 @@ STD st_create(int st_size){
|
|||
-1 if an internal error occurs
|
||||
* Algorithm:
|
||||
*/
|
||||
int st_install(STD s_table, char *lexeme, char type, int line){
|
||||
unsigned int offset, i, j, lex_len;
|
||||
short bd_offset, flag;
|
||||
int st_install(STD s_table, char *lexeme, char type, int line) {
|
||||
int offset = -1, i, j, lex_len, lex_offset = 0, flag = 0;
|
||||
|
||||
/* Cannot add new entry, table full */
|
||||
if (s_table.st_offset >= s_table.st_size)
|
||||
return -1;
|
||||
|
||||
/* Look for an existing entry in the symbol table */
|
||||
if (s_table.st_offset > 0)
|
||||
offset = st_lookup(s_table, lexeme);
|
||||
|
||||
if (offset > -1)
|
||||
return offset;
|
||||
|
||||
/* Add lexeme to the symbol table's lexeme buffer */
|
||||
lex_len = strlen(lexeme);
|
||||
for (i = 0; i <= lex_len; ++i){
|
||||
for (i = 0; i <= lex_len; ++i) {
|
||||
if (!b_addc(s_table.plsBD, lexeme[i]))
|
||||
return -1;
|
||||
if (b_rflag(s_table.plsBD) == SET_R_FLAG){ /* COPY NEW ADDRESSES TO PLEX ENTRIES */
|
||||
if (b_rflag(s_table.plsBD) == SET_R_FLAG) { /* Trigger reassignment loop once lexeme is stored */
|
||||
flag = 1;
|
||||
}
|
||||
}
|
||||
if (flag == 1){
|
||||
bd_offset = 0;
|
||||
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);
|
||||
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' */
|
||||
/* If reallocation happened...*/
|
||||
if (flag == 1) {
|
||||
for (j = 0; j < s_table.st_offset; ++j) {
|
||||
s_table.pstvr[j].plex = b_setmark(s_table.plsBD, (short) s_table.pstvr[j].i_value.str_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;
|
||||
/* Get buffer offset location of the lexeme stored */
|
||||
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 */
|
||||
s_table.pstvr[s_table.st_offset].status_field = DFT_MASK;
|
||||
|
||||
switch (type){
|
||||
switch (type) {
|
||||
case 'I': /* Integer type */
|
||||
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 */
|
||||
s_table.pstvr[s_table.st_offset].status_field |= FLT_MASK;
|
||||
|
@ -115,13 +118,13 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
|||
break;
|
||||
case 'S': /* String type */
|
||||
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;
|
||||
default:
|
||||
return -1; /* Not supposed to fall into here */
|
||||
}
|
||||
|
||||
|
||||
/* Increment the symbol table offset */
|
||||
st_incoffset();
|
||||
return s_table.st_offset;
|
||||
|
@ -139,15 +142,21 @@ int st_install(STD s_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 s_table, char *lexeme){
|
||||
int st_lookup(STD s_table, char *lexeme) {
|
||||
int i; /* idx: index location for symbol table, i: increment counter*/
|
||||
|
||||
for (i = s_table.st_offset; i >= 0; --i) {
|
||||
if (strcmp(lexeme, s_table.pstvr[i].plex) == 0)
|
||||
#ifdef DEBUG
|
||||
printf("Looking up %s...", lexeme);
|
||||
#endif
|
||||
for (i = s_table.st_offset - 1; i >= 0; --i) {
|
||||
if (strcmp(lexeme, s_table.pstvr[i].plex) == 0) {
|
||||
#ifdef DEBUG
|
||||
printf("YES\n");
|
||||
#endif
|
||||
return i;
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
printf("found nothing\n");
|
||||
printf("NO\n");
|
||||
#endif
|
||||
return -1; /* Found nothing */
|
||||
}
|
||||
|
@ -175,15 +184,12 @@ int st_change_type(STD s_table, int vid_offset, char v_type) {
|
|||
/*sym_table.pstvr[vid_offset].status_field = 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){
|
||||
switch (v_type) {
|
||||
case 'I': /* Integer type */
|
||||
s_table.pstvr[s_table.st_offset].status_field |= INT_MASK;
|
||||
s_table.pstvr[vid_offset].status_field |= INT_MASK;
|
||||
break;
|
||||
case 'F': /* Floating point type */
|
||||
s_table.pstvr[s_table.st_offset].status_field |= FLT_MASK;
|
||||
s_table.pstvr[vid_offset].status_field |= FLT_MASK;
|
||||
break;
|
||||
case 'S': /* String type, do nothing as it cannot be changed */
|
||||
break;
|
||||
|
@ -204,7 +210,7 @@ int st_change_type(STD s_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 s_table, int vid_offset, 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;
|
||||
}
|
||||
|
@ -220,10 +226,10 @@ int st_change_value(STD s_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 s_table, int vid_offset){
|
||||
char st_get_type(STD s_table, int vid_offset) {
|
||||
unsigned short mask;
|
||||
mask = s_table.pstvr[vid_offset].status_field & CHK_MASK;
|
||||
switch (mask){
|
||||
switch (mask) {
|
||||
case INT_MASK:
|
||||
return 'I';
|
||||
case FLT_MASK:
|
||||
|
@ -246,7 +252,7 @@ char st_get_type(STD s_table, int vid_offset){
|
|||
Value - the value of the VID
|
||||
Incorrect parameters will cause undefined behaviour
|
||||
*/
|
||||
Value st_get_value(STD s_table, int vid_offset){
|
||||
Value st_get_value(STD s_table, int vid_offset) {
|
||||
return s_table.pstvr[vid_offset].i_value;
|
||||
}
|
||||
|
||||
|
@ -257,8 +263,8 @@ Value st_get_value(STD s_table, int vid_offset){
|
|||
* Parameters:
|
||||
STD - The symbol table
|
||||
*/
|
||||
void st_destroy(STD s_table){
|
||||
if (s_table.pstvr != NULL){
|
||||
void st_destroy(STD s_table) {
|
||||
if (s_table.pstvr != NULL) {
|
||||
free(s_table.pstvr);
|
||||
s_table.pstvr = NULL;
|
||||
}
|
||||
|
@ -274,17 +280,17 @@ void st_destroy(STD s_table){
|
|||
* Return value:
|
||||
int - the number of items printed to stdout
|
||||
*/
|
||||
int st_print(STD s_table){
|
||||
int st_print(STD s_table) {
|
||||
int i;
|
||||
|
||||
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");
|
||||
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);
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
/* Store the symbol table to a file named $stable.ste.
|
||||
It overwrites any existing file named $stable.ste.
|
||||
|
@ -297,7 +303,7 @@ int st_print(STD s_table){
|
|||
int - the number of items stored
|
||||
-1 if the file stream cannot be opened
|
||||
*/
|
||||
int st_store(STD s_table){
|
||||
int st_store(STD s_table) {
|
||||
FILE *out; /* The target file*/
|
||||
int i;
|
||||
|
||||
|
@ -315,7 +321,7 @@ int st_store(STD s_table){
|
|||
|
||||
fprintf(out, "%d", s_table.st_size);
|
||||
|
||||
for(i = 0; i < s_table.st_size; ++i){
|
||||
for (i = 0; i < s_table.st_offset; ++i) {
|
||||
fprintf(out, " %4X %d %s %d",
|
||||
s_table.pstvr[i].status_field, /* Status flag */
|
||||
(int)strlen(s_table.pstvr[i].plex), /* Length of lexeme */
|
||||
|
@ -324,7 +330,7 @@ int st_store(STD s_table){
|
|||
|
||||
/* Initial value */
|
||||
char type = st_get_type(s_table, i);
|
||||
switch (type){
|
||||
switch (type) {
|
||||
case 'I':
|
||||
case 'F':
|
||||
case 'S':
|
||||
|
@ -344,7 +350,7 @@ int st_store(STD s_table){
|
|||
* Parameters: N/A
|
||||
* Return value: N/A
|
||||
*/
|
||||
static void st_setsize(void){
|
||||
static void st_setsize(void) {
|
||||
sym_table.st_size = 0;
|
||||
}
|
||||
|
||||
|
@ -355,7 +361,7 @@ static void st_setsize(void){
|
|||
* Parameters: N/A
|
||||
* Return value: N/A
|
||||
*/
|
||||
static void st_incoffset(void){
|
||||
static void st_incoffset(void) {
|
||||
++sym_table.st_offset;
|
||||
}
|
||||
|
||||
|
@ -368,9 +374,9 @@ static void st_incoffset(void){
|
|||
* Return value:
|
||||
* Algorithm:
|
||||
*/
|
||||
int st_sort(STD s_table, char s_order){
|
||||
/* Compiler warning about unused parameter,
|
||||
* this is fine for this "future" implementation
|
||||
int st_sort(STD s_table, char s_order) {
|
||||
/* Compiler warning about unused parameters,
|
||||
* this is fine for this dummy method
|
||||
*/
|
||||
return 0; /* SYKE! */
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue