-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZInterpFunction.cpp
139 lines (127 loc) · 4.42 KB
/
ZInterpFunction.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
#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)
#define MARK() ISTREAM->mark(ISTREAM)
#define REWIND(m) ISTREAM->rewind(ISTREAM, m)
#define REWINDLAST() ISTREAM->rewindLast(ISTREAM)
namespace ZInterp
{
void Function :: Defination ( pANTLR3_BASE_TREE funNode , yatgFW_Ctx_struct *xyz )
{
ANTLR3_MARKER funend = ( ( pANTLR3_BASE_TREE ) funNode -> getChild ( funNode , 3 ) ) -> savedIndex ;
pANTLR3_BASE_TREE t2 = ( pANTLR3_BASE_TREE ) funNode -> getChild ( funNode , 0 ) ;
ZChar* fName = getNodeText ( ( pANTLR3_BASE_TREE ) t2 -> getChild ( t2 , 0 ) ) ;
ZTvarp fun;
fun = ZSym . getSymbol ( fName , true ) ;
if ( fun == NULL )
{
fun = ZAlloc(ZTvar,1);
ZIFunction* ifun=ZAlloc(ZIFunction,1);
ifun -> FunData . NodeID = ( funNode ) -> savedIndex ;
ifun -> FunT = ZInternal;
ifun -> NumArgs = ( ( pANTLR3_BASE_TREE ) funNode -> getChild ( funNode , 1 ) ) ->getChildCount(( ( pANTLR3_BASE_TREE ) funNode -> getChild ( funNode , 1 ) ));
*fun=ZTFunction(ifun);
ZSym.InsertSymbol ( fName , fun );
}
SEEK ( funend ) ;
MATCHT ( FUN_DEF_END , NULL ) ;
setCustomNodeField(funNode,fun);
}
/*--------------------------------------------*/
////////////////////////////////////////////////
void CallFunction(ZTvarS& Fargs,ZTvarp zfun,pANTLR3_BASE_TREE t1,pANTLR3_BASE_TREE arg,yatgFW_Ctx_struct* xyz)
{
ZIFunction* var= FUNCTION_ZCONV( *zfun );
switch ( var -> FunT )
{
case ZInternal :
if( Helper :: Function :: ZTestArg ( var , Fargs ) )
{
ANTLR3_MARKER Mst=MARK();
SEEK ( var -> FunData . NodeID ) ;
pANTLR3_BASE_TREE fun =LT(1);
pANTLR3_BASE_TREE argtree = ( pANTLR3_BASE_TREE ) fun -> getChild ( fun , 1 ) ;
pANTLR3_BASE_TREE body = ( pANTLR3_BASE_TREE ) fun -> getChild ( fun , 2 ) ;
int i=0;
ZInterp::ZSym.InitScope();
while(i < var -> NumArgs )
{
ZChar* name = getNodeText ( ( pANTLR3_BASE_TREE ) argtree -> getChild ( argtree , i ) ) ;
ZInterp :: ZSym . currentScope -> VarTable . Insert ( ( ZTvarp ) Fargs [ i ] , name ) ;
i++;
}
SEEK ( body -> savedIndex );
MATCHT(BODY,NULL);
MATCHT(ANTLR3_TOKEN_DOWN,NULL);
ZTvarp ret=ZAlloc(ZTvar,1);
ret=(ZTvarp)((xyz -> expr_g ( xyz )).start->u) ;
ZInterp::setCustomNodeField(t1,ret);
ZInterp::ZSym.FinScope();
REWIND(Mst);
}
else
{
/* Fire an Exception */
std::cout<<"Insufficient Numer of Arguments"<<std::endl;
}
break;
case ZExternal:
ZInterp::setCustomNodeField(t1,(*(var->FunData.pFun))(Fargs));
break;
case ZMExternal:
ZTvarp res = (boost::apply_visitor(ZTCallBridge(var->FunData.pMFun,Fargs), *(var->obj) ));
ZInterp::setCustomNodeField(t1,res);
break;
}
}
void CreateObject(ZTvarS Fargs,ZTvarp zobj,pANTLR3_BASE_TREE t1)
{
ZTOInstance zin;
ZTvarp hv = ZAlloc(ZTvar,1);
zin.val = ZAlloc(ZObjP,1);
gOBJECT_ZCONV(*zobj).cont->cpy(zin.val,Fargs);
*hv=zin;
ZInterp::setCustomNodeField(t1,hv);
}
void Operand::FunCall(pANTLR3_BASE_TREE t1,pANTLR3_BASE_TREE arg,yatgFW_Ctx_struct* xyz)
{
//Collect arguments in a vector before calling
ZTvarS Fargs;
ZTvarp vp;
if(arg->children!=NULL)
{
for ( int i = 0 ; i < arg -> children -> count ; i ++ )
{
vp=ZAlloc(ZTvar,1);
*vp = *((ZTvarp)((pANTLR3_BASE_TREE)arg->getChild(arg,i))->u);
Fargs . push_back ( vp ) ;
}
}
pANTLR3_BASE_TREE t ;
ZTvarp inp=(ZTvarp)(t1->u);
switch( boost::apply_visitor(getType(),*inp) )
{
case ZETObject:
CreateObject(Fargs,inp,t1);
break;
case ZETFunction:
CallFunction(Fargs,inp,t1,arg,xyz);
break;
default:
cout<<"This is not defined Function "<<getNodeText((pANTLR3_BASE_TREE)t1->getChild(t1,0))<<endl;
exit(5);
}
}
////////////////////////////////////////////////
/*--------------------------------------------*/
}