15 struct variable** res;
17 res = xmalloc (HASHSIZE * sizeof(struct variable*));
18 memset(res, 0, sizeof(struct variable*)*HASHSIZE);
24 get_bucket_number(char* name)
27 unsigned char* p = name;
37 /* value NULL for finding without modyfiing */
38 static struct variable*
39 find_var(char* name, char* value, struct variable** hash)
44 n = get_bucket_number(name);
47 while(p && strcmp(p->name,name))
53 } else if (p && !value)
56 p = xmalloc(sizeof(struct variable));
59 p->name = xstrdup(name);
60 p->value = (value? value:xstrdup(""));
67 regex_cmp(char* s, char* r)
72 int ovector[OVECCOUNT];
74 brum = pcre_compile(r,0,&error,&erroroffset,NULL);
78 int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
85 print_tree(struct tree* t, int ind)
92 printf("%*s if\n", ind, "");
93 print_tree(t->pt.tif.c,ind+1);
94 printf("%*s then\n", ind, "");
95 print_tree(t->pt.tif.i,ind+1);
96 printf("%*s else\n", ind, "");
97 print_tree(t->pt.tif.e,ind+1);
100 #define UPPER(a) ((a) >> 8)
101 #define LOWER(a) ((a) & 0xFF)
102 print_tree(t->pt.cond.left, ind+1);
103 printf("%*s", ind, "");
104 if (UPPER(t->pt.cond.op) > 0)
105 putchar(UPPER(t->pt.cond.op));
106 putchar(LOWER(t->pt.cond.op));
108 print_tree(t->pt.cond.right, ind+1);
111 print_tree(t->pt.block.head,ind);
112 print_tree(t->pt.block.tail,ind);
115 print_tree(t->pt.ass.left, ind+1);
116 printf("%*s =\n", ind, "");
117 print_tree(t->pt.ass.right, ind+1);
120 printf("%*s", ind, "");
121 switch (t->pt.leaf.type){
125 puts(t->pt.leaf.value);
130 if (t->pt.arrow.kw_left)
131 printf("%*s%s\n", ind+1, "", t->pt.arrow.kw_left);
132 printf("%*s ->\n", ind, "");
133 if (t->pt.arrow.kw_right)
134 printf("%*s%s\n", ind+1, "", t->pt.arrow.kw_right);
135 print_tree(t->pt.arrow.s,ind+1);
138 print_tree(t->pt.op.left, ind+1);
139 printf("%*s%c\n", ind, "", t->pt.op.op);
140 print_tree(t->pt.op.right, ind+1);
150 xcat(char* left, char* right)
152 char* res = xmalloc(strlen(left) + strlen(right) + 1);
164 interp_ass_right(struct tree* t, struct variable** hash)
168 if (t->pt.leaf.type == L_VAR)
169 return xstrdup(find_var(t->pt.leaf.value,NULL,hash)->value);
171 return xstrdup(t->pt.leaf.value);
173 switch (t->pt.op.op){
175 return xcat(interp_ass_right(t->pt.op.left, hash),interp_ass_right(t->pt.op.right, hash));
180 die("interp_ass_right: got to default");
185 interp_cond(struct tree* t, struct variable** hash)
187 if (t->st != ST_COND)
188 die("Muhehehechlemst?");
190 if (t->pt.cond.type == OP_REL){
191 if (t->pt.cond.left->st != ST_LEAF || t->pt.cond.right->st != ST_LEAF)
194 char* left = (t->pt.cond.left->pt.leaf.type == L_VAR ? find_var(t->pt.cond.left->pt.leaf.value,NULL,hash)->value : t->pt.cond.left->pt.leaf.value);
195 char* right = (t->pt.cond.right->pt.leaf.type == L_VAR ? find_var(t->pt.cond.right->pt.leaf.value,NULL,hash)->value : t->pt.cond.right->pt.leaf.value);
196 switch (t->pt.cond.op){
198 return !strcmp(left,right);
200 return strcmp(left,right);
202 return regex_cmp(left,right);
204 return !regex_cmp(left,right);
208 int left = interp_cond(t->pt.cond.left, hash);
211 if (t->pt.cond.op != '!')
212 right = interp_cond(t->pt.cond.right, hash);
214 switch (t->pt.cond.op){
216 return left && right;
218 return left || right;
220 return (left || right) && !(left && right);
228 interp(struct tree* t, struct variable** hash)
235 interp(t->pt.block.head, hash);
236 interp(t->pt.block.tail, hash);
239 find_var(t->pt.ass.left->pt.leaf.value, interp_ass_right(t->pt.ass.right, hash), hash);
242 if (interp_cond(t->pt.tif.c, hash))
243 interp(t->pt.tif.i, hash);
245 interp(t->pt.tif.e, hash);
250 die("interp: got to default");
256 print_vars(struct variable** hash)
261 for (i=0; i<HASHSIZE; i++){
264 printf("%s=%s\n",p->name, p->value);