still working through symbol_table to try and get it to compile soon but fixed issues with storing values and calling the right element of a struct

This commit is contained in:
Partho Bhattacharya
2025-03-14 12:04:53 -04:00
parent 392a4b3ba5
commit f0e0d7bdbc
3 changed files with 54 additions and 21 deletions

View File

@ -2,6 +2,8 @@
/* The Translators - Spring 2025 */ /* The Translators - Spring 2025 */
#include "runner.h" #include "runner.h"
extern TableNode* funprime;
extern TableNode* arrayprim;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 1) { if (argc == 1) {

View File

@ -8,6 +8,8 @@
#include <stdlib.h> #include <stdlib.h>
char * typey = "type"; char * typey = "type";
char * funy = "function"; char * funy = "function";
TableNode* funprime;
TableNode* arrayprim;
typedef enum { typedef enum {
//First 4 below are primitive types that are all encapsulated in primitive type //First 4 below are primitive types that are all encapsulated in primitive type
@ -81,18 +83,20 @@ primitive_info* CreatePrimitiveInfo(size){
return prim; return prim;
} }
string_info* CreateStringInfo(int length, char* loc){ //probably don't need the below structure since can create from an array
/*string_info* CreateStringInfo(int length, char* loc){
string_info* stringy = (string_info*)malloc(sizeof(string_info)); string_info* stringy = (string_info*)malloc(sizeof(string_info));
stringy.length=length; stringy.length=length;
char* location = loc; char* location = loc;
return stringy; return stringy;
} }
*/
array_info* CreateArrayInfo(int dim, int* sizes, TableNode* type){ array_info* CreateArrayInfo(int dim, /*int* sizes,*/ TableNode* type){
array_info* arr = (array_info*)malloc(sizeof(array_info)); array_info* arr = (array_info*)malloc(sizeof(array_info));
arr.numofdimensions=dim; arr.numofdimensions=dim;
arr.typeofarray=type; arr->typeofarray=type;
int* dimensionsizes = loc; //avoiding storing any types like below
//int* dimensionsizes = loc;
return arr; return arr;
} }
@ -103,6 +107,7 @@ record_info* CreateRecordInfo(int length, TableNode* typesarray){
return reccy; return reccy;
} }
//below function takes a bool to see if parameter should be decomposed or not
function_declaration_info* CreateFunctionDeclarationInfo(int line, bool asorregular){ function_declaration_info* CreateFunctionDeclarationInfo(int line, bool asorregular){
function_declaration_info* fundec = (function_declaration_info*)malloc(sizeof(function_declaration_info)); function_declaration_info* fundec = (function_declaration_info*)malloc(sizeof(function_declaration_info));
fundec.startlinenumber=line; fundec.startlinenumber=line;
@ -158,19 +163,21 @@ SymbolTable* init(SymbolTable* start){
TableNode* chara = (TableNode*)malloc(sizeof(SymbolTable)); TableNode* chara = (TableNode*)malloc(sizeof(SymbolTable));
TableNode* stri = (TableNode*)malloc(sizeof(SymbolTable)); TableNode* stri = (TableNode*)malloc(sizeof(SymbolTable));
TableNode* boo = (TableNode*)malloc(sizeof(SymbolTable)); TableNode* boo = (TableNode*)malloc(sizeof(SymbolTable));
TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable)); //TableNode* arr = (TableNode*)malloc(sizeof(SymbolTable));
start->entries = integ; start->entries = integ;
integ->next = addr; integ->next = addr;
addr->next = chara; addr->next = chara;
chara->next = stri; chara->next = stri;
stri->next = boo; stri->next = boo;
boo->next = arr; //boo->next = arr;
boo->next = NULL;
integ->theName= "integer"; integ->theName= "integer";
addr->theName= "address"; addr->theName= "address";
chara->theName= "character"; chara->theName= "character";
boo->theName= "Boolean"; boo->theName= "Boolean";
stri->theName= "string"; stri->theName= "string";
arr->theName= "array" //arr->theName= "array"
//root TableNode that all are pointing to but not in table //root TableNode that all are pointing to but not in table
TableNode* prime = (TableNode*)malloc(sizeof(SymbolTable)); TableNode* prime = (TableNode*)malloc(sizeof(SymbolTable));
@ -178,19 +185,37 @@ SymbolTable* init(SymbolTable* start){
prime->theType=NULL; prime->theType=NULL;
prime->additionalinfo = NULL; prime->additionalinfo = NULL;
prime->next = NULL; prime->next = NULL;
TableNode* striprim = (TableNode*)malloc(sizeof(SymbolTable)); //note sur exatly how to get array types to look right so using a dummy Table Node below and updating the print symbol table function to access the additional information to print for array types, similar to function types
prime->theName= "1->character"; TableNode* arrayprim = (TableNode*)malloc(sizeof(SymbolTable));
prime->theName= "array";
prime->theType=NULL; prime->theType=NULL;
prime->additionalinfo = NULL; prime->additionalinfo = NULL;
prime->next = NULL; prime->next = NULL;
//funprime = CreateEntry(NULL,NULL,strdup("function primitive"),NULL);
//similar workaround to arrays above
TableNode* funprime = (TableNode*)malloc(sizeof(SymbolTable));
prime->theName= "primitive function";
prime->theType=NULL;
prime->additionalinfo = NULL;
prime->next = NULL;
integ->theType=prime; integ->theType=prime;
addr->theType=prime; addr->theType=prime;
chara->theType=prime; chara->theType=prime;
stri->theType=striprim; stri->theType=striprim;
boo->theType=prime; boo->theType=prime;
arr->theType=prime; arr->theType=arrayprim;
//filling in all the values for the additional info for initial types
integ->additionalinfo = CreatePrimitiveInfo(4);
addr->additionalinfo = CreatePrimitiveInfo(8);
chara->additionalinfo = CreatePrimitiveInfo(1);
stri->additionalinfo = CreateArrayInfo(1,chara);
boo->additionalinfo = CreatePrimitiveInfo(1);
//addr->additionalinfo = CreatePrimitiveInfo(8);
start->Line_Number = 1; start->Line_Number = 1;
start->Column_Number = 1; start->Column_Number = 1;
@ -304,6 +329,7 @@ void print_symbol_table(SymbolTable* table, FILE* file_ptr) {
} }
for (; entrie != NULL; entrie = entrie->next) { for (; entrie != NULL; entrie = entrie->next) {
if (parant_scope == 0) { if (parant_scope == 0) {
if(strcmp(entrie->theType->theName,"function primitive")|| strccmp(entrie->theType->theName,"
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n", fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
entrie->theName, current_scope, entrie->theType->theName, entrie->theName, current_scope, entrie->theType->theName,
"Extra annotation"); "Extra annotation");

View File

@ -7,15 +7,20 @@ typedef struct{
int size; int size;
}primitive_info; }primitive_info;
/*This structure can be subsumed into the structure below (1-d array of chars)
typedef struct{ typedef struct{
int length; //shouldn't need to store any values since would be compiled at runtime
char* location; //int length;
//char* location;
}string_info; }string_info;
*/
typedef struct{ typedef struct{
int numofdimensions; int numofdimensions;
//the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define > int* arr; //the above value tells you how long the below array is. For example if num of dimensions is 5, I can store 1,3,2,5,9 to define > int* arr;
int* sizesofdimensions; //shouldn't need to store any values (like sizes of dimenions or the location
//int* sizesofdimensions;
//do have to store type of array
TableNode* typeofarray; TableNode* typeofarray;
}array_info; }array_info;
@ -35,12 +40,12 @@ typedef struct{
}function_type_info; }function_type_info;
typedef union { typedef union {
PrimAdInfo* primitive_info; primitive_info* PrimAdInfo;
ArrayAdInfo* array_info; array_info* ArrayAdInfo;
RecAdInfo* record_info; record_info* RecAdInfo;
StringAdInfo* string_info; //string_info* StringAdInfo;
FunDecAdInfo* func_dec_info; func_dec_info* FunDecAdInfo;
FunTypeAdInfo* func_type_info; func_type_info* FunTypeAdInfo;
}AdInfo; }AdInfo;
typedef struct ListOfTable { typedef struct ListOfTable {