Skip to content

Commit

Permalink
Added parser context
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka committed Feb 17, 2012
1 parent 91349aa commit 199cada
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
17 changes: 12 additions & 5 deletions cdata.h
Expand Up @@ -4,8 +4,15 @@ struct Token {
int dval;
char* sval;
};

typedef struct Token Token;

struct strucContx {
int level;
};

typedef struct strucContx strucContx;

#define TYPE_CHAR 0
#define TYPE_SHORT 1
#define TYPE_INT 2
Expand Down Expand Up @@ -79,8 +86,8 @@ struct item_lst {
item_list *head;
};

int new_variable_node(char* name, int type);
int new_pointer_node(char* name, int type);
int new_array_node(char* name, int type, long size);
int new_struct_node(char* name);
int new_union_node(char* name);
int new_variable_node(strucContx *ctx, char* name, int type);
int new_pointer_node(strucContx *ctx, char* name, int type);
int new_array_node(strucContx *ctx, char* name, int type, long size);
int new_struct_node(strucContx *ctx, char* name);
int new_union_node(strucContx *ctx, char* name);
14 changes: 9 additions & 5 deletions cparse.y
Expand Up @@ -14,6 +14,8 @@
%token_type {Token}
%default_type {Token}

%extra_argument {strucContx *ctx}

program ::= deflist.
deflist ::= deflist def.
deflist ::= def.
Expand All @@ -24,18 +26,20 @@ def ::= variable.
def ::= pointer.
def ::= array.
struct ::= STRUCT name(A) OBRACE deflist EBRACE SEMICOLON. {
new_struct_node(A.sval);
new_struct_node(ctx, A.sval);
}
union ::= UNION name(A) OBRACE deflist EBRACE SEMICOLON. {
new_union_node(ctx, A.sval);
}
union ::= UNION name(A) OBRACE deflist EBRACE SEMICOLON.
enum ::= ENUM name(A) OBRACE deflist EBRACE SEMICOLON.
variable ::= modifier signedness type(B) name(A) SEMICOLON. {
new_variable_node(A.sval, B.dval);
new_variable_node(ctx, A.sval, B.dval);
}
pointer ::= modifier signedness type(B) ASTERISK name(A) SEMICOLON. {
new_pointer_node(A.sval, B.dval);
new_pointer_node(ctx, A.sval, B.dval);
}
array ::= modifier signedness type(C) name(A) LBRACKET size(B) RBRACKET SEMICOLON. {
new_array_node(A.sval, B.dval, C.dval);
new_array_node(ctx, A.sval, B.dval, C.dval);
}
size(A) ::= NUMBER(B). { A.dval = B.dval; }
type ::= .
Expand Down
2 changes: 1 addition & 1 deletion parser.sh
Expand Up @@ -3,6 +3,6 @@
flex cparse.l
gcc -c -o lex.o lex.yy.c -lm
gcc -c -o cparse.o cparse.c
gcc -c -p tree.o tree.c
gcc -c -o tree.o tree.c
gcc -c -o main.o main.c
gcc -o cparse main.o tree.o cparse.o lex.o -lm
9 changes: 9 additions & 0 deletions test_parser.c
Expand Up @@ -7,10 +7,19 @@ struct highlight {
int linux;
char *qwe;
char* lst;
struct wefs {
long a;
long b;
char c;
};
long zied[15];
};
int flg;
int palette;
struct qwe125 {
char* sdf;
short l15;
};
long qwe;
int toggle;
};
Expand Down
37 changes: 32 additions & 5 deletions tree.c
@@ -1,24 +1,51 @@
int new_variable_node(char* name, int type)
#include <stdio.h>
#include "cdata.h"

int new_variable_node(strucContx *ctx, char* name, int type)
{
struct item_variable ivar;
ivar.name = name;
ivar.type = type;
// calculate var size
// cursor->insert_item
return 0;
}

int new_pointer_node(char* name, int type)
int new_pointer_node(strucContx *ctx, char* name, int type)
{
struct item_pointer iptr;
iptr.name = name;
iptr.type = type;
// calculate pointer size
// cursor->insert_item
return 0;
}

int new_array_node(char* name, int type, long size)
int new_array_node(strucContx *ctx, char* name, int type, long size)
{
struct item_array iarr;
iarr.name = name;
iarr.type = type;
iarr.count = size;
// calculate array size
return 0;
}

int new_struct_node(char* name)
int new_struct_node(strucContx *ctx, char* name)
{
struct item_struct istr;
istr.name = name;
printf("Creating new structure \"%s\"\n", name);
// cursor->insert_item
// set cursor here
return 0;
}

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

0 comments on commit 199cada

Please sign in to comment.