Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wsstree.h
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wsstree.h
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * Syntax tree creation, manipulation and byte-code assembler
67  * generation.
68  *
69  */
70 
71 #ifndef WSSTREE_H
72 #define WSSTREE_H
73 
74 /********************* Linked list for syntax tree items ****************/
75 
76 /* A list item. */
78 {
80  void *data;
81 };
82 
83 typedef struct WsListItemRec WsListItem;
84 
85 /* The linked list object. */
86 struct WsListRec
87 {
91 
92  /* These are used in blocks to record the first and last line
93  information. They might also be used in other grammar
94  constructs. */
97 };
98 
99 typedef struct WsListRec WsList;
100 
101 /* Create a new syntax tree linked list for the compiler `compiler'.
102  The list is allocated from the `compiler->pool_stree' fast malloc
103  pool. */
104 WsList *ws_list_new(WsCompilerPtr compiler);
105 
106 /* Append the item `value' to the end of the list `list'. The item is
107  allocated from the `compiler->pool_stree' fast malloc pool. */
108 void ws_list_append(WsCompilerPtr compiler, WsList *list, void *value);
109 
110 /********************* Namespace for arguments and locals ***************/
111 
112 /* A namespace record. */
114 {
115  /* The line where this argument or local variable is declared. */
117 
118  /* The index of this variable. */
120 };
121 
123 
124 /* Create a new variable hash. */
126 
127 /* Define the new local variable or argument `name' to the local
128  variables namespace. The argument `line' specifies the location
129  where the variable `name' is defined. The argument `variablep' is
130  WS_TRUE for local variables and WS_FALSE for arguments. The
131  function performs all necessary initializations and sanity checks
132  needed. It will also report errors, etc. The function returns
133  NULL if there were any errors. */
135  WsBool variablep, char *name);
136 
137 /* Lookup the variable `name' from the variables namespace. The
138  function returns NULL if the variable `name' is undefined. The
139  function does not report any errors. */
141 
142 /********************* Top-level declarations ***************************/
143 
144 /* An external compilation unit pragma. */
145 
147 {
148  /* The line number of the pragma. */
150 
151  /* The byte-code pool constant index of the external compilation
152  unit URL. */
154 };
155 
157 
158 /* Create a hash for the external compilation unit pragmas. */
160 
161 /* Add a new external compilation unit pragma to the compiler
162  `compiler'. The function inserts the URL string `url' to the
163  byte-code structure of the compiler `compiler'. It updates the
164  external compilation unit hash to have mapping from the identifier
165  `identifier' to the URL `url' (or its constant index). The
166  function reports errors if the identifier `identifier' does already
167  have a mapping the external compilation unit namespace. */
168 void ws_pragma_use(WsCompilerPtr compiler, WsUInt32 line, char *identifier,
169  WsUtf8String *url);
170 
171 
172 /* MetaBody handling of the `use meta' pragmas. */
173 
175 {
179 };
180 
182 
183 /* Create a meta body pragma. */
188 
189 /* Free the MetaBody `mb'. */
191 
192 
193 /* A top-level function declaration. */
194 
196 {
199  char *name;
203 
204  /* The usage count of this function. This is used when sorting the
205  functions by their usage count. */
207 };
208 
209 typedef struct WsFunctionRec WsFunction;
210 
211 /* Function hash item. The function hash contains mapping from the
212  function names and their usage counts to the actual function
213  declaration. */
215 {
216  /* Does this mapping have a function declaration. */
218 
219  /* If declared, this is the index. */
221 
223 };
224 
226 
227 /* Create a new hash for functions. */
229 
230 /* Returns a pointer to the function hash item for the function name
231  `name'. The function creates a new hash slot if the name `name' is
232  currently unknown. The function returns NULL if the memory
233  allocation failed. */
235 
236 /* Add a new function definition to the compiler `compiler'. The
237  argument `externp' specifies whether the function is extern or not.
238  The argument `name' is the name of the function. The function name
239  is ws_malloc() allocated and must be freed when it is not needed
240  anymore. The argument `line' specifies the definition location of
241  the function. It is the line where the function name was in the
242  source stream. The argument `params' contains the formal
243  parameters of the function and its body is specified in the
244  argument `block'. */
245 void ws_function(WsCompilerPtr compiler, WsBool externp, char *name,
246  WsUInt32 line, WsList *params, WsList *block);
247 
248 /********************* Expressions **************************************/
249 
250 /* Expression types. */
251 typedef enum
252 {
270 
271 /* An expression. */
273 {
276 
277  union
278  {
279  struct
280  {
283  }
284  comma;
285 
286  struct
287  {
288  /* The identifier that is modified. */
289  char *identifier;
290 
291  /* The type of the assignment. This is the assignment token
292  value: '=', tMULA, tDA, ... */
293  int op;
294 
295  /* The expression to assign to the identifier `identifier'. */
297  }
298  assign;
299 
300  struct
301  {
305  }
306  conditional;
307 
308  struct
309  {
310  /* The type is the opcode of the short-circuit logical byte-code
311  operand. */
312  int type;
313  struct WsExpressionRec *left;
314  struct WsExpressionRec *right;
315  }
316  logical;
317 
318  struct
319  {
320  /* The type is the opcode of the binary byte-code operand. */
321  int type;
322  struct WsExpressionRec *left;
323  struct WsExpressionRec *right;
324  }
325  binary;
326 
327  struct
328  {
329  /* The type is the opcode of the unary byte-code operand. */
330  int type;
331  struct WsExpressionRec *expr;
332  }
333  unary;
334 
335  struct
336  {
337  /* Is this an unary addition or substraction. */
339  char *variable;
340  }
341  unary_var;
342 
343  struct
344  {
345  /* Is this a postfix addition or substraction. */
346  WsBool addp;
347  char *variable;
348  }
349  postfix_var;
350 
351  struct
352  {
353  /* The type of the call: ' ', '#', '.' */
354  int type;
355 
356  /* The name of the external module or library. */
357  char *base;
358 
359  /* The name of the function to call. */
360  char *name;
361 
362  /* The arguments of the call. */
364  }
365  call;
366 
367  struct
368  {
369  /* Separate sign bit, so that we can tell the difference
370  * between -2147483648 and +2147483648. We have to deal
371  * with both, because the former is parsed as "-" "2147483648".
372  * Sign is 1 for positive numbers, -1 for negative numbers,
373  * and can be either 1 or -1 for zero.
374  */
375  int sign;
377  } integer;
378 
379  char *symbol;
380 
382 
383 
386  } u;
387 };
388 
390 
391 /* Linearize the expression `expr' into symbolic byte-code
392  assembler. */
394 
395 
396 /* Constructors for different expression types. */
397 
398 /* Create a comma expression for `left' and `right'. */
401 
402 /* Create an assignment expression. The argument `type' specifies the
403  type of the expression. It is the assignment token value. */
405  char *identifier, int op, WsExpression *expr);
406 
407 /* Create a conditional expression with condition `e_cond' and
408  expressions `e_then' and `e_else'. */
412 
413 /* Create a logical expression of type `type'. The argument `type' is
414  the opcode of the logical shoft-circuit byte-code operand. */
416  int type, WsExpression *left,
418 
419 /* Create a binary expression of type `type'. The argument `type' is
420  the opcode of the binary byte-code operand. */
422  int type, WsExpression *left,
424 
425 /* Create an unary expression of type `type'. The argument `type' is
426  the opcode of the unary byte-code operand. */
428  int type, WsExpression *expr);
429 
430 /* Create an unary variable modification expression. The argument
431  `addp' specified whether the expression is an addition (++) or a
432  substraction (--) expression. */
434  WsBool addp, char *variable);
435 
436 /* Create a postfix variable modification expression. The argument
437  `addp' specified whether the expression is an addition (++) or a
438  substraction (--) expression. */
440  WsBool addp, char *variable);
441 
442 /* A generic call expression. The argument `type' must be one of ' ',
443  '#', or '.' for local, extern, or library function call
444  respectively. */
446  int type, char *base, char *name,
447  WsList *arguments);
448 
449 /* A symbol reference expression. */
451  char *identifier);
452 
453 /* Constant `invalid'. */
455 
456 /* Constant `true'. */
458 
459 /* Constant `false'. */
461 
462 /* An unsigned 32 bit integer. */
464  WsUInt32 ival);
465 
466 /* A floating point number. */
468  WsFloat fval);
469 
470 /* An UTF-8 encoded string. */
472  WsUtf8String *string);
473 
474 /********************* Misc syntax tree structures **********************/
475 
476 /* A variable declaration */
478 {
479  char *name;
481 };
482 
483 typedef struct WsVarDecRec WsVarDec;
484 
485 /* Create a new variable declaration */
487  char *name, WsExpression *expr);
488 
489 /* A function formal parameter */
491 {
493  char *name;
494 };
495 
497 
498 /* Create a new formal parameter */
500  WsUInt32 line, char *name);
501 
502 
503 /********************* Statements ***************************************/
504 
505 /* Statement types. */
506 typedef enum
507 {
519 
520 /* A statement. */
522 {
526 
527  union
528  {
532 
533  struct
534  {
538  }
539  s_if ;
540 
541  struct
542  {
548  }
549  s_for ;
550 
551  struct
552  {
554  struct WsStatementRec *stmt;
555  }
556  s_while ;
557  } u;
558 };
559 
561 
562 /* Linearize the statement `stmt' into symbolic byte-code
563  assembler. */
565 
566 /* Constructors for statements. */
567 
568 /* Create a new block statement from the statements `block'. The
569  arguments `first_line' and `last_line' specify the first and last
570  line numbers of the block (the line numbers of the '{' and '}'
571  tokens). */
574 
575 /* Create a new variable initialization statement. */
577  WsList *variables);
578 
579 /* Create a new empty statement. */
581 
582 /* Create a new expression statement. */
584  WsExpression *expr);
585 
586 /* Create a new if statement. */
590 
591 /* Create a new for statement. Only one of the arguments `init' and
592  `e1' can be defined. The init must be given for statements which
593  has a VariableDeclarationList in the initialization block. For the
594  C-like statements, the argument `e1' must be given for the
595  initialization expression. */
598  WsStatement *stmt);
599 
600 /* Create a new while statement. */
603 
604 /* Create a new continue statement. */
606 
607 /* Create a new break statement. */
609 
610 /* Create a new return statement. The argument `expr' is the
611  expression to return. If it is NULL, the return statement returns
612  an empty string. */
614  WsExpression *expr);
615 
616 #endif /* not WSSTREE_H */
WsList * block
Definition: wsstree.h:202
WsPragmaMetaBody * ws_pragma_meta_body(WsCompilerPtr compiler, WsUtf8String *property_name, WsUtf8String *content, WsUtf8String *scheme)
Definition: wsstree.c:271
WsExpression * ws_expr_const_string(WsCompilerPtr compiler, WsUInt32 linenum, WsUtf8String *string)
struct WsListItemRec * next
Definition: wsstree.h:79
WsHashPtr ws_pragma_use_hash_create(void)
Definition: wsstree.c:214
WsUInt32 line
Definition: wsstree.h:200
WsList * block
Definition: wsstree.h:529
void ws_stmt_linearize(WsCompilerPtr compiler, WsStatement *stmt)
WsUInt8 vindex
Definition: wsstree.h:119
struct WsStatementRec::@129::@130 s_if
WsUInt32 line
Definition: wsstree.h:116
WsNamespace * ws_variable_lookup(WsCompilerPtr compiler, char *name)
Definition: wsstree.c:199
WsStatement * ws_stmt_if(WsCompilerPtr compiler, WsUInt32 line, WsExpression *expr, WsStatement *s_then, WsStatement *s_else)
WsList * ws_list_new(WsCompilerPtr compiler)
unsigned long WsUInt32
Definition: wsint.h:122
WsExpression * ws_expr_unary_var(WsCompilerPtr compiler, WsUInt32 line, WsBool addp, char *variable)
Definition: wsstree.c:993
WsVarDec * ws_variable_declaration(WsCompilerPtr compiler, char *name, WsExpression *expr)
Definition: wsstree.c:78
WsExpression * ws_expr_assign(WsCompilerPtr compiler, WsUInt32 line, char *identifier, int op, WsExpression *expr)
Definition: wsstree.c:903
WsExpression * ws_expr_call(WsCompilerPtr compiler, WsUInt32 linenum, int type, char *base, char *name, WsList *arguments)
char * name
Definition: wsstree.h:360
WsNamespace * ws_variable_define(WsCompilerPtr compiler, WsUInt32 line, WsBool variablep, char *name)
Definition: wsstree.c:160
double WsFloat
Definition: wsint.h:126
WsExpression * e1
Definition: wsstree.h:544
WsUtf8String * scheme
Definition: wsstree.h:178
WsExpression * e2
Definition: wsstree.h:545
WsExpression * ws_expr_const_integer(WsCompilerPtr compiler, WsUInt32 linenum, WsUInt32 ival)
WsExpression * ws_expr_const_true(WsCompilerPtr compiler, WsUInt32 linenum)
WsExpression * ws_expr_comma(WsCompilerPtr compiler, WsUInt32 line, WsExpression *left, WsExpression *right)
Definition: wsstree.c:889
WsStatement * ws_stmt_variable(WsCompilerPtr compiler, WsUInt32 line, WsList *variables)
Definition: wsstree.c:1401
int type
Definition: smsc_cimd2.c:215
WsExpression * ws_expr_logical(WsCompilerPtr compiler, WsUInt32 line, int type, WsExpression *left, WsExpression *right)
Definition: wsstree.c:941
WsExpression * e3
Definition: wsstree.h:546
WsStatement * ws_stmt_block(WsCompilerPtr compiler, WsUInt32 first_line, WsUInt32 last_line, WsList *block)
void ws_expr_linearize(WsCompilerPtr compiler, WsExpression *expr)
struct WsExpressionRec::@118::@123 binary
WsExpression * expr
Definition: wsstree.h:531
WsUInt16 urlindex
Definition: wsstree.h:153
WsUInt16 cindex
Definition: wsstree.h:381
WsHashPtr ws_variable_hash_create(void)
Definition: wsstree.c:154
struct WsStatementRec * s_else
Definition: wsstree.h:537
WsListItem * head
Definition: wsstree.h:88
char * base
Definition: wsstree.h:357
WsUInt32 line
Definition: wsstree.h:149
WsList * params
Definition: wsstree.h:201
WsUtf8String * property_name
Definition: wsstree.h:176
WsList * var
Definition: wsstree.h:530
struct WsExpressionRec * e_else
Definition: wsstree.h:304
WsStatement * ws_stmt_expr(WsCompilerPtr compiler, WsUInt32 line, WsExpression *expr)
char * name
Definition: wsstree.h:493
struct WsExpressionRec::@118::@124 unary
WsExpression * ws_expr_conditional(WsCompilerPtr compiler, WsUInt32 line, WsExpression *e_cond, WsExpression *e_then, WsExpression *e_else)
Definition: wsstree.c:925
WsStatement * ws_stmt_for(WsCompilerPtr compiler, WsUInt32 line, WsList *init, WsExpression *e1, WsExpression *e2, WsExpression *e3, WsStatement *stmt)
Definition: wsstree.c:1447
unsigned char WsUInt8
Definition: wsint.h:116
void ws_function(WsCompilerPtr compiler, WsBool externp, char *name, WsUInt32 line, WsList *params, WsList *block)
WsUInt32 last_line
Definition: wsstree.h:525
struct WsStatementRec::@129::@132 s_while
WsBool externp
Definition: wsstree.h:198
WsStatement * ws_stmt_continue(WsCompilerPtr compiler, WsUInt32 line)
char * variable
Definition: wsstree.h:339
struct WsStatementRec * stmt
Definition: wsstree.h:547
WsUInt32 line
Definition: wsstree.h:492
WsExpression * ws_expr_symbol(WsCompilerPtr compiler, WsUInt32 linenum, char *identifier)
WsFunctionHash * ws_function_hash(WsCompilerPtr compiler, char *name)
Definition: wsstree.c:318
struct WsExpressionRec::@118::@126 postfix_var
void * data
Definition: wsstree.h:80
WsFloat fval
Definition: wsstree.h:384
WsExpressionType
Definition: wsstree.h:251
unsigned short WsUInt16
Definition: wsint.h:119
struct WsExpressionRec::@118::@125 unary_var
WsUInt32 line
Definition: wsstree.h:275
WsExpression * ws_expr_postfix_var(WsCompilerPtr compiler, WsUInt32 line, WsBool addp, char *variable)
Definition: wsstree.c:1010
WsListItem * tail
Definition: wsstree.h:89
WsUInt32 ival
Definition: wsstree.h:376
WsStatement * ws_stmt_break(WsCompilerPtr compiler, WsUInt32 line)
struct WsExpressionRec * right
Definition: wsstree.h:282
char * name
Definition: smsc_cimd2.c:212
WsBool addp
Definition: wsstree.h:338
struct WsExpressionRec::@118::@122 logical
struct WsExpressionRec::@118::@121 conditional
WsList * arguments
Definition: wsstree.h:363
WsUInt32 usage_count
Definition: wsstree.h:206
struct WsExpressionRec * e_then
Definition: wsstree.h:303
WsUInt32 first_line
Definition: wsstree.h:524
static int init
Definition: gwlib.c:69
WsExpression * ws_expr_const_invalid(WsCompilerPtr compiler, WsUInt32 linenum)
WsBool defined
Definition: wsstree.h:217
WsUtf8String string
Definition: wsstree.h:385
WsStatementType
Definition: wsstree.h:506
WsExpression * expr
Definition: wsstree.h:480
WsExpression * ws_expr_unary(WsCompilerPtr compiler, WsUInt32 line, int type, WsExpression *expr)
Definition: wsstree.c:971
void ws_list_append(WsCompilerPtr compiler, WsList *list, void *value)
WsBool
Definition: wsint.h:128
struct WsExpressionRec * expr
Definition: wsstree.h:296
WsStatementType type
Definition: wsstree.h:523
char * identifier
Definition: wsstree.h:289
WsUtf8String * content
Definition: wsstree.h:177
WsExpression * ws_expr_const_float(WsCompilerPtr compiler, WsUInt32 linenum, WsFloat fval)
WsStatement * ws_stmt_return(WsCompilerPtr compiler, WsUInt32 line, WsExpression *expr)
Definition: wsstree.c:1491
void ws_pragma_meta_body_free(WsCompilerPtr compiler, WsPragmaMetaBody *mb)
Definition: wsstree.c:291
WsFormalParm * ws_formal_parameter(WsCompilerPtr compiler, WsUInt32 line, char *name)
Definition: wsstree.c:93
WsUInt8 findex
Definition: wsstree.h:197
WsUInt32 last_line
Definition: wsstree.h:96
char * symbol
Definition: wsstree.h:379
WsHashPtr ws_function_hash_create(void)
Definition: wsstree.c:312
union WsStatementRec::@129 u
WsList * init
Definition: wsstree.h:543
union WsExpressionRec::@118 u
void ws_pragma_use(WsCompilerPtr compiler, WsUInt32 line, char *identifier, WsUtf8String *url)
Definition: wsstree.c:220
struct WsExpressionRec::@118::@120 assign
struct WsExpressionRec * e_cond
Definition: wsstree.h:302
struct WsExpressionRec::@118::@127 call
struct WsExpressionRec::@118::@128 integer
WsExpression * ws_expr_const_false(WsCompilerPtr compiler, WsUInt32 linenum)
struct WsExpressionRec::@118::@119 comma
WsStatement * ws_stmt_while(WsCompilerPtr compiler, WsUInt32 line, WsExpression *expr, WsStatement *stmt)
static Octstr * url
Definition: test_xmlrpc.c:84
struct WsStatementRec::@129::@131 s_for
WsUInt32 first_line
Definition: wsstree.h:95
char * name
Definition: wsstree.h:199
char * name
Definition: wsstree.h:479
WsExpression * ws_expr_binary(WsCompilerPtr compiler, WsUInt32 line, int type, WsExpression *left, WsExpression *right)
Definition: wsstree.c:956
struct WsStatementRec * s_then
Definition: wsstree.h:536
WsUInt32 usage_count
Definition: wsstree.h:222
WsStatement * ws_stmt_empty(WsCompilerPtr compiler, WsUInt32 line)
WsUInt8 findex
Definition: wsstree.h:220
WsExpressionType type
Definition: wsstree.h:274
struct WsExpressionRec * left
Definition: wsstree.h:281
WsUInt32 num_items
Definition: wsstree.h:90
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.