-
Notifications
You must be signed in to change notification settings - Fork 11
/
pref2inf.m
189 lines (148 loc) · 5.42 KB
/
pref2inf.m
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
function [sendup,status] = pref2inf(expr,gp)
%PREF2INF Recursively extract arguments from a prefix expression and convert to infix where possible.
%
% [SENDUP,STATUS] = PREF2INF(EXPR,GP)
%
% Copyright (c) 2009-2015 Dominic Searson
%
% GPTIPS 2
%
% See also PICKNODE, EXTRACT, GPREFORMAT, TREE2EVALSTR
%get indices of all nodes in current expression
ind = picknode(expr,6,gp);
if isempty(ind)
sendup = expr; %exits if current expression has no further extractable args
status = 0;
return
end
%get first node
ind = ind(1);
%get number of arguments of node
try
args = gp.nodes.functions.arity_argt0(strfind(gp.nodes.functions.afid,expr(ind)));
if isempty(args)
% if has zero arity then exit
sendup = expr;
status = 0;
return;
end
catch
%also exit if error
sendup = expr;
status = 0;
return;
end
if args == 1
%get subtree rooted in this node
[~,subtree] = extract(ind,expr);
node = subtree(1);
%get index of 2nd node in 'sub_tree' (i.e. first logical argument)
keynodeInd = picknode(subtree,6,gp);
keynodeInd = keynodeInd(2);
%extract the 1st argument expression
[~,arg1] = extract(keynodeInd,subtree);
[rec1,rec1_status] = pref2inf(arg1,gp);
if rec1_status==1
arg1 = rec1;
end
sendup = [node '(' arg1 ')'];
status = 1;
elseif args > 2
%get subtree rooted in this node
[~,subtree] = extract(ind,expr);
node = subtree(1);
%get index of 2nd node in 'sub_tree' (i.e. first logical argument)
keynodeInd = picknode(subtree,6,gp); %
keynodeInd = keynodeInd(2);
%extract the 1st argument expression
[remainder,arg1] = extract(keynodeInd,subtree);
%extract the second argument expression from the remainder
%find the 1st node after $ in the remainder as this will be the
%keynode of the 2nd argument we wish to extract
keynodeInd = picknode(remainder,6,gp);
tokenInd = strfind(remainder,'$');
keynodeLoc = find(keynodeInd > tokenInd);
keynodeInd = keynodeInd(keynodeLoc);
keynode_ind_1 = keynodeInd(1);
[remainder2,arg2] = extract(keynode_ind_1,remainder);
%extract the third argument expression from the remainder
%find the 1st node after $ in remainder2 as this will be the keynode of
%the 3nd argument we wish to extract
keynodeInd = picknode(remainder2,6,gp);
tokenInd = strfind(remainder2,'$');
keynodeLoc = find(keynodeInd > max(tokenInd));
keynodeInd = keynodeInd(keynodeLoc);
keynode_ind_1 = keynodeInd(1);
[remainder3,arg3] = extract(keynode_ind_1,remainder2);
[rec1,rec1_status] = pref2inf(arg1,gp);
[rec2,rec2_status] = pref2inf(arg2,gp);
[rec3,rec3_status] = pref2inf(arg3,gp);
if rec1_status==1
arg1 = rec1;
end
if rec2_status==1
arg2 = rec2;
end
if rec3_status==1
arg3 = rec3;
end
sendup = [node '(' arg1 ',' arg2 ',' arg3 ')'];
status = 1;
if args > 3
%extract the fourth argument expression from the remainder
%find the 1st node after $ in remainder3 as this will be the
%keynode of the 4nd argument we wish to extract
keynodeInd = picknode(remainder3,6,gp);
tokenInd = strfind(remainder3,'$');
keynodeLoc = find(keynodeInd > max(tokenInd));
keynodeInd = keynodeInd(keynodeLoc);
keynode_ind_1 = keynodeInd(1);
[~,arg4] = extract(keynode_ind_1,remainder3);
[rec4,rec4_status] = pref2inf(arg4,gp);
if rec4_status==1
arg4 = rec4;
end
sendup = [node '(' arg1 ',' arg2 ',' arg3 ',' arg4 ')'];
status = 1;
end
else %must have exactly 2 args
ind = picknode(expr,6,gp);
%get subtree rooted in this node
[maintree,subtree] = extract(ind,expr);
node = subtree(1);
%get index of 2nd node in 'sub_tree' (i.e. first logical argument)
keynodeInd = picknode(subtree,6,gp); %
keynodeInd = keynodeInd(2);
%extract the 1st argument expression
[remainder,arg1] = extract(keynodeInd,subtree);
%extract the second argument expression from the remainder
%find the 1st node after $ in the remainder as this will be the
%keynode of the 2nd argument we wish to extract
keynodeInd = picknode(remainder,6,gp);
tokenInd = strfind(remainder,'$');
keynodeLoc = find(keynodeInd>tokenInd);
keynodeInd = keynodeInd(keynodeLoc);
keynode_ind_1 = keynodeInd(1);
[~,arg2] = extract(keynode_ind_1,remainder);
%process arguments of these arguments if any exist
[rec1,rec1_status] = pref2inf(arg1,gp);
[rec2,rec2_status] = pref2inf(arg2,gp);
if rec1_status==1
arg1 = rec1;
end
if rec2_status==1
arg2 = rec2;
end
%If the node is of infix type (for Matlab symbolic purposes)
% then send up the results differently
afid_ind = strfind(gp.nodes.functions.afid, node);
nodename = gp.nodes.functions.active_name_UC{afid_ind};
if strcmpi(nodename,'times') || strcmpi(nodename,'minus') || ...
strcmpi(nodename,'plus') || strcmpi(nodename,'rdivide') || strcmpi(nodename,'power')
sendup = ['(' arg1 ')' node '(' arg2 ')'];
else
sendup = [node '(' arg1 ',' arg2 ')'];
end
sendup = strrep(maintree,'$',sendup);
status = 1; %i.e. ok
end