absyntax_utils/case_element_iterator.hh
author Edouard Tisserant <edouard.tisserant@gmail.com>
Thu, 20 Jul 2023 22:03:23 +0200
changeset 1102 1610b6528b27
parent 661 f537c3315f83
permissions -rw-r--r--
Add .gitignore
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     1
/*
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     3
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     5
 *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     6
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    10
 *  (at your option) any later version.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    11
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    15
 *  GNU General Public License for more details.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    16
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    19
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    20
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    21
 * This code is made available on the understanding that it will not be
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    23
 */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    24
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    25
/*
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    26
 * An IEC 61131-3 compiler.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    27
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    28
 * Based on the
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    29
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    30
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    31
 */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    32
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    33
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    34
/*
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    35
 * Case element iterator.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    36
 * Iterate through the elements of a case list.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    37
 *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    38
 * This is part of the 4th stage that generates
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    39
 * a c++ source program equivalent to the IL and ST
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    40
 * code.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    41
 */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    42
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    43
/* Given a case_list_c and a type of element, iterate through
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    44
 * each element, returning the symbol of each element if from
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    45
 * the good type...case_element_iterator_c
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    46
 */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    47
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    48
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    49
#include "../absyntax/visitor.hh"
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    50
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    51
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    52
class case_element_iterator_c : public null_visitor_c {
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    53
  public:
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    54
    /* A type to specify the type of element.
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    55
     */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    56
    typedef enum {
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    57
      element_single,
661
f537c3315f83 Minor changes needed to build with pedantic flag.
Manuele Conti <conti.ma@alice.it>
parents: 377
diff changeset
    58
      element_subrange
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    59
    } case_element_t ;
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    60
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    61
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    62
  private:
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 347
diff changeset
    63
    /* a pointer to the case_list_c currently being analyzed */
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    64
    symbol_c *case_list;
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    65
    /* used when called to iterate() for a parameter */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    66
    symbol_c *current_case_element;
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    67
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    68
    /* used to indicate the type of case element on which iterate */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    69
    case_element_t wanted_element_type;
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    70
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    71
  private:
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    72
    void* handle_case_element(symbol_c *case_element);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    73
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    74
    void* iterate_list(list_c *list);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    75
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    76
  public:
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    77
    /* start off at the first case element once again... */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    78
    void reset(void);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    79
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 347
diff changeset
    80
    /* initialize the iterator object.
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 347
diff changeset
    81
     * We must be given a reference to a case_list_c that will be analyzed...
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    82
     */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    83
    case_element_iterator_c(symbol_c *list, case_element_t element_type);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    84
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    85
    /* Skip to the next case element of type chosen. After object creation,
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    86
     * the object references on case element _before_ the first, so
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    87
     * this function must be called once to get the object to
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    88
     * reference the first element...
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    89
     *
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    90
     * Returns the case element's symbol!
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    91
     */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    92
    symbol_c *next(void);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    93
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    94
    private:
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    95
    
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    96
    /******************************/
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    97
    /* B 1.2.1 - Numeric Literals */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    98
    /******************************/
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
    99
    void *visit(integer_c *symbol);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   100
    void *visit(neg_integer_c *symbol);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   101
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   102
    /********************************/
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   103
    /* B 1.3.3 - Derived data types */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   104
    /********************************/
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   105
    /*  signed_integer DOTDOT signed_integer */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   106
    void *visit(subrange_c *symbol);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   107
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   108
    /* enumerated_type_name '#' identifier */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   109
    void *visit(enumerated_value_c *symbol);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   110
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   111
    /********************************/
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   112
    /* B 3.2.3 Selection Statements */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   113
    /********************************/
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   114
    void *visit(case_list_c *symbol);
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   115
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   116
    
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   117
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   118
}; // function_param_iterator_c
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   119
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   120
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   121
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   122
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   123
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   124
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents:
diff changeset
   125