]> SALOME platform Git repositories - modules/yacs.git/blob - src/wrappergen/src/parse4.awk
Salome HOME
copy tag mergefrom_BR_V0_1_CC_Salome_04oct07
[modules/yacs.git] / src / wrappergen / src / parse4.awk
1 # This awk program contains the type mapping tables - and the treatments
2 # for code generation
3 #
4 BEGIN { 
5 #
6 # file name generation
7   print "Functions parsing (for debug parse4)" > "parse_result";
8   dispatch_file="code_dispatch"
9   class_cpp=class_name"_cpp"
10   print "//\n// generated part\n//\n" > dispatch_file
11
12   print "#include <exception>\n" >> dispatch_file
13   print "#include \"Any.hxx\"\n" >> dispatch_file
14   print "struct returnInfo {\n   int code;\n   std::string message;\n};\n\n" >> dispatch_file
15   print "extern \"C\"" >> dispatch_file
16   print "void * __init() {" >> dispatch_file
17   print "\n  "class_name " *Obj = new " class_name ";\n  return Obj;\n}\n" >> dispatch_file
18
19   print "extern \"C\"" >> dispatch_file
20   print "void __terminate(void ** vObj) {" >> dispatch_file
21   print "\n  "class_name " *Obj = ("class_name" *) *vObj;\n  delete Obj;\n  *vObj = NULL;\n}\n" >> dispatch_file
22
23   print "extern \"C\"" >> dispatch_file
24   print "void __run(void * O, const char * service, int nbIn, int nbOut,"                    >> dispatch_file
25   print "           YACS::ENGINE::Any ** argIn, YACS::ENGINE::Any ** argOut, returnInfo *r)" >> dispatch_file
26   print "  {"                                                                                >> dispatch_file
27   print "    if (O == NULL) {"                                                             >> dispatch_file
28   print "       r->code = -1;"                                                               >> dispatch_file
29   print "       r->message = \"Component "class_name" has not been initialized\";"       >> dispatch_file
30   print "       return;"                                                                     >> dispatch_file
31   print "       }\n"                                                                         >> dispatch_file
32   print "    returnInfo return_code;"                                                        >> dispatch_file
33   print "    return_code.message = \"\";"                                                      >> dispatch_file
34   print "    return_code.code = 0;\n"                                                        >> dispatch_file
35   print "    int kIn = 0, kOut = 0;" >> dispatch_file;
36   print "    "class_name" * Obj = ("class_name" *) O;">> dispatch_file;
37   print "\n    try {\n" >> dispatch_file;
38
39 #
40 #
41 #
42 # table for c++ code generation : argument's processing
43 #
44   cpp_arg["int"]="    int argIn%d = argIn[kIn++]->getIntValue();\n"
45   cpp_arg["double"]="    double argIn%d = argIn[kIn++]->getDoubleValue();\n"
46   cpp_arg["float"]="    float argIn%d = argIn[kIn++]->getDoubleValue();\n"
47   cpp_arg["long"]="    long argIn%d = argIn[kIn++]->getIntValue();\n"
48   cpp_arg["short"]="    short argIn%d = (short) argIn[kIn++]->getIntValue();\n"
49   cpp_arg["unsigned"]="    unsigned argIn%d = (unsigned ) argIn[kIn++];->getIntValue()\n"
50   cpp_arg["const char*"]="    const char * argIn%d = argIn[kIn++]->getStringValue().c_str();\n"
51   cpp_arg["const std::string&"]="    const std::string& argIn%d = argIn[kIn++]->getStringValue();\n"
52   cpp_arg["const std::vector<double>&"]="    YACS::ENGINE::SequenceAny * sA\n"\
53                                         "          = dynamic_cast<YACS::ENGINE::SequenceAny *>(argIn[kIn]);\n"\
54                                         "    if (NULL == sA) {\n"\
55                                         "      r->code = -1;\n"\
56                                         "      r->message = \"sequence expected\";\n"\
57                                         "      return;\n"\
58                                         "    }\n"\
59                                         "    unsigned int i, n = sA->size();\n"\
60                                         "    std::vector<double> argIn%d(n);\n"\
61                                         "    for (i=0; i<n; i++) argIn%d[i] = ((*sA)[i])->getDoubleValue();\n"\
62                                         "    kIn++;\n"
63
64   cpp_arg["int&"]="    int argOut%d;\n"
65   cpp_arg["double&"]="    double argOut%d;\n"
66   cpp_arg["float&"]="    float argOut%d;\n"
67   cpp_arg["long&"]="    long argOut%d;\n"
68   cpp_arg["short&"]="    short argOut%d;\n"
69   cpp_arg["unsigned&"]="    unsigned argOut%d;\n"
70   cpp_arg["std::string&"]="    std::string argOut%d;\n"
71   cpp_arg["std::vector<double>&"]="    std::vector<double> argOut%d;\n"
72
73   cpp_out["int&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
74   cpp_out["double&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
75   cpp_out["float&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
76   cpp_out["long&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
77   cpp_out["short&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
78   cpp_out["unsigned&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
79   cpp_out["std::string&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
80   cpp_out["std::vector<double>&"]="    argOut[kOut++] = YACS::ENGINE::SequenceAny::New(argOut%d);"
81
82   cpp_out["int"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
83   cpp_out["double"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
84   cpp_out["float"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
85   cpp_out["long"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
86   cpp_out["short"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
87   cpp_out["unsigned"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
88   cpp_out["std::string"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
89   cpp_out["std::vector<double>"]="    argOut[kOut++] = YACS::ENGINE::SequenceAny::New(argOut%d);"
90
91   type_in=1
92   type_out=2
93   
94   type_arg["int"]= type_in
95   type_arg["double"]= type_in
96   type_arg["float"]= type_in
97   type_arg["long"]= type_in
98   type_arg["short"]= type_in
99   type_arg["unsigned"]= type_in
100   type_arg["const char*"]= type_in
101   type_arg["const std::string&"]= type_in
102   type_arg["const std::vector<double>&"]= type_in
103
104   type_arg["int&"]= type_out
105   type_arg["double&"]= type_out
106   type_arg["float&"]= type_out
107   type_arg["long&"]= type_out
108   type_arg["short&"]= type_out
109   type_arg["unsigned&"]= type_out
110   type_arg["std::string&"]= type_out
111   type_arg["std::vector<double>&"]= type_out
112
113 #
114 #
115 # record sep is ");\n" whith blanks all around, and optional "(" at the beginning
116   RS="[(]?[   ]*[)][   ]*;[   ]*\n?"  
117   FS="[   ]*[(,][   ]*"  # field sep is either "(" or "," surrounded by blanks 
118 }
119
120 # --------------------- treatment 1 ----------------------------------
121 #
122 #  extract from fields types, function name, and argument's names
123 #
124 {
125   nitems = split($0, items);
126   print "Function : ",$0 >> "parse_result";  # print for debug
127   for (i=1; i<=nitems; i++) {
128     print "  -> ",i," : ",items[i] >> "parse_result";
129     split(items[i], j, " ");
130     l=0; for (k in j) {l++;}
131     k=j[1];
132     for (ll=2; ll<l; ll++) k=k " " j[ll];
133     type[i] = k;
134     name[i] = j[ll];
135     way[i] = type_arg[k];
136   }
137
138   print "  if (strcmp(service, \""name[1]"\") == 0) {\n" >> dispatch_file;
139
140   for (i=2; i<=nitems; i++) {
141     printf cpp_arg[type[i]],(i-1),(i-1) >> dispatch_file;
142   }
143
144   # Internal function call with local arguments
145   
146   # if no return value, return NULL
147   if (type[1] == "void") {
148     s = "    ";
149   }
150   else {
151     s="    "type[1]" argOut0 = ";
152   }
153
154   s = s"Obj->"name[1]"(";
155   for (i=2; i<nitems; i++) {
156     if (way[i] == type_in) {
157        s=s"argIn"(i-1)",";
158        }
159     else if (way[i] == type_out) {
160        s=s"argOut"(i-1)",";
161        }
162   }
163   if (nitems>1) {
164     if (way[nitems] == type_in) {
165        s=s"argIn"(nitems-1);
166        }
167     else if (way[nitems] == type_out) {
168        s=s"argOut"(nitems-1);
169        }
170   } 
171   s=s");"
172   print s >> dispatch_file;
173   
174   if (type[1] != "void") {
175     printf cpp_out[type[1]],0  >> dispatch_file;
176   }
177   
178   for (i=2; i<nitems; i++) {
179     if (way[i] == type_out) {
180        printf cpp_out[type[i]],(i-1) >> dispatch_file;
181        }
182     }
183   
184   print "\n  }\n  else" >> dispatch_file;
185
186 }
187 #
188 END {
189   print "  {\n    // error in function name" >> dispatch_file
190   print "    return_code.code = -1;" >> dispatch_file
191   print "    return_code.message = std::string(service) + \" is not the name of a service\";\n  }" >> dispatch_file
192   print "\n  }\n  catch(std::exception & e) {\n" >> dispatch_file
193   print "    return_code.code = -1;" >> dispatch_file;
194   print "    return_code.message = std::string(\"internal exception in \") + service + \" : \" + e.what();" >> dispatch_file;
195   print "\n  }\n  catch(...) {\n" >> dispatch_file
196   print "    return_code.code = -1;" >> dispatch_file;
197   print "    return_code.message = std::string(\"internal exception in \") + service;" >> dispatch_file;
198   print "  }" >> dispatch_file
199   print "  *r = return_code;\n}" >> dispatch_file
200   
201   print "\n#include <iostream>\n" >> dispatch_file
202   print "extern \"C\"" >> dispatch_file
203   print "\nvoid __ping() {\n" >> dispatch_file
204   print "  std::cerr << \"ping\" << std::endl;\n}" >> dispatch_file
205 }