~zorba-coders/zorba/bug-950621

« back to all changes in this revision

Viewing changes to src/compiler/expression/expr_test.cpp

  • Committer: brantmat at ETHZ
  • Date: 2007-10-09 12:58:38 UTC
  • Revision ID: svn-v4:8046edc3-af21-0410-8661-ec7318497eea:trunk/zorba:904
commit of the new directory structure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*-
 
2
 *
 
3
 *  $Id: expr_test.cpp,v 1.1.1.1 2006/11/06 08:42:18 Paul Pedersen Exp $
 
4
 *
 
5
 *  Copyright 2006-2007 FLWOR FOundation.
 
6
 *
 
7
 *      Author: Paul Pedersen
 
8
 */
 
9
 
 
10
#include "expr.h"
 
11
#include "../context/context.h"
 
12
#include "../functions/signature.h"
 
13
#include "../functions/function.h"
 
14
#include "../parser/location.hh"
 
15
#include "../parser/symbol_table.h"
 
16
#include "../types/sequence_type.h"
 
17
#include "../errors/xqp_exception.h"
 
18
 
 
19
#include <iostream>
 
20
 
 
21
/*
 
22
...........................................
 
23
:                                         :
 
24
: expression tree unit test               :
 
25
:.........................................:
 
26
*/
 
27
 
 
28
using namespace std;
 
29
using namespace xqp;
 
30
 
 
31
typedef rchandle<expr> exprref_t;
 
32
 
 
33
int main(int argc, char* argv[])
 
34
{
 
35
        //typedef rchandle<expr> exprref_t;
 
36
        yy::location loc;
 
37
        context ctx;
 
38
        symbol_table symtab;
 
39
 
 
40
        uint32_t i1 = symtab.put("symbol1", 7);
 
41
        uint32_t i2 = symtab.put("symbol2", 7);
 
42
        uint32_t i3 = symtab.put("symbol3", 7);
 
43
 
 
44
        try {
 
45
 
 
46
                rchandle<qname_expr> qnv1_h = new qname_expr("var1");
 
47
                rchandle<qname_expr> qnv2_h = new qname_expr("var2");
 
48
                rchandle<qname_expr> qnv3_h = new qname_expr("var3");
 
49
                rchandle<qname_expr> qna1_h = new qname_expr("attr1");
 
50
                rchandle<qname_expr> qna2_h = new qname_expr("attr2");
 
51
                rchandle<qname_expr> qna3_h = new qname_expr("attr3");
 
52
                rchandle<qname_expr> qne1_h = new qname_expr("elem1");
 
53
                rchandle<qname_expr> qne2_h = new qname_expr("elem2");
 
54
                rchandle<qname_expr> qne3_h = new qname_expr("elem3");
 
55
                rchandle<qname_expr> qnf1_h = new qname_expr("func1");
 
56
                rchandle<qname_expr> qnp1_h = new qname_expr("prag1");
 
57
                rchandle<qname_expr> qnp2_h = new qname_expr("prag1");
 
58
 
 
59
        /*...........................................
 
60
                : literal expresions                      :
 
61
                :.........................................*/
 
62
                cout << "\n>>>literal_expr\n";
 
63
                rchandle<literal_expr> lit1_h = new literal_expr(loc, i1, true);
 
64
                rchandle<literal_expr> lit2_h = new literal_expr(loc, i2, true);
 
65
                rchandle<literal_expr> lit3_h = new literal_expr(loc, i3, true);
 
66
                rchandle<literal_expr> lit4_h = new literal_expr(loc, 123);
 
67
                rchandle<literal_expr> lit5_h = new literal_expr(loc, 123456);
 
68
                rchandle<literal_expr> lit6_h = new literal_expr(loc, 123.456);
 
69
        
 
70
                cout << "lit1 [" << literal_expr::decode_type(lit1_h->get_type()) << "] = ";
 
71
                lit1_h->put(cout,ctx) << " : " << symtab.get(lit1_h->get_sref()) << endl;
 
72
                cout << "lit2 [" << literal_expr::decode_type(lit2_h->get_type()) << "] = ";
 
73
                lit2_h->put(cout,ctx) << " : " << symtab.get(lit2_h->get_sref()) << endl;
 
74
                cout << "lit3 [" << literal_expr::decode_type(lit3_h->get_type()) << "] = ";
 
75
                lit3_h->put(cout,ctx) << " : " << symtab.get(lit3_h->get_sref()) << endl;
 
76
        
 
77
                cout << "lit4 [" << literal_expr::decode_type(lit4_h->get_type()) << "] = ";
 
78
                lit4_h->put(cout,ctx) << endl;
 
79
                cout << "lit5 [" << literal_expr::decode_type(lit5_h->get_type()) << "] = ";
 
80
                lit5_h->put(cout,ctx) << endl;
 
81
                cout << "lit6 [" << literal_expr::decode_type(lit6_h->get_type()) << "] = ";
 
82
                lit6_h->put(cout,ctx) << endl;
 
83
        
 
84
        
 
85
        /*...........................................
 
86
                : variables                               :
 
87
                :.........................................*/
 
88
                cout << "\n>>>var_expr\n";
 
89
                rchandle<var_expr> var1_h = new var_expr(loc);
 
90
          var1_h->set_varname(&*qnv1_h);
 
91
          var1_h->set_valexpr(&*lit1_h);
 
92
                var1_h->set_kind(var_expr::for_var);
 
93
          var1_h->set_type(xs_untypedAtomicValue);
 
94
        
 
95
                rchandle<var_expr> var2_h = new var_expr(loc);
 
96
          var2_h->set_varname(&*qnv2_h);
 
97
          var2_h->set_valexpr(&*lit1_h);
 
98
                var2_h->set_kind(var_expr::let_var);
 
99
          var2_h->set_type(xs_untypedAtomicValue);
 
100
        
 
101
                rchandle<var_expr> var3_h = new var_expr(loc);
 
102
          var3_h->set_varname(&*qnv3_h);
 
103
          var3_h->set_valexpr(&*lit1_h);
 
104
                var3_h->set_kind(var_expr::pos_var);
 
105
          var3_h->set_type(xs_untypedAtomicValue);
 
106
        
 
107
                var1_h->put(cout,ctx) << endl;
 
108
                var2_h->put(cout,ctx) << endl;
 
109
                var3_h->put(cout,ctx) << endl;
 
110
        
 
111
        
 
112
        /*...........................................
 
113
                : expression list                         :
 
114
                :.........................................*/
 
115
                cout << "\n>>>expr_list\n";
 
116
                rchandle<expr_list> exlist1_h = new expr_list(loc);
 
117
                exlist1_h->add(&*var1_h);
 
118
                exlist1_h->add(&*var2_h);
 
119
                exlist1_h->add(&*var3_h);
 
120
                exlist1_h->put(cout,ctx);
 
121
        
 
122
        
 
123
        /*...........................................
 
124
                : constructors                            :
 
125
                :.........................................*/
 
126
                cout << "\n>>>text_expr\n";
 
127
                rchandle<text_expr> text1_h = new text_expr(loc, &*lit4_h);
 
128
                rchandle<text_expr> text2_h = new text_expr(loc, &*lit5_h);
 
129
                rchandle<text_expr> text3_h = new text_expr(loc, &*lit6_h);
 
130
        
 
131
                text1_h->put(cout,ctx) << endl;
 
132
                text2_h->put(cout,ctx) << endl;
 
133
                text3_h->put(cout,ctx) << endl;
 
134
        
 
135
                cout << "\n>>>comment_expr\n";
 
136
                rchandle<comment_expr> comment1_h = new comment_expr(loc, &*lit1_h);
 
137
                rchandle<comment_expr> comment2_h = new comment_expr(loc, &*lit2_h);
 
138
                rchandle<comment_expr> comment3_h = new comment_expr(loc, &*lit3_h);
 
139
        
 
140
                comment1_h->put(cout,ctx) << endl;
 
141
                comment2_h->put(cout,ctx) << endl;
 
142
                comment3_h->put(cout,ctx) << endl;
 
143
        
 
144
                cout << "\n>>>pi_expr\n";
 
145
                rchandle<pi_expr> pi1_h = new pi_expr(loc, "?PI1", &*lit1_h);
 
146
                rchandle<pi_expr> pi2_h = new pi_expr(loc, "?PI2", &*lit2_h);
 
147
                rchandle<pi_expr> pi3_h = new pi_expr(loc, "?PI3", &*lit3_h);
 
148
        
 
149
                pi1_h->put(cout,ctx) << endl;
 
150
                pi2_h->put(cout,ctx) << endl;
 
151
                pi3_h->put(cout,ctx) << endl;
 
152
        
 
153
                cout << "\n>>>attr_expr\n";
 
154
                rchandle<attr_expr> attr1_h = new attr_expr(loc, &*qna1_h, &*lit1_h);
 
155
                rchandle<attr_expr> attr2_h = new attr_expr(loc, &*qna2_h, &*lit2_h);
 
156
                rchandle<attr_expr> attr3_h = new attr_expr(loc, &*qna3_h, &*lit3_h);
 
157
        
 
158
                attr1_h->put(cout,ctx) << endl;
 
159
                attr2_h->put(cout,ctx) << endl;
 
160
                attr3_h->put(cout,ctx) << endl;
 
161
        
 
162
                rchandle<expr_list> exprlist2_h = new expr_list(loc);
 
163
                exprlist2_h->add(&*attr1_h);
 
164
                exprlist2_h->add(&*attr2_h);
 
165
                exprlist2_h->add(&*attr3_h);
 
166
                exprlist2_h->add(&*text1_h);
 
167
                exprlist2_h->add(&*text2_h);
 
168
                exprlist2_h->add(&*text3_h);
 
169
        
 
170
                cout << "\n>>>elem_expr\n";
 
171
                rchandle<elem_expr> elem1_h = new elem_expr(loc, &*qne1_h, &*exprlist2_h);
 
172
                elem_expr::nsbinding ns1("pre1","http://a1.b1.c1");
 
173
                elem_expr::nsbinding ns2("pre2","http://a2.b2.c2");
 
174
                elem1_h->add(ns1);
 
175
                elem1_h->add(ns2);
 
176
                elem1_h->put(cout,ctx) << endl;
 
177
        
 
178
                rchandle<elem_expr> elem2_h = new elem_expr(loc, &*qne2_h, &*exprlist2_h);
 
179
                elem2_h->add(ns1);
 
180
                elem2_h->add(ns2);
 
181
                elem2_h->put(cout,ctx) << endl;
 
182
        
 
183
                cout << "\n>>>doc_expr\n";
 
184
                rchandle<doc_expr> doc1_h = new doc_expr(loc, &*elem1_h);
 
185
                doc1_h->put(cout,ctx) << endl;
 
186
        
 
187
        
 
188
        /*...........................................
 
189
                : function call                           :
 
190
                :.........................................*/
 
191
                cout << "\n>>>funcall_expr\n";
 
192
                rchandle<funcall_expr> fun1_h = new funcall_expr(loc, &*qnf1_h);
 
193
                fun1_h->add_arg(&*lit1_h);
 
194
                fun1_h->add_arg(&*lit2_h);
 
195
                fun1_h->add_arg(&*lit3_h);
 
196
                fun1_h->put(cout,ctx) << endl;
 
197
                for (unsigned i=0; i<fun1_h->arg_count(); ++i) {
 
198
                        cout << "arg[" << i << "] = ";
 
199
                        (*fun1_h)[i]->put(cout,ctx) << endl;
 
200
                }
 
201
        
 
202
        
 
203
        /*...........................................
 
204
                : typeswitch expression                   :
 
205
                :.........................................*/
 
206
                cout << "\n>>>typeswitch_expr\n";
 
207
                rchandle<typeswitch_expr> sw1_h = new typeswitch_expr(loc);
 
208
                sw1_h->set_switch_expr(&*elem1_h);
 
209
                sw1_h->set_default_varname(&*var1_h);
 
210
                sw1_h->set_default_clause(&*elem1_h);
 
211
        
 
212
                case_clause cc1;
 
213
                cc1.var_h = var2_h;
 
214
                cc1.case_expr_h = &*lit1_h;
 
215
                cc1.type = xs_anyTypeSeq;
 
216
                sw1_h->add_clause(&cc1);
 
217
        
 
218
                case_clause cc2;
 
219
                cc2.var_h = var3_h;
 
220
                cc2.case_expr_h = &*lit2_h;
 
221
                cc2.type = xs_anyTypeSeq;
 
222
                sw1_h->add_clause(&cc2);
 
223
                sw1_h->put(cout,ctx);
 
224
        
 
225
        
 
226
        /*...........................................
 
227
                : conditional expression                  :
 
228
                :.........................................*/
 
229
                cout << "\n>>>if_expr\n";
 
230
                rchandle<if_expr> if1_h = new if_expr(loc, &*lit1_h, &*lit2_h, &*lit3_h);
 
231
                if1_h->put(cout,ctx);
 
232
        
 
233
        
 
234
        /*...........................................
 
235
                : cast-related expressions                :
 
236
                :.........................................*/
 
237
                cout << "\n>>>instanceof_expr\n";
 
238
                rchandle<instanceof_expr> inst1_h =
 
239
                        new instanceof_expr(loc,&*lit4_h,xs_anyType);
 
240
                inst1_h->put(cout,ctx) << endl;
 
241
        
 
242
                cout << "\n>>>treat_expr\n";
 
243
                rchandle<treat_expr> treat1_h =
 
244
                        new treat_expr(loc, &*lit5_h, xs_int);
 
245
                treat1_h->put(cout,ctx) << endl;
 
246
        
 
247
                cout << "\n>>>castable_expr\n";
 
248
                rchandle<castable_expr> castable1_h =
 
249
                        new castable_expr(loc, &*lit6_h, xs_untypedAtomicValue);
 
250
                castable1_h->put(cout,ctx) << endl;
 
251
        
 
252
                cout << "\n>>>cast_expr\n";
 
253
                rchandle<cast_expr> cast1_h =
 
254
                        new cast_expr(loc, &*lit1_h, xs_untypedAtomicValue);
 
255
                cast1_h->put(cout,ctx) << endl;
 
256
        
 
257
        
 
258
        /*...........................................
 
259
                : validate expression                     :
 
260
                :.........................................*/
 
261
                cout << "\n>>>validate_expr\n";
 
262
                rchandle<validate_expr> val1_h =
 
263
                        new validate_expr(loc, val_strict, &*elem1_h);
 
264
                val1_h->put(cout,ctx) << endl;
 
265
        
 
266
        
 
267
        /*...........................................
 
268
                : extension expressions                   :
 
269
                :.........................................*/
 
270
                /*
 
271
                cout << "\n>>>extension_expr\n";
 
272
                rchandle<extension_expr> ext1_h =
 
273
                        new extension_expr(loc,&*lit1_h);
 
274
                rchandle<pragma> prag1_h = new pragma(&*qnp1_h, "content1");
 
275
                ext1_h->add(prag1_h);
 
276
                ext1_h->put(cout,ctx) << endl;
 
277
                */
 
278
 
 
279
        
 
280
        /*...........................................
 
281
                : path expressions                        :
 
282
                :.........................................*/
 
283
                cout << "\n>>>axis_step_expr\n";
 
284
                rchandle<axis_step_expr> ax1_h = new axis_step_expr(loc);
 
285
                ax1_h->set_axis(axis_step_expr::child);
 
286
                ax1_h->set_test(axis_step_expr::name_test);
 
287
                ax1_h->set_name(&*qne1_h);
 
288
                ax1_h->add_pred(&*elem1_h);
 
289
                ax1_h->put(cout,ctx) << endl;
 
290
        
 
291
                rchandle<axis_step_expr> ax2_h = new axis_step_expr(loc);
 
292
                ax2_h->set_axis(axis_step_expr::child);
 
293
                ax2_h->set_test(axis_step_expr::name_test);
 
294
                ax2_h->set_name(&*qne2_h);
 
295
                ax2_h->add_pred(&*elem2_h);
 
296
                ax2_h->put(cout,ctx) << endl;
 
297
        
 
298
                rchandle<relpath_expr> path1_h = new relpath_expr(loc);
 
299
                path1_h->add_back(&*ax1_h);
 
300
                path1_h->add_back(&*ax2_h);
 
301
                path1_h->put(cout,ctx) << endl;
 
302
        
 
303
        
 
304
        /*...........................................
 
305
                : quantified expressions                  :
 
306
                :.........................................*/
 
307
                cout << "\n>>>quantified_expr\n";
 
308
                rchandle<quantified_expr> quant1_h =
 
309
                        new quantified_expr(loc,quant_some);
 
310
                quant1_h->add(&*var1_h);
 
311
                quant1_h->set_sat_expr(&*elem1_h);
 
312
                quant1_h->put(cout,ctx) << endl;
 
313
        
 
314
                rchandle<quantified_expr> quant2_h =
 
315
                        new quantified_expr(loc,quant_some);
 
316
                quant2_h->add(&*var2_h);
 
317
                quant2_h->set_sat_expr(&*elem2_h);
 
318
                quant2_h->put(cout,ctx) << endl;
 
319
        
 
320
        
 
321
        /*...........................................
 
322
                : FO expressions                          :
 
323
                :.........................................*/
 
324
                cout << "\n>>>fo_expr\n";
 
325
                rchandle<signature> sig1_h = new signature(&ctx,"func1",anyNodeSeq);
 
326
                sig1_h->add_arg(anyNodeSeq);
 
327
                sig1_h->add_arg(anyNodeSeq);
 
328
                function func1(*sig1_h);
 
329
                rchandle<fo_expr> fo1_h = new fo_expr(loc);
 
330
                fo1_h->add(&*lit1_h);
 
331
                fo1_h->add(&*lit2_h);
 
332
                fo1_h->set_func(&func1);
 
333
                fo1_h->put(cout,ctx) << endl;
 
334
        
 
335
        
 
336
        /*...........................................
 
337
                : FLWOR expressions                       :
 
338
                :.........................................*/
 
339
                cout << "\n>>>flwor_expr\n";
 
340
                rchandle<flwor_expr> flwor1_h = new flwor_expr(loc);
 
341
                rchandle<forlet_clause> flc1_h =
 
342
                        new forlet_clause(forlet_clause::for_clause,&*var1_h,NULL,NULL,&*elem1_h);
 
343
                rchandle<forlet_clause> flc2_h =
 
344
                        new forlet_clause(forlet_clause::let_clause,&*var2_h,NULL,NULL,&*elem2_h);
 
345
                flwor1_h->add(&*flc1_h);
 
346
                flwor1_h->add(&*flc2_h);
 
347
                flwor1_h->set_where(&*var1_h);
 
348
                flwor1_h->set_retval(&*var2_h);
 
349
                flwor1_h->put(cout,ctx) << endl;
 
350
 
 
351
        } catch (null_pointer& e) {
 
352
                cout << "Application exception: " << e.get_msg() << endl;
 
353
        } catch (xqp_exception& e) {
 
354
                cout << "Application exception: " << e.get_msg() << endl;
 
355
        } catch (exception& e) {
 
356
                cout << "C++ exception: " << e.what() << endl;
 
357
        } catch (...) {
 
358
                cout << "unrecognized exception" << endl;
 
359
        }
 
360
 
 
361
 
 
362
};
 
363
 
 
364
 
 
365
 
 
366
 
 
367
 
 
368
/*
 
369
class ft_contains_expr : public expr
 
370
        ft_contains_expr(loc, ctx, exprref_t, exprref_t, exprref_t);
 
371
        exprref_t get_range() const { return range_h; }
 
372
        exprref_t get_ft_select() const { return ft_select_h; }
 
373
        exprref_t get_ignore() const { return ft_ignore_h; }
 
374
        virtual std::ostream& put(std::ostream&, ctx) const;
 
375
};
 
376
 
 
377
class unary_expr : public expr
 
378
        unary_expr(loc, ctx, bool neg_b, exprref_t);
 
379
        bool is_negative() const { return neg_b; }
 
380
        exprref_t get_expr() const { return expr_h; }
 
381
        virtual std::ostream& put(std::ostream&,ctx) const;
 
382
};
 
383
 
 
384
class order_expr : public expr
 
385
        enum order_type_t { ordered, unordered };
 
386
        order_expr(loc, ctx, order_type_t, exprref_t);
 
387
        order_type_t get_type() const { return type; }
 
388
        exprref_t get_expr() const { return expr_h; }
 
389
        std::ostream& put(std::ostream&, ctx) const;
 
390
};
 
391
*/
 
392