]> mj.ucw.cz Git - umpf.git/commitdiff
all headers moved to brum.h
authorAnicka Bernathova <anicka@anicka.net>
Thu, 10 Jul 2008 08:50:03 +0000 (10:50 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Thu, 10 Jul 2008 08:50:03 +0000 (10:50 +0200)
Makefile
brum.c [new file with mode: 0644]
brum.h [new file with mode: 0644]
cond.y
int.c [new file with mode: 0644]
lex.c
lex.h [deleted file]

index 67e0187dbbd00fd77af4724558b9d94ef217f93d..1ae92919d5daa9dd84192d0b23c69a60a9da3913 100644 (file)
--- 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 (file)
index 0000000..06216b6
--- /dev/null
+++ b/brum.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#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 (file)
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 4816a03635660129b2a6873b19a8d5ca5e7a4e7c..a9d28d728719c9e3d8fdd52c196a462dba633deb 100644 (file)
--- a/cond.y
+++ b/cond.y
@@ -3,8 +3,7 @@
 #include <stdio.h>
 #include <string.h>
 
-#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 (file)
index 0000000..0661c41
--- /dev/null
+++ b/int.c
@@ -0,0 +1,198 @@
+#include <stdio.h>
+#include <string.h>
+#include <pcre.h>
+
+#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 9b48b680868efbd977aaee773d7c5dd31ca6c816..ae541d95701ad6d67ea7e56c1a5dd0ea26487ee3 100644 (file)
--- a/lex.c
+++ b/lex.c
@@ -5,7 +5,7 @@
 #include <stdarg.h>
 
 #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 (file)
index 6e5e524..0000000
--- a/lex.h
+++ /dev/null
@@ -1,2 +0,0 @@
-void* xmalloc(size_t size);
-int line;