diff --git a/SQLPRefExp.y b/SQLPRefExp.y index 896aca6a06d5f3a3eb1f09f1618fed2f29f527e2..81c35b01a52cf3ef2336483297fe5e335b5327ac 100644 --- a/SQLPRefExp.y +++ b/SQLPRefExp.y @@ -21,63 +21,68 @@ SQLPRefExp : RefExpTypeComponentList { - $$ = $1; + $$ = create_cons_cell_w_atom("SQLP_REF_EXP", create_list("REF_EXP_TYPE_COMPONENT", $1)); }; RefExpTypeComponentList - : RefExpTypeComponentList ';' RefExpTypeComponent + : RefExpTypeComponent ';' RefExpTypeComponentList { - $$ = NULL; + cdr($1) = $3; + $$ = $1; } | RefExpTypeComponent { - $$ = NULL; + $$ = $1; }; RefExpTypeComponent : Guard REF AttributePath { - $$ = NULL; + cdr($1) = $3; + $$ = create_cons_cell_w_atom("REF_EXP_TYPE_COMPONENT", $1); } | Guard REF '(' AttributePathList ')' { - $$ = NULL; + cdr($1) = $4; + $$ = create_cons_cell_w_atom("REF_EXP_TYPE_COMPONENT", $1); }; Guard : TableIdentifier { - $$ = NULL; + $$ = create_cons_cell_w_atom("GUARD", $1); }; AttributePathList - : AttributePathList ',' AttributePath + : AttributePath ',' AttributePathList { - $$ = NULL; + cdr($1) = $3; + $$ = $1; } | AttributePath { - $$ = NULL; + $$ = create_list("ATTRIBUTE", $1); }; AttributePath - : AttributePath '.' AttributeIdentifier + : AttributeIdentifier '.' AttributePath { - $$ = NULL; + cdr($1) = $3; + $$ = $1; } | AttributeIdentifier { - $$ = NULL; + $$ = $1; }; AttributeIdentifier : IDENTIFIER { - $$ = NULL; + $$ = create_identifier("ATTRIBUTE\0", $1); } TableIdentifier : IDENTIFIER { - $$ = NULL; + $$ = create_identifier("TABLE\0", $1); }; diff --git a/util.c b/util.c index 2225f3fa9abdd6041a21fdfa268a530775ecbe9d..b459befcbb4ba5bb28f45c2875123eb5dda2772a 100644 --- a/util.c +++ b/util.c @@ -11,7 +11,7 @@ cons_cell* create_cons_cell(void* car, cons_cell* cdr) { atom* create_atom(char* val) { - printf("create_atom: %s\n", val); + // printf("create_atom: %s\n", val); atom* new_atom = malloc(sizeof(atom)); new_atom->val = malloc(sizeof(char) * (strlen(val) + 1)); strcpy(new_atom->val, val); @@ -28,10 +28,45 @@ cons_cell* create_cons_cell_w_atom(char* val, cons_cell* cdr) { return new_cons; } -void * car(cons_cell * cc) { +void *& car(cons_cell * cc) { return cc->car; } -void * cdr(cons_cell * cc) { +cons_cell *& cdr(cons_cell * cc) { return cc->cdr; } + +cons_cell* create_table_identifier(char *type, char *identifier) { + char str[100]; + strcpy(str, type); + strcat(str, "_IDENTIFIER\0"); + cons_cell* cc = create_cons_cell_w_atom(identifier, NULL); + return create_cons_cell_w_atom(str, cc); +} + +cons_cell * create_list(char *type, cons_cell *list) { + char str[100]; + strcpy(str, type); + strcat(str, "_LIST\0"); + return create_cons_cell(str, list); +} + +static void print_cons_tree_helper(cons_cell *cc, unsigned int padding) { + if (cc == NULL) { + return; + } + if (cc->is_atom) { + for (unsigned int i = 0; i < padding; ++i) { + printf(" "); + } + printf("%s\n", ((atom*) car(cc))->val); + print_cons_tree_helper(cdr(cc), padding); + } else { + print_cons_tree_helper(car(cc), padding + 2); + print_cons_tree_helper(cdr(cc), padding); + } +} + +void print_cons_tree(cons_cell *cc) { + print_cons_tree_helper(cc, 0); +} diff --git a/util.h b/util.h index 497221e20c477a6e15e8949cb3c41f68cd011ef9..0747274602d2fdd61c123d6cb31599637b0698d7 100644 --- a/util.h +++ b/util.h @@ -29,8 +29,14 @@ atom* create_atom(char* val); // Creates a cons_cell that has an atom as its car cons_cell* create_cons_cell_w_atom(char* val, cons_cell* cdr); -void * car(cons_cell * cc); +void *& car(cons_cell * cc); -void * cdr(cons_cell * cc); +cons_cell *& cdr(cons_cell * cc); + +cons_cell * create_identifier(char *type, char *identifier); + +cons_cell * create_list(char *type, cons_cell *list); + +void print_cons_tree(cons_cell *cc); #endif