static	char	sccsid[]="%Z% %M% %I% %E% %U%";
/**********************************************************************
*                                                                     *
*      処理目的　　：  比較演算処理 	　                            *
*                                                                     *
*      関数名　　　：　int cl_if_comp_ctl( pLeaf )		              *
*                                                                     *
*      引き数      ：　(I)Leaf          * pLeaf                       *
*                                                                     *
*                                                                     *
*      戻り値　　　：　ERROR                                          *
*                      NORMAL                                         *
*                                                                     *
*      処理概要　　：　                                               *
*                                                                     *
**********************************************************************/
#include <colmn.h>

extern GlobalCt  *pGlobTable;
extern CLCOMMON  CLcommon;
extern int giOptions[];

static int iEROUT_NDEF;

int cl_if_comp_ctl(pLeaf,proc)
Leaf    *pLeaf;
ProcCT  *proc;
{
	int  	Flag;

	if (pLeaf->cmd.prmnum > 0) {
		if ((Flag = cl_if_compare(pLeaf,proc)) < NORMAL)
			return ERROR;
	}
	else {
		ERROROUT1(FORMAT(44),"cl_if_comp_ctl");	/* %s: パラメータが必要です。 */
		Flag  = ERROR;
	}

	return Flag;
}

int cl_is_true(pInfoParm)
tdtInfoParm *pInfoParm;
{
/*	int rc,Ans,atr;
	double dVal;
	char *pWork;	*/

DEBUGOUT_InfoParm(190,"cl_is_true: ",pInfoParm,0,0);

#if 1	/* 2022.6.9 */
	return cl_get_logic_val(pInfoParm);
#else
	atr   = pInfoParm->pi_attr;
	pWork = pInfoParm->pi_data;
	if (pInfoParm->pi_dlen == 0) rc = L_OFF;
	else if (atr == DEF_ZOK_BINA) {
		Ans = cl_get_data_long(pInfoParm);
		if (Ans) rc = L_ON;
		else rc = L_OFF;
	}
	else if (atr == DEF_ZOK_FLOA) {
		memcpy(&dVal,pWork,sizeof(double));
		if (dVal == 0.0) rc = L_OFF;
		else rc = L_ON;
	}
	else if (atr == DEF_ZOK_DECI) {
		if (m_is_mpa0((MPA *)pWork)) rc = L_OFF;
		else rc = L_ON;
	}
	else {
		rc = L_ON;
	}
	return rc;
#endif
}

int cl_if_compare_sub(nparm,prmp,Obj)
int nparm;
parmList *prmp[];
int      *Obj;
{
	int rc,Ans;
	double dVal;
	char *pWork;
	tdtInfoParm InfoParm1;

#if 1	/* 2021.12.6 */
	if (cl_get_option(24,0) & 0x01)
		rc = cl_gx_exp_tree_obj(nparm,prmp,Obj,&InfoParm1);
	else
		rc = cl_gx_exp_obj(nparm,prmp,Obj,&InfoParm1);
#else
#if 1	/* 2021.9.29 */
	rc = cl_gx_exp_tree_obj(nparm,prmp,Obj,&InfoParm1);
#else
	rc = cl_gx_exp_obj(nparm,prmp,Obj,&InfoParm1);
#endif
#endif
	pWork = InfoParm1.pi_data;
	if (!rc) {
#if 1	/* 2019.4.7 */
		rc = cl_is_true(&InfoParm1);
#else
		if (InfoParm1.pi_dlen == 0) rc = L_OFF;
		else if (InfoParm1.pi_attr == DEF_ZOK_BINA) {
			Ans = cl_get_data_long(&InfoParm1);
			if (Ans) rc = L_ON;
			else rc = L_OFF;
		}
		else if (InfoParm1.pi_attr == DEF_ZOK_FLOA) {
			memcpy(&dVal,pWork,sizeof(double));
			if (dVal == 0.0) rc = L_OFF;
			else rc = L_ON;
		}
		else if (InfoParm1.pi_attr == DEF_ZOK_DECI) {
			if (m_is_mpa0((MPA *)pWork)) rc = L_OFF;
			else rc = L_ON;
		}
		else {
			rc = L_ON;
		}
#endif
	}
	else if (rc == 100) rc = L_OFF;
	return rc;
}

int cl_if_compare(pLeaf,proc)
Leaf    *pLeaf;
ProcCT  *proc;
{
	return cl_if_compare_sub(pLeaf->cmd.prmnum,pLeaf->cmd.prmp,proc->Obj);
}

/****************************************/
/*										*/
/****************************************/
int cl_gx_exp_tree_obj(nparm,prmp,Obj,pInfoParmW)
int nparm;
parmList  *prmp[];
tdtInfoParm **Obj;
tdtInfoParm *pInfoParmW;
{
DEBUGOUTL2(150,"cl_gx_exp_obj: nparm=%d prmp[0]->opt=0x%08x",nparm,prmp[0]->opt);
#if 1	/* 2021.11.13 */
	return cl_gx_exp_obj_opt_tree(nparm,prmp,Obj,pInfoParmW,0,1);
#else
	return cl_gx_exp_tree_obj_opt(nparm,prmp,Obj,pInfoParmW,0);
#endif
}

#if 1	/* 2021.11.1 */
int cl_gx_ex_tree(pObj0,pGxObj,pInfoParmW,iOpt)
#else
int cl_gx_ex_tree(tree,pObj0,pGxObj,pInfoParmW,iOpt)
WHERE_TREE *tree;
#endif
tdtInfoParm **pObj0;
GXObject *pGxObj;
tdtInfoParm *pInfoParmW;
int iOpt;
{
	int mc1,mc2;
	int a1,a2;
	int a,b,j,v,t;
	char *ap,*bp,w1[13],w2[13];
	static MCAT mcat={'M','C',256,0,0,0,NULL,0};
	static char *name="cl_gx_ex_tree";
	int ida;
	char **da,**obj_stack;
	short *obj;
	int iob;
	int irs,ir2s,irs_max;
	int iMAX_PMSTK,iMAX_STACK;
	MCAT Ms_pp2stk,Ms_ppmstk,Ms_pmstk,Ms_ppmstack,Ms_stack,Ms_dastk;
	MCAT *Msa[7];
	tdtInfoParm **pp2stk;
	tdtInfoParm **ppmstk;
	tdtInfoParm *pmstk,**ppmstack;
	short *stack;
	char **dastk;
	char *da1,*da2,*da3,id,id2,*p1,*p2;
	int i,scale,rc,opt0,optW,opt1,im,val,opt2,val1;
	char pOperator[32], *pWork, *p, c;
	tdtInfoParm InfoParm1,*pInfoParm1,InfoParmW2[2];
	tdtInfoParm InfoParm2,*pInfoParm2,*pInfoParm,**obj0;
	tdtInfoParm InfoParm3,*pInfoParm3;
	parmList  *prmL, qprmL;
	int sp,atr,attr0,i1,i2,amc,mc,atrn,iFN_level,iAR_level,len,len1,len2,aux1,iDEF_ZOK_DATA,type;
	int ib,ib0,kind,iva[10];
	tdtArrayIndex *pIndex;
#if 1	/* 2021.11.1 */
	WHERE_TREE *tree=pGxObj->tree;
#endif

	if (!pGxObj) return -1;
	iob = pGxObj->nobj;
DEBUGOUTL3(110,"cl_gx_ex_obj: *** start nobj=%d iOpt=%08x pInfoParmW=%08x",iob,iOpt,pInfoParmW);
	if (iob<1) return 100;
	obj = pGxObj->obj;
	if (iob>=1 && obj[0]==99) return 100;
/*	da  = pGxObj->da;
	obj0 = pObj0;	*/

	iMAX_PMSTK = (iob+1)/2;
	iMAX_STACK = iMAX_PMSTK;
#if 1	/* 2021.11.1 */
	if (!(obj_stack = _mk_obj_stack(pGxObj->nobj))) return ECL_MALLOC_ERROR;
#else
	if (rc=_mk_obj_stack(pGxObj)) return rc;
	obj_stack = pGxObj->stack;
#endif
	pp2stk = (tdtInfoParm **)obj_stack[0];
	ppmstk = (tdtInfoParm **)obj_stack[1];
	pmstk = (tdtInfoParm *)obj_stack[2];
	ppmstack = (tdtInfoParm **)obj_stack[3];
	stack = (short *)obj_stack[4];
	dastk = (char **)obj_stack[5];
DEBUGOUTL2(120,"cl_gx_ex_obj: pGxObj->index0=%08x dastk=%08x",pGxObj->index0,dastk);
/*	if (pGxObj->index0) {
		if (pGxObj->index0 & 0x01)	*/
			cl_gx_init_set_expand(&Ms_pp2stk,"P2",sizeof(tdtInfoParm *),iMAX_PMSTK,pp2stk);
	/*	if (pGxObj->index0 & 0x02) {	*/
			cl_gx_init_set_expand(&Ms_ppmstack,"PS",sizeof(tdtInfoParm *),iMAX_PMSTK,ppmstack);
			cl_gx_init_set_expand(&Ms_stack,"ST",sizeof(short),iMAX_STACK,stack);
	/*	}
		if (pGxObj->index0 & 0x04) {	*/
			cl_gx_init_set_expand(&Ms_ppmstk,"PM",sizeof(tdtInfoParm *),iMAX_PMSTK,ppmstk);
			cl_gx_init_set_expand(&Ms_pmstk,"MS",sizeof(tdtInfoParm),iMAX_PMSTK,pmstk);
			cl_gx_init_set_expand(&Ms_dastk,"DS",sizeof(char *),iMAX_PMSTK,dastk);
	/*	}
	}	*/
	Msa[0] = &Ms_pp2stk;
	Msa[1] = &Ms_ppmstk;
	Msa[2] = &Ms_pmstk;
	Msa[3] = &Ms_ppmstack;
	Msa[4] = &Ms_stack;
	Msa[5] = &Ms_dastk;
	Msa[6] = &mcat;

	if (!pInfoParmW) pInfoParmW = InfoParmW2;
	memset(pInfoParmW,0,sizeof(tdtInfoParm));
	if (iOpt & D_GX_OPT_PARMINFO2) memset(pInfoParmW+1,0,sizeof(tdtInfoParm));
	stack[0]=0;
	mem_set_int(iva,0,9);

#if 1	/* 2021.11.1 */
	rc = cl_gx_ex_tree_sub(0,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva);
#else
	rc = cl_gx_ex_tree_sub(0,tree,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva);
#endif
	return rc;
}

#if 1	/* 2021.11.1 */
int cl_gx_ex_tree_sub(is,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva)
int is;
#else
int cl_gx_ex_tree_sub(is,tree,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva)
int is;
WHERE_TREE *tree;
#endif
tdtInfoParm **pObj0;
GXObject *pGxObj;
tdtInfoParm *pInfoParmW;
int iOpt,iva[];
MCAT *Msa[];
{
	static char *name="cl_gx_ex_tree_sub";
	short *obj;
	int mc,mc1,mc2,mc3,i1,i2,i3,rc,i,lv,rv,ii,mca[2],ia[2],ib,j,n,mc0;
	long Value1,Value2;
	tdtInfoParm tInfoParm1,tInfoParm2;
#if 1	/* 2021.11.1 */
	WHERE_TREE *tree;
#endif

	tree = pGxObj->tree;
	mc = tree[is].mcode;
	i1 = tree[is].t1;
	i2 = tree[is].t2;
	lv = tree[is].lv;
	rv = tree[is].rv;
	i3 = tree[is].t3;

DEBUGOUTL2(161,"cl_gx_ex_tree_sub:Enter is=%d mc=%d ",is,mc);
DEBUGOUTL5(161,"cl_gx_ex_tree_sub:Enter i1=%d i2=%d lv=%d rv=%d i3=%d",i1,i2,lv,rv,i3);

	if (i1) mc1 = tree[i1].mcode;
	else mc1 = 0;

	if (i2) mc2 = tree[i2].mcode;
	else mc2 = 0;
/*
printf("cl_gx_ex_tree_sub: mc1=%d tree[i1].t1=%d mc2=%d tree[i2].t1=%d\n",mc1,tree[i1].t1,mc2,tree[i2].t1);
*/
/*
printf("Enter any key.");getchar();
*/
#if 1	/* 2022.1.26 */
	if (!i3 && (mc==3 || (mc>=5 && mc<=7) || mc==99)) {
#else
	if (mc==3 || (mc>=5 && mc<=7) || mc==99) {
#endif
		ii=lv;
		for (i=0;i<rv;i++,ii++) {
/*
printf("cl_gx_ex_tree_sub: tree[ii].mcode=%d tree[ii].lv=%d\n",tree[ii].mcode,tree[ii].lv);
*/
			iva[0] = tree[ii].mcode;
			iva[1] = tree[ii].lv;
			if ((rc=cl_gx_ex_obj_sub(pObj0,pGxObj,&tInfoParm1,iOpt,Msa,iva)) < 0) return rc;
		}
	}
	mca[0] = mc1;
	mca[1] = mc2;
	ia[0] = i1;
	ia[1] = i2;
	if (mc == 59) {
		if (rv = tree[is].rv) {
			iva[0] = tree[rv].mcode;
			iva[1] = tree[rv].lv;
			if ((rc=cl_gx_ex_obj_sub(pObj0,pGxObj,&tInfoParm1,iOpt,Msa,iva)) < 0) return rc;
		}
	}
	for (i=0;i<2;i++) {
		mc1 = mca[i];
		i1 = ia[i];
		lv = tree[i1].lv;
		if ((rc=cl_gx_ex_tree_t(mc,mc1,i1,lv,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva)) < 0) return rc;
		if (rc == 1) break;
	}
#if 0
	if (mc==6) {
		rv = tree[is].rv;
		lv = tree[is].lv;
		ii=lv;
		for (i=0;i<rv;i++,ii++) {
/*
printf("cl_gx_ex_tree_sub: tree[ii].mcode=%d tree[ii].lv=%d\n",tree[ii].mcode,tree[ii].lv);
*/
			iva[0] = tree[ii].mcode;
			iva[1] = tree[ii].lv;
			if ((rc=cl_gx_ex_obj_sub(pObj0,pGxObj,&tInfoParm1,iOpt,Msa,iva)) < 0) return rc;
		}
	}
#endif
	if (rc == 1) mc = 53;	/* 2項 SET */
#if 1	/* 2022.1.26 */
	else if (i3 && (mc==1 || mc==3 || (mc>=5 && mc<=7))) {
		for (i=is-1;i>=i3;i--) {
#if 1
			obj = (short *)&tree[i];
			n = obj[1];
			j = n + 1;
/*
printf("cl_gx_ex_tree_sub: i=%d n=%d j=%d\n",i,n,j);
*/
			while (n-- > 0) {
				ii = obj[j--];
				mc0 = 0;
				mc1 = tree[ii].mcode;
			/*	i1  = tree[ii].t1;	*/
				lv  = tree[ii].lv;
/*
printf("cl_gx_ex_tree_sub: ii=%d mc1=%d lv=%d\n",ii,mc1,lv);
*/
				if ((rc=cl_gx_ex_tree_t(mc0,mc1,ii,lv,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva)) < 0) return rc;
			}
#else
			iva[0] = tree[i].mcode;
			iva[1] = tree[i].lv;
			if ((rc=cl_gx_ex_tree_sub(i,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva)) < 0) return rc;
#endif
		}
	}
#endif
	iva[0] = mc;
	ib = tree[is].lv;
	iva[1] = ib;
	rc = cl_gx_ex_obj_sub(pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva);
	if (rc==0 && mc==59) {
/*
printf("cl_gx_ex_tree_sub: ib+2=%d iva[1]=%d iva[8]=%d\n",ib+2,iva[1],iva[8]);
*/
		i3 = tree[is].t3;
/*
printf("mcode=%d t1=%d t2=%d lv=%d rv=%d t3=%d\n",
tree[i3].mcode,tree[i3].t1,tree[i3].t2,tree[i3].lv,tree[i3].rv,tree[i3].t3);
*/
		if (i3 > 0) {
			if (iva[8])
				i2 = tree[i3].t1;
			else 
				i2 = tree[i3].t2;
			if (i2) mc2 = tree[i2].mcode;
			else mc2 = 0;
			if (mc2) {
				lv = tree[i2].lv;
				rc = cl_gx_ex_tree_t(mc,mc2,i2,lv,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva);
			}
		}
	}
/*
printf("cl_gx_ex_tree_sub:Exit mc=%d rc=%d\n",mc,rc);
*/
	return rc;
}

int cl_gx_ex_tree_t(mc,mc1,i1,lv,pObj0,pGxObj,pInfoParmW,iOpt,Msa,iva)
int mc,mc1,i1,lv;
tdtInfoParm **pObj0;
GXObject *pGxObj;
tdtInfoParm *pInfoParmW;
int iOpt,iva[];
MCAT *Msa[];
{
	int rc;
	long Value1;
	tdtInfoParm tInfoParm1;

DEBUGOUTL4(161,"cl_gx_ex_tree_t:Enter mc=%d mc1=%d i1=%d lv=%d",mc,mc1,i1,lv);

	rc = 0;
	if (mc1==91 || mc1==92) {
/*
printf("cl_gx_ex_tree_t: mc1=%d lv=%d\n",mc1,lv);
*/
		iva[0] = mc1;
		iva[1] = lv;	/*tree[i1].lv;*/
		if ((rc=cl_gx_ex_obj_sub(pObj0,pGxObj,&tInfoParm1,iOpt,Msa,iva)) < 0) return rc;
	}
	else if (mc1) {
		if ((rc=cl_gx_ex_tree_sub(i1,pObj0,pGxObj,&tInfoParm1,iOpt,Msa,iva)) < 0) return rc;
		if (mc==51 || mc==52) {	/* && || */
			Value1 = cl_get_logic_val(&tInfoParm1);
			if (mc==51 && !Value1) {
				cl_set_parm_bin(pInfoParmW,0);
				rc = 1;
			}
			else if (mc==52 && Value1) {
				cl_set_parm_bin(pInfoParmW,1);
				rc = 1;
			}
		}
	}
/*
printf("cl_gx_ex_tree_t:Exit mc=%d mc1=%d rc=%d\n",mc,mc1,rc);
*/
	return rc;
}
