/[ascend]/trunk/pygtk/interface/library.cpp
ViewVC logotype

Annotation of /trunk/pygtk/interface/library.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 190 - (hide annotations) (download) (as text)
Mon Jan 16 08:53:30 2006 UTC (14 years, 1 month ago) by johnpye
File MIME type: text/x-c++src
File size: 8455 byte(s)
- Simplifying many 'error_reporter' calls with new macros:
    ERROR_REPORTER_NOLINE and ERROR_REPORTER_HERE
- Adding some cygwin cases in the PyGTK build.
1 johnpye 132 #include <iostream>
2     #include <stdexcept>
3     #include <sstream>
4     using namespace std;
5    
6     #undef NDEBUG
7    
8     //#include <compiler/module.h>
9     extern "C"{
10 johnpye 190 #include <utilities/ascConfig.h>
11 johnpye 132 #include <general/list.h>
12     #include <compiler/slist.h>
13 johnpye 190 #include <compiler/ascCompiler.h>
14     #include <compiler/fractions.h>
15 johnpye 132 #include <compiler/compiler.h>
16     #include <compiler/redirectFile.h>
17     #include <compiler/module.h>
18     #include <compiler/prototype.h>
19     #include <compiler/dump.h>
20     #include <compiler/dimen.h>
21     #include <compiler/child.h>
22     #include <compiler/childio.h>
23     #include <compiler/type_desc.h>
24     #include <compiler/typedef.h>
25     #include <compiler/library.h>
26     #include <compiler/childinfo.h>
27     #include <solver/slv_types.h>
28     #include <solver/system.h>
29     #include <utilities/ascEnvVar.h>
30     #include <compiler/symtab.h>
31     #include <general/table.h>
32     #include <compiler/instance_enum.h>
33     #include <compiler/notate.h>
34 johnpye 164 #include <compiler/simlist.h>
35 johnpye 132
36     extern int zz_parse();
37     }
38    
39     #include "library.h"
40     #include "simulation.h"
41     #include "solver.h"
42    
43     Library::Library(){
44     static int have_init;
45     if(!have_init){
46     Asc_RedirectCompilerDefault(); // Ensure that error message reach stderr
47     Asc_CompilerInit(1);
48     Asc_ImportPathList(PATHENVIRONMENTVAR);
49     char *x = Asc_GetEnv(PATHENVIRONMENTVAR);
50     std::cerr << PATHENVIRONMENTVAR << " = " << x << std::endl;
51     std::cerr << "Created LIBRARY" << std::endl;
52     std::cerr << "----------------------------" << std::endl;
53     registerStandardSolvers();
54     }/*else{
55     std::cerr << "Reusing LIBRARY" << std::endl;
56     }*/
57     have_init=1;
58     }
59    
60     Library::~Library(){
61 johnpye 190 //ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"DESTROYED LIBRARY!");
62 johnpye 175 //DestroyLibrary();
63 johnpye 190 // ... need to use some kind of reference counting before you can do that...
64 johnpye 132 }
65    
66 johnpye 180 /**
67     Load an ASCEND model file into the Library. It will be parsed such that
68     its types will be visible to Library::findType.
69    
70     @param filename Filename, will be searched for relative to ASCENDLIBRARY environment
71     variable, if necessary.
72     */
73 johnpye 132 void
74     Library::load(const char *filename){
75    
76     //std::cerr << "Loading '" << filename << "'" << std::endl;
77    
78     int status;
79     struct module_t *m=Asc_RequireModule(filename,&status);
80     if(m!=NULL){
81     //std::cerr << "Loaded module '" << Asc_ModuleName(m) << "'" << std::endl;
82     }else{
83     std::cerr << "Error: unable to load module '" << filename << "'." << std::endl;
84     }
85    
86     char *msg;
87     switch(status){
88     case 5:
89     msg = "The module '%s' already exists. "; break;
90     case 4:
91     msg = "Caught an attempt to do a recursive require under '%s'."; break;
92     case 3:
93     msg = "A new module was created from '%s', overwriting a module's alias."; break;
94     case 2:
95     msg = "An existing module is being returned for '%s'." ; break;
96     case 1:
97     msg = "An new version of an existing module was created for '%s'."; break;
98     case 0:
99     msg = "Module for '%s' created OK."; break;
100     case -1:
101     msg = "Error: File not found for '%s'. (-1)"; break;
102 johnpye 190 case -2:
103 johnpye 132 msg = "Error: Unable to open '%s' for reading. (-2)";break;
104     case -3:
105     msg = "Error: Insuffient memory to create module for '%s'. (-3)"; break;
106     case -4:
107     msg = "Error: bad input, null or zero length filename in '%s'. (-4)"; break;
108     default:
109     throw std::runtime_error("Invalid status code in library.cpp");
110     }
111    
112     char msg1[100];
113     sprintf(msg1,msg,filename);
114    
115     if(status<0 || status>0){
116     throw std::runtime_error(msg1);
117     }else{
118     std::cerr << "Note: Module " << Asc_ModuleName(m) << ": " << msg1 << std::endl;
119     }
120    
121     std::cerr << "Note: Beginning zz_parse of " << Asc_ModuleName(m) << "..." << std::endl;
122     zz_parse();
123     std::cerr << "Note: ...zz_parse of " << Asc_ModuleName(m) << " completed." << std::endl;
124    
125     struct gl_list_t *l = Asc_TypeByModule(m);
126     std::cerr << "Note: " << gl_length(l) << " library entries loaded from '" << filename << "'" << std::endl;
127     }
128    
129 johnpye 180 /**
130     Return a vector of all the Modules which have been loaded into
131     the current library.
132     */
133 johnpye 132 vector<Module>
134     Library::getModules(){
135     //cerr << "GET MODULES\n" << endl;
136    
137     vector<Module> v;
138     struct gl_list_t *l = Asc_ModuleList(0);
139     for(int i=0, end=gl_length(l); i<end; ++i){
140     symchar *name = (symchar *)gl_fetch(l,i+1);
141     if(AscFindSymbol(name)==NULL){
142     throw runtime_error("Library::getModules: invalid symchar *");
143     }
144     //cerr << "GOT " << SCP( name ) << endl;
145     const module_t *m = Asc_GetModuleByName((const char *)name);
146     v.push_back(Module(m));
147     }
148     /*cerr << "LENGTH OF V IS " << v.size() << endl;
149     if(v.size()){
150     cerr << "MODULE 0's NAME IS " << v[0].getName() << endl;
151     }*/
152     return v;
153     }
154    
155 johnpye 180 /**
156     Output to stderr the names of the modules loaded into the current Library.
157     */
158 johnpye 132 void
159     Library::listModules(const int &module_type) const{
160    
161     if(module_type < 0 || module_type > 2){
162     throw std::runtime_error("Library::listModules: invalid module_type parameter");
163     }
164    
165     struct gl_list_t *l;
166    
167     l = Asc_ModuleList(module_type);
168    
169     if(l==NULL){
170     std::cerr << "Library::listModules: list is empty" << std::endl;
171     return;
172     //throw std::runtime_error("Library::listModules: Asc_ModuleList returned NULL");
173     }
174    
175     char *type;
176     switch(module_type){
177     case 0: type = "modules containing defined types"; break;
178     case 1: type = "modules with string definitions"; break;
179     case 2: type = "modules with statements"; break;
180     }
181     int n=gl_length(l);
182     if(n){
183     std::cerr << "Listing " << gl_length(l) << " " << type << std::endl;
184     gl_iterate(l,Library::displayModule);
185     }else{
186     std::cerr << "Notice: No " << type << " found in module list." << std::endl;
187     }
188     }
189    
190     void
191     Library::displayModule(void *v){
192     //module_t *m = (module_t *)v;
193     std::cerr << " - " << (char *)v << std::endl;
194     }
195    
196     Type &
197     Library::findType(SymChar sym){
198     TypeDescription *t = FindType(sym.getInternalType());
199     if(t==NULL){
200     stringstream ss;
201     ss << "Library::findType: type '" << sym << "' not found in library";
202     throw runtime_error(ss.str());
203     }/*else{
204     cerr << "Found something for type " << sym << endl;
205     }*/
206     Type *t2=new Type(t);
207     return *t2;
208     }
209    
210     /**
211     This could be quite a bit more efficient if we could get a gl_list_t of TypeDescription rather than names
212     */
213     vector<Type>
214     Library::getModuleTypes(const Module &m){
215     //cerr << "GET MODULE TYPES\n" << endl;
216     vector<Type> v;
217     struct gl_list_t *l = Asc_TypeByModule(m.getInternalType());
218     for(int i=0,end=gl_length(l); i<end; ++i){
219     char *name = (char *)gl_fetch(l,i+1);
220 johnpye 150 //CONSOLE_DEBUG("Found type %s",name);
221 johnpye 132 TypeDescription *t = FindType((const symchar *)name);
222     v.push_back(Type(t));
223     }
224     return v;
225     }
226    
227     /**
228     This function is kinda fighting against the Table implementation of the external function library. What we really need is some kind of iterator on the Table struct, but it doesn't seem to be implemented. Instead there is a C-style equivalent of the STL 'bind1st' function which we can use, but it's not exported from the current extfunc.h so we need to add it.
229     */
230     vector<ExtFn>
231     Library::getExtFns(){
232     // Clear the vector
233     extfn_vector = vector<ExtFn>();
234    
235     // Traverse the vector
236     TraverseExtFuncLibrary(Library::extFuncTraverse, (void *)this);
237    
238     return extfn_vector;
239     }
240    
241 johnpye 180 /**
242     This method exists only to allow the TraverseExtFuncLibrary function
243     to make callbacks to the Library class from C.
244    
245     @NOTE there might be issues with C/C++ linking here?
246     */
247 johnpye 132 void
248     Library::extFuncTraverse(void *a1, void *a2){
249     Library *self = (Library *)a2;
250     self->appendToExtFnVector(a1);
251     }
252    
253     void
254     Library::appendToExtFnVector(void *a1){
255     struct ExternalFunc *e = (struct ExternalFunc *)a1;
256     extfn_vector.push_back(ExtFn(e));
257     }
258    
259     /**
260     Clear the library: 'DESTROY TYPES'
261    
262     @TODO do this more efficiently, don't destroy the whole ASCEND compiler.
263     */
264     void
265     Library::clear(){
266     //DestroyNotesDatabase(LibraryNote());
267     /* Asc_CompilerDestroy();
268     cerr << "COMPLETED ASC_COMPILERDESTROY" << endl;
269     Asc_CompilerInit(1);
270     cerr << "... ASC_COMPILERINIT OK" << endl;
271     Asc_ImportPathList(PATHENVIRONMENTVAR);
272     registerStandardSolvers();
273     cerr << "... REGISTER_STANDARD_SOLVERS" << endl;
274     DefineFundamentalTypes();
275     cerr << "... DEFINED FUND TYPES" << endl;
276     /*SetUniversalProcedureList(NULL);
277     */
278 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Destroying simulations...\n");
279 johnpye 164 Asc_DestroySimulations();
280    
281 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"Clearing library...\n");
282 johnpye 132 DestroyNotesDatabase(LibraryNote());
283 johnpye 161 SetUniversalProcedureList(NULL);
284 johnpye 132 DestroyLibrary();
285     DestroyPrototype();
286     EmptyTrash();
287     Asc_DestroyModules((DestroyFunc)DestroyStatementList);
288     WriteChildMissing(NULL,NULL,NULL);
289     //Asc_CompilerInit(1)
290     DefineFundamentalTypes();
291     InitNotesDatabase(LibraryNote());
292 johnpye 190 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING,"LIBRARY CLEARED!");
293 johnpye 132 }
294    
295    

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22