aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/shared/mo_lambda.cpp
blob: ab4bb0d8e470a709f994d5e4c2ea4ead397b8462 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
//  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
#include "mo_lambda.hpp"
#include <string.h>
#include <stdlib.h>

namespace boost {
namespace locale {
namespace gnu_gettext {
namespace lambda {

namespace { // anon
    struct identity : public plural {
        virtual int operator()(int n) const 
        {
            return n;
        };
        virtual identity *clone() const
        {
            return new identity();
        }
    };

    struct unary : public plural 
    {
        unary(plural_ptr ptr) : 
            op1(ptr)
        {
        }
    protected:
        plural_ptr op1;
    };


    struct binary : public plural 
    {
        binary(plural_ptr p1,plural_ptr p2) :
            op1(p1),
            op2(p2)
        {
        }
    protected:
        plural_ptr op1,op2;
    };

    struct number : public plural
    {
        number(int v) : 
            val(v)
        {
        }
        virtual int operator()(int /*n*/) const 
        {
            return val;
        }
        virtual number *clone() const
        {
            return new number(val);
        }

    private:
        int val;
    };

    #define UNOP(name,oper)                         \
    struct name: public unary {                     \
        name(plural_ptr op) : unary(op)             \
        {                                           \
        };                                          \
        virtual int operator()(int n) const         \
        {                                           \
            return oper (*op1)(n);                  \
        }                                           \
        virtual name *clone() const                 \
        {                                           \
            plural_ptr op1_copy(op1->clone());      \
            return new name(op1_copy);              \
        }                                           \
    };

    #define BINOP(name,oper)                        \
    struct name : public binary                     \
    {                                               \
        name(plural_ptr p1,plural_ptr p2) :         \
            binary(p1,p2)                           \
        {                                           \
        }                                           \
                                                    \
        virtual int operator()(int n) const         \
        {                                           \
            return (*op1)(n) oper (*op2)(n);        \
        }                                           \
        virtual name *clone() const                 \
        {                                           \
            plural_ptr op1_copy(op1->clone());      \
            plural_ptr op2_copy(op2->clone());      \
            return new name(op1_copy,op2_copy);     \
        }                                           \
    };

    #define BINOPD(name,oper)                       \
    struct name : public binary {                   \
        name(plural_ptr p1,plural_ptr p2) :         \
            binary(p1,p2)                           \
        {                                           \
        }                                           \
        virtual int operator()(int n) const         \
        {                                           \
            int v1=(*op1)(n);                       \
            int v2=(*op2)(n);                       \
            return v2==0 ? 0 : v1 oper v2;          \
        }                                           \
        virtual name *clone() const                 \
        {                                           \
            plural_ptr op1_copy(op1->clone());      \
            plural_ptr op2_copy(op2->clone());      \
            return new name(op1_copy,op2_copy);     \
        }                                           \
    };

    enum { END = 0 , SHL = 256,  SHR, GTE,LTE, EQ, NEQ, AND, OR, NUM, VARIABLE };

    UNOP(l_not,!)
    UNOP(minus,-)
    UNOP(bin_not,~)

    BINOP(mul,*)
    BINOPD(div,/)
    BINOPD(mod,%)
    static int level10[]={3,'*','/','%'};

    BINOP(add,+)
    BINOP(sub,-)
    static int level9[]={2,'+','-'};

    BINOP(shl,<<)
    BINOP(shr,>>)
    static int level8[]={2,SHL,SHR};

    BINOP(gt,>)
    BINOP(lt,<)
    BINOP(gte,>=)
    BINOP(lte,<=)
    static int level7[]={4,'<','>',GTE,LTE};

    BINOP(eq,==)
    BINOP(neq,!=)
    static int level6[]={2,EQ,NEQ};

    BINOP(bin_and,&)
    static int level5[]={1,'&'};

    BINOP(bin_xor,^)
    static int level4[]={1,'^'};

    BINOP(bin_or,|)
    static int level3[]={1,'|'};

    BINOP(l_and,&&)
    static int level2[]={1,AND};

    BINOP(l_or,||)
    static int level1[]={1,OR};

    struct conditional : public plural {
        conditional(plural_ptr p1,plural_ptr p2,plural_ptr p3) :
             op1(p1),
             op2(p2),
             op3(p3)
        {
        }
        virtual int operator()(int n) const
        {
            return (*op1)(n) ? (*op2)(n) : (*op3)(n);
        }
        virtual conditional *clone() const                 
        {                                           
            plural_ptr op1_copy(op1->clone());      
            plural_ptr op2_copy(op2->clone());      
            plural_ptr op3_copy(op3->clone());      
            return new conditional(op1_copy,op2_copy,op3_copy);     
        }                                           
    private:
        plural_ptr op1,op2,op3;
    };


    plural_ptr bin_factory(int value,plural_ptr left,plural_ptr right)
    {

        switch(value) {
        case '/':  return plural_ptr(new div(left,right));
        case '*':  return plural_ptr(new mul(left,right));
        case '%':  return plural_ptr(new mod(left,right));
        case '+':  return plural_ptr(new add(left,right));
        case '-':  return plural_ptr(new sub(left,right));
        case SHL:  return plural_ptr(new shl(left,right));
        case SHR:  return plural_ptr(new shr(left,right));
        case '>':  return plural_ptr(new  gt(left,right));
        case '<':  return plural_ptr(new  lt(left,right));
        case GTE:  return plural_ptr(new gte(left,right));
        case LTE:  return plural_ptr(new lte(left,right));
        case  EQ:  return plural_ptr(new  eq(left,right));
        case NEQ:  return plural_ptr(new neq(left,right));
        case '&':  return plural_ptr(new bin_and(left,right));
        case '^':  return plural_ptr(new bin_xor(left,right));
        case '|':  return plural_ptr(new bin_or (left,right));
        case AND:  return plural_ptr(new l_and(left,right));
        case  OR:  return plural_ptr(new l_or(left,right));
        default:
            return plural_ptr();
        }
    }

    static inline bool is_in(int v,int *p)
    {
        int len=*p;
        p++;
        while(len && *p!=v) { p++;len--; }
        return len!=0;
    }


    class tokenizer {
    public:
        tokenizer(char const *s) { text=s; pos=0; step(); };
        int get(int *val=NULL){
            int iv=int_value;
            int res=next_tocken;
            step();
            if(val && res==NUM){
                *val=iv;
            }
            return res;
        };
        int next(int *val=NULL) {
            if(val && next_tocken==NUM) {
                *val=int_value;
                return NUM;
            }
            return next_tocken;
        }
    private:
        char const *text;
        int pos;
        int next_tocken;
        int int_value;
        bool is_blank(char c)
        {
            return c==' ' || c=='\r' || c=='\n' || c=='\t';
        }
        bool isdigit(char c) 
        {
            return '0'<=c && c<='9'; 
        }
        void step() 
        {
            while(text[pos] && is_blank(text[pos])) pos++;
            char const *ptr=text+pos;
            char *tmp_ptr;
            if(strncmp(ptr,"<<",2)==0) { pos+=2; next_tocken=SHL; }
            else if(strncmp(ptr,">>",2)==0) { pos+=2; next_tocken=SHR; }
            else if(strncmp(ptr,"&&",2)==0) { pos+=2; next_tocken=AND; }
            else if(strncmp(ptr,"||",2)==0) { pos+=2; next_tocken=OR; }
            else if(strncmp(ptr,"<=",2)==0) { pos+=2; next_tocken=LTE; }
            else if(strncmp(ptr,">=",2)==0) { pos+=2; next_tocken=GTE; }
            else if(strncmp(ptr,"==",2)==0) { pos+=2; next_tocken=EQ; }
            else if(strncmp(ptr,"!=",2)==0) { pos+=2; next_tocken=NEQ; }
            else if(*ptr=='n') { pos++; next_tocken=VARIABLE; }
            else if(isdigit(*ptr)) { int_value=strtol(text+pos,&tmp_ptr,0); pos=tmp_ptr-text; next_tocken=NUM; }
            else if(*ptr=='\0') { next_tocken=0; }
            else { next_tocken=*ptr; pos++; }
        }
    };


    #define BINARY_EXPR(expr,hexpr,list)                            \
        plural_ptr expr()                                           \
        {                                                           \
            plural_ptr op1,op2;                                     \
            if((op1=hexpr()).get()==0)                              \
                return plural_ptr();                                \
            while(is_in(t.next(),list)) {                           \
                int o=t.get();                                      \
                if((op2=hexpr()).get()==0)                          \
                    return plural_ptr();                            \
                op1=bin_factory(o,op1,op2);                         \
            }                                                       \
            return op1;                                             \
        }

    class parser {
    public:

        parser(tokenizer &tin) : t(tin) {};

        plural_ptr compile()
        {
            plural_ptr res=cond_expr();
            if(res.get() && t.next()!=END) {
                return plural_ptr();
            };
            return res;
        }

    private:

        plural_ptr value_expr()
        {
            plural_ptr op;
            if(t.next()=='(') {
                t.get();
                if((op=cond_expr()).get()==0)
                    return plural_ptr();
                if(t.get()!=')')
                    return plural_ptr();
                return op;
            }
            else if(t.next()==NUM) {
                int value;
                t.get(&value);
                return plural_ptr(new number(value));
            }
            else if(t.next()==VARIABLE) {
                t.get();
                return plural_ptr(new identity());
            }
            return plural_ptr();
        };

        plural_ptr un_expr()
        {
            plural_ptr op1;
            static int level_unary[]={3,'-','!','~'};
            if(is_in(t.next(),level_unary)) {
                int op=t.get();
                if((op1=un_expr()).get()==0) 
                    return plural_ptr();
                switch(op) {
                case '-': 
                    return plural_ptr(new minus(op1));
                case '!': 
                    return plural_ptr(new l_not(op1));
                case '~': 
                    return plural_ptr(new bin_not(op1));
                default:
                    return plural_ptr();
                }
            }
            else {
                return value_expr();
            }
        };

        BINARY_EXPR(l10,un_expr,level10);
        BINARY_EXPR(l9,l10,level9);
        BINARY_EXPR(l8,l9,level8);
        BINARY_EXPR(l7,l8,level7);
        BINARY_EXPR(l6,l7,level6);
        BINARY_EXPR(l5,l6,level5);
        BINARY_EXPR(l4,l5,level4);
        BINARY_EXPR(l3,l4,level3);
        BINARY_EXPR(l2,l3,level2);
        BINARY_EXPR(l1,l2,level1);

        plural_ptr cond_expr()
        {
            plural_ptr cond,case1,case2;
            if((cond=l1()).get()==0)
                return plural_ptr();
            if(t.next()=='?') {
                t.get();
                if((case1=cond_expr()).get()==0)
                    return plural_ptr();
                if(t.get()!=':')
                    return plural_ptr();
                if((case2=cond_expr()).get()==0)
                    return plural_ptr();
            }
            else {
                return cond;
            }
            return plural_ptr(new conditional(cond,case1,case2));
        }

        tokenizer &t;

    };

} // namespace anon

plural_ptr compile(char const *str)
{
    tokenizer t(str);
    parser p(t);
    return p.compile();
}


} // lambda
} // gnu_gettext
} // locale
} // boost

// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4