Skip to content

Commit

Permalink
Fixed tree building, add print tree function
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka committed Feb 17, 2012
1 parent fbf54f6 commit fa920a1
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 66 deletions.
34 changes: 20 additions & 14 deletions cdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ struct Token {

typedef struct Token Token;

struct tree_thread {
int level;
};

typedef struct tree_thread;

#define TYPE_CHAR 0
#define TYPE_SHORT 1
#define TYPE_INT 2
Expand All @@ -33,9 +27,11 @@ typedef struct tree_thread;
#define VAR_VOLATILE 3

#define ITEM_STRUCT 1
#define ITEM_ARRAY 2
#define ITEM_POINTER 3
#define ITEM_VARIABLE 4
#define ITEM_UNION 2
#define ITEM_ENUM 3
#define ITEM_ARRAY 4
#define ITEM_POINTER 5
#define ITEM_VARIABLE 6

struct item_variable {
char* name;
Expand Down Expand Up @@ -89,21 +85,31 @@ struct item_struct {
item_list *items;
};

struct item_union {
char* name;
long size;
int level;
item_list *items;
};

struct item_lst {
short item_type;
union {
struct item_variable *var;
struct item_pointer *ptr;
struct item_array *arr;
struct item_struct *str;
struct item_union *un;
} item;
item_list *next;
item_list *prev;
item_list *head;
};

int new_variable_node(item_list *ctx, char* name, short type, short sign, short modifier);
int new_pointer_node(item_list *ctx, char* name, short type, short sign, short modifier);
int new_array_node(item_list *ctx, char* name, short type, short sign, short modifier, long size);
int new_struct_node(item_list *ctx, char* name);
int new_union_node(item_list *ctx, char* name);
item_list* new_variable_node(item_list *ctx, char* name, short type, short sign, short modifier);
item_list* new_pointer_node(item_list *ctx, char* name, short type, short sign, short modifier);
item_list* new_array_node(item_list *ctx, char* name, short type, short sign, short modifier, long size);
item_list* new_struct_node(item_list *ctx, char* name, item_list *defs);
item_list* new_union_node(item_list *ctx, char* name, item_list *defs);

int print_tree(item_list *tmp);
1 change: 0 additions & 1 deletion cparse.l
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"unsigned" { return(UNSIGNED); }
"static" { return(STATIC); }
"volatile" { return(VOLATILE); }
"enum" { return(ENUM); }
"struct" { return(STRUCT); }
"union" { return(UNION); }

Expand Down
47 changes: 27 additions & 20 deletions cparse.y
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,37 @@

%extra_argument {item_list *ctx}

program ::= deflist.
deflist ::= deflist def.
deflist ::= def.
def ::= struct.
def ::= union.
def ::= enum.
def ::= variable.
def ::= pointer.
def ::= array.
struct ::= STRUCT name(A) OBRACE deflist EBRACE SEMICOLON. {
new_struct_node(ctx, A.sval);
%type source {item_list *}
%type deflist {item_list *}
%type def {item_list *}
%type struct {item_list *}
%type union {item_list *}
%type variable {item_list *}
%type pointer {item_list *}
%type array {item_list *}

source(A) ::= deflist(B). { A = B; ctx = A; }
deflist(A) ::= deflist(B) def(C). { B->next = C; A = B; }
deflist(A) ::= def(B). { A = B; }
def(A) ::= struct(B). { A = B; }
def(A) ::= union(B). { A = B; }
def(A) ::= variable(B). { A = B; }
def(A) ::= pointer(B). { A = B; }
def(A) ::= array(B). { A = B; }
struct(A) ::= STRUCT name(B) OBRACE deflist(C) EBRACE SEMICOLON. {
A = new_struct_node(ctx, B.sval, C);
}
union ::= UNION name(A) OBRACE deflist EBRACE SEMICOLON. {
new_union_node(ctx, A.sval);
union(A) ::= UNION name(B) OBRACE deflist(C) EBRACE SEMICOLON. {
A = new_union_node(ctx, B.sval, C);
}
enum ::= ENUM name(A) OBRACE deflist EBRACE SEMICOLON.
variable ::= modifier(D) signedness(C) type(B) name(A) SEMICOLON. {
new_variable_node(ctx, A.sval, B.dval, C.dval, D.dval);
variable(A) ::= modifier(E) signedness(D) type(C) name(B) SEMICOLON. {
A = new_variable_node(ctx, B.sval, C.dval, D.dval, E.dval);
}
pointer ::= modifier(D) signedness(C) type(B) ASTERISK name(A) SEMICOLON. {
new_pointer_node(ctx, A.sval, B.dval, C.dval, D.dval);
pointer(A) ::= modifier(E) signedness(D) type(C) ASTERISK name(B) SEMICOLON. {
A = new_pointer_node(ctx, B.sval, C.dval, D.dval, E.dval);
}
array ::= modifier(E) signedness(D) type(C) name(A) LBRACKET size(B) RBRACKET SEMICOLON. {
new_array_node(ctx, A.sval, C.dval, D.dval, E.dval, B.dval);
array(A) ::= modifier(F) signedness(E) type(D) name(B) LBRACKET size(C) RBRACKET SEMICOLON. {
A = new_array_node(ctx, B.sval, D.dval, E.dval, F.dval, C.dval);
}
size(A) ::= NUMBER(B). { A.dval = B.dval; }
type ::= .
Expand Down
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ int main(int argc, char** argv)

cdataParse(pParser, 0, yylval, ctx);
cdataParseFree(pParser, free);
print_tree(ctx);
return EXIT_SUCCESS;
}
103 changes: 72 additions & 31 deletions tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,116 @@ int new_tree(){
return 0;
}

int new_variable_node(item_list *ctx, char* name, short type, short sign, short modifier)
int print_tree(item_list *tmp) {
item_list *p;
p = tmp;
if (p != NULL) {
while (p != NULL) {
switch (p->item_type) {
case ITEM_VARIABLE:
printf("var %s\n", p->item.var->name);
break;
case ITEM_POINTER:
printf("ptr %s\n", p->item.ptr->name);
break;
case ITEM_ARRAY:
printf("arr %s[%ld]\n", p->item.arr->name, p->item.arr->count);
break;
case ITEM_STRUCT:
printf("Entering struct %s...\n", p->item.str->name);
print_tree(p->item.str->items);
break;
case ITEM_UNION:
printf("Entering union %s...\n", p->item.un->name);
print_tree(p->item.un->items);
break;
default:
printf("invalid item!\n");
break;
}
p = p->next;
}
} else {
printf("Empty tree!\n");
}
return 0;
}

item_list* new_variable_node(item_list *ctx, char* name, short type, short sign, short modifier)
{
struct item_variable *ivar = (struct item_variable *)malloc(sizeof(struct item_variable));
item_list *tmp;
ivar->name = name;
ivar->type = type;
ivar->sign = sign;
ivar->modifier = modifier;
printf("Creating new var \"%s\"\n", name);
// calculate var size
// cursor->insert_item
ctx->next = (item_list *)malloc(sizeof(item_list));
ctx = ctx->next;
ctx->next = NULL;
ctx->item_type = ITEM_VARIABLE;
ctx->item.var = ivar;
return 0;
//printf("Creating new var \"%s\"\n", name);
tmp = (item_list *)malloc(sizeof(item_list));
tmp->next = NULL;
tmp->item_type = ITEM_VARIABLE;
tmp->item.var = ivar;
return tmp;
}

int new_pointer_node(item_list *ctx, char* name, short type, short sign, short modifier)
item_list* new_pointer_node(item_list *ctx, char* name, short type, short sign, short modifier)
{
struct item_pointer *iptr = (struct item_pointer *)malloc(sizeof(struct item_pointer));
item_list *tmp;
iptr->name = name;
iptr->type = type;
iptr->sign = sign;
iptr->modifier = modifier;
printf("Creating new ptr \"%s\"\n", name);
//printf("Creating new ptr \"%s\"\n", name);
// calculate pointer size
// cursor->insert_item
ctx->next = (item_list *)malloc(sizeof(item_list));
ctx = ctx->next;
ctx->next = NULL;
ctx->item_type = ITEM_POINTER;
ctx->item.ptr = iptr;
return 0;
tmp = (item_list *)malloc(sizeof(item_list));
tmp->next = NULL;
tmp->item_type = ITEM_POINTER;
tmp->item.ptr = iptr;
return tmp;
}

int new_array_node(item_list *ctx, char* name, short type, short sign, short modifier, long size)
item_list* new_array_node(item_list *ctx, char* name, short type, short sign, short modifier, long size)
{
struct item_array *iarr = (struct item_array *)malloc(sizeof(struct item_array));
item_list *tmp;
iarr->name = name;
iarr->type = type;
iarr->count = size;
iarr->sign = sign;
iarr->modifier = modifier;
printf("Creating new array \"%s\"[%ld]\n", name, size);
//printf("Creating new array \"%s\"[%ld]\n", name, size);
// calculate array size
ctx->next = (item_list *)malloc(sizeof(item_list));
ctx = ctx->next;
ctx->next = NULL;
ctx->item_type = ITEM_ARRAY;
ctx->item.arr = iarr;
return 0;
tmp = (item_list *)malloc(sizeof(item_list));
tmp->next = NULL;
tmp->item_type = ITEM_ARRAY;
tmp->item.arr = iarr;
return tmp;
}

int new_struct_node(item_list *ctx, char* name)
item_list* new_struct_node(item_list *ctx, char* name, item_list *defs)
{
printf("Members are:\n");
print_tree(defs);
struct item_struct *istr = (struct item_struct *)malloc(sizeof(struct item_struct));
item_list *tmp = (item_list *)malloc(sizeof(item_list));
istr->name = name;
printf("Creating new structure \"%s\"\n", name);
istr->items = defs;
//printf("Creating new structure \"%s\"\n", name);
// cursor->insert_item
// set cursor here
return 0;
tmp->next = NULL;
tmp->item_type = ITEM_STRUCT;
tmp->item.str = istr;
//print_tree(tmp);
return tmp;
}

int new_union_node(item_list *ctx, char* name)
item_list* new_union_node(item_list *ctx, char* name, item_list *defs)
{
//struct item_union iun;
//iun.name = name;
// cursor->insert_item
// set cursor here
return 0;
return NULL;
}

0 comments on commit fa920a1

Please sign in to comment.