absyntax/visitor.hh
changeset 0 fb772792efd1
child 69 41cb5b80416e
equal deleted inserted replaced
-1:000000000000 0:fb772792efd1
       
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /*
       
    27  * VISITOR.HH
       
    28  *
       
    29  *
       
    30  * The asbract syntax tree is scaned several times
       
    31  * during the whole compilation process. For e.g., stage 3 verifies
       
    32  * semantics, and stage 4 produces output.
       
    33  *
       
    34  * Since the number of functions that need to scan through
       
    35  * the abtract syntax tree may increase in time, (e.g. new
       
    36  * output formats, new semantic checks, etc...) while the
       
    37  * abstract syntax tree class structure is pretty stable
       
    38  * (i.e. the ST and IL languages will not be changing much...),
       
    39  * we use the 'visitor' design patern.
       
    40  *
       
    41  * It is not strictly necessary to use this pattern, but it does
       
    42  * have the advantage of bringing together the functions
       
    43  * that implement the same algorithm, each on a different
       
    44  * class of object in the abstract syntax tree.
       
    45  *
       
    46  *
       
    47  * This file contains the interface that each visitor class
       
    48  * must implement in order to be able to visit the abstract
       
    49  * syntax tree (class visitor_c)
       
    50  *
       
    51  * Three implementations of this interface are also provided,
       
    52  * that may be later extended to execute a particular algorithm.
       
    53  *
       
    54  * The null (class null_visitor_c) does nothing.
       
    55  *
       
    56  * The iterator (class iterator_visitor_c) iterates through
       
    57  * every object in the syntax tree.
       
    58  *
       
    59  * The search class (class search_visitor_c) iterates through
       
    60  * every object, until one returns a value != NULL.
       
    61  */
       
    62 
       
    63 
       
    64 #ifndef _VISITOR_HH
       
    65 #define _VISITOR_HH
       
    66 
       
    67 
       
    68 
       
    69 
       
    70 #include "absyntax.hh"
       
    71 
       
    72 
       
    73 
       
    74 #define SYM_LIST(class_name_c)	\
       
    75   virtual void *visit(class_name_c *symbol) = 0;
       
    76 
       
    77 #define SYM_TOKEN(class_name_c)	\
       
    78   virtual void *visit(class_name_c *symbol) = 0;
       
    79 
       
    80 #define SYM_REF0(class_name_c)	\
       
    81   virtual void *visit(class_name_c *symbol) = 0;
       
    82 
       
    83 #define SYM_REF2(class_name_c, ref1, ref2)	\
       
    84   virtual void *visit(class_name_c *symbol) = 0;
       
    85 
       
    86 #define SYM_REF4(class_name_c, ref1, ref2, ref3, ref4)	\
       
    87   virtual void *visit(class_name_c *symbol) = 0;
       
    88 
       
    89 #define SYM_REF6(class_name_c, ref1, ref2, ref3, ref4, ref5, ref6)	\
       
    90   virtual void *visit(class_name_c *symbol) = 0;
       
    91 
       
    92 class visitor_c {
       
    93   public:
       
    94   #include "absyntax.def"
       
    95 
       
    96   virtual ~visitor_c(void);
       
    97 };
       
    98 
       
    99 #undef SYM_LIST
       
   100 #undef SYM_TOKEN
       
   101 #undef SYM_REF0
       
   102 #undef SYM_REF2
       
   103 #undef SYM_REF4
       
   104 #undef SYM_REF6
       
   105 
       
   106 
       
   107 
       
   108 
       
   109 
       
   110 
       
   111 #define SYM_LIST(class_name_c)	\
       
   112   virtual void *visit(class_name_c *symbol);
       
   113 
       
   114 #define SYM_TOKEN(class_name_c)	\
       
   115   virtual void *visit(class_name_c *symbol);
       
   116 
       
   117 #define SYM_REF0(class_name_c)	\
       
   118   virtual void *visit(class_name_c *symbol);
       
   119 
       
   120 #define SYM_REF2(class_name_c, ref1, ref2)	\
       
   121   virtual void *visit(class_name_c *symbol);
       
   122 
       
   123 #define SYM_REF4(class_name_c, ref1, ref2, ref3, ref4)	\
       
   124   virtual void *visit(class_name_c *symbol);
       
   125 
       
   126 #define SYM_REF6(class_name_c, ref1, ref2, ref3, ref4, ref5, ref6)	\
       
   127   virtual void *visit(class_name_c *symbol);
       
   128 
       
   129 
       
   130 
       
   131 
       
   132 
       
   133 class null_visitor_c: public visitor_c {
       
   134   public:
       
   135   #include "absyntax.def"
       
   136 
       
   137   virtual ~null_visitor_c(void);
       
   138 };
       
   139 
       
   140 
       
   141 
       
   142 class iterator_visitor_c: public visitor_c {
       
   143   protected:
       
   144   void *visit_list(list_c *list);
       
   145 
       
   146   public:
       
   147   #include "absyntax.def"
       
   148 
       
   149   virtual ~iterator_visitor_c(void);
       
   150 };
       
   151 
       
   152 
       
   153 class search_visitor_c: public visitor_c {
       
   154   protected:
       
   155   void *visit_list(list_c *list);
       
   156 
       
   157   public:
       
   158   #include "absyntax.def"
       
   159 
       
   160   virtual ~search_visitor_c(void);
       
   161 };
       
   162 
       
   163 
       
   164 #undef SYM_LIST
       
   165 #undef SYM_TOKEN
       
   166 #undef SYM_REF0
       
   167 #undef SYM_REF2
       
   168 #undef SYM_REF4
       
   169 #undef SYM_REF6
       
   170 
       
   171 #endif /*  _VISITOR_HH */