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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 175 - (show annotations) (download) (as text)
Tue Jan 10 04:27:34 2006 UTC (14 years, 6 months ago) by johnpye
File MIME type: text/x-c++src
File size: 7862 byte(s)
More work on adding plotting capability under PyGTK
1 #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 #include <utilities/ascConfig.h>
11 #include <general/list.h>
12 #include <compiler/slist.h>
13 #include <compiler/ascCompiler.h>
14 #include <compiler/fractions.h>
15 #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 #include <compiler/simlist.h>
35
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 //error_reporter(ASC_PROG_WARNING,NULL,0,"DESTROYED LIBRARY!");
62 //DestroyLibrary();
63 // ... need to use some kind of reference counting before you can do that...
64 }
65
66 void
67 Library::load(const char *filename){
68
69 //std::cerr << "Loading '" << filename << "'" << std::endl;
70
71 int status;
72 struct module_t *m=Asc_RequireModule(filename,&status);
73 if(m!=NULL){
74 //std::cerr << "Loaded module '" << Asc_ModuleName(m) << "'" << std::endl;
75 }else{
76 std::cerr << "Error: unable to load module '" << filename << "'." << std::endl;
77 }
78
79 char *msg;
80 switch(status){
81 case 5:
82 msg = "The module '%s' already exists. "; break;
83 case 4:
84 msg = "Caught an attempt to do a recursive require under '%s'."; break;
85 case 3:
86 msg = "A new module was created from '%s', overwriting a module's alias."; break;
87 case 2:
88 msg = "An existing module is being returned for '%s'." ; break;
89 case 1:
90 msg = "An new version of an existing module was created for '%s'."; break;
91 case 0:
92 msg = "Module for '%s' created OK."; break;
93 case -1:
94 msg = "Error: File not found for '%s'. (-1)"; break;
95 case -2:
96 msg = "Error: Unable to open '%s' for reading. (-2)";break;
97 case -3:
98 msg = "Error: Insuffient memory to create module for '%s'. (-3)"; break;
99 case -4:
100 msg = "Error: bad input, null or zero length filename in '%s'. (-4)"; break;
101 default:
102 throw std::runtime_error("Invalid status code in library.cpp");
103 }
104
105 char msg1[100];
106 sprintf(msg1,msg,filename);
107
108 if(status<0 || status>0){
109 throw std::runtime_error(msg1);
110 }else{
111 std::cerr << "Note: Module " << Asc_ModuleName(m) << ": " << msg1 << std::endl;
112 }
113
114 std::cerr << "Note: Beginning zz_parse of " << Asc_ModuleName(m) << "..." << std::endl;
115 zz_parse();
116 std::cerr << "Note: ...zz_parse of " << Asc_ModuleName(m) << " completed." << std::endl;
117
118 struct gl_list_t *l = Asc_TypeByModule(m);
119 std::cerr << "Note: " << gl_length(l) << " library entries loaded from '" << filename << "'" << std::endl;
120 }
121
122 vector<Module>
123 Library::getModules(){
124 //cerr << "GET MODULES\n" << endl;
125
126 vector<Module> v;
127 struct gl_list_t *l = Asc_ModuleList(0);
128 for(int i=0, end=gl_length(l); i<end; ++i){
129 symchar *name = (symchar *)gl_fetch(l,i+1);
130 if(AscFindSymbol(name)==NULL){
131 throw runtime_error("Library::getModules: invalid symchar *");
132 }
133 //cerr << "GOT " << SCP( name ) << endl;
134 const module_t *m = Asc_GetModuleByName((const char *)name);
135 v.push_back(Module(m));
136 }
137 /*cerr << "LENGTH OF V IS " << v.size() << endl;
138 if(v.size()){
139 cerr << "MODULE 0's NAME IS " << v[0].getName() << endl;
140 }*/
141 return v;
142 }
143
144 void
145 Library::listModules(const int &module_type) const{
146
147 if(module_type < 0 || module_type > 2){
148 throw std::runtime_error("Library::listModules: invalid module_type parameter");
149 }
150
151 struct gl_list_t *l;
152
153 l = Asc_ModuleList(module_type);
154
155 if(l==NULL){
156 std::cerr << "Library::listModules: list is empty" << std::endl;
157 return;
158 //throw std::runtime_error("Library::listModules: Asc_ModuleList returned NULL");
159 }
160
161 char *type;
162 switch(module_type){
163 case 0: type = "modules containing defined types"; break;
164 case 1: type = "modules with string definitions"; break;
165 case 2: type = "modules with statements"; break;
166 }
167 int n=gl_length(l);
168 if(n){
169 std::cerr << "Listing " << gl_length(l) << " " << type << std::endl;
170 gl_iterate(l,Library::displayModule);
171 }else{
172 std::cerr << "Notice: No " << type << " found in module list." << std::endl;
173 }
174 }
175
176 void
177 Library::displayModule(void *v){
178 //module_t *m = (module_t *)v;
179 std::cerr << " - " << (char *)v << std::endl;
180 }
181
182 Type &
183 Library::findType(SymChar sym){
184 TypeDescription *t = FindType(sym.getInternalType());
185 if(t==NULL){
186 stringstream ss;
187 ss << "Library::findType: type '" << sym << "' not found in library";
188 throw runtime_error(ss.str());
189 }/*else{
190 cerr << "Found something for type " << sym << endl;
191 }*/
192 Type *t2=new Type(t);
193 return *t2;
194 }
195
196 /**
197 This could be quite a bit more efficient if we could get a gl_list_t of TypeDescription rather than names
198 */
199 vector<Type>
200 Library::getModuleTypes(const Module &m){
201 //cerr << "GET MODULE TYPES\n" << endl;
202 vector<Type> v;
203 struct gl_list_t *l = Asc_TypeByModule(m.getInternalType());
204 for(int i=0,end=gl_length(l); i<end; ++i){
205 char *name = (char *)gl_fetch(l,i+1);
206 //CONSOLE_DEBUG("Found type %s",name);
207 TypeDescription *t = FindType((const symchar *)name);
208 v.push_back(Type(t));
209 }
210 return v;
211 }
212
213 /**
214 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.
215 */
216 vector<ExtFn>
217 Library::getExtFns(){
218 // Clear the vector
219 extfn_vector = vector<ExtFn>();
220
221 // Traverse the vector
222 TraverseExtFuncLibrary(Library::extFuncTraverse, (void *)this);
223
224 return extfn_vector;
225 }
226
227 void
228 Library::extFuncTraverse(void *a1, void *a2){
229 Library *self = (Library *)a2;
230 self->appendToExtFnVector(a1);
231 }
232
233 void
234 Library::appendToExtFnVector(void *a1){
235 struct ExternalFunc *e = (struct ExternalFunc *)a1;
236 extfn_vector.push_back(ExtFn(e));
237 }
238
239 /**
240 Clear the library: 'DESTROY TYPES'
241
242 @TODO do this more efficiently, don't destroy the whole ASCEND compiler.
243 */
244 void
245 Library::clear(){
246 //DestroyNotesDatabase(LibraryNote());
247 /* Asc_CompilerDestroy();
248 cerr << "COMPLETED ASC_COMPILERDESTROY" << endl;
249 Asc_CompilerInit(1);
250 cerr << "... ASC_COMPILERINIT OK" << endl;
251 Asc_ImportPathList(PATHENVIRONMENTVAR);
252 registerStandardSolvers();
253 cerr << "... REGISTER_STANDARD_SOLVERS" << endl;
254 DefineFundamentalTypes();
255 cerr << "... DEFINED FUND TYPES" << endl;
256 /*SetUniversalProcedureList(NULL);
257 */
258 error_reporter(ASC_PROG_NOTE,NULL,0,"Destroying simulations...\n");
259 Asc_DestroySimulations();
260
261 error_reporter(ASC_PROG_NOTE,NULL,0,"Clearing library...\n");
262 DestroyNotesDatabase(LibraryNote());
263 SetUniversalProcedureList(NULL);
264 DestroyLibrary();
265 DestroyPrototype();
266 EmptyTrash();
267 Asc_DestroyModules((DestroyFunc)DestroyStatementList);
268 WriteChildMissing(NULL,NULL,NULL);
269 //Asc_CompilerInit(1)
270 DefineFundamentalTypes();
271 InitNotesDatabase(LibraryNote());
272 error_reporter(ASC_PROG_WARNING,NULL,0,"LIBRARY CLEARED!");
273 }
274
275

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