]> mj.ucw.cz Git - umpf.git/blob - cond.y
6473557660d944e472b17612517f7440092b6a4c
[umpf.git] / cond.y
1 %{
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <pcre.h>
6
7 #define OVECCOUNT 3
8
9 int yylex (void);
10 void yyerror (char const *);
11
12 int regex_cmp(char* s, char* r);
13
14 %}
15
16 %union {
17         int b;  
18         int n;
19         char* str;
20 }
21
22 %token <str> CONST
23 %token <n> NUM
24 %token <str> VAR
25 %token KW_IF KW_ELSE KW_PIPE KW_MAIL KW_COPY
26 %token '(' ')' '{' '}'
27 %left ARROW
28 %left EQ NEQ GE LE '<' '>' RE NRE
29 %left '+' '-' 
30 %left '*' '/'
31 %left '|' '&' '^'
32 %left '!'
33 %left '=' 
34 %type <b> boo
35
36 %%
37 input:  /* empty */
38         | input line
39 ;
40
41 line:   '\n'
42         | boo '\n'              { printf("%s\n",$1?"true":"false"); }
43         | error '\n'            { yyerrok; }
44 ;
45
46 boo:     CONST EQ CONST         { $$ = ! strcmp($1, $3); }
47         | CONST NEQ CONST       { $$ = !! strcmp($1, $3); }
48         | CONST RE CONST        { $$ = regex_cmp($1,$3) >= 0 }
49         | CONST NRE CONST       { $$ = regex_cmp($1,$3) < 0 }
50         | NUM EQ NUM            { $$ = $1 == $3 }
51         | NUM NEQ NUM           { $$ = $1 != $3 }
52         | NUM GE NUM            { $$ = $1 >= $3 }
53         | NUM LE NUM            { $$ = $1 <= $3 }
54         | NUM '>' NUM           { $$ = $1 > $3 }
55         | NUM '<' NUM           { $$ = $1 < $3 }
56         | boo '|' boo           { $$ = $1 || $3 }
57         | boo '&' boo           { $$ = $1 && $3 }
58         | boo '^' boo           { $$ = ($1 || $3) && !($1 && $3) }
59         | '!' boo               { $$ = ! $2 }
60 ;
61
62 ;
63 %%
64
65 int 
66 regex_cmp(char* s, char* r)
67 {
68         pcre *brum;
69         int erroroffset;
70         const char* error;
71         int ovector[OVECCOUNT];
72         
73         brum = pcre_compile(r,0,&error,&erroroffset,NULL);
74         if (!brum)
75                 return -1;
76         
77         int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
78         
79         pcre_free(brum);
80
81         return res;
82 }
83
84 void
85 yyerror (char const *s)
86 {
87         fprintf (stderr, "%s\n", s);
88 }
89
90 int
91 main(void)
92 {
93 //      yydebug=1;
94         return yyparse ();
95 }