util/symtable.cc
author Edouard Tisserant
Wed, 24 Nov 2021 08:58:58 +0100
changeset 1095 36bbd0b64816
parent 974 a47c2df5ae3d
permissions -rw-r--r--
Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
     1
/*
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
     3
 *
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
     5
 *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
     6
 *
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    10
 *  (at your option) any later version.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    11
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    15
 *  GNU General Public License for more details.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    16
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 0
diff changeset
    19
 *
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    20
 *
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    21
 * This code is made available on the understanding that it will not be
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    23
 */
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    24
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    25
/*
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    26
 * A generic symbol table.
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    27
 *
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    28
 * This is used to create symbol tables such as a list of
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    29
 * variables currently in scope, etc...
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    30
 * Note that the list of previously defined funstions uses the
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    31
 * dsymtable_c instead, as it requires the table to store duplicate values.
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    32
 */
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    33
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    34
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    35
#include <iostream>
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    36
#include "symtable.hh"
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 279
diff changeset
    37
#include "../main.hh" // required for ERROR() and ERROR_MSG() macros.
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    38
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    39
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    40
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    41
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    42
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    43
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    44
template<typename value_type>
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    45
symtable_c<value_type>::symtable_c(void) {inner_scope = NULL;}
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    46
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    47
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    48
 /* clear all entries... */
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    49
template<typename value_type>
973
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
    50
void symtable_c<value_type>::clear(void) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    51
  _base.clear();
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    52
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    53
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    54
 /* create new inner scope */
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    55
template<typename value_type>
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    56
void symtable_c<value_type>::push(void) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    57
  if (inner_scope != NULL) {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    58
    inner_scope->push();
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    59
  } else {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    60
    inner_scope = new symtable_c();
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    61
  }
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    62
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    63
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    64
  /* clear most inner scope */
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    65
  /* returns 1 if this is the inner most scope	*/
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    66
  /*         0 otherwise			*/
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    67
template<typename value_type>
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    68
int symtable_c<value_type>::pop(void) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    69
  if (inner_scope != NULL) {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    70
    if (inner_scope->pop() == 1) {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    71
      delete inner_scope;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    72
      inner_scope = NULL;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    73
    }
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    74
    return 0;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    75
  } else {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    76
    _base.clear();
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    77
    return 1;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    78
  }
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    79
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    80
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    81
template<typename value_type>
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    82
void symtable_c<value_type>::set(const symbol_c *symbol, value_t new_value) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    83
  if (inner_scope != NULL) {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    84
    inner_scope->set(symbol, new_value);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    85
    return;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    86
  }
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    87
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    88
  const token_c *name = dynamic_cast<const token_c *>(symbol);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    89
  if (name == NULL)
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    90
    ERROR;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    91
  set(name->value, new_value);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    92
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    93
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    94
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    95
template<typename value_type>
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
    96
void symtable_c<value_type>::set(const char *identifier_str, value_t new_value) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    97
  if (inner_scope != NULL) {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    98
    inner_scope->set(identifier_str, new_value);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
    99
    return;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   100
  }
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   101
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   102
  // std::cout << "set_identifier(" << identifier_str << "): \n";
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   103
  iterator i = _base.find(identifier_str);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   104
  if (i == _base.end())
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   105
    /* identifier not already in map! */
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   106
    ERROR;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   107
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   108
  _base[identifier_str] = new_value;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   109
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   110
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   111
template<typename value_type>
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   112
void symtable_c<value_type>::insert(const char *identifier_str, value_t new_value) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   113
  if (inner_scope != NULL) {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   114
    inner_scope->insert(identifier_str, new_value);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   115
    return;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   116
  }
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   117
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   118
  // std::cout << "store_identifier(" << identifier_str << "): \n";
952
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 596
diff changeset
   119
  iterator i = _base.find(identifier_str);
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 596
diff changeset
   120
  if ((i != _base.end()) && (i->second != new_value)) {ERROR;}  /* error inserting new identifier: identifier already in map associated to a different value */
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 596
diff changeset
   121
  if ((i != _base.end()) && (i->second == new_value)) {return;} /* identifier already in map associated with the same value */
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 596
diff changeset
   122
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   123
  std::pair<const char *, value_t> new_element(identifier_str, new_value);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   124
  std::pair<iterator, bool> res = _base.insert(new_element);
952
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 596
diff changeset
   125
  if (!res.second) {ERROR;} /* unknown error inserting new identifier */
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   126
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   127
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   128
template<typename value_type>
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   129
void symtable_c<value_type>::insert(const symbol_c *symbol, value_t new_value) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   130
/*
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   131
// not required...
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   132
  if (inner_scope != NULL) {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   133
    inner_scope->insert(symbol, new_value);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   134
    return;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   135
  }
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   136
*/
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   137
  const token_c *name = dynamic_cast<const token_c *>(symbol);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   138
  if (name == NULL)
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   139
    ERROR;
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   140
  insert(name->value, new_value);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   141
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   142
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   143
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   144
template<typename value_type>
973
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   145
int symtable_c<value_type>::count(const       char *identifier_str) {return _base.count(identifier_str)+((inner_scope == NULL)?0:inner_scope->count(identifier_str));}
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   146
template<typename value_type>
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   147
int symtable_c<value_type>::count(const std::string identifier_str) {return _base.count(identifier_str)+((inner_scope == NULL)?0:inner_scope->count(identifier_str));}
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   148
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   149
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   150
// in the operator[] we delegate to find(), since that method will also search in the inner scopes!
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   151
template<typename value_type>
974
a47c2df5ae3d Fix previous commit (symtable operator[] must return a reference to the stored value!)
mjsousa
parents: 973
diff changeset
   152
typename symtable_c<value_type>::value_t& symtable_c<value_type>::operator[] (const       char *identifier_str) {iterator i = find(identifier_str); return (i!=end())?i->second:_base[identifier_str];}
a47c2df5ae3d Fix previous commit (symtable operator[] must return a reference to the stored value!)
mjsousa
parents: 973
diff changeset
   153
template<typename value_type>
a47c2df5ae3d Fix previous commit (symtable operator[] must return a reference to the stored value!)
mjsousa
parents: 973
diff changeset
   154
typename symtable_c<value_type>::value_t& symtable_c<value_type>::operator[] (const std::string identifier_str) {iterator i = find(identifier_str); return (i!=end())?i->second:_base[identifier_str];}
973
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   155
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   156
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   157
template<typename value_type>
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   158
typename symtable_c<value_type>::iterator symtable_c<value_type>::end  (void) {return _base.end  ();}
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   159
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   160
template<typename value_type>
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   161
typename symtable_c<value_type>::iterator symtable_c<value_type>::begin(void) {return _base.begin();}
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   162
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   163
/* returns end() if not found! */
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   164
template<typename value_type>
973
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   165
typename symtable_c<value_type>::iterator symtable_c<value_type>::find(const       char *identifier_str) {
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   166
  iterator i;
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   167
  if ((inner_scope != NULL) && ((i = inner_scope->find(identifier_str)) != inner_scope->end()))  // NOTE: must use the end() value of the inner scope!
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   168
      return i;  // found in the lower level
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   169
  /* if no lower level, or not found in lower level... */
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   170
  return _base.find(identifier_str);
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   171
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   172
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   173
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   174
template<typename value_type>
973
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   175
typename symtable_c<value_type>::iterator symtable_c<value_type>::find(const std::string identifier_str) {
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   176
  iterator i;
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   177
  if ((inner_scope != NULL) && ((i = inner_scope->find(identifier_str)) != inner_scope->end()))  // NOTE: must use the end() value of the inner scope!
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   178
      return i;  // found in the lower level
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   179
  /* if no lower level, or not found in lower level... */
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   180
  return _base.find(identifier_str);
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   181
}
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   182
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   183
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   184
template<typename value_type>
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 971
diff changeset
   185
typename symtable_c<value_type>::iterator symtable_c<value_type>::find(const   symbol_c *symbol) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   186
  const token_c *name = dynamic_cast<const token_c *>(symbol);
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   187
  if (name == NULL)
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   188
    ERROR;
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   189
  return find(name->value);
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   190
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   191
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   192
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   193
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   194
/* debuging function... */
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   195
template<typename value_type>
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   196
void symtable_c<value_type>::print(void) {
0
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   197
  for(iterator i = _base.begin();
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   198
      i != _base.end();
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   199
      i++)
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   200
    std::cout << i->second << ":" << i->first << "\n";
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   201
  std::cout << "=====================\n";
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   202
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   203
  if (inner_scope != NULL) {
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   204
    inner_scope->print();
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   205
  }
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   206
}
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   207
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   208
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   209
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   210
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   211
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   212
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   213
fb772792efd1 Initial commit. Last MatPLC CVS with some makefile inclusion removed in order to compile fine out of MatPLC.
etisserant
parents:
diff changeset
   214