stage1_2/stage1_2.cc
author Mario de Sousa <msousa@fe.up.pt>
Wed, 13 Feb 2019 14:16:26 +0000
changeset 1086 ee66a14e38f7
parent 971 8aee27d46208
permissions -rw-r--r--
merge
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     1
/*
264
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     3
 *
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 264
diff changeset
     5
 *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
264
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     6
 *
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    10
 *  (at your option) any later version.
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    11
 *
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    15
 *  GNU General Public License for more details.
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    16
 *
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    19
 *
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    20
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    21
 * This code is made available on the understanding that it will not be
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    23
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    24
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    25
/*
264
6facabd9c2cd Updating License info (with Edoaurd's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents: 177
diff changeset
    26
 * An IEC 61131-3 compiler.
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    27
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    28
 * Based on the
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    29
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    30
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    31
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    32
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    33
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    34
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    35
 * This file contains the code that calls the stage 1 (lexical anayser) 
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    36
 * and stage 2 (syntax parser) during the first pass.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    37
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    38
139
668a54686827 added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents: 136
diff changeset
    39
#include <string.h>
668a54686827 added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents: 136
diff changeset
    40
#include <stdlib.h>
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    41
20
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    42
/* file with declaration of absyntax classes... */
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    43
#include "../absyntax/absyntax.hh"
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    44
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    45
946
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
    46
#include "../main.hh"
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    47
#include "stage1_2.hh"
822
a7d9e0b8636b Fix build on Ubuntu 13.10
Edouard Tisserant
parents: 757
diff changeset
    48
#include "iec_bison.hh"
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    49
#include "stage1_2_priv.hh"
748
ad4265253528 Start to change code like Mario suggestions.
Manuele Conti <conti.ma@alice.it>
parents: 746
diff changeset
    50
#include "create_enumtype_conversion_functions.hh"
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    51
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    52
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    53
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    54
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    55
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    56
/******************************************************/
746
c7219a37cc39 Add conversion functions option.
Manuele Conti <conti.ma@alice.it>
parents: 745
diff changeset
    57
/* whether we are supporting safe extensions          */
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    58
/* as defined in PLCopen - Technical Committee 5      */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    59
/* Safety Software Technical Specification,           */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    60
/* Part 1: Concepts and Function Blocks,              */
746
c7219a37cc39 Add conversion functions option.
Manuele Conti <conti.ma@alice.it>
parents: 745
diff changeset
    61
/* Version 1.0 – Official Release                   */
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
    62
/******************************************************/
946
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
    63
bool get_opt_safe_extensions() {return runtime_options.safe_extensions;}
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    64
867
a435684a5223 Add option to control support for nested comments (default is off, as defined in IEC 61131-3 v2)
mjsousa
parents: 757
diff changeset
    65
/************************************/
a435684a5223 Add option to control support for nested comments (default is off, as defined in IEC 61131-3 v2)
mjsousa
parents: 757
diff changeset
    66
/* whether to allow nested comments */
a435684a5223 Add option to control support for nested comments (default is off, as defined in IEC 61131-3 v2)
mjsousa
parents: 757
diff changeset
    67
/************************************/
946
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
    68
bool get_opt_nested_comments() {return runtime_options.nested_comments;}
934
2a42a68f4b59 Turn support of derefencing operator '^' an option (default is not supported).
mjsousa
parents: 924
diff changeset
    69
2a42a68f4b59 Turn support of derefencing operator '^' an option (default is not supported).
mjsousa
parents: 924
diff changeset
    70
/**************************************************************************/
2a42a68f4b59 Turn support of derefencing operator '^' an option (default is not supported).
mjsousa
parents: 924
diff changeset
    71
/* whether to allow REF(), DREF(), REF_TO, NULL and ^ operators/keywords  */
2a42a68f4b59 Turn support of derefencing operator '^' an option (default is not supported).
mjsousa
parents: 924
diff changeset
    72
/**************************************************************************/
946
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
    73
bool get_opt_ref_standard_extensions() {return runtime_options.ref_standard_extensions;}
934
2a42a68f4b59 Turn support of derefencing operator '^' an option (default is not supported).
mjsousa
parents: 924
diff changeset
    74
746
c7219a37cc39 Add conversion functions option.
Manuele Conti <conti.ma@alice.it>
parents: 745
diff changeset
    75
952
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    76
/**********************************************************************************************/
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    77
/* whether bison is doing the pre-parsing, where POU bodies and var declarations are ignored! */
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    78
/**********************************************************************************************/
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    79
static bool preparse_state__ = false;
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    80
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    81
void set_preparse_state(void) {preparse_state__ = true; }
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    82
void rst_preparse_state(void) {preparse_state__ = false;}
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    83
bool get_preparse_state(void) {return preparse_state__;}     // returns true if bison is in preparse state
e984cfdf3b10 Add a pre-parsing phase to stage1_2 (allows source code that references POUs and datatypes before they are declared)
mjsousa
parents: 946
diff changeset
    84
746
c7219a37cc39 Add conversion functions option.
Manuele Conti <conti.ma@alice.it>
parents: 745
diff changeset
    85
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    86
/****************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    87
/* Controlling the entry to the body_state in flex. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    88
/****************************************************/
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    89
static int goto_body_state__ = 0;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    90
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    91
void cmd_goto_body_state(void) {goto_body_state__ = 1;}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    92
int  get_goto_body_state(void) {return goto_body_state__;}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    93
void rst_goto_body_state(void) {goto_body_state__ = 0;}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    94
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    95
/*************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    96
/* Controlling the entry to the sfc_qualifier_state in flex. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    97
/*************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    98
static int goto_sfc_qualifier_state__ = 0;
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    99
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   100
void cmd_goto_sfc_qualifier_state(void) {goto_sfc_qualifier_state__ = 1;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   101
int  get_goto_sfc_qualifier_state(void) {return goto_sfc_qualifier_state__;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   102
void rst_goto_sfc_qualifier_state(void) {goto_sfc_qualifier_state__ = 0;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   103
74
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   104
/*************************************************************/
86
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   105
/* Controlling the entry to the sfc_priority_state in flex.  */
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   106
/*************************************************************/
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   107
static int goto_sfc_priority_state__ = 0;
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   108
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   109
void cmd_goto_sfc_priority_state(void) {goto_sfc_priority_state__ = 1;}
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   110
int  get_goto_sfc_priority_state(void) {return goto_sfc_priority_state__;}
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   111
void rst_goto_sfc_priority_state(void) {goto_sfc_priority_state__ = 0;}
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   112
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
   113
/*************************************************************/
74
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   114
/* Controlling the entry to the sfc_qualifier_state in flex. */
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   115
/*************************************************************/
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   116
static int goto_task_init_state__ = 0;
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   117
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   118
void cmd_goto_task_init_state(void) {goto_task_init_state__ = 1;}
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   119
int  get_goto_task_init_state(void) {return goto_task_init_state__;}
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
   120
void rst_goto_task_init_state(void) {goto_task_init_state__ = 0;}
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   121
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   122
/****************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   123
/* Returning to state in flex previously pushed onto the stack. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   124
/****************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   125
static int pop_state__ = 0;
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   126
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   127
void cmd_pop_state(void) {pop_state__ = 1;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   128
int  get_pop_state(void) {return pop_state__;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   129
void rst_pop_state(void) {pop_state__ = 0;}
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   130
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   131
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   132
/*********************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   133
/* The global symbol tables...   */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   134
/*********************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   135
/* NOTE: only accessed indirectly by the lexical parser (flex)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   136
 *       through the function get_identifier_token()
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   137
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   138
/* A symbol table to store all the library elements */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   139
/* e.g.: <function_name , function_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   140
 *       <fb_name , fb_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   141
 *       <type_name , type_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   142
 *       <program_name , program_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   143
 *       <configuration_name , configuration_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   144
 */
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   145
/* static */ library_element_symtable_t library_element_symtable;
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   146
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   147
/* A symbol table to store the declared variables of
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   148
 * the function currently being parsed...
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   149
 */
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   150
/* static */ variable_name_symtable_t   variable_name_symtable;
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   151
175
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   152
/* A symbol table to store the declared direct variables of
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   153
 * the function currently being parsed...
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   154
 */
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   155
/* static */ direct_variable_symtable_t direct_variable_symtable;
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   156
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   157
/* Function only called from within flex!
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   158
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   159
 * search for a symbol in either of the two symbol tables
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   160
 * declared above, and return the token id of the first
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   161
 * symbol found.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   162
 * Searches first in the variables, and only if not found
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   163
 * does it continue searching in the library elements
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   164
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   165
int get_identifier_token(const char *identifier_str) {
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   166
//  std::cout << "get_identifier_token(" << identifier_str << "): \n";
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   167
  variable_name_symtable_t  ::iterator iter1;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   168
  library_element_symtable_t::iterator iter2;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   169
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   170
  if ((iter1 = variable_name_symtable.find(identifier_str)) != variable_name_symtable.end())
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   171
    return iter1->second;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   172
    
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   173
  if ((iter2 = library_element_symtable.find(identifier_str)) != library_element_symtable.end())
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   174
    return iter2->second;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   175
  
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   176
  return identifier_token;
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   177
}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   178
175
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   179
/* Function only called from within flex!
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   180
 *
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   181
 * search for a symbol in direct variables symbol table
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   182
 * declared above, and return the token id of the first
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   183
 * symbol found.
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   184
 */
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   185
int get_direct_variable_token(const char *direct_variable_str) {
971
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   186
  direct_variable_symtable_t::iterator iter;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   187
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   188
  if ((iter = direct_variable_symtable.find(direct_variable_str)) != direct_variable_symtable.end())
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   189
    return iter->second;
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   190
8aee27d46208 Change symbtable_c -> use design pattern used by C++ standard library (STL)
mjsousa
parents: 952
diff changeset
   191
  return direct_variable_token;
175
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   192
}
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   193
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   194
/************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   195
/* Utility Functions... */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   196
/************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   197
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   198
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   199
 * Join two strings together. Allocate space with malloc(3).
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   200
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   201
char *strdup2(const char *a, const char *b) {
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   202
  char *res = (char *)malloc(strlen(a) + strlen(b) + 1);
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   203
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   204
  if (!res)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   205
    return NULL;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   206
  return strcat(strcpy(res, a), b);  /* safe, actually */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   207
}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   208
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   209
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   210
 * Join three strings together. Allocate space with malloc(3).
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   211
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   212
char *strdup3(const char *a, const char *b, const char *c) {
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   213
  char *res = (char *)malloc(strlen(a) + strlen(b) + strlen(c) + 1);
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   214
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   215
  if (!res)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   216
    return NULL;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   217
  return strcat(strcat(strcpy(res, a), b), c);  /* safe, actually */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   218
}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   219
757
f1fc4aa6f0e3 Code cleanup: move tracking functions to flex.
Mario de Sousa <msousa@fe.up.pt>
parents: 756
diff changeset
   220
136
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   221
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   222
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   223
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   224
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   225
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   226
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   227
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   228
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   229
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   230
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   231
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   232
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   233
int stage2__(const char *filename, 
946
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
   234
             symbol_c **tree_root_ref
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   235
            );
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   236
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   237
946
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
   238
int stage1_2(const char *filename, symbol_c **tree_root_ref) {
177
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   239
      /* NOTE: we only call stage2 (bison - syntax analysis) directly, as stage 2 will itself call stage1 (flex - lexical analysis)
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   240
       *       automatically as needed
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   241
       */
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   242
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   243
      /* NOTE: Since we do not call stage1__ (flex) directly, we cannot directly pass any parameters to that function either.
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   244
       *       In this case, we use callback functions, i.e. stage1__ (i.e. flex) will call functions defined in this file
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   245
       *       whenever it needs info/parameters coming from stage1_2().
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   246
       *       These callback functions will get their data from local (to this file) global variables...
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   247
       *       We now set those variables...
697562a5da7c Adding support for SAFEBOOL. Some code generation yet to do...
mario
parents: 175
diff changeset
   248
       */
946
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
   249
  return stage2__(filename, tree_root_ref);
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
   250
}
c012a64dc2fa Make the relaxed datatype model a runtime option (off by default)
mjsousa
parents: 934
diff changeset
   251