PageRenderTime 665ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/matlab_tools/KhorosToMatLab.old

http://github.com/aludnam/MATLAB
Perl | 451 lines | 428 code | 3 blank | 20 comment | 32 complexity | 922604cf7d371f68376f26a0ea737d99 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #!/usr/bin/perl
  2. # first argument is the .pane file, which is used to generate a matlab script
  3. # second (optional) argument is the corresponding help-file
  4. @lines = `cat $ARGV[0]`;
  5. @fname = split /[\/\.]/,$ARGV[0];
  6. $fname = @fname[-2];
  7. $rname = $fname;
  8. @paramtype = (); @paramtags = (); @paramname = (); @paramdesc = (); @defaults = (); @optional = (); @minval = (); @maxval = ();
  9. @inputtags = (); @inputname = (); @inputdesc = (); @inputdef = ();
  10. @outputtags = (); @outputname = (); @outputdesc = (); @outputdef = ();
  11. $inMultiToggle=0;
  12. $NumReqOutputs=0;
  13. foreach (@lines) {
  14. # print "separating: $_\n";
  15. @fields = split;
  16. @jfields = ();
  17. $instring=0;
  18. $astring="";
  19. # rejoin fields that were protected by '
  20. foreach (@fields) {
  21. if ($instring == 0)
  22. {
  23. # Expression starts with whitespace and a ', but not a protected whitespace
  24. if (/^\s*'/ && !/^\s*\\'/)
  25. {
  26. $instring=1;
  27. $astring=$_;
  28. # same expression also ends with a non-protected ' and trailing whitespace
  29. if (/^\s*'.+'\s*$/ && !/^\s*'.+\\'\s*$/)
  30. {
  31. $instring=0;
  32. push @jfields,$astring;
  33. }
  34. }
  35. else
  36. {
  37. push @jfields,$_;
  38. }
  39. }
  40. else
  41. {
  42. $astring= "$astring $_";
  43. # This is a non-protected ending ' with maybe whitespace
  44. if (/'\s*/ && !/\\'\s*/)
  45. {
  46. $instring=0;
  47. push @jfields,$astring;
  48. }
  49. }
  50. }
  51. if (@jfields[0] eq '-M')
  52. {
  53. $progdescr = @jfields[5];
  54. }
  55. if (@jfields[0] eq '-T')
  56. {
  57. push @paramtype, 'MultiChoice';
  58. push @paramtags, @jfields[-1];
  59. push @paramname, @jfields[-3];
  60. # paramdesc will follow at end
  61. $choices = "@jfields[-2]\n% Choices are:";
  62. push @minval, 0;
  63. push @maxval, 0;
  64. push @defaults, @jfields[-4];
  65. $inMultiToggle=1;
  66. }
  67. if (@jfields[0] eq '-E')
  68. {
  69. if ($inMultiToggle != 0)
  70. {
  71. $inMultiToggle=0;
  72. push @paramdesc, $choices;
  73. }
  74. }
  75. if (@jfields[0] eq '-t')
  76. {
  77. if ($inMultiToggle == 0)
  78. {
  79. push @paramtype, 'Toggle';
  80. push @paramtags, @jfields[-1];
  81. push @paramname, @jfields[-3];
  82. push @paramdesc, @jfields[-2];
  83. push @minval, 0;
  84. push @maxval, 0;
  85. push @defaults, @jfields[-5];
  86. }
  87. else
  88. {
  89. $choices = "$choices\n% $inMultiToggle: @jfields[-3]";
  90. $inMultiToggle += 1;
  91. }
  92. }
  93. if (@jfields[0] eq '-s')
  94. {
  95. push @paramtype, 'String';
  96. push @paramtags, @jfields[-1];
  97. push @paramname, @jfields[-3];
  98. push @paramdesc, @jfields[-2];
  99. push @minval, 0;
  100. push @maxval, 0;
  101. push @defaults, @jfields[-4];
  102. }
  103. if (@jfields[0] eq '-i')
  104. {
  105. push @paramtype, 'Integer';
  106. push @paramtags, @jfields[-1];
  107. push @paramname, @jfields[-3];
  108. push @paramdesc, @jfields[-2];
  109. push @minval, @jfields[-8];
  110. push @maxval, @jfields[-7];
  111. push @defaults, @jfields[-6];
  112. }
  113. if (@jfields[0] eq '-f')
  114. {
  115. push @paramtype, 'Double';
  116. push @paramtags, @jfields[-1];
  117. push @paramname, @jfields[-3];
  118. push @paramdesc, @jfields[-2];
  119. push @minval, @jfields[-9];
  120. push @maxval, @jfields[-8];
  121. push @defaults, @jfields[-7];
  122. }
  123. if (@jfields[0] eq '-I')
  124. {
  125. push @inputtags, @jfields[-1];
  126. push @inputname, @jfields[-3];
  127. push @inputdesc, @jfields[-2];
  128. push @inputdef, @jfields[-4];
  129. push @paramtype, 'InputFile';
  130. push @paramtags, @jfields[-1];
  131. push @paramname, @jfields[-3];
  132. push @paramdesc, @jfields[-2];
  133. push @minval, @jfields[3];
  134. push @maxval, @jfields[3];
  135. push @defaults, '\'__input\'';
  136. }
  137. if (@jfields[0] eq '-O')
  138. {
  139. push @outputtags, @jfields[-1];
  140. push @outputname, @jfields[-3];
  141. push @outputdesc, @jfields[-2];
  142. push @outputdef, @jfields[-4];
  143. push @paramtype, 'OutputFile';
  144. push @paramtags, @jfields[-1];
  145. push @paramname, @jfields[-3];
  146. push @paramdesc, @jfields[-2];
  147. push @minval, @jfields[3];
  148. push @maxval, @jfields[3];
  149. push @defaults, '\'__output\'';
  150. if (@jfields[3] == 0) # required output
  151. {$NumReqOutputs += 1;}
  152. }
  153. if (@jfields[0] eq '-R')
  154. {
  155. $requiredParams = "$requiredParams @jfields[8]";
  156. @myfields = split /\//,@jfields[7];
  157. $rname = @myfields[-1];
  158. }
  159. # $j=0; foreach (@jfields) { print "item $j: $_\n"; $j += 1; }
  160. }
  161. # now it's time to generate the MatLab program
  162. print "%k$fname $progdescr\n";
  163. print "% This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros $fname.pane file\n";
  164. print "%\n% Parameters: \n";
  165. $i=0;
  166. foreach (@paramtags) {
  167. $mydefault=@defaults[$i];
  168. if ($mydefault eq '\'__input\'' || $mydefault eq '\'__output\'')
  169. {
  170. if (@minval[$i] > 0) {$mydefault= "optional";}
  171. else {$mydefault="required";}
  172. }
  173. else {$mydefault="default: $mydefault";}
  174. print "% @paramtype[$i]: $_ @paramname[$i], $mydefault: @paramdesc[$i]\n";
  175. $i += 1;
  176. }
  177. # $i=0;
  178. # print "% Ordered Inputs (can be omitted from the trailing end)\n";
  179. # foreach (@inputtags) {
  180. # print "% @inputname[$i] $_, default: @inputdef[$i]: @inputdesc[$i]\n";
  181. # $i += 1;
  182. # }
  183. # $i=0;
  184. # print "% Ordered Outputs (can be omitted from the trailing end)\n";
  185. # foreach (@outputtags) {
  186. # print "% @outputname[$i] $_, default: @outputdef[$i]: @outputdesc[$i]\n";
  187. # $i += 1;
  188. # }
  189. print "%\n% Example: ";
  190. if ($#outputtags >= 1) {print "[";}
  191. $i=0;
  192. foreach (@outputtags)
  193. {
  194. if ($#outputtags >= 0) {print "$_";}
  195. if ($i < $#outputtags)
  196. {print ", ";}
  197. $i += 1;
  198. }
  199. if ($#outputtags >= 1) {print "]";}
  200. if ($#outputtags >= 0) {print " = ";}
  201. print "k$fname(";
  202. $i=0;
  203. if ($#inputtags >= 1) {print "{";}
  204. foreach (@inputtags)
  205. {
  206. print "$_";
  207. if ($i < $#inputtags)
  208. {print ", ";}
  209. $i += 1;
  210. }
  211. if ($#inputtags >= 1) {print "}";}
  212. if ($i > 0) {print ",";}
  213. print " {";
  214. $i=0;
  215. foreach (@paramtags) {
  216. $mydefault=@defaults[$i];
  217. if ($mydefault eq '\'__input\'' || $mydefault eq '\'__output\'') { print "\'$_\',\'\'"; }
  218. else {print "\'$_\',$mydefault";}
  219. if ($i < $#paramtags)
  220. {print ";";}
  221. $i += 1;
  222. }
  223. print "})\n%\n";
  224. # Parse the helpfile and append it to the description
  225. $helpfile=$ARGV[1];
  226. if ($helpfile)
  227. {
  228. print "% Khoros helpfile follows below:\n";
  229. @lines = `cat $helpfile`;
  230. $indent = "";
  231. foreach (@lines) {
  232. # Expression starts with whitespace and a ', but not a protected whitespace
  233. s/\\-/-/; # correct protected "-"
  234. if (s/\\\(bu/* /) { $_=""; $bullet="true";}
  235. if (s/\.br//) { $_="";}
  236. if (s/\.sp//) { $_="";}
  237. if (s/\.LP//) { $_="\n";}
  238. if (s/\.IP//) { $_="\n% $_";}
  239. if (s/\.RS//) { $indent="$indent\t";}
  240. if (s/\.RE//) { $indent=substr($indent,0,-1);}
  241. s/\\fH/\"/;
  242. s/\\fP/\"/;
  243. s/\\fI/\"/;
  244. s/\.nf//;
  245. s/\.fi//;
  246. s/\.paragraph//;
  247. s/\"PANE ARGUMENTS\"//;
  248. if (s/.section 1//)
  249. {
  250. print "%\n";
  251. }
  252. if (/^\s*.onlineHelp/)
  253. {$_=""} # ignore
  254. if (/^\s*.syntax/)
  255. {$_=""} # ignore
  256. if ($_ ne "")
  257. {
  258. if ($bullet eq "true")
  259. {print "% $indent- $_";$bullet=""}
  260. else
  261. {print "% $indent$_";}
  262. }
  263. }
  264. }
  265. print "\n\n";
  266. print "function varargout = k$fname(varargin)\n";
  267. if ($#inputtags >= 0)
  268. {
  269. print "if nargin ==0\n Inputs={};arglist={'',''};\n";
  270. print "elseif nargin ==1\n Inputs=varargin{1};arglist={'',''};\n";
  271. print "elseif nargin ==2\n Inputs=varargin{1}; arglist=varargin{2};\n";
  272. print "else error('Usage: [out1,..] = k$fname(Inputs,arglist).');\nend\n";
  273. }
  274. else
  275. {
  276. print "Inputs={};\n";
  277. print "if nargin ==0\n arglist={'',''};\n";
  278. print "elseif nargin ==1\n arglist=varargin{1};\n";
  279. print "else error('Usage: [out1,..] = k$fname(arglist).');\nend\n";
  280. }
  281. print "if size(arglist,2)~=2\n error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')\n end\n";
  282. print "narglist={";
  283. $i=0;
  284. foreach (@paramtags) {
  285. print "\'$_\', @defaults[$i]";
  286. if ($i < $#paramtags)
  287. {print ";";}
  288. $i += 1;
  289. }
  290. print "};\n";
  291. print "maxval={";
  292. $i=0;
  293. foreach (@maxval) {
  294. print "$_";
  295. if ($i < $#maxval)
  296. {print ",";}
  297. $i += 1;
  298. }
  299. print "};\n";
  300. print "minval={";
  301. $i=0;
  302. foreach (@minval) {
  303. print "$_";
  304. if ($i < $#minval)
  305. {print ",";}
  306. $i += 1;
  307. }
  308. print "};\n";
  309. print "paramtype={";
  310. $i=0;
  311. foreach (@paramtype) {
  312. print "\'$_\'";
  313. if ($i < $#paramtype)
  314. {print ",";}
  315. $i += 1;
  316. }
  317. print "};\n";
  318. print "% identify the input arrays and assign them to the arguments as stated by the user\n";
  319. print "if ~iscell(Inputs)
  320. Inputs = {Inputs};
  321. end\n";
  322. print "NumReqOutputs=$NumReqOutputs; nextinput=1; nextoutput=1;
  323. for ii=1:size(arglist,1)
  324. wasmatched=0;
  325. for jj=1:size(narglist,1)
  326. if strcmp(arglist{ii,1},narglist{jj,1}) % a given argument was matched to the possible arguments
  327. wasmatched = 1;
  328. if strcmp(narglist{jj,2}, '__input')
  329. if (nextinput > length(Inputs))
  330. error(['Input ' narglist{jj,1} ' has no corresponding input!']);
  331. end
  332. narglist{jj,2} = 'OK_in';
  333. nextinput = nextinput + 1;
  334. elseif strcmp(narglist{jj,2}, '__output')
  335. if (nextoutput > nargout)
  336. error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
  337. end
  338. narglist{jj,2} = 'OK_out';
  339. nextoutput = nextoutput + 1;
  340. if (minval{jj} == 0)
  341. NumReqOutputs = NumReqOutputs - 1;
  342. end
  343. elseif isstr(arglist{ii,2})
  344. narglist{jj,2} = arglist{ii,2};
  345. else
  346. if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
  347. error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
  348. end
  349. if (minval{jj} ~= 0 | maxval{jj} ~= 0)
  350. if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
  351. error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
  352. elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
  353. error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
  354. elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
  355. error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
  356. elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
  357. error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
  358. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
  359. error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
  360. elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
  361. error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
  362. end
  363. end
  364. end
  365. if ~strcmp(narglist{jj,2},'OK_out') & ~strcmp(narglist{jj,2},'OK_in')
  366. narglist{jj,2} = arglist{ii,2};
  367. end
  368. end
  369. end
  370. if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
  371. error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
  372. end
  373. end\n";
  374. # print "if (nextoutput > 1 & (nextoutput-1) ~= nargout)
  375. # error('Number of outputs does not correspond to number of output tags in argument list!');
  376. #end\n";
  377. #print "narglist\nInputs";
  378. print "% match the remaining inputs/outputs to the unused arguments and test for missing required inputs
  379. for jj=1:size(narglist,1)
  380. if strcmp(paramtype{jj}, 'Toggle')
  381. if (narglist{jj,2} ==0)
  382. narglist{jj,1} = '';
  383. end;
  384. narglist{jj,2} = '';
  385. end;
  386. if strcmp(narglist{jj,2}, '__input')
  387. if (minval{jj} == 0) % meaning this input is required
  388. if (nextinput > size(Inputs))
  389. error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
  390. else
  391. narglist{jj,2} = 'OK_in';
  392. nextinput = nextinput + 1;
  393. end
  394. else % this is an optional input
  395. if (nextinput <= length(Inputs))
  396. narglist{jj,2} = 'OK_in';
  397. nextinput = nextinput + 1;
  398. else
  399. narglist{jj,1} = '';
  400. narglist{jj,2} = '';
  401. end;
  402. end;
  403. else
  404. if strcmp(narglist{jj,2}, '__output')
  405. if (minval{jj} == 0) % this is a required output
  406. if (nextoutput > nargout & nargout > 1)
  407. error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
  408. else
  409. narglist{jj,2} = 'OK_out';
  410. nextoutput = nextoutput + 1;
  411. NumReqOutputs = NumReqOutputs-1;
  412. end
  413. else % this is an optional output
  414. if (nargout - nextoutput >= NumReqOutputs)
  415. narglist{jj,2} = 'OK_out';
  416. nextoutput = nextoutput + 1;
  417. else
  418. narglist{jj,1} = '';
  419. narglist{jj,2} = '';
  420. end;
  421. end
  422. end
  423. end
  424. end\n";
  425. # print "Inputs\nnarglist\narglist\nminval\nmaxval\nparamtype\n";
  426. # print "narglist\n";
  427. print "if nargout
  428. varargout = cell(1,nargout);
  429. else\n";
  430. if ($#outputtags >= 0) {print " varargout = cell(1,1);\n"}
  431. else {print " varargout = cell(0);\n"}
  432. print "end\n";
  433. print "[s,w] = system('which cantata');\nw=w(1:end-8);";
  434. # print "\[w \'$rname $requiredParams\'\]\n";
  435. if ($#outputtags >= 0) { print "[varargout{:}]=callKhoros(\[w '$rname $requiredParams\'],Inputs,narglist);\n";}
  436. else { print "callKhoros(\[w \'$rname $requiredParams\'\],Inputs,narglist);\n";}