stage1_2/iec_bison.yy
changeset 751 0a5c050d64bb
parent 749 76c87fdb5fc8
child 756 634f476cb60f
--- a/stage1_2/iec_bison.yy	Thu Nov 22 19:19:48 2012 +0000
+++ b/stage1_2/iec_bison.yy	Thu Nov 22 19:26:56 2012 +0000
@@ -100,7 +100,7 @@
 
 /* The interface through which bison and flex interact. */
 #include "stage1_2_priv.hh"
-
+#include "create_enumtype_conversion_functions.hh"
 
 #include "../absyntax_utils/add_en_eno_param_decl.hh"	/* required for  add_en_eno_param_decl_c */
 
@@ -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.
  */
@@ -233,6 +238,9 @@
                    const char *last_filename,
                    long int last_order,
                    const char *additional_error_msg);
+                   
+/* Create entry in symbol table for function conversion data type*/
+void add_enumtype_conversion_functions(const char * dname);
 %}
 
 
@@ -2704,7 +2712,13 @@
  *       and include the library_element_symtable.insert(...) code in the rule actions!
  */
   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(@$));}
+	{
+      $$ = new enumerated_type_declaration_c($1, new enumerated_spec_init_c($3, NULL, locloc(@3)), locloc(@$));
+      if (conversion_functions_) {
+        const char *name = ((identifier_c *)$1)->value;
+	    add_enumtype_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(@$));}
 /* ERROR_CHECK_BEGIN */
@@ -8340,5 +8354,84 @@
   return 0;
 }
 
-
-
+/* Create a tmp file from a char buffer. */
+FILE *ftmpopen (void *buf, size_t size, const char *opentype)
+{
+  FILE *f;
+  f = tmpfile();
+  fwrite(buf, 1, size, f);
+  rewind(f);
+  return f;
+}
+
+/*  sstage2__ function allow to parse a ST code inside a string.
+ *  We use this function to add into AST auto generated code like enum conversion functions.
+ *  This appoach allow us to write future changes code indipendetly and to check generate code
+ *  during developing.
+ */
+int sstage2__(const char *text, 
+              symbol_c **tree_root_ref,
+              bool full_token_loc_        /* error messages specify full token location */
+             ) {
+
+  FILE *in_file = NULL;
+    
+  if((in_file = ftmpopen((void *)text, strlen(text), "r")) == NULL) {
+    perror("Error creating temp file.");
+    return -1;
+  }
+
+  /* now parse the input file... */
+  #if YYDEBUG
+    yydebug = 1;
+  #endif
+  yyin = in_file;
+  
+  /* We turn on "allow_function_overloading" flag to disable some checks. 
+   * In detail when we add to symboltable a symbol already processed we
+   * don't want to get any error. 
+   */  
+   
+  allow_function_overloading = true;
+  allow_extensible_function_parameters = false;
+  full_token_loc = full_token_loc_;
+  current_filename = "built-in";
+  current_tracking = GetNewTracking(yyin);
+  {int res;
+    if ((res = yyparse()) != 0) {
+      fprintf (stderr, "\nParsing failed because of too many consecutive syntax errors. Bailing out!\n");
+        exit(EXIT_FAILURE);
+    }
+  }
+
+  if (yynerrs > 0) {
+    fprintf (stderr, "\n%d error(s) found. Bailing out!\n", yynerrs /* global variable */);
+    exit(EXIT_FAILURE);
+  }
+  
+  if (tree_root_ref != NULL)
+    *tree_root_ref = tree_root;
+
+  fclose(in_file);
+  return 0;
+}
+
+
+/* Create entry in symbol table for function conversion data type*/
+void add_enumtype_conversion_functions(const char * dname) {
+  std::string strname;
+  std::string tmp;
+  
+  strname = dname; 
+  for (int i = 0; create_enumtype_conversion_functions_c::functionDataType[i] != NULL; i++) {
+    tmp = strname + std::string("_TO_") + create_enumtype_conversion_functions_c::functionDataType[i];
+    library_element_symtable.insert(tmp.c_str(), prev_declared_derived_function_name_token);
+    
+    tmp = create_enumtype_conversion_functions_c::functionDataType[i] + std::string("_TO_") + strname;
+    library_element_symtable.insert(tmp.c_str(), prev_declared_derived_function_name_token);
+  }  
+}
+
+
+
+