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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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