Salome HOME
Updated copyright comment
[modules/yacs.git] / src / wrappergen / src / parse4.awk
1 # Copyright (C) 2006-2024  CEA, EDF
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 # This awk program contains the type mapping tables - and the treatments
21 # for code generation
22 #
23 BEGIN { 
24 #
25 # file name generation
26   print "Functions parsing (for debug parse4)" > "parse_result";
27   dispatch_file="code_dispatch"
28   class_cpp=class_name"_cpp"
29   print "//\n// generated part\n//\n" > dispatch_file
30
31   print "#include <exception>\n" >> dispatch_file
32   print "#include \"Any.hxx\"\n" >> dispatch_file
33   print "struct returnInfo {\n   int code;\n   std::string message;\n};\n\n" >> dispatch_file
34   print "extern \"C\"" >> dispatch_file
35   print "void * __init() {" >> dispatch_file
36   print "\n  "class_name " *Obj = new " class_name ";\n  return Obj;\n}\n" >> dispatch_file
37
38   print "extern \"C\"" >> dispatch_file
39   print "void __terminate(void ** vObj) {" >> dispatch_file
40   print "\n  "class_name " *Obj = ("class_name" *) *vObj;\n  delete Obj;\n  *vObj = NULL;\n}\n" >> dispatch_file
41
42   print "extern \"C\"" >> dispatch_file
43   print "void __run(void * O, const char * service, int nbIn, int nbOut,"                    >> dispatch_file
44   print "           YACS::ENGINE::Any ** argIn, YACS::ENGINE::Any ** argOut, returnInfo *r)" >> dispatch_file
45   print "  {"                                                                                >> dispatch_file
46   print "    if (O == NULL) {"                                                             >> dispatch_file
47   print "       r->code = -1;"                                                               >> dispatch_file
48   print "       r->message = \"Component "class_name" has not been initialized\";"       >> dispatch_file
49   print "       return;"                                                                     >> dispatch_file
50   print "       }\n"                                                                         >> dispatch_file
51   print "    returnInfo return_code;"                                                        >> dispatch_file
52   print "    return_code.message = \"\";"                                                      >> dispatch_file
53   print "    return_code.code = 0;\n"                                                        >> dispatch_file
54   print "    int kIn = 0, kOut = 0;" >> dispatch_file;
55   print "    "class_name" * Obj = ("class_name" *) O;">> dispatch_file;
56   print "\n    try {\n" >> dispatch_file;
57
58 #
59 #
60 #
61 # table for c++ code generation : argument's processing
62 #
63   cpp_arg["int"]="    int argIn%d = argIn[kIn++]->getIntValue();\n"
64   cpp_arg["double"]="    double argIn%d = argIn[kIn++]->getDoubleValue();\n"
65   cpp_arg["float"]="    float argIn%d = argIn[kIn++]->getDoubleValue();\n"
66   cpp_arg["long"]="    long argIn%d = argIn[kIn++]->getIntValue();\n"
67   cpp_arg["short"]="    short argIn%d = (short) argIn[kIn++]->getIntValue();\n"
68   cpp_arg["unsigned"]="    unsigned argIn%d = (unsigned ) argIn[kIn++];->getIntValue()\n"
69   cpp_arg["const char*"]="    const char * argIn%d = argIn[kIn++]->getStringValue().c_str();\n"
70   cpp_arg["const std::string&"]="    const std::string& argIn%d = argIn[kIn++]->getStringValue();\n"
71   cpp_arg["const std::vector<double>&"]="    YACS::ENGINE::SequenceAny * sA\n"\
72                                         "          = dynamic_cast<YACS::ENGINE::SequenceAny *>(argIn[kIn]);\n"\
73                                         "    if (NULL == sA) {\n"\
74                                         "      r->code = -1;\n"\
75                                         "      r->message = \"sequence expected\";\n"\
76                                         "      return;\n"\
77                                         "    }\n"\
78                                         "    unsigned int i, n = sA->size();\n"\
79                                         "    std::vector<double> argIn%d(n);\n"\
80                                         "    for (i=0; i<n; i++) argIn%d[i] = ((*sA)[i])->getDoubleValue();\n"\
81                                         "    kIn++;\n"
82
83   cpp_arg["int&"]="    int argOut%d;\n"
84   cpp_arg["double&"]="    double argOut%d;\n"
85   cpp_arg["float&"]="    float argOut%d;\n"
86   cpp_arg["long&"]="    long argOut%d;\n"
87   cpp_arg["short&"]="    short argOut%d;\n"
88   cpp_arg["unsigned&"]="    unsigned argOut%d;\n"
89   cpp_arg["std::string&"]="    std::string argOut%d;\n"
90   cpp_arg["std::vector<double>&"]="    std::vector<double> argOut%d;\n"
91
92   cpp_out["int&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
93   cpp_out["double&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
94   cpp_out["float&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
95   cpp_out["long&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
96   cpp_out["short&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
97   cpp_out["unsigned&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
98   cpp_out["std::string&"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
99   cpp_out["std::vector<double>&"]="    argOut[kOut++] = YACS::ENGINE::SequenceAny::New(argOut%d);"
100
101   cpp_out["int"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
102   cpp_out["double"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
103   cpp_out["float"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
104   cpp_out["long"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
105   cpp_out["short"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
106   cpp_out["unsigned"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
107   cpp_out["std::string"]="    argOut[kOut++] = YACS::ENGINE::AtomAny::New(argOut%d);"
108   cpp_out["std::vector<double>"]="    argOut[kOut++] = YACS::ENGINE::SequenceAny::New(argOut%d);"
109
110   type_in=1
111   type_out=2
112   
113   type_arg["int"]= type_in
114   type_arg["double"]= type_in
115   type_arg["float"]= type_in
116   type_arg["long"]= type_in
117   type_arg["short"]= type_in
118   type_arg["unsigned"]= type_in
119   type_arg["const char*"]= type_in
120   type_arg["const std::string&"]= type_in
121   type_arg["const std::vector<double>&"]= type_in
122
123   type_arg["int&"]= type_out
124   type_arg["double&"]= type_out
125   type_arg["float&"]= type_out
126   type_arg["long&"]= type_out
127   type_arg["short&"]= type_out
128   type_arg["unsigned&"]= type_out
129   type_arg["std::string&"]= type_out
130   type_arg["std::vector<double>&"]= type_out
131
132 #
133 #
134 # record sep is ");\n" whith blanks all around, and optional "(" at the beginning
135   RS="[(]?[   ]*[)][   ]*;[   ]*\n?"  
136   FS="[   ]*[(,][   ]*"  # field sep is either "(" or "," surrounded by blanks 
137 }
138
139 # --------------------- treatment 1 ----------------------------------
140 #
141 #  extract from fields types, function name, and argument's names
142 #
143 {
144   nitems = split($0, items);
145   print "Function : ",$0 >> "parse_result";  # print for debug
146   for (i=1; i<=nitems; i++) {
147     print "  -> ",i," : ",items[i] >> "parse_result";
148     split(items[i], j, " ");
149     l=0; for (k in j) {l++;}
150     k=j[1];
151     for (ll=2; ll<l; ll++) k=k " " j[ll];
152     type[i] = k;
153     name[i] = j[ll];
154     way[i] = type_arg[k];
155   }
156
157   print "  if (strcmp(service, \""name[1]"\") == 0) {\n" >> dispatch_file;
158
159   for (i=2; i<=nitems; i++) {
160     printf cpp_arg[type[i]],(i-1),(i-1) >> dispatch_file;
161   }
162
163   # Internal function call with local arguments
164   
165   # if no return value, return NULL
166   if (type[1] == "void") {
167     s = "    ";
168   }
169   else {
170     s="    "type[1]" argOut0 = ";
171   }
172
173   s = s"Obj->"name[1]"(";
174   for (i=2; i<nitems; i++) {
175     if (way[i] == type_in) {
176        s=s"argIn"(i-1)",";
177        }
178     else if (way[i] == type_out) {
179        s=s"argOut"(i-1)",";
180        }
181   }
182   if (nitems>1) {
183     if (way[nitems] == type_in) {
184        s=s"argIn"(nitems-1);
185        }
186     else if (way[nitems] == type_out) {
187        s=s"argOut"(nitems-1);
188        }
189   } 
190   s=s");"
191   print s >> dispatch_file;
192   
193   if (type[1] != "void") {
194     printf cpp_out[type[1]],0  >> dispatch_file;
195   }
196   
197   for (i=2; i<nitems; i++) {
198     if (way[i] == type_out) {
199        printf cpp_out[type[i]],(i-1) >> dispatch_file;
200        }
201     }
202   
203   print "\n  }\n  else" >> dispatch_file;
204
205 }
206 #
207 END {
208   print "  {\n    // error in function name" >> dispatch_file
209   print "    return_code.code = -1;" >> dispatch_file
210   print "    return_code.message = std::string(service) + \" is not the name of a service\";\n  }" >> dispatch_file
211   print "\n  }\n  catch(std::exception & e) {\n" >> dispatch_file
212   print "    return_code.code = -1;" >> dispatch_file;
213   print "    return_code.message = std::string(\"internal exception in \") + service + \" : \" + e.what();" >> dispatch_file;
214   print "\n  }\n  catch(...) {\n" >> dispatch_file
215   print "    return_code.code = -1;" >> dispatch_file;
216   print "    return_code.message = std::string(\"internal exception in \") + service;" >> dispatch_file;
217   print "  }" >> dispatch_file
218   print "  *r = return_code;\n}" >> dispatch_file
219   
220   print "\n#include <iostream>\n" >> dispatch_file
221   print "extern \"C\"" >> dispatch_file
222   print "\nvoid __ping() {\n" >> dispatch_file
223   print "  std::cerr << \"ping\" << std::endl;\n}" >> dispatch_file
224 }