-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZInterpControl.cpp
273 lines (247 loc) · 9.13 KB
/
ZInterpControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "stdafx.h"
#include "ZInterp.h"
#include "Helper.h"
#define PARSER xyz->pTreeParser
#define RECOGNIZER PARSER->rec
#define INPUT PARSER->ctnstream
#define ISTREAM INPUT->tnstream->istream
#define MATCHT(t, fs) RECOGNIZER->match(RECOGNIZER, t, fs)
#define MATCHANYT() RECOGNIZER->matchAny(RECOGNIZER)
#define CONSUME() ISTREAM->consume(ISTREAM)
#define LA(n) ISTREAM->_LA(ISTREAM, n)
#define LT(n) INPUT->tnstream->_LT(INPUT->tnstream, n)
#define SEEK(n) ISTREAM->seek(ISTREAM, n)
namespace ZInterp
{
void IfExpr::Exec(pANTLR3_BASE_TREE ifnode,pANTLR3_BASE_TREE cond,yatgFW_Ctx_struct* xyz)
{
pANTLR3_BASE_TREE r;
if(boost::apply_visitor(BoolVal(),*((ZTvarp)(cond->u)))== ZBTrue )
{
r=(pANTLR3_BASE_TREE)(ifnode->getChild(ifnode,1));
SEEK(r->savedIndex);
MATCHT(EIF_THEN,NULL);
MATCHT(ANTLR3_TOKEN_DOWN,NULL);
ZInterp::ifScope=true;
r=(xyz->expr_g(xyz)).start;
ZInterp::ifScope=false;
ifnode->u=r->u;
r=(pANTLR3_BASE_TREE)(ifnode->getChild(ifnode,3));
SEEK(r->savedIndex);
MATCHT(EIF_END,NULL);
}
else
{
r=(pANTLR3_BASE_TREE)(ifnode->getChild(ifnode,2));
SEEK(r->savedIndex);
MATCHT(EIF_ELSE,NULL);
if(LA(1)==ANTLR3_TOKEN_DOWN)
{
MATCHT(ANTLR3_TOKEN_DOWN,NULL);
ZInterp::ifScope=true;
r=(xyz->expr_g(xyz)).start;
ZInterp::ifScope=false;
ifnode->u=r->u;
MATCHT(ANTLR3_TOKEN_UP,NULL);
}
MATCHT(EIF_END,NULL);
}
}
void WhileExpr::Exec(pANTLR3_BASE_TREE wnode,pANTLR3_BASE_TREE cond,yatgFW_Ctx_struct* xyz)
{
ANTLR3_MARKER Wcond=((pANTLR3_BASE_TREE)(wnode->getChild(wnode,0)))->savedIndex;
ANTLR3_MARKER Wexpr=((pANTLR3_BASE_TREE)(wnode->getChild(wnode,1)))->savedIndex;
ANTLR3_MARKER Wend =((pANTLR3_BASE_TREE)(wnode->getChild(wnode,2)))->savedIndex;
ZInterp::lend.push_back(Wend);
ZInterp::lCon.push_back(Wcond);
while ( boost :: apply_visitor ( BoolVal ( ) , * ( ( ZTvarp ) ( cond -> u ) ) ) == ZBTrue )
{
SEEK(Wexpr);
MATCHT(EWHILE_EXP,NULL);
MATCHT(ANTLR3_TOKEN_DOWN,NULL);
if ( ZInterp :: isContinue )
{
ZInterp :: isContinue= ZBFalse;
continue ;
}
if(ZInterp::isExit)
{
ZInterp :: actual ++ ;
if(ZInterp :: actual == ZInterp :: loopNum)
{
ZInterp :: actual = 0 ;
ZInterp :: isExit = ZBFalse ;
}
break ;
}
xyz->expr_g(xyz);
SEEK(Wcond);
MATCHT(EWHILE_CON,NULL);
MATCHT(ANTLR3_TOKEN_DOWN,NULL);
cond = (xyz->expr_g(xyz)).start;
}
SEEK(Wend);
MATCHT(EWHILE_END,NULL);
}
void ForExpr::Exec ( pANTLR3_BASE_TREE fnode , pANTLR3_BASE_TREE src , yatgFW_Ctx_struct* xyz )
{
ANTLR3_MARKER fexpr = ( ( pANTLR3_BASE_TREE ) ( fnode -> getChild ( fnode , 2 ) ) ) -> savedIndex ;
ANTLR3_MARKER fend = ( ( pANTLR3_BASE_TREE ) ( fnode -> getChild ( fnode , 3 ) ) ) -> savedIndex ;
ZInterp::lend.push_back(fend);
ZInterp::lCon.push_back(fend);
pANTLR3_BASE_TREE tsrc=( ( pANTLR3_BASE_TREE ) ( fnode -> getChild ( fnode , 1 ) ) );
pANTLR3_BASE_TREE tstart= ( pANTLR3_BASE_TREE ) tsrc -> getChild ( tsrc , 0 ) ;
pANTLR3_BASE_TREE tend= ( pANTLR3_BASE_TREE ) tsrc -> getChild ( tsrc , 1 ) ;
pANTLR3_BASE_TREE tby= ( pANTLR3_BASE_TREE ) tsrc -> getChild ( tsrc , 2 ) ;
pANTLR3_BASE_TREE twhere= ( pANTLR3_BASE_TREE ) tsrc -> getChild ( tsrc , 3 ) ;
ZIFloat start,end,scale=1;ZIBool cond=ZBTrue;ZTvarp v;ZTList* list=NULL;ZChar* arr;
ZTFloat f;
v = ZAlloc(ZTvar,1);
if(tend==NULL)
{
//arr=getNodeText(((pANTLR3_BASE_TREE)(( pANTLR3_BASE_TREE ) tstart -> getChild ( tstart , 0 ))->getChild((( pANTLR3_BASE_TREE ) tstart -> getChild ( tstart , 0 )),0)));
//ZTvarp var=ZInterp::ZSym.getSymbol(arr,true);
list=( gLIST_ZCONV( *(ZTvarp)( ( ( pANTLR3_BASE_TREE ) tstart -> getChild ( tstart , 0 ) ) ->u) )).cont;
start=0;
end=list->val.size();
//f.val=atof(boost::apply_visitor(ToString(),*(list->val[0])));
//v = ZAlloc(ZTvar , 1);
*v=*(list->val[0]);
}
else
{
start= gFLOAT_ZCONV(*(ZTvarp)((( pANTLR3_BASE_TREE ) tstart -> getChild ( tstart , 0 ))->u)).cont->val;
end=gFLOAT_ZCONV(*(ZTvarp)((( pANTLR3_BASE_TREE ) tend -> getChild ( tend , 0 ))->u)).cont->val;
scale=( tby != NULL ) ? gFLOAT_ZCONV(*(ZTvarp)((( pANTLR3_BASE_TREE ) tby -> getChild ( tby , 0 ))->u)).cont->val : 1.0;
cond= (twhere!=NULL) ? gBOOL_ZCONV(*(ZTvarp)((( pANTLR3_BASE_TREE ) twhere -> getChild ( twhere , 0 ))->u)).cont->val == ZBTrue : ZBTrue;
//f.val=start;
*v =ZTFloat(start);
}
ZChar* id=getNodeText((pANTLR3_BASE_TREE)fnode->getChild(fnode,0));
ZInterp::ZSym.currentScope->VarTable.Insert(v,id);
bool direct = ( start < end ) ? true : false ;
for ( ; (direct? start <=end : start >= end) && cond ; start += scale )
{
cond= (twhere!=NULL) ? gBOOL_ZCONV(*(ZTvarp)((( pANTLR3_BASE_TREE ) twhere -> getChild ( twhere , 0 ))->u)).cont->val == ZBTrue : ZBTrue;
if(!cond)
break;
if(list==NULL) {
//f.val+=scale;
//*v =(ZTvar)f;
//*v=boost::apply_visitor(Sum(),*v,ZTvar(ZTFloat(start)));
*v =ZTFloat(start);
}
else
{
if(start+1>end)
break;
// f.val=atof(boost::apply_visitor(ToString(),*(list->val[start])));
*v=*(list->val[start]);
}
if(ZInterp :: isContinue)
{
ZInterp :: isContinue=ZBFalse;
continue ;
}
if(ZInterp::isExit)
{
ZInterp::actual++;
if(ZInterp::actual==ZInterp::loopNum)
{
ZInterp::actual=0;
ZInterp::isExit = ZBFalse ;
}
break ;
}
SEEK ( fexpr ) ;
MATCHT ( EFOR_EXP , NULL ) ;
MATCHT ( ANTLR3_TOKEN_DOWN , NULL ) ;
xyz -> expr_g ( xyz ) ;
}
SEEK ( fend ) ;
MATCHT ( EFOR_END , NULL ) ;
}
void DoExpr::Exec ( pANTLR3_BASE_TREE dnode , pANTLR3_BASE_TREE cond , yatgFW_Ctx_struct* xyz )
{
ANTLR3_MARKER dexpr=(((pANTLR3_BASE_TREE)dnode->getChild(dnode,1))->savedIndex);
ANTLR3_MARKER dcond=(((pANTLR3_BASE_TREE)dnode->getChild(dnode,0))->savedIndex);
ANTLR3_MARKER dend=(((pANTLR3_BASE_TREE)dnode->getChild(dnode,2))->savedIndex);
do
{
SEEK ( dexpr ) ;
MATCHT ( EDO_EXP , NULL ) ;
MATCHT ( ANTLR3_TOKEN_DOWN , NULL ) ;
xyz -> expr_g ( xyz ) ;
SEEK ( dcond ) ;
MATCHT ( EDO_CON , NULL ) ;
MATCHT ( ANTLR3_TOKEN_DOWN , NULL ) ;
cond = ( xyz -> expr_g ( xyz ) ) . start ;
}
while(boost :: apply_visitor ( BoolVal ( ) , * ( ( ZTvarp ) ( cond -> u ) ) ) == ZBTrue );
SEEK(dend);
MATCHT(EDO_END,NULL);
}
void ContextExpr::Exec(pANTLR3_BASE_TREE timeNode,yatgFW_Ctx_struct* )
{
currentFrame = (int)gFLOAT_ZCONV(*(ZTvarp)((( pANTLR3_BASE_TREE ) timeNode -> getChild ( timeNode , 2 ))->u)).cont->val;
}
void ContextExpr::Flush()
{
currentFrame = 0;
}
void CaseExpr::Exec(pANTLR3_BASE_TREE caseNode, yatgFW_Ctx_struct * xyz)
{
int cnt = ((caseNode))->children->count;
ANTLR3_MARKER cend = (((pANTLR3_BASE_TREE)caseNode->getChild(caseNode,cnt-1))->savedIndex);
pANTLR3_BASE_TREE t = (pANTLR3_BASE_TREE)((pANTLR3_BASE_TREE)caseNode->getChild(caseNode,0));
pANTLR3_BASE_TREE p=(pANTLR3_BASE_TREE)t->getChild(t,0);
ZChar* var = getNodeText((pANTLR3_BASE_TREE)p->getChild(p,0));
ZTvarp x= ZInterp::ZSym.getSymbol(var,ZBTrue);
int c=1;
//while((pANTLR3_BASE_TREE)(caseNode)->getChild(caseNode,c)!=NULL)
for(int j=0;j<cnt-2;j++)
{
ANTLR3_MARKER item=(((pANTLR3_BASE_TREE)caseNode->getChild(caseNode,c))->savedIndex);
SEEK(item);
MATCHT(CASE_Item,NULL);
MATCHT ( ANTLR3_TOKEN_DOWN , NULL ) ;
pANTLR3_BASE_TREE cur=((pANTLR3_BASE_TREE)((pANTLR3_BASE_TREE)(caseNode)->getChild(caseNode,c))->getChild((pANTLR3_BASE_TREE)(caseNode)->getChild(caseNode,c),0));
char* curStr=getNodeText(cur);
if(strcmp(curStr,"DEFAULT")==0)
{
pANTLR3_BASE_TREE tt = ( pANTLR3_BASE_TREE ) caseNode -> getChild ( caseNode , c ) ;
pANTLR3_BASE_TREE tt1 = ( pANTLR3_BASE_TREE ) tt -> getChild ( tt , 1 ) ;
SEEK ( tt1-> savedIndex);
caseScope=true;
xyz -> expr_g ( xyz );
caseScope=false;
break;
}
else
{
xyz->expr_g(xyz);
ZTvarp y = ((ZTvarp)cur->u);
string str=boost::apply_visitor(ToString(),*y);
ZTvarp var=ZAlloc(ZTvar,1);
*var = boost::apply_visitor(Boolean::Equal(),*x,*y);
string str1=boost::apply_visitor(ToString(),*var);
if(BOOL_ZCONV(*var))
{
pANTLR3_BASE_TREE tt = ( pANTLR3_BASE_TREE ) caseNode -> getChild ( caseNode , c ) ;
pANTLR3_BASE_TREE tt1 = ( pANTLR3_BASE_TREE ) tt -> getChild ( tt , 1 ) ;
SEEK ( tt1-> savedIndex);
caseScope=true;
xyz -> expr_g ( xyz );
caseScope=false;
//MATCHT ( ANTLR3_TOKEN_UP , NULL ) ;
break;
}
//MATCHT ( ANTLR3_TOKEN_UP , NULL ) ;
}
c ++ ;
}
SEEK(cend);
//MATCHT(ANTLR3_TOKEN_UP,NULL);
MATCHT(ECASE_END,NULL);
}
};