]> mj.ucw.cz Git - umpf.git/blob - int.c
NUM token is dead
[umpf.git] / int.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <pcre.h>
4
5 #include "cond.tab.h"
6 #include "brum.h"
7
8 #define OVECCOUNT 3
9
10 #define HASHSIZE 103
11 #define MAGIC 19
12
13 struct variable {
14         char* name;
15         char* value;
16         struct variable* next;
17 };
18
19 struct variable**
20 new_var_hash(void)
21 {
22         struct variable** res;
23
24         res = xmalloc (HASHSIZE * sizeof(struct variable*));
25         memset(res, 0, sizeof(struct variable*)*HASHSIZE);
26
27         return res;
28 }
29
30 static int
31 get_bucket_number(char* name)
32 {
33         unsigned int n = 0;
34         unsigned char* p = name;
35
36         while (*p != '\0'){
37                 n = n * MAGIC + *p++;
38         }
39         n %= HASHSIZE;
40
41         return n;
42 }
43
44 /* value NULL for finding without modyfiing */
45 static struct variable*
46 find_var(char* name, char* value, struct variable** hash)
47 {
48         int n;
49         struct variable *p;
50
51         n = get_bucket_number(name);
52
53         p = hash[n];
54         while(p && strcmp(p->name,name))
55                 p = p->next;
56
57         if (!value)
58                 return p;
59         if (p)
60                 free(p->value);
61         else {
62                 p = xmalloc(sizeof(struct variable));
63                 p->next = hash[n]->next;
64                 hash[n]->next = p;
65                 p->name = xstrdup(name);
66         }
67
68         p->value = xstrdup(value);
69
70         return p;
71 }
72
73 static void
74 print_ind(int num, char c)
75 {
76         int i;
77
78         for (i = 0; i < num; i++){
79                 putchar(c);
80         }
81 //printf("%*s", num, "");
82 }
83
84 static int 
85 regex_cmp(char* s, char* r)
86 {
87         pcre *brum;
88         int erroroffset;
89         const char* error;
90         int ovector[OVECCOUNT];
91         
92         brum = pcre_compile(r,0,&error,&erroroffset,NULL);
93         if (!brum)
94                 return -1;
95         
96         int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
97         pcre_free(brum);
98
99         return res;
100 }
101
102 void
103 print_tree(struct tree* t, int ind)
104 {
105         if (!t)
106                 return; 
107
108         switch (t->st){
109                 case ST_IF:
110                         print_ind(ind,' ');
111                         puts("if");
112                         print_tree(t->pt.tif.c,ind+1);
113                         print_ind(ind,' ');
114                         puts("then");
115                         print_tree(t->pt.tif.i,ind+1);
116                         print_ind(ind,' ');
117                         puts("else");
118                         print_tree(t->pt.tif.e,ind+1);
119                         break;
120                 case ST_COND:
121 #define UPPER(a) ((a) >> 8)
122 #define LOWER(a) ((a) & 0xFF)
123                         print_tree(t->pt.cond.left, ind+1);
124                         print_ind(ind,' ');
125
126                         if (UPPER(t->pt.cond.op) > 0)
127                                 putchar(UPPER(t->pt.cond.op));
128                         putchar(LOWER(t->pt.cond.op));
129                         putchar('\n'); 
130                         print_tree(t->pt.cond.right, ind+1);    
131                         break;
132                 case ST_BLOCK:
133                         print_tree(t->pt.block.head,ind);
134                         print_tree(t->pt.block.tail,ind);
135                         break;
136                 case ST_ASS:
137                         print_tree(t->pt.ass.left, ind+1);
138                         print_ind(ind,' ');     
139                         puts("=");
140                         print_tree(t->pt.ass.right, ind+1);
141                         break;
142                 case ST_LEAF:
143                         print_ind(ind, ' ');
144                         switch (t->pt.leaf.type){
145                                 case L_VAR:
146                                         putchar('$');
147                                 case L_CONST:
148                                         puts(t->pt.leaf.value);
149                                         break;
150                         }
151                         break;
152                 case ST_ARROW:
153                         if (t->pt.arrow.kw_left){
154                                 print_ind(ind+1, ' ');
155                                 puts(t->pt.arrow.kw_left);
156                         }
157                         print_ind(ind, ' ');
158                         printf("->\n");
159                         if (t->pt.arrow.kw_right){
160                                 print_ind(ind+1, ' ');
161                                 puts(t->pt.arrow.kw_right);
162                         }
163                         print_tree(t->pt.arrow.s,ind+1);
164                         break;
165                 case ST_OP:
166                         print_tree(t->pt.op.left, ind+1);
167                         print_ind(ind,' ');     
168                         putchar(t->pt.op.op);
169                         putchar('\n');
170                         print_tree(t->pt.op.right, ind+1);
171                         break;  
172                 case ST_EMPTY:
173                         break;  
174
175
176         }
177 }
178
179 void
180 interp(struct tree* t)
181 {
182         if (!t)
183                 return;
184
185         switch(t->st){
186                 case ST_BLOCK:
187                         interp(t->pt.block.head);
188                         interp(t->pt.block.tail);
189                 break;
190                 case ST_ASS:
191                 //      find_name(t->pt.ass.left->pt.leaf.value.s, interp_ass_right(t->pt.ass.right), var_hash);
192                 break;
193         }
194
195 };