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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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