OSDN Git Service

12ecfa18d01bb4cac077d448d05b3475d8ac3e05
[hmh/hhml.git] / lib / mlenv.h
1 #ifndef MLENV_H
2 #define MLENV_H
3
4 #include "ftable.h"
5 #include "motorvar.h"
6 #include <boost/unordered_map.hpp>
7 #include <boost/ptr_container/ptr_vector.hpp>
8 #include <vector>
9 #include <iostream>
10
11 class  MNode;
12 class  MotorEnv;
13
14 class  MlPool {
15  public:
16     boost::unordered_map<MNode*, int>  linenum;
17     MotorVar  globalVar;
18     boost::ptr_vector<MotorVar>  localVar;
19 #ifdef DEBUG
20     MotorSet  nodebugFunc;
21 #endif /* DEBUG */
22     time_t  starttime;
23     time_t  limittime;
24     int  includeCount;
25     MotorSet  includedFile;
26     MNodePtr  breaksym;
27     MNodePtr  breakval;
28     bool  nolog;
29
30     MlPool () {
31         includeCount = 0;
32         nolog = false;
33         starttime = limittime = 0;
34     };
35     virtual  ~MlPool () {};
36
37     virtual void  setStartTime ();
38     virtual bool  qtimeup (std::ostream* log);
39     virtual void  resetProg () {
40         breaksym = NULL;
41         breakval = NULL;
42     };
43     virtual void  breakProg () {
44         breaksym = new MNode;
45         breaksym ()->set_car (new MNode);
46         breakval = NULL;
47     };
48     virtual void  inclIncCount ();
49     virtual bool  inclIncCount_nothrow ();
50     virtual void  declIncCount ();
51 };
52
53 class  MlFTable {
54  public:
55     FTable*  ftable;
56     MTable*  mtable;
57     class  MStackVal {
58     public:
59         FTableVal*  mfunc;
60         MLFunc*  mobj;
61
62         MStackVal (FTableVal* mf): mfunc (mf), mobj (NULL) {};
63         virtual  ~MStackVal () {};
64     };
65     std::vector<MStackVal>  mstack;
66
67     MlFTable () {
68         ftable = NULL;
69         mtable = NULL;
70     };
71     virtual  ~MlFTable () {};
72
73     virtual void  setFTable (FTable* ft, MTable* mt) {
74         ftable = ft;
75         mtable = mt;
76     };
77 };
78
79 class  MlEnv {
80  public:
81     typedef  void (*datastoreFunc_t)(MLFunc*);
82     class  autoDatastoreFunc {
83     public:
84         MlEnv*  mlenv;
85         
86         autoDatastoreFunc (MlEnv* _mlenv, MLFunc* mobj, datastoreFunc_t fn) {
87             mlenv = _mlenv;
88             mlenv->datastoreFuncStack.push_back (std::pair<MLFunc*,datastoreFunc_t> (mobj, fn));
89         };
90         virtual  ~autoDatastoreFunc () {
91             mlenv->datastoreFuncStack.pop_back ();
92         };
93     };
94
95     MlPool*  mlPool;
96     MlFTable*  mlFTable;
97     std::vector<std::pair<MLFunc*,datastoreFunc_t> >  datastoreFuncStack;
98     std::wstring  regtext;
99     boost::wsmatch  regmatch;
100     MNodePtr  retval;
101     MNodePtr  currentCell;
102     MotorEnv*  env;
103     std::ostream*  log;
104
105     MlEnv (MlPool* _mlpool, MlFTable* _mlftable) {
106         mlPool = _mlpool;
107         mlFTable = _mlftable;
108         env = NULL;
109         log = NULL;
110     };
111     virtual  ~MlEnv () {};
112
113     virtual bool  searchMTable (const ustring& name, FTableVal*& ans);
114     virtual bool  searchSTable (const ustring& name, FTableVal*& ans, MLFunc*& mobj);
115     virtual bool  searchFTable (const ustring& name, FTableVal*& ans);
116     virtual void  pushMStack (FTableVal* obj) {
117         mlFTable->mstack.push_back (MlFTable::MStackVal (obj));
118     };
119     virtual void  popMStack () {
120         mlFTable->mstack.pop_back ();
121     };
122     virtual void  setStartTime () {
123         mlPool->setStartTime ();
124     };
125     virtual bool  qtimeup () {
126         mlPool->qtimeup (log);
127     };
128     virtual void  resetProg () {
129         mlPool->resetProg ();
130     };
131     virtual void  breakProg () {
132         mlPool->breakProg ();
133     };
134     virtual void  inclIncCount () {
135         mlPool->inclIncCount ();
136     };
137     virtual bool  inclIncCount_nothrow () {
138         mlPool->inclIncCount_nothrow ();
139     };
140     virtual void  declIncCount () {
141         mlPool->declIncCount ();
142     };
143     virtual bool  validName (const ustring& name);
144     virtual void  setGlobalAry (const ustring& name, size_t i, MNode* val);
145     virtual void  setGlobalArySize (const ustring& name, size_t n);
146     virtual void  setLocalAry (MotorVar* pool, const ustring& name, size_t i, MNode* val);
147     virtual void  setLocalArySize (MotorVar* pool, const ustring& name, size_t n);
148     virtual MNode*  getGlobalAry (const ustring& name, size_t i);
149     virtual size_t  getGlobalArySize (const ustring& name);
150     virtual MNode*  getLocalAry (MotorVar* pool, const ustring& name, size_t i);
151     virtual size_t  getLocalArySize (MotorVar* pool, const ustring& name);
152     virtual void  setVar (const ustring& name, MNode* val);
153     virtual void  setVar_nolog (const ustring& name, MNode* val);
154     virtual void  setVar2 (const ustring& name, MNode* val);
155     virtual void  setAry (const ustring& name, size_t i, MNode* val);
156     virtual void  setArySize (const ustring& name, size_t n);
157     virtual void  setAry (const ustring& name, MNode* list);
158     virtual MNode*  getVar (const ustring& name);
159     virtual ustring  getVar_string (const ustring& name);
160     virtual MNode*  getAry (const ustring& name, size_t i);
161     virtual ustring  getAry_string (const ustring& name, size_t i);
162     virtual size_t  getArySize (const ustring& name);
163     virtual void  beginLocal ();
164     virtual void  setLocalVar (const ustring& name, MNode* val);
165     virtual void  defineLocalVar (const ustring& name);
166     virtual void  endLocal ();
167     virtual MotorVar*  findLocal (const ustring& name);
168 #ifdef DEBUG
169     virtual void  logSetVar (const ustring& name, MNode* val, bool flocal = false);
170     virtual void  logSetVarError (const ustring& name, MNode* val);
171     virtual void  logSetAryError (const ustring& name, size_t i, MNode* val);
172     virtual void  logSetArySizeError (const ustring& name, size_t n);
173 #endif /* DEBUG */
174
175     virtual void  push_linenum (MNode* c, int ln);
176     virtual void  logLinenum (MNode* c);
177     virtual void  logSexp (MNode* c);
178     virtual void  setMStack (MLFunc* mobj);
179     virtual void  execDatastoreFunc ();
180     virtual MNode*  breaksym () {
181         return mlPool->breaksym ();
182     };
183     virtual MNode*  breakval () {
184         return mlPool->breakval ();
185     };
186     virtual void  setBreak (MNode* sym, MNode* val) {
187         mlPool->breaksym = sym;
188         mlPool->breakval = val;
189     };
190     virtual bool  stopBreak (MNode* fnname) {
191         if (breaksym ()
192             && (breaksym ()->isNil ()
193                 || equal (breaksym (), fnname))) {
194             setBreak (NULL, NULL);
195             return true;
196         }
197         return false;
198     };
199 #ifdef DEBUG
200     virtual void  setNodebugFunc (const ustring& name) {
201         mlPool->nodebugFunc.set (name);
202     };
203 #endif /* DEBUG */
204     virtual bool  includedFile (const ustring& name) {
205         return mlPool->includedFile.get (name);
206     };
207     virtual void  setIncludedFile (const ustring& name) {
208         mlPool->includedFile.set (name);
209     };
210 };
211
212 template<class T>  class  AutoDelete {
213  public:
214     T*  p;
215
216     AutoDelete (): p (NULL) {};
217     virtual  ~AutoDelete () {
218         delete p;
219         p = NULL;
220     };
221     T*  operator () () {
222         return p;
223     };
224     T*  operator = (T* b) {
225         delete p;
226         p = b;
227         return p;
228     };
229     T*  release () {
230         T*  ans = p;
231         p = NULL;
232         return ans;
233     };
234 };
235
236 class  AutoLocalVariable {
237  public:
238     MlEnv*  mlenv;
239
240     AutoLocalVariable (MlEnv* e) {
241         mlenv = e;
242         mlenv->beginLocal ();
243     };
244     virtual  ~AutoLocalVariable () {
245         mlenv->endLocal ();
246     };
247 };
248
249 class  AutoInclCount {
250  public:
251     MlEnv*  mlenv;
252     int  count;
253
254     AutoInclCount (MlEnv* e) {
255         mlenv = e;
256         count = 0;
257     };
258     virtual  ~AutoInclCount () {
259         assert (count <= 1);
260         if (count == 1)
261             mlenv->declIncCount ();
262     };
263     void  inc () {
264         mlenv->inclIncCount ();
265         count ++;
266     };
267     bool  inc_test () {
268         if (mlenv->inclIncCount_nothrow ()) {
269             count ++;
270             return true;
271         } else {
272             return false;
273         }
274     };
275 };
276
277 class  AutoBackupBool {
278 public:
279     bool*  ptr;
280     bool  b;
281
282     AutoBackupBool (bool* _ptr) {
283         ptr = _ptr;
284         b = *ptr;
285     };
286     virtual  ~AutoBackupBool () {
287         *ptr = b;
288     };
289 };
290
291 template<class T> class  AutoBackupPtr {
292  public:
293     T**  ptr;
294     T*  p;
295
296     AutoBackupPtr (T** _p, T* _v) {
297         ptr = _p;
298         p = *ptr;
299         *ptr = _v;
300     };
301     virtual  ~AutoBackupPtr () {
302         *ptr = p;
303     };
304 };
305
306 #endif /* MLENV_H */