-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpression_Parsing.java
More file actions
116 lines (115 loc) · 5.53 KB
/
Expression_Parsing.java
File metadata and controls
116 lines (115 loc) · 5.53 KB
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
class Expression_Parsing
{
Function_Evaluate call=new Function_Evaluate();
// Evaluate using recursion
public StringBuffer evaltr(StringBuffer fneq)throws Exception
{
StringBuffer g=new StringBuffer("");
for(int i=0;i<fneq.length();i++)
{
if(fneq.charAt(i)=='f')
{
int nf=0,nf1=0,nf2=0,inx=i+1,evlt=1;
for(int k=i+1;k<fneq.length();k++)
{
if(fneq.charAt(k)=='f')
{
nf++;
inx=k;
break;
}
}
int cdpt=0,strt=i+1,mid=i+1,end=i+1;
if(nf!=0)
{
for(int j=i+1;j<fneq.length();j++)
{
if(fneq.charAt(j)=='(') cdpt++;
else if(fneq.charAt(j)==')') cdpt--;
if(fneq.charAt(j)==',')
if(cdpt==1) mid=j;
}
}
if(nf!=0)
{
for(int j=i+1;j<fneq.length();j++)
{
if(fneq.charAt(j)=='(')
{
if(cdpt==0) strt=j;
cdpt++;
}
else if(fneq.charAt(j)==')')
{
cdpt--;
nf1=0;
nf2=0;
if(cdpt==1)
{
for(int k=inx;k<mid;k++)
{
if(fneq.charAt(k)=='f')
nf1++;
}
for(int k=(mid+1);k<fneq.length();k++)
{
if(fneq.charAt(k)=='f')
nf2++;
}
if(nf1==0 || nf2==0)
fneq=(new StringBuffer(fneq.substring(0,inx))).append(evaltr(new StringBuffer(fneq.substring(inx,j+1)))).append(new StringBuffer(fneq.substring(j+1)));
else
fneq=(new StringBuffer(fneq.substring(0,inx))).append(evaltr(new StringBuffer(fneq.substring(inx,mid)))).append(",").append(evaltr(new StringBuffer(fneq.substring(mid+1,fneq.length()-1)))).append(new StringBuffer(fneq.substring(fneq.length()-1)));
evlt=1;
}
}
}
}
if(evlt==1) //fneq doesn't have any more nested function
{
for(int j=i+1;j<fneq.length();j++)
{
if(fneq.charAt(j)=='(')
{
strt=j;
break;
}
}
for(int j=i+1;j<fneq.length();j++)
{
if(fneq.charAt(j)==',')
{
mid=j;
break;
}
}
for(int j=i+1;j<fneq.length();j++)
{
if(fneq.charAt(j)==')')
{
end=j;
break;
}
}
switch(Integer.parseInt(fneq.substring(i+1,strt)))
{
case 1: g=call.f1(new StringBuffer(fneq.substring(strt+1,mid)),new StringBuffer(fneq.substring(mid+1,end)));
break;
case 2: g=call.f2(new StringBuffer(fneq.substring(strt+1,mid)),new StringBuffer(fneq.substring(mid+1,end)));
break;
case 3: g=call.f3(new StringBuffer(fneq.substring(strt+1,mid)),new StringBuffer(fneq.substring(mid+1,end)));
break;
case 4: g=call.f4(new StringBuffer(fneq.substring(strt+1,mid)),new StringBuffer(fneq.substring(mid+1,end)));
break;
case 5: g=call.f5(new StringBuffer(fneq.substring(strt+1,mid)),new StringBuffer(fneq.substring(mid+1,end)));
break;
case 6: g=call.f6(new StringBuffer(fneq.substring(strt+1,mid)),new StringBuffer(fneq.substring(mid+1,end)));
break;
}
return g;
}
}
}
return fneq;
}
}