From 43cde7c49ea65097d4118175e3849a566467395e Mon Sep 17 00:00:00 2001 From: Anicka Bernathova Date: Thu, 10 Jul 2008 10:50:03 +0200 Subject: [PATCH] all headers moved to brum.h --- Makefile | 2 +- brum.c | 20 ++++++ brum.h | 75 +++++++++++++++++++++ cond.y | 3 +- int.c | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lex.c | 4 +- lex.h | 2 - 7 files changed, 297 insertions(+), 7 deletions(-) create mode 100644 brum.c create mode 100644 brum.h create mode 100644 int.c delete mode 100644 lex.h diff --git a/Makefile b/Makefile index 67e0187..1ae9291 100644 --- a/Makefile +++ b/Makefile @@ -17,4 +17,4 @@ cond.tab.c: cond.y bison -dvt cond.y clean: - rm -rf cond.tab.[ch] cond.output cond *.o + rm -rf cond.tab.[ch] cond.output cond brum *.o diff --git a/brum.c b/brum.c new file mode 100644 index 0000000..06216b6 --- /dev/null +++ b/brum.c @@ -0,0 +1,20 @@ +#include +#include "brum.h" + +int +main(void) +{ + int res; + +// yydebug=1; + res = yyparse (); + + if (res) + return res; + + print_tree(input_tree,0); + + interp(input_tree); + + return 0; +} diff --git a/brum.h b/brum.h new file mode 100644 index 0000000..639bb59 --- /dev/null +++ b/brum.h @@ -0,0 +1,75 @@ +/* cond.h */ +int yylex (void); +void yyerror (char const *); + +struct tree { + enum { + ST_IF, + ST_COND, + ST_BLOCK, + ST_ASS, + ST_LEAF, + ST_EMPTY, + ST_ARROW, + ST_OP + } st; /* subtree type */ + union { + struct { + struct tree* c; /* condition */ + struct tree* i; /* if */ + struct tree* e; /* else */ + } tif; + + struct { + int op; + struct tree* left; + struct tree* right; + } cond; /* binary operator */ + + struct { + struct tree* head; + struct tree* tail; + } block; + + struct { + struct tree* left; + struct tree* right; + } ass; + + struct { + enum { + L_VAR, + L_CONST, + L_NUM + } type; + union { + char* s; + int n; + } value; + } leaf; + + struct { + char* kw_left; + char* kw_right; + struct tree* s; + } arrow; + + struct { + int op; + struct tree* left; + struct tree* right; + } op; + + } pt; +}; + +struct tree* input_tree; + +/* lex.c */ +void* xmalloc(size_t size); +char* xstrdup(char* s); +int line; + +/* int.c */ +void print_tree(struct tree* t, int ind); +void interp(struct tree* t); diff --git a/cond.y b/cond.y index 4816a03..a9d28d7 100644 --- a/cond.y +++ b/cond.y @@ -3,8 +3,7 @@ #include #include -#include "lex.h" -#include "cond.h" +#include "brum.h" static struct tree* tree_malloc(int type); diff --git a/int.c b/int.c new file mode 100644 index 0000000..0661c41 --- /dev/null +++ b/int.c @@ -0,0 +1,198 @@ +#include +#include +#include + +#include "cond.tab.h" +#include "brum.h" + +#define OVECCOUNT 3 + +#define HASHSIZE 103 +#define MAGIC 19 + +struct variable { + char* name; + char* value; + struct variable* next; +}; + +struct variable** +new_var_hash(void) +{ + struct variable** res; + + res = xmalloc (HASHSIZE * sizeof(struct variable*)); + memset(res, 0, sizeof(struct variable*)*HASHSIZE); + + return res; +} + +int +get_bucket_number(char* name) +{ + unsigned int n = 0; + unsigned char* p = name; + + while (*p != '\0'){ + n = n * MAGIC + *p++; + } + n %= HASHSIZE; + + return n; +} + +/* value NULL for finding without modyfiing */ +struct variable* +find_var(char* name, char* value, struct variable** hash) +{ + int n; + struct variable *p; + + n = get_bucket_number(name); + + p = hash[n]; + while(p && strcmp(p->name,name)) + p = p->next; + + if (!value) + return p; + if (p) + free(p->value); + else { + p = xmalloc(sizeof(struct variable)); + p->next = hash[n]->next; + hash[n]->next = p; + p->name = xstrdup(name); + } + + p->value = xstrdup(value); + + return p; +} + +static void +print_ind(int num, char c) +{ + int i; + + for (i = 0; i < num; i++){ + putchar(c); + } +//printf("%*s", num, ""); +} + +static int +regex_cmp(char* s, char* r) +{ + pcre *brum; + int erroroffset; + const char* error; + int ovector[OVECCOUNT]; + + brum = pcre_compile(r,0,&error,&erroroffset,NULL); + if (!brum) + return -1; + + int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT); + pcre_free(brum); + + return res; +} + +void +print_tree(struct tree* t, int ind) +{ + if (!t) + return; + + switch (t->st){ + case ST_IF: + print_ind(ind,' '); + puts("if"); + print_tree(t->pt.tif.c,ind+1); + print_ind(ind,' '); + puts("then"); + print_tree(t->pt.tif.i,ind+1); + print_ind(ind,' '); + puts("else"); + print_tree(t->pt.tif.e,ind+1); + break; + case ST_COND: +#define UPPER(a) ((a) >> 8) +#define LOWER(a) ((a) & 0xFF) + print_tree(t->pt.cond.left, ind+1); + print_ind(ind,' '); + + if (UPPER(t->pt.cond.op) > 0) + putchar(UPPER(t->pt.cond.op)); + putchar(LOWER(t->pt.cond.op)); + putchar('\n'); + print_tree(t->pt.cond.right, ind+1); + break; + case ST_BLOCK: + print_tree(t->pt.block.head,ind); + print_tree(t->pt.block.tail,ind); + break; + case ST_ASS: + print_tree(t->pt.ass.left, ind+1); + print_ind(ind,' '); + puts("="); + print_tree(t->pt.ass.right, ind+1); + break; + case ST_LEAF: + print_ind(ind, ' '); + switch (t->pt.leaf.type){ + case L_VAR: + putchar('$'); + case L_CONST: + puts(t->pt.leaf.value.s); + break; + case L_NUM: + printf("%d\n",t->pt.leaf.value.n); + break; + } + break; + case ST_ARROW: + if (t->pt.arrow.kw_left){ + print_ind(ind+1, ' '); + puts(t->pt.arrow.kw_left); + } + print_ind(ind, ' '); + printf("->\n"); + if (t->pt.arrow.kw_right){ + print_ind(ind+1, ' '); + puts(t->pt.arrow.kw_right); + } + print_tree(t->pt.arrow.s,ind+1); + break; + case ST_OP: + print_tree(t->pt.op.left, ind+1); + print_ind(ind,' '); + putchar(t->pt.op.op); + putchar('\n'); + print_tree(t->pt.op.right, ind+1); + break; + case ST_EMPTY: + break; + + + } +} + +void +interp(struct tree* t) +{ + if (!t) + return; + + switch(t->st){ + case ST_BLOCK: + interp(t->pt.block.head); + interp(t->pt.block.tail); + break; + case ST_ASS: + + break; + } + +}; diff --git a/lex.c b/lex.c index 9b48b68..ae541d9 100644 --- a/lex.c +++ b/lex.c @@ -5,7 +5,7 @@ #include #include "cond.tab.h" -#include "lex.h" +#include "brum.h" #define BUFSIZE 4096 #define KLEN 10 @@ -58,7 +58,7 @@ parse_err(char* msg, ...) exit(1); } -static char* +char* xstrdup(char* s) { void* ret; diff --git a/lex.h b/lex.h deleted file mode 100644 index 6e5e524..0000000 --- a/lex.h +++ /dev/null @@ -1,2 +0,0 @@ -void* xmalloc(size_t size); -int line; -- 2.39.2