stage1_2/stage1_2.cc
author lbessard
Mon, 18 May 2009 13:19:30 +0200
changeset 175 dadda1b5401e
parent 139 668a54686827
child 177 697562a5da7c
permissions -rw-r--r--
Adding support for checking direct variable declared before using
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     1
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     2
 * (c) 2007 Mario de Sousa
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     3
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     4
 * Offered to the public under the terms of the GNU General Public License
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     5
 * as published by the Free Software Foundation; either version 2 of the
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     6
 * License, or (at your option) any later version.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     7
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     8
 * This program is distributed in the hope that it will be useful, but
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
     9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    11
 * Public License for more details.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    12
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    13
 * 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
    14
 * 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
    15
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    16
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    17
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    18
 * An IEC 61131-3 IL and ST compiler.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    19
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    20
 * Based on the
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    21
 * 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
    22
 *
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
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    26
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    27
 * 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
    28
 * 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
    29
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    30
139
668a54686827 added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents: 136
diff changeset
    31
#include <string.h>
668a54686827 added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents: 136
diff changeset
    32
#include <stdlib.h>
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    33
20
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    34
/* file with declaration of absyntax classes... */
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    35
#include "../absyntax/absyntax.hh"
81a06a308b7e *** empty log message ***
mario
parents: 15
diff changeset
    36
15
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
// #include "stage1_2.hh"
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    39
#include "iec.y.hh"
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    40
#include "stage1_2_priv.hh"
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    41
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    42
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    43
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    44
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    45
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    46
/**************************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    47
/* The name of the file being parsed. */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    48
/**************************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    49
/* The name of the file currently being parsed...
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    50
 * Note that flex accesses and updates this global variable
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    51
 * apropriately whenever it comes across an (*#include <filename> *)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    52
 * directive...
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    53
 * ... and bison will use it when producing error messages.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    54
 * Note that bison also sets this variable correctly to the first
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    55
 * file being parsed.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    56
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    57
const char *current_filename = NULL;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    58
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    59
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    60
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    61
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    62
/****************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    63
/* Controlling the entry to the body_state in flex. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    64
/****************************************************/
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    65
static int goto_body_state__ = 0;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    66
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
    67
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
    68
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
    69
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
    70
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    71
/*************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    72
/* Controlling the entry to the sfc_qualifier_state in flex. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    73
/*************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    74
static int goto_sfc_qualifier_state__ = 0;
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    75
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    76
void cmd_goto_sfc_qualifier_state(void) {goto_sfc_qualifier_state__ = 1;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    77
int  get_goto_sfc_qualifier_state(void) {return goto_sfc_qualifier_state__;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    78
void rst_goto_sfc_qualifier_state(void) {goto_sfc_qualifier_state__ = 0;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    79
74
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
    80
/*************************************************************/
86
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    81
/* 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
    82
/*************************************************************/
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    83
static int goto_sfc_priority_state__ = 0;
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    84
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    85
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
    86
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
    87
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
    88
1988bf1634da Adding support for use of PRIORITY as identifier inside SFC.
mario
parents: 74
diff changeset
    89
/*************************************************************/
74
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
    90
/* Controlling the entry to the sfc_qualifier_state in flex. */
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
    91
/*************************************************************/
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
    92
static int goto_task_init_state__ = 0;
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
    93
85af9521bf63 fixing identifier issues of PRIORITY, SINGLE, and INTERVAL
mario
parents: 68
diff changeset
    94
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
    95
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
    96
void rst_goto_task_init_state(void) {goto_task_init_state__ = 0;}
68
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    97
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    98
/****************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
    99
/* Returning to state in flex previously pushed onto the stack. */
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   100
/****************************************************************/
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   101
static int pop_state__ = 0;
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   102
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   103
void cmd_pop_state(void) {pop_state__ = 1;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   104
int  get_pop_state(void) {return pop_state__;}
b0a43002dcac Adding basic error checking.
mario
parents: 20
diff changeset
   105
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
   106
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   107
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   108
/*********************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   109
/* The global symbol tables...   */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   110
/*********************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   111
/* 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
   112
 *       through the function get_identifier_token()
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   113
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   114
/* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec.y.hh.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   115
 *       We need this constant defined before we can declare the symbol tables.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   116
 *       However, we cannot #include "iec.y.hh" in this file (stage1_2_priv.hh) directly
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   117
 *       because of the way bison ver. 3.2 is copying all declarations in the prologue
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   118
 *       of iec.y to the iec.y.hh file (including an #include stage1_2_priv.hh).
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   119
 *       So, if we were to include "iec.y.hh" here, we would get a circular include.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   120
 *       All this means that whoever includes this file (stage1_2_priv.hh) will need
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   121
 *       to take care to first inlcude iec.y.hh !!
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   122
 */ 
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   123
/* 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
   124
/* e.g.: <function_name , function_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   125
 *       <fb_name , fb_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   126
 *       <type_name , type_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   127
 *       <program_name , program_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   128
 *       <configuration_name , configuration_decl>
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   129
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   130
/* static */ symtable_c<int, BOGUS_TOKEN_ID> library_element_symtable;
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
/* 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
   133
 * the function currently being parsed...
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
/* static */ symtable_c<int, BOGUS_TOKEN_ID> variable_name_symtable;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   136
175
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   137
/* 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
   138
 * the function currently being parsed...
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   139
 */
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   140
/* static */ symtable_c<int, BOGUS_TOKEN_ID> direct_variable_symtable;
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   141
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   142
/* Function only called from within flex!
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   143
 *
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   144
 * 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
   145
 * 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
   146
 * symbol found.
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   147
 * 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
   148
 * 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
   149
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   150
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
   151
//  std::cout << "get_identifier_token(" << identifier_str << "): \n";
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   152
  int token_id;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   153
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   154
  if ((token_id = variable_name_symtable.find_value(identifier_str)) == variable_name_symtable.end_value())
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   155
    if ((token_id = library_element_symtable.find_value(identifier_str)) == library_element_symtable.end_value())
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   156
      return identifier_token;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   157
  return token_id;
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
175
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   160
/* Function only called from within flex!
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   161
 *
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   162
 * search for a symbol in direct variables symbol table
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   163
 * 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
   164
 * symbol found.
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   165
 */
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   166
int get_direct_variable_token(const char *direct_variable_str) {
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   167
  int token_id;
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   168
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   169
  if ((token_id = direct_variable_symtable.find_value(direct_variable_str)) == direct_variable_symtable.end_value())
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   170
    return direct_variable_token;
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   171
  return token_id;
dadda1b5401e Adding support for checking direct variable declared before using
lbessard
parents: 139
diff changeset
   172
}
15
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   173
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   174
/************************/
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   175
/* Utility Functions... */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   176
/************************/
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
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   179
 * 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
   180
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   181
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
   182
  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
   183
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   184
  if (!res)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   185
    return NULL;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   186
  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
   187
}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   188
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   189
/*
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   190
 * 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
   191
 */
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   192
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
   193
  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
   194
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   195
  if (!res)
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   196
    return NULL;
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   197
  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
   198
}
0b472e25eb16 Adding changes to make the compiler compatible with bison ver 2.3
mario
parents:
diff changeset
   199
136
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   200
/*************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   201
/* Tracking Functions... */
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   202
/*************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   203
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   204
extern tracking_t* current_tracking;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   205
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   206
/*--------------------------------------------------------------------
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   207
 * GetNextChar
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   208
 * 
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   209
 * reads a character from input for flex
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   210
 *------------------------------------------------------------------*/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   211
int GetNextChar(char *b, int maxBuffer) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   212
  char *p;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   213
  
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   214
  if (  current_tracking->eof  )
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   215
    return 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   216
  
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   217
  while (  current_tracking->currentChar >= current_tracking->lineLength  ) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   218
    current_tracking->currentChar = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   219
    current_tracking->currentTokenStart = 1;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   220
    current_tracking->eof = false;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   221
    
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   222
    p = fgets(current_tracking->buffer, MAX_BUFFER_LENGTH, current_tracking->in_file);
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   223
    if (  p == NULL  ) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   224
      if (  ferror(current_tracking->in_file)  )
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   225
        return 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   226
      current_tracking->eof = true;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   227
      return 0;
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
    current_tracking->lineNumber++;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   231
    current_tracking->lineLength = strlen(current_tracking->buffer);
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   232
  }
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   233
  
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   234
  b[0] = current_tracking->buffer[current_tracking->currentChar];
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   235
  if (b[0] == ' ' || b[0] == '\t')
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   236
    current_tracking->currentTokenStart++;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   237
  current_tracking->currentChar++;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   238
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   239
  return b[0]==0?0:1;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   240
}
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   241
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   242
tracking_t* GetNewTracking(FILE* in_file) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   243
  tracking_t* new_env = new tracking_t;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   244
  new_env->eof = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   245
  new_env->lineNumber = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   246
  new_env->currentChar = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   247
  new_env->lineLength = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   248
  new_env->currentTokenStart = 0;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   249
  new_env->buffer = (char*)malloc(MAX_BUFFER_LENGTH);
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   250
  new_env->in_file = in_file;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   251
  return new_env;
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   252
}
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   253
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   254
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   255
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   256
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   257
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   258
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   259
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   260
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   261
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   262
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   263
/***********************************************************************/
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   264
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   265
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   266
int stage1_2__(const char *filename, const char *includedir, symbol_c **tree_root_ref, bool full);
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   267
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   268
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   269
int stage1_2(const char *filename, const char *includedir, symbol_c **tree_root_ref, bool full) {
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   270
  return stage1_2__(filename, includedir, tree_root_ref, full);
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   271
}
32bd7ef40897 Adding some improvements:
lbessard
parents: 86
diff changeset
   272