Add conversion functions option.
--- a/main.cc Thu Nov 15 17:30:35 2012 +0100
+++ b/main.cc Fri Nov 16 10:26:23 2012 +0100
@@ -67,21 +67,28 @@
-//#include <stdio.h> // printf()
+#include <stdio.h>
#include <getopt.h>
#include <string.h>
-#include <stdlib.h> // EXIT_FAILURE
-#include "absyntax/absyntax.hh" // symbol_c type
-
-/* A macro for printing out internal parser errors... */
-#include <iostream> // required for std::cerr
+#include <stdlib.h>
+#include <stdarg.h>
+#include <iostream>
+
+
+#include "config/config.h"
+#include "absyntax/absyntax.hh"
+#include "absyntax_utils/absyntax_utils.hh"
+#include "stage1_2/stage1_2.hh"
+#include "stage3/stage3.hh"
+#include "stage4/stage4.hh"
+#include "main.hh"
+
#ifndef HGVERSION
-#define HGVERSION ""
+ #define HGVERSION ""
#endif
-#include "main.hh" // symbol_c type
-#include <stdarg.h> // required for va_start(), va_list
+
void error_exit(const char *file_name, int line_no, const char *errmsg, ...) {
va_list argptr;
@@ -101,28 +108,20 @@
}
-
-#include "config/config.h"
-#include "stage1_2/stage1_2.hh"
-#include "absyntax_utils/absyntax_utils.hh"
-
-int stage3(symbol_c *tree_root);
-int stage4(symbol_c *tree_root, const char *builddir);
-
-
static void printusage(const char *cmd) {
- printf("syntax: %s [-h] [-v] [-f] [-s] [-I <include_directory>] [-T <target_directory>] <input_file>\n", cmd);
+ printf("\nsyntax: %s [-h] [-v] [-f] [-s] [-c] [-I <include_directory>] [-T <target_directory>] <input_file>\n", cmd);
printf(" h : show this help message\n");
printf(" v : print version number\n");
printf(" f : display full token location on error messages\n");
/******************************************************/
- /* whether we are suporting safe extensions */
+ /* whether we are supporting safe extensions */
/* as defined in PLCopen - Technical Committee 5 */
/* Safety Software Technical Specification, */
/* Part 1: Concepts and Function Blocks, */
- /* Version 1.0 – Official Release */
+ /* Version 1.0 is Official Release */
/******************************************************/
printf(" s : allow use of safe extensions\n");
+ printf(" c : create conversion functions\n");
printf("\n");
printf("%s - Copyright (C) 2003-2011 \n"
"This program comes with ABSOLUTELY NO WARRANTY!\n"
@@ -137,29 +136,34 @@
stage1_2_options_t stage1_2_options = {false, false, NULL};
int optres, errflg = 0;
int path_len;
-/*
- extern char *optarg;
- extern int optind, optopt;
-*/
+
/******************************************/
/* Parse command line options... */
/******************************************/
- while ((optres = getopt(argc, argv, ":hvfsI:T:")) != -1) {
+ while ((optres = getopt(argc, argv, ":hvfscI:T:")) != -1) {
switch(optres) {
case 'h':
printusage(argv[0]);
return 0;
+
case 'v':
fprintf(stdout, "%s version %s\n"
"changeset id: %s\n", PACKAGE_NAME, PACKAGE_VERSION, HGVERSION);
- return 0;
+ return 0;
+
case 'f':
stage1_2_options.full_token_loc = true;
break;
+
case 's':
stage1_2_options.safe_extensions = true;
break;
+
+ case 'c':
+ stage1_2_options.conversion_functions = true;
+ break;
+
case 'I':
/* NOTE: To improve the usability under windows:
* We delete last char's path if it ends with "\".
@@ -170,20 +174,24 @@
if (optarg[path_len] == '\\') optarg[path_len]= '\0';
stage1_2_options.includedir = optarg;
break;
+
case 'T':
/* NOTE: see note above */
path_len = strlen(optarg) - 1;
if (optarg[path_len] == '\\') optarg[path_len]= '\0';
builddir = optarg;
break;
+
case ':': /* -I or -T without operand */
fprintf(stderr, "Option -%c requires an operand\n", optopt);
errflg++;
break;
+
case '?':
fprintf(stderr, "Unrecognized option: -%c\n", optopt);
errflg++;
break;
+
default:
fprintf(stderr, "Unknown error while parsing command line options.");
errflg++;
@@ -202,9 +210,8 @@
}
if (errflg) {
- printf("\n");
- printusage(argv[0]);
- return EXIT_FAILURE;
+ printusage(argv[0]);
+ return EXIT_FAILURE;
}
@@ -225,7 +232,6 @@
if (stage3(tree_root) < 0)
return EXIT_FAILURE;
-
/* 3rd Pass */
if (stage4(tree_root, builddir) < 0)
return EXIT_FAILURE;
--- a/stage1_2/derived_conversion_functions.hh Thu Nov 15 17:30:35 2012 +0100
+++ b/stage1_2/derived_conversion_functions.hh Fri Nov 16 10:26:23 2012 +0100
@@ -31,12 +31,13 @@
*
*/
-#ifndef _DERIVED_CONVERSION_FUNCTIONS_H
-#define _DERIVED_CONVERSION_FUNCTIONS_H
+#ifndef _DERIVED_CONVERSION_FUNCTIONS_HH
+#define _DERIVED_CONVERSION_FUNCTIONS_HH
+
+#include <string>
+#include <list>
#include "../absyntax_utils/absyntax_utils.hh"
-#include <string>
-#include <list>
class derived_conversion_functions_c: public iterator_visitor_c {
public:
@@ -57,12 +58,12 @@
private:
std::string text;
std::string currentToken;
- std::list< std::string> currentTokenList;
+ std::list <std::string> currentTokenList;
std::string getIntegerName(bool isSigned, size_t size);
- void printStringToEnum (std::string &enumerateName, std::list<std::string> &enumerateValues);
- void printEnumToString (std::string &enumerateName, std::list<std::string> &enumerateValues);
- void printIntegerToEnum (std::string &enumerateName, std::list<std::string> &enumerateValues, bool isSigned, size_t size);
- void printEnumToInteger (std::string &enumerateName, std::list<std::string> &enumerateValues, bool isSigned, size_t size);
+ void printStringToEnum (std::string &enumerateName, std::list <std::string> &enumerateValues);
+ void printEnumToString (std::string &enumerateName, std::list <std::string> &enumerateValues);
+ void printIntegerToEnum (std::string &enumerateName, std::list <std::string> &enumerateValues, bool isSigned, size_t size);
+ void printEnumToInteger (std::string &enumerateName, std::list <std::string> &enumerateValues, bool isSigned, size_t size);
};
#endif /* _DERIVED_CONVERSION_FUNCTIONS_H */
--- a/stage1_2/iec_bison.yy Thu Nov 15 17:30:35 2012 +0100
+++ b/stage1_2/iec_bison.yy Fri Nov 16 10:26:23 2012 +0100
@@ -203,6 +203,11 @@
*/
extern bool full_token_loc;
+/* A global flag used to tell the parser whether to generate conversion function
+ * for enumerated data types.
+ */
+extern bool conversion_functions_;
+
/* A pointer to the root of the parsing tree that will be generated
* by bison.
*/
@@ -2709,8 +2714,10 @@
identifier ':' enumerated_specification {library_element_symtable.insert($1, prev_declared_enumerated_type_name_token);}
{
$$ = new enumerated_type_declaration_c($1, new enumerated_spec_init_c($3, NULL, locloc(@3)), locloc(@$));
- const char *name = ((identifier_c *)$1)->value;
- make_derived_conversion_functions(name);
+ if (conversion_functions_) {
+ const char *name = ((identifier_c *)$1)->value;
+ make_derived_conversion_functions(name);
+ }
}
| identifier ':' enumerated_specification {library_element_symtable.insert($1, prev_declared_enumerated_type_name_token);} ASSIGN enumerated_value
{$$ = new enumerated_type_declaration_c($1, new enumerated_spec_init_c($3, $6, locf(@3), locl(@6)), locloc(@$));}
--- a/stage1_2/stage1_2.cc Thu Nov 15 17:30:35 2012 +0100
+++ b/stage1_2/stage1_2.cc Fri Nov 16 10:26:23 2012 +0100
@@ -66,15 +66,22 @@
/******************************************************/
-/* whether we are suporting safe extensions */
+/* whether we are supporting safe extensions */
/* as defined in PLCopen - Technical Committee 5 */
/* Safety Software Technical Specification, */
/* Part 1: Concepts and Function Blocks, */
-/* Version 1.0 – Official Release */
+/* Version 1.0 – Official Release */
/******************************************************/
bool safe_extensions_ = false;
bool get_opt_safe_extensions() {return safe_extensions_;}
+/******************************************************/
+/* whether we are supporting conversion functions */
+/* for enumerate data types */
+/******************************************************/
+bool conversion_functions_ = false;
+
+
/****************************************************/
/* Controlling the entry to the body_state in flex. */
/****************************************************/
@@ -304,10 +311,14 @@
*/
safe_extensions_ = options.safe_extensions;
+ conversion_functions_ = options.conversion_functions;
int ret = stage2__(filename, options.includedir, tree_root_ref, options.full_token_loc);
- derived_conversion_functions_c derived_conversion_functions(*tree_root_ref);
- std::string source_code = derived_conversion_functions.get_declaration(*tree_root_ref);
- ret = sstage2__(source_code.c_str(), tree_root_ref, false);
+
+ if (conversion_functions_) {
+ derived_conversion_functions_c derived_conversion_functions(*tree_root_ref);
+ std::string source_code = derived_conversion_functions.get_declaration(*tree_root_ref);
+ ret = sstage2__(source_code.c_str(), tree_root_ref, false);
+ }
return ret;
}
--- a/stage1_2/stage1_2.hh Thu Nov 15 17:30:35 2012 +0100
+++ b/stage1_2/stage1_2.hh Fri Nov 16 10:26:23 2012 +0100
@@ -60,6 +60,8 @@
/* error messages specify full token location */
bool full_token_loc;
/* Include directory, where included files will be searched for... */
+ bool conversion_functions;
+ /* Create a conversion function for derived datatype */
const char *includedir;
} stage1_2_options_t;
--- a/stage3/stage3.hh Thu Nov 15 17:30:35 2012 +0100
+++ b/stage3/stage3.hh Fri Nov 16 10:26:23 2012 +0100
@@ -30,9 +30,12 @@
*
*/
+#ifndef _STAGE3_HH
+#define _STAGE3_HH
#include "../util/symtable.hh"
int stage3(symbol_c *tree_root);
+#endif /* _STAGE3_HH */
--- a/stage4/stage4.hh Thu Nov 15 17:30:35 2012 +0100
+++ b/stage4/stage4.hh Fri Nov 16 10:26:23 2012 +0100
@@ -36,6 +36,8 @@
* by each specific version of the 4th stage.
*/
+#ifndef _STAGE4_HH
+#define _STAGE4_HH
#include "../absyntax/absyntax.hh"
@@ -100,6 +102,6 @@
+int stage4(symbol_c *tree_root, const char *builddir);
-
-int stage4(symbol_c *tree_root);
+#endif /* _STAGE4_HH */