/
matlab2fortran.m
1535 lines (1353 loc) · 42.8 KB
/
matlab2fortran.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% # DOCUMENTATION : matlab2fortran
%
% ## Brief description:
%
% * matlab2fortran(matlab-to-fortran) : converting matlab code to fortran
% * Usage: matlab2fortran(filename);
% * Author:Emmanuel Branlard (contributors are welcome)
% * Creation Date : December 2012
% * Last revision : 2015-08-26
% * Version: 1.0-29-g83df194
% * Web-Sites:
% - http://github.com/elmanuelito/matlab2fortran
% - http://emmanuel.branlard.free.fr/work/programming/
% * License: None. Thank you for sharing your improvements to me by email.
%
%
% ## DESCRIPTION
%
% matlab2fortran(matlab-to-fortran) is a code that performs an automatic conversion of one or multiple matlab files to fortran format. The script translate some of the main features of matlab to fortran. matlab2fortran performs a quick and dirty conversion on a line-by-line basis (but still supports conditional loops, subroutine). The script allows multiple matlab command per line if these commands are separated by ";", but not by ",".
%
% matlab2fortran does not intend to produce ready-to-compile code, but performs several necessary conversions. The generated code keeps the structure, variables names and comments of the original code.
%
%
% ## INSTALLATION AND REQUIREMENTS
%
% The script is located in the release directory of the repository.
% The script matlab2fortran (matlab-to-fortran) is a single script file written in matlab. It does not require a particular installation other than maybe adding this script to your current folder or matlab path.
%
%
% The script has been tested on linux and windows.
% The script has been tested on matlab and octave.
% (Octave generates some warnings. Ouputs are the same expect for some replacement of "...")
%
%
%
% ## REVISIONS
%
% * 14/12/12 : first release
%
% * 15/12/12 : added declaration of variables at beginning of script or subroutines
% added handling of intent(inout),
% corrected small bug for do loops and number of ":" since they can be in length(a(:,1))
%
% * 18/12/12 :
% - Get the function arguments do the decl_stack
% - Handling of [] for array constructor and string concanenation
% - support for transpose '
% - When allocation for zeros, and ones, don't add the line x=x if no operation if present
% - when removing duplicates from the stack, don't loose data
% - Solved Bug for parenthesis in case like: if (a) && b which needs to give ((a) .and. b)
%
% * 16/10/13 : From now on, revisions will only be listed vith git commits
%
%
%
% ## FEATURES AND TODOs
%
% ### Main Features of matlab2fortran:
%
% - conversion of nested structure: do, if, switch (select), while
% - conversion of function to subroutine with recognition of output and input arguments, (output arguments are put at the end)
% - perform subroutine list of arguments with intent(in), intent(out), intent(inout)
% - make a declaration list for simple variable , sort them by Name or Type
% - determines type based on variable name (function fgetVarTypeFromVarName) and some syntax
% - does its best to determine array size with things like :, [], zeros
% - recognize simple function call and convert them to subroutine calls (syntax [y] = f(x) );
% - performs allocation when converting matlab function zeros, ones (linspace possible)
% - splitting of lines that contain multiple matlab command separated by ;
% - comments
% - small support for sprintf and fprintf
% - small support for string concatenation
% - small support for transpose when written as '
% - misc conversions such as logical operators
% - few intrinsic functions conversion
% - replaces end by size(XXX,XXX)
% - Provides Doxygen compatible comments for subroutine and arguments
%
% ### TODOs:
% - easy: replace also & and | after && and ||
% - better printf handling
% - better handling of function calls and nested function calls..
% - inline ifs?
% - isequal
%
% Requests:
% - In “do while” construct, it does not put the logical expression in brackets.
% - It does not convert “pi” to a real parameter having the value of 3.1415…
% - It does not detect some of Matlab’s intrinsic functions (e.g., mean, std, nnz). It considers them as variables.
% - It does not convert some intrinsic functions such as “fopen”.
% - It considers the variables in array bounds subscript (e.g., t in M(s,t)) real, while they are integers.
%
%
% ### Features that are not intented to be supported
%
% - Ready-to-compile output and perfection...
% - Parsing of line and tree-like syntax detection
% - Full type detection
% - Type detection based on Matlab workspace output
% - Detecting all variables
%
%
% ## RUN INSTRUCTIONS and PARAMETERS
%
% The script should run in few seconds for a file a 1000 lines (say 2s). If it takes longer, activate the Debug flag bDebug=1; to see where the script is stuck...
% Several parameters are found at the beginning of the script.
%
%
% ## EXAMPLES
%
% example 1: one file
% matlab2fortran('tests/test1.m');
%
% example 2: list of files
% matlab2fortran('tests/test1.m','tests/test2.m');
%
% example 3: all files in current directory (does not work with subfolders with dir..)
% FileList=dir('*.m');
% matlab2fortran(FileList.name);
%
%
%
%
%
function [ ] = matlab2fortran( varargin )
%% Parameters
global DOUBLE_KIND DOUBLE_KIND_SUFFIX;
bDebug=0;% show input in stdout
bPipe=0; % pipe output to stdout as well as in the file
bSortDeclarationbyNames=1; %
bSortDeclarationbyTypes=0; %
DOUBLE_KIND='MK'; DOUBLE_KIND_SUFFIX='_MK';
% DOUBLE_KIND='8'; DOUBLE_KIND_SUFFIX='D0';
%%
if nargin==0
error('Empty file list')
end
%% dealing with multiple input files "recursively"..
if length(varargin)>1
for ifile = 1:length(varargin)
f=varargin{ifile};
matlab2fortran(f);
end
else
%% One file call
file=varargin{1};
fidm=fopen(file,'r');
if fidm==-1
error(['Unable to open input file: ' file]);
end
filef=regexprep(file,'\.m$','.f90');
fileftmp=regexprep(file,'\.m$','_tmp.f90');
fprintf('Converting to file: %s\n',filef);
% warning off
if ~bDebug
fidftmp=fopen(fileftmp,'w');
if fidftmp==-1
error(['Unable to open temporary output file: ' fileftmp]);
end
fidf=fopen(filef,'w');
if fidf==-1
error(['Unable to open output file: ' filef]);
end
else
fidftmp=1; % standard output
fidf=1; % standard output
end
%% Conversion line by line and ouput to file
if ~bDebug
warning off ; %WATCH OUT
end
[Declarations]=fconvertlinebyline(fidm,fidftmp,bDebug,bPipe);
warning on
fclose(fidm);
if ~bDebug
fclose(fidftmp);
end
%% Sorting Declarations for each subroutine and main program
Declarations=fSortDeclarations(Declarations,bSortDeclarationbyNames,bSortDeclarationbyTypes,bDebug);
%% Reading temp file, outputting to final file with declarations , only possible without Debug
if ~bDebug
fidftmp=fopen(fileftmp,'r');
if fidftmp==-1
error(['Unable to re open temporary output file: ' fileftmp]);
end
fWriteDeclarationsAtCorrectLocation(fidftmp,fidf,Declarations)
fclose(fidftmp);
delete(fileftmp);
fclose(fidf);
end % end output of declaration if not debug mode
end % end switch between one or two files
end %function
function [Declarations]=fconvertlinebyline(fidm,fidf,bDebug,bPipe)
sLine = fgets(fidm);
end_stack=[];
current_unit=1;
Declarations{current_unit}=[]; % contains all variables declaration
decl_stack=[]; % contains variable declaration for current subroutine or main..
pref_in='';
pref_out='';
if bDebug
pref_in='In :';
pref_out='Out:';
end
while sLine ~= -1
% remove useless spaces
sLine=strtrim(sLine);
sLine=fremovespaces(sLine);
if bDebug
fprintf('%s%s \n',pref_in,sLine);
end
% Simple case: emptyline
if isempty(sLine)
sf=[sLine '\n'];
fprintf(fidf,[pref_out,sf]);
if bPipe ; fprintf(1,[pref_out,sf]); end
% Simple case: start with a comment, still needs small handling
elseif sLine(1)=='%'
% kbd
sLine(1)='!';
sLine=strrep(sLine,'%','!');
% sLine=strrep(sLine,'%','!');
sLine=strrep(sLine,'\n','NewLine');
sLine=strrep(sLine,'\','\\');
sf=[sLine ' \n'];
% sf=strrep(sf,'%','%%');
fprintf(fidf,[pref_out,sf]);
if bPipe ; fprintf(1,[pref_out,sf]); end
else
% the problematic printf case is dealt then
if ~isempty(regexp(sLine,'[fs]+printf\('))
[ sf ] = freplaceprintf( sLine );
fprintf(fidf,[pref_out,sf]);
if bPipe ; fprintf(1,[pref_out,sf]); end
else
% comments replacement
sLine=strrep(sLine,'%','!');
% splitting matlab lines if commands on the same line
[ sout ] = fsplitmatlablines( sLine,[] );
for isplit=1:length(sout)
s=sout{isplit};
% ---------------------------------
% The Main function
% ---------------------------------
[sf end_stack decl_stack unit_switch decl_stack_new]=fconvertline(s,end_stack,decl_stack);
if unit_switch~=0
% storing the stack of declaration of previous unit
if bDebug
disp('Entering new unit')
end
Declarations{current_unit}=decl_stack;
current_unit=current_unit+unit_switch;
if unit_switch==1
Declarations{current_unit}=[];
decl_stack=decl_stack_new;
end
end
% ---------------------------------
sf=fremovespaces(sf);
fprintf(fidf,[pref_out,sf]);
if bPipe ; fprintf(1,[pref_out,sf]); end
end
end
end
sLine = fgets(fidm);
end
Declarations{current_unit}=decl_stack;
% if at the end we still have some end, output them
for ie=1:length(end_stack)
fprintf(fidf,'end %s \n',end_stack{ie});
end
end %function
function [sf end_stack decl_stack unit_switch decl_stack_new]=fconvertline(s,end_stack,decl_stack)
unit_switch=0;
decl_stack_new=[];
% function replace
I=strfind(s,'function');
if ~isempty(I) && I(1)==1
[ sf end_stack decl_stack_new] = freplacefunction( s , end_stack );
unit_switch=1; %we are entering a new unit
return;
end
%% Simple regexprep or rep
% logical
s=regexprep(s,'~=','/=');
s=regexprep(s,'~(.*)','.not.($1)');
s=strrep(s,'&&','.and.');
s=strrep(s,'||','.or.');
s=strrep(s,'break','exit');
s=strrep(s,'true','.true.');
s=strrep(s,'false','.false.');
% intrisic
s=strrep(s,'strtrim(','trim(');
s=strrep(s,'norm(','norm2(');
s=strrep(s,'ceil(','ceiling(');
s=strrep(s,'round(','anint('); % or just int?
s=strrep(s,'floor(','floor(');
s=strrep(s,'mod(','modulo('); %mod exists but return negative values
% array stuff
s=regexprep(s,'size\(([^,]*)\)','shape($1)'); %replace size by reshape when no comma inside, otherwise, size is good
s=strrep(s,'length(','size('); % or count maybe..
% math
s=strrep(s,'dot','dot_product'); %replace size by reshape when no comma inside, otherwise, size is good
s=strrep(s,'^','**');
% strings
% s=strrep(s,'&','\\');
s=strrep(s,'\','\\');
%% Things that are easily recognizable by their first position on the line
% case
s=regexprep(s,'^case([^!;]*)','case ($1) !');
s=regexprep(s,'^otherwise*','case default ');
sold=s;
s=regexprep(sold,'^switch([^!;]*)','select case ($1) !');
if length(s)~=length(sold) % we did replace something
% stacking
end_stack=fstack_push(end_stack,'select');
end
% for replace
I=regexp(s,'for[( ]');
if ~isempty(I) && I(1)==1
[ sf end_stack] = freplacefor( s , end_stack );
return;
end
% while replace
I=strfind(s,'while');
if ~isempty(I) && I(1)==1
[ sf end_stack] = freplacewhile( s , end_stack );
return;
end
% if replace
I=regexp(s,'elseif|else if|if\ |if\('); % watch out for the order here
if ~isempty(I) && min(I)==1
[ sf end_stack] = freplaceif( s , end_stack );
return;
end
% end replace
I=strfind(s,'end');
if ~isempty(I)
if I(1)==1
%let's check that this is not a variable
if length(s)==3 || s(4)==' ' || s(4)==';' ||s(4)=='!'
[ sf end_stack] = freplaceend( s , end_stack );
return;
end
else
% Dealing with end in the middle
s=regexprep(s,'([^a-zA-Z0-9_]+)end([^a-zA-Z0-9_]+)','$1size(XXX,XXX)$2');
end
end
% function calls
I=strfind(s,'[');
if ~isempty(I) && I(1)==1
[ sf, decl_stack] = freplacefunctioncall( s, decl_stack );
return;
end
% multiline
% s=strrep(s,'...','\&'); %char(8) not enough.. %HACK : the escape sequence is not recognize and hence it merges with the next line...
s=regexprep(s,'(?<!!.*)\.\.\.','&'); % this line requires some explanation below
% If uses a negative lookbehind "(?<!)" It looks if there is no !.* before (ie, no comment)
% If this is not a commen then we replace the ... by &. This regexp will not work with octave, but it doesnt matter since it's a comment.
% Octave will replace by & all the time
% If one want, & can be replace by \& so that matlab will not recognize this bad escpae character and hence will merge the current line with the next.. but that's a HACK
%% More tricky things that should require syntax parsing and recursion but that we'll not do!..
% zeros and ones
% I=regexp(s,'(zeros\(|ones\(|linspace\()');
I=regexp(s,'(zeros\(|ones\()');
if ~isempty(I)
[ s , decl_stack] = freplacezeros( s ,decl_stack);
else
% stacking assignements so that they can be used for declarations
[ s , decl_stack] = fassignement( s ,decl_stack); % calls freplacesbracket
end
%% brackets
% I=regexp(s,'(zeros\(|ones\(|linspace\()');
% if ~isempty(I) && mod(length(I),2)==0
% else
% TODO with better string handling
% s=strrep(s,'[','(/');
% s=strrep(s,']','/)');
% default return
sf=[s '\n'];
end %function
function fWriteDeclarationsAtCorrectLocation(fidftmp,fidf,Declarations)
% kbd
current_unit=1;
sread = fgets(fidftmp);
while sread ~= -1
if current_unit==1
% wait for the first non comment empty to output the declaration
if ~isempty(Declarations{1,1})
if ~isempty(sread) && sread(1)~='!'
fprintf(fidf,'! Variable declarations \n');
fwrite_Declarations(fidf,Declarations{current_unit,1});
fprintf(fidf,'! \n');
Declarations{1}=[];
end
end
end
I=strfind(sread,'subroutine');
if ~isempty(I) && I(1)==1
% if current_unit==1 && ~isempty(Declarations{current_unit})
% % case of first unit, i.e. main program
% % % we need to output the declaration here
% fwrite_Declarations(fidf,Declarations{current_unit});
% end
current_unit=current_unit+1; %
end
I=strfind(sread,'!M2F-HERE-XXX');
if ~isempty(I) && I(1)==1
if ~isempty(Declarations{current_unit,2})
fprintf(fidf,'! Arguments declarations \n');
fwrite_Declarations(fidf,Declarations{current_unit,2});
end
if ~isempty(Declarations{current_unit,1})
fprintf(fidf,'! Variable declarations \n');
fwrite_Declarations(fidf,Declarations{current_unit,1});
end
sread='';
end
sread=strrep(sread,'\','\\'); % required
fprintf(fidf,sread);
sread = fgets(fidftmp);
end %while reading file tmp
end %function fWriteDeclarationsAtCorrectLocation
function DeclarationsOut=fSortDeclarations(Declarations,bSortDeclarationbyNames,bSortDeclarationbyTypes,bDebug)
for iud=1:length(Declarations)
if bDebug
fprintf(['-------------Declaration for unit ' num2str(iud) '\n']);
end
% fprintf(2,Declarations{iud}{id});
Decl=Declarations{iud};
DeclArguments=[];
if length(Decl)>0
VarNames = cellfun(@(x)x.name, Decl, 'UniformOutput',false);
% sort by name first for convenience
[~, Isort] = sort(lower(VarNames)); % NOTE: the lower here is a matter of choice
Decl=Decl(Isort);
%% Removing duplicates declaration entry (now by merging)
iD=1;
while iD+1<=length(Decl)
if ~isequal(Decl{iD}.name,Decl{iD+1}.name)
% we just increment
iD=iD+1;
else
% we merge
D=fmergeDeclarations(Decl{iD},Decl{iD+1});
Decl{iD}=D;
Decl=fstack_pop_element(Decl,iD+1);
end
end
%% Check for same variable different case
[~, Isort] = unique(VarNames); % NOTE: case ins-sensitive so that the user sees the possible clash since fortran is case insensitive
[~, Isort2] = unique(lower(VarNames));
if length(Isort)~=length(Isort2)
warning('Be careful, there are variables that have the same characters but different cases.');
end
%% Dealing with intent first
VarProp = cellfun(@(x)x.prop, Decl, 'UniformOutput',false);
Pintent=strfind(VarProp,'intent');
Iintent = cellfun(@(x)~isempty(x), Pintent, 'UniformOutput',true);
DeclArguments=Decl(Iintent);
Decl=Decl(~logical(Iintent));
% Decl=Decl(Isort);
VarNamesA = cellfun(@(x)x.name, DeclArguments, 'UniformOutput',false);
VarTypesA = cellfun(@(x)x.name, DeclArguments, 'UniformOutput',false);
VarNames = cellfun(@(x)x.name, Decl, 'UniformOutput',false);
VarTypes = cellfun(@(x)x.name, Decl, 'UniformOutput',false);
% sorting
if bSortDeclarationbyNames
[~, IsortA] = sort(lower(VarNamesA)); % NOTE: the lower here is a matter of choice
[~, Isort] = sort(lower(VarNames)); % NOTE: the lower here is a matter of choice
elseif bSortDeclarationbyTypes
[~, IsortA] = sort(lower(VarTypesA));
[~, Isort] = sort(lower(VarTypes));
else
IsortA=1:length(VarNamesA);
Isort=1:length(VarNames);
end
DeclArguments=DeclArguments(IsortA);
Decl=Decl(Isort);
% Printing
if bDebug
fwrite_Declarations(1,Decl);
end
end
DeclarationsOut{iud,1}=Decl;
DeclarationsOut{iud,2}=DeclArguments;
end
end %function format Declarations
function []=fwrite_Declarations(fidout,Decl)
for id=1:length(Decl)
[ sf ] = fgetDeclaration(Decl{id} );
fprintf(fidout,sf);
end
end % function
function [ s , decl_stack ] = fassignement( s , decl_stack )
% extract the LHS
Ieq=strfind(s,'=');
if isempty(Ieq)
return
end
sLHS=s(1:Ieq(1)); % !!! includes the equal for now!
% extract the RHS and comment
Ic=strfind(s,'!');
if ~isempty(Ic) % could be smarter
bwithin=0;
is=Ieq(1)+1;
while is<=length(s)
if s(is)==''''
if ~bwithin
bwithin=1;
else
bwithin=0;
end
end
if s(is)=='!' && ~bwithin
break
end
is=is+1;
end
sRHS=s((Ieq(1)+1):(is-1));
sCOM=s(is:end);
else
sRHS=s((Ieq(1)+1):end);
sCOM='';
end
% if ~isempty(Ic) % could be smarter
% % make sure the comment is not surrounded by apostroph
% [b p1 p2]=fissurroundedby(s,Ic(1),'''','''');
% if b
%
% else
% end
% Initialization of variable properties
v.comment='';
v.type='';
v.shape='';
v.name='TODO';
v.prop='';
vb.comment='';
vb.type='';
vb.shape='';
vb.name='TODO';
vb.prop='';
% hack for matrices assignemnt: in the form A(1,:)=[a a] , then the shape can be determined in one dimension
bFirstDimMayBeDetermined=0;
bSecondDimMayBeDetermined=0;
%% Finding variable name and properties if possible in LHS
% test simple case pure variable
% only simple assignments are allowed x1_var =.. , I.element =
% It is chosen to keep the dot in the variable name..
Isimple=regexp(sLHS,'[a-z.A-Z0-9_ ]+=');
if ~isempty(Isimple) && Isimple(1)==1
% that's the simple case
vb.name=strtrim(sLHS(1:(end-1)));
[vb.type, vb.shape, vb.prop] =fgetVarTypeFromName(vb.name);
else
% if an assignment is of the form a(:,1) we dont cons
% is there is some : and (, try to find the dimension
Ipar=strfind(sLHS,'(');
Icurl=strfind(sLHS,'{');
if ~isempty(Icurl) % has to be first
vb.name=strtrim(sLHS(1:(Icurl(1)-1)));
vb.type='matlabcell';
elseif ~isempty(Ipar)
vb.name=strtrim(sLHS(1:(Ipar(1)-1)));
[vb.type, ~, vb.prop] =fgetVarTypeFromName(vb.name);
ncommas=length(strfind(sLHS,','))+1;
vb.shape=':';
for is=2:ncommas;
vb.shape=[vb.shape,',:'];
end
% let's a simple hack for matrices
bFirstDimMayBeDetermined=~isempty(strfind(fremovesinglespace(sLHS),':,'));
bSecondDimMayBeDetermined=~isempty(strfind(fremovesinglespace(sLHS),',:'));
else
%TODO
end
end
%% merging info
v.name=vb.name;
v=fmergeDeclarations(v,vb);
%% Dealing with squared brackets in RHS
if ~isempty(strfind(sRHS,'['));
[ sRHS , vb.type, ncol,nlines] = freplacesbracket( sRHS );
if min(ncol,nlines)==1
vb.shape=sprintf('%d',max(ncol,nlines));
% hack
if bFirstDimMayBeDetermined
vb.shape=sprintf('%d,:',max(ncol,nlines));
elseif bSecondDimMayBeDetermined
vb.shape=sprintf(':,%d',max(ncol,nlines));
end
elseif ncol*nlines~=0
% TODO more than matrices...
vb.shape=sprintf('%d,%d',ncol,nlines);
end
% TODO possibility to handle some types here
% elseif ~isempty(regexp(sLHS,'[0-9]+'))
% v.type=
end
v.name=vb.name;
v=fmergeDeclarations(v,vb);
%% Dealing with transpose as apostroph
if mod(length(strfind(sRHS,'''')),2)==1 % clearly this will fail if there is an even number of transpose on the line..
s=['!m2f: ' sLHS sRHS sCOM '\n'];
sRHSb=regexprep(sRHS,'([a-zA-Z\(\)\_0-9:]*)''','transpose($1)');
s=[s sLHS sRHSb sCOM];
else
% default
s=[sLHS sRHS sCOM];
end
decl_stack=fstack_push(decl_stack,v);
% if isequal(v.name,'TODO')
% disp(s);
% end
end %function
function [b po pc]=ffindmatching(s,pstart,co,cc,bforward);
% string, start position, character open, character close
%
% [b po pc]=ffindmatching('1(3(5)7()df) d',2,'(',')',1 )
%
b=0;
po=0;
pc=0;
if ~bforward
error('not done')
end
found=false;
nopen=0;
Iopen=[];
i=pstart;
while ~found && i<=length(s)
if s(i)==co
nopen=nopen+1;
Iopen=[Iopen i];
end
if s(i)==cc
if nopen>=1 %NOTE: this is a choice, if a closing character is found, before an opened one, we don't care..
nopen=nopen-1;
if nopen==0
found=true;
pc=i;
po=Iopen(1);
break
end
end
end
i=i+1;
end
b=found;
end
function [ sf ] = fgetDeclaration( v )
global DOUBLE_KIND
if isempty(v.type)
vartype=sprintf('real(%s)',DOUBLE_KIND);
else
vartype=v.type;
end
if ~isempty(v.shape)
varshape=['dimension(' v.shape ')'];
if ~isempty(v.prop)
varshape=[ varshape ', '];
end
else
varshape='';
end
if ~isempty(v.prop)
varprop=[v.prop];
else
varprop='';
end
if ~isempty(v.name)
varname=v.name;
else
varname='TODO';
end
if ~isempty(varprop)|| ~isempty(varshape)
vartype=[ vartype ', '];
end
% varcomment=[v.comment ' !> '];
varcomment=[' !< ' v.comment(1:min(20,length(v.comment)))];
sf=sprintf('%s%s%s :: %s %s \n',vartype,varshape,varprop,varname,varcomment);
end
function [ vartype, varshape,varprop ] = fgetVarTypeFromName( varname )
global DOUBLE_KIND;
% default type
vartype='';
varshape='';
varprop='';
% attempt a type conversion based on first character, this is my own convention, comment if needed
if varname(1)=='i' || varname(1)=='n'
vartype='integer';
elseif varname(1)=='I'
vartype='integer';
varshape=':';
elseif varname(1)=='b'
vartype='logical';
elseif varname(1)=='s'
vartype='character(len=*)';
elseif length(varname)>=3 & isequal(varname(1:3),'cpt')
vartype='integer';
elseif varname(1)=='v'
vartype=sprintf('real(%s)',DOUBLE_KIND);
varshape=':';
else
vartype=''; % this is postponed to the writting
end
end
function [ b, p1,p2 ] = fissurroundedby( s,p,c1,c2 )
% looks if character s(p) is surrounded by the characters c1 and c2
% returns a boolean , and the postion of these charactesrs
% [b p1 p2]=fissurroundedby('function [a b c]=issur([,])',10,'[',']')
% [b p1 p2]=fissurroundedby('function [a b c]=issur([,])',11,'[',']')
% [b p1 p2]=fissurroundedby('function [a b c]=issur([,])',10,'[',']')
% [b p1 p2]=fissurroundedby('function [''a b c'']=issur([,])',10,'''','''')
% [b p1 p2]=fissurroundedby('function [''a b c'']=issur([,])',12,'''','''')
% [b p1 p2]=fissurroundedby('(),()',4,'(',')')
p1=0;
p2=0;
%% stupid whiles
% look backward
notfound=1;
i=p-1;
while i>=1 && notfound
if c1~=c2
% then if c2 is encountered we should break
if s(i)==c2
break
end
end
if s(i)==c1
notfound=0;
p1=i;
end
i=i-1;
end
% look forward
notfound=1;
i=p+1;
while i<=length(s) && notfound
if c1~=c2
% then if c1 is encountered we should break
if s(i)==c1
break
end
end
if s(i)==c2
notfound=0;
p2=i;
end
i=i+1;
end
if p1==0 || p2==0
b=0; % not surrounded
else
b=1;
end
end
function [ decl ] = fmergeDeclarations( v1, v2 )
v1.name=strtrim(v1.name);
v2.name=strtrim(v2.name);
if ~isequal(v1.name ,v2.name)
error('Can''t merge if name different');
end
decl.name=v1.name;
%% comment
decl.comment=[v1.comment v2.comment];
%% dimension
b1=isempty(regexp(v1.shape,'[0-9]+'));
b2=isempty(regexp(v2.shape,'[0-9]+'));
% give priority to numbers
if ~b1 && ~b2
n1=length(strfind(v1.shape,':'));
n2=length(strfind(v2.shape,':'));
s_shape=':';
for is=2:max(n1,n2)
s_shape=[s_shape,',:'];
end
elseif b1
s_shape=v2.shape;
if ~isempty(v1.shape)
decl.comment=[decl.comment '!m2f: check dim(' v1.shape ')'];
end
else
s_shape=v1.shape;
if ~isempty(v2.shape)
decl.comment=[decl.comment '!m2f: check dim(' v2.shape ')'];
end
end
decl.shape=s_shape;
%% prop
if isempty(v1.prop)
decl.prop=v2.prop;
elseif isempty(v2.prop)
decl.prop=v1.prop;
else
% none of them are empty;
s_prop=[v1.prop,',' v2.prop];
[~,~,~,~,~,~,c]=regexp(s_prop,',');
c=strtrim(c);
c=sort(c);
% cannot be allocatable and intent
if ~isempty(strfind(s_prop,'allocatable')) && ~isempty(s_prop,'intent')
% remove allocatable prop
I=cellfun(@(x)~isequal(x,'allocatable'),c);
c=c(I);
end
s_shape=c{1}
for ic=2:length(c)
s_prop=[', ' c{ic}];
end
end
%% type. NOTE: a more advanced merging could be thought
if ~isempty(strfind([v1.type v2.type],'matlabcell'))
decl.type='matlabcell';
else
vartype = fgetVarTypeFromName( v1.name );
if isempty(v1.type)
decl.type=v2.type;
elseif isempty(v2.type)
decl.type=v1.type;
elseif isequal(v1.type, vartype)
decl.type=v2.type;
elseif isequal(v2.type, vartype)
decl.type=v1.type;
else
decl.type=''; % don't add information you don't have
end
end
end
function [ s ] = fremovesinglespace( s )
% remove multiple spaces
s=regexprep(s,'[\ ]+','');
end %function
function [ s ] = fremovespaces( s )
% remove multiple spaces
s=regexprep(s,'[\ ]+',' ');
end
function [ sf, end_stack ] = freplaceend( s,end_stack )
% freplaceend('end !(ksdjf)',[])
% freplaceend('end ; ! why not?',[])
if isempty(end_stack)
warning('Too much ends in the file.. Some stuff have not been accounted for?');
end_stack{1}='';
end
[end_stack, elmt] = fstack_pop(end_stack);
sf=['end ' elmt ' ' s(4:end) '\n']; % could add a security comment..
end %function
function [ sf, end_stack ] = freplacefor( s,end_stack )
% freplacefor('for i=I',[])
% freplacefor('for i=1:0.2:10 ; ! why not?',[])
% freplacefor('for iarg=1:length(c_all_args(:,1)',[])
Icol=strfind(s,':');
Ieq=strfind(s,'=');
ncol=length(Icol);
loopvar=strtrim(s(4:(Ieq(1)-1)));
sf=['do ' loopvar '='];
sloopextent=s((Ieq(1)+1):end);
[~,~,~,~,~,~,split]=regexp(sloopextent,':');
% nasty handling of colum that are used for index ranges..
splitb=[];
for is=1:length(split)
ssplit=split{is};
no=length(strfind(ssplit,'('));
nc=length(strfind(ssplit,')'));
if no>nc
if (is+1)<=length(split)
splitb{end+1}=[split{is},':' split{is+1}];
end
elseif nc>no
% should have been accounted by previous step
else
splitb{end+1}=split{is};
end
end
split=splitb;
ncol=length(split)-1;
switch ncol
case 0
sf=[sf s((Ieq(1)+1):end)];
case 1
sf=[sf strtrim(split{1}) ',' strtrim(split{2})];
case 2
sf=[sf strtrim(split{1}) ',' strtrim(split{3}) ',' strtrim(split{2})];
end
sf=[sf ' \n'];
% stacking
end_stack=fstack_push(end_stack,'do');
end
function [ sf, end_stack,decl_stack ] = freplacefunction( s,end_stack)
decl_stack=[];
% freplacefunction('function [ sf, end_stack ] = ffunctionreplace( s,end_stack )',[])