/lib/gptips1.0/pref2inf.m

http://calib.googlecode.com/ · Objective C · 196 lines · 146 code · 50 blank · 0 comment · 20 complexity · 150aede52120fd28856528f50be6262c MD5 · raw file

  1. function [sendup,sendup_status]=pref2inf(expr,gp)
  2. %PREF2INF Utility function to recursively extract arguments from a prefix
  3. %expression and convert to infix where possible.
  4. %
  5. % (c) Dominic Searson 2009
  6. %
  7. % v1.0
  8. %
  9. % See also: PICKNODE, EXTRACT
  10. %get indices of nodes of any type
  11. ind=picknode(expr,6,gp);
  12. if isempty(ind)
  13. sendup=expr;%exits if current expression has no further extractable args
  14. sendup_status=0;
  15. return
  16. end
  17. %get first node
  18. ind=ind(1);
  19. %get number of arguments of node
  20. try
  21. args=gp.nodes.functions.arity_argt0(findstr(gp.nodes.functions.afid,expr(ind)));
  22. if isempty(args)
  23. % if has zero arity then exit
  24. sendup=[expr];
  25. sendup_status=0;
  26. return;
  27. end
  28. catch
  29. %also exit if error
  30. sendup=[expr];
  31. sendup_status=0;
  32. return;
  33. end
  34. if args==1
  35. %get subtree rooted in this node
  36. [main_tree,sub_tree]=extract(ind,expr);
  37. node=sub_tree(1);
  38. %get index of 2nd node in 'sub_tree' (i.e. first logical argument)
  39. keynode_ind=picknode(sub_tree,6,gp); %
  40. keynode_ind=keynode_ind(2);
  41. %extract the 1st argument expression
  42. [remainder,arg1]=extract(keynode_ind,sub_tree);
  43. [rec1,rec1_status]=pref2inf(arg1,gp);
  44. if rec1_status==1
  45. arg1=rec1;
  46. end
  47. sendup=[node '(' arg1 ')'];
  48. sendup_status=1;
  49. elseif args>2
  50. %get subtree rooted in this node
  51. [main_tree,sub_tree]=extract(ind,expr);
  52. node=sub_tree(1);
  53. %get index of 2nd node in 'sub_tree' (i.e. first logical argument)
  54. keynode_ind=picknode(sub_tree,6,gp); %
  55. keynode_ind=keynode_ind(2);
  56. %extract the 1st argument expression
  57. [remainder,arg1]=extract(keynode_ind,sub_tree);
  58. %extract the second argument expression from the remainder
  59. % find the 1st node after $ in the remainder as this will be the
  60. % keynode of the 2nd argument we wish to extract
  61. keynode_ind=picknode(remainder,6,gp);
  62. token_ind=findstr(remainder,'$');
  63. hib=find(keynode_ind>token_ind);
  64. keynode_ind=keynode_ind(hib);
  65. keynode_ind_1=keynode_ind(1);
  66. [remainder2,arg2]=extract(keynode_ind_1,remainder);
  67. %extract the thirdargument expression from the remainder
  68. % find the 1st node after $ in remainder2 as this will be the keynode of
  69. % the 3nd argument we wish to extract
  70. keynode_ind=picknode(remainder2,6,gp);
  71. token_ind=findstr(remainder2,'$');
  72. hib=find(keynode_ind>max(token_ind));
  73. keynode_ind=keynode_ind(hib);
  74. keynode_ind_1=keynode_ind(1);
  75. [remainder3,arg3]=extract(keynode_ind_1,remainder2);
  76. [rec1,rec1_status]=pref2inf(arg1,gp);
  77. [rec2,rec2_status]=pref2inf(arg2,gp);
  78. [rec3,rec3_status]=pref2inf(arg3,gp);
  79. if rec1_status==1
  80. arg1=rec1;
  81. end
  82. if rec2_status==1
  83. arg2=rec2;
  84. end
  85. if rec3_status==1
  86. arg3=rec3;
  87. end
  88. sendup=[node '(' arg1 ',' arg2 ',' arg3 ')'];
  89. sendup_status=1;
  90. if args>3
  91. %extract the fourth argument expression from the remainder
  92. % find the 1st node after $ in remainder3 as this will be the keynode of
  93. % the 4nd argument we wish to extract
  94. keynode_ind=picknode(remainder3,6,gp);
  95. token_ind=findstr(remainder3,'$');
  96. hib=find(keynode_ind>max(token_ind));
  97. keynode_ind=keynode_ind(hib);
  98. keynode_ind_1=keynode_ind(1);
  99. [remainder4,arg4]=extract(keynode_ind_1,remainder3);
  100. [rec4,rec4_status]=pref2inf(arg4,gp);
  101. if rec4_status==1
  102. arg4=rec4;
  103. end
  104. sendup=[node '(' arg1 ',' arg2 ',' arg3 ',' arg4 ')'];
  105. sendup_status=1;
  106. end
  107. else %must have exactly 2 args
  108. ind=picknode(expr,6,gp);
  109. %get subtree rooted in this node
  110. [main_tree,sub_tree]=extract(ind,expr);
  111. node=sub_tree(1);
  112. %get index of 2nd node in 'sub_tree' (i.e. first logical argument)
  113. keynode_ind=picknode(sub_tree,6,gp); %
  114. keynode_ind=keynode_ind(2);
  115. %extract the 1st argument expression
  116. [remainder,arg1]=extract(keynode_ind,sub_tree);
  117. %extract the second argument expression from the remainder
  118. % find the 1st node after $ in the remainder as this will be the
  119. % keynode of the 2nd argument we wish to extract
  120. keynode_ind=picknode(remainder,6,gp);
  121. token_ind=findstr(remainder,'$');
  122. hib=find(keynode_ind>token_ind);
  123. keynode_ind=keynode_ind(hib);
  124. keynode_ind_1=keynode_ind(1);
  125. [remainder2,arg2]=extract(keynode_ind_1,remainder);
  126. %process arguments of these arguments if any exist
  127. [rec1,rec1_status]=pref2inf(arg1,gp);
  128. [rec2,rec2_status]=pref2inf(arg2,gp);
  129. if rec1_status==1
  130. arg1=rec1;
  131. end
  132. if rec2_status==1
  133. arg2=rec2;
  134. end
  135. %If the node is of infix type (for Matlab symbolic purposes)
  136. % then send up the results differently
  137. afid_ind = findstr(node, gp.nodes.functions.afid);
  138. nodename = gp.nodes.functions.active_name_UC{afid_ind};
  139. if strcmpi(nodename,'times') || strcmpi(nodename,'minus') || strcmpi(nodename,'plus') || strcmpi(nodename,'rdivide')
  140. sendup=['(' arg1 ')' node '(' arg2 ')'];
  141. else
  142. sendup= [node '(' arg1 ',' arg2 ')'];
  143. end
  144. sendup=strrep(main_tree,'$',sendup);
  145. sendup_status=1; %i.e. ok
  146. end