forked from lmendo/MATL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matl_disp.m
86 lines (84 loc) · 4.03 KB
/
matl_disp.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
function matl_disp(S, F, indentBase, indentStep, indentCommentSymbol, indentCommentText, pOutFile, listing, commentSymbols, commentTexts, verbose)
%
% MATL display.
%
% Show parsed statements with indentation, and save to file.
%
% Luis Mendo
% Save in pOutFile. indentBase, indentCommentSymbol, indentCommentText not used
if verbose
fprintf(' Writing to file ''%s''\n', pOutFile')
end
fid = fopen(pOutFile,'w');
if ~isempty(S)
indentation = arrayfun(@(x) {blanks(x*indentStep)}, [S(:).nesting]).';
d = strcat(indentation, {S(:).source}.'); % indentation and source
d([S.implicit]) = []; % remove implicit source statements for parsed file
for n = 1:numel(d)
if ispc % Windows
linebreak = '\r\n';
elseif ismac % Mac
linebreak = '\r';
elseif isunix % Unix, Linux
linebreak = '\n';
else % others. Not sure what to use here
linebreak = '\r\n';
end
fprintf(fid, ['%s' repmat(linebreak,1,n<numel(d))], d{n}); % avoid linebreak in last line
end
end
fclose(fid);
% Display on command window
if listing && ~isempty(S)
indentation = arrayfun(@(x) {blanks(x*indentStep+indentBase)}, [S(:).nesting]).';
d = strcat(indentation, {S(:).source}.'); % indentation and source
if commentTexts
d([S.implicit]) = indentation([S.implicit]); % remove implicit source statements but keep comment texts
else
d([S.implicit]) = []; % remove implicit source statements for display without comment texts
end
if commentSymbols
d = char(d);
d = [d, repmat([blanks(indentCommentSymbol) '%'],size(d,1),1)];
end
if commentTexts % Changes done here should be done in `genHelp.m` too
texts = cell(numel(S),1);
Stype = {S.type};
texts(ismember(Stype, {'literal.colonArray.numeric' 'literal.colonArray.char' 'literal.array'})) = {'array literal'};
texts(ismember(Stype, {'literal.number'})) = {'number literal'};
texts(ismember(Stype, {'literal.logicalRowArray'})) = {'logical row array literal'};
texts(ismember(Stype, {'literal.cellArray'})) = {'cell array literal'};
texts(ismember(Stype, {'literal.string'})) = {'string literal'};
texts(strcmp(Stype, 'metaFunction.inSpec')) = {'input specification'};
texts(strcmp(Stype, 'metaFunction.outSpec')) = {'output specification'};
texts(strcmp(Stype, 'metaFunction.altInOut')) = {'alternative input/output specification'};
texts(strcmp(Stype, 'controlFlow.for')) = {'for'};
texts(strcmp(Stype, 'controlFlow.doTwice')) = {'do twice'};
texts(strcmp(Stype, 'controlFlow.doWhile')) = {'do...while'};
texts(strcmp(Stype, 'controlFlow.while')) = {'while'};
texts(strcmp(Stype, 'controlFlow.if')) = {'if'};
texts(strcmp(Stype, 'controlFlow.else')) = {'else'};
texts(strcmp(Stype, 'controlFlow.finally')) = {'finally'};
texts(strcmp(Stype, 'controlFlow.forValue')) = {'for loop variable'};
texts(strcmp(Stype, 'controlFlow.forIndex')) = {'for loop index'};
texts(strcmp(Stype, 'controlFlow.doTwiceValue')) = {'do twice variable'};
texts(strcmp(Stype, 'controlFlow.doWhileIndex')) = {'do...while loop iteration index'};
texts(strcmp(Stype, 'controlFlow.whileIndex')) = {'while loop iteration index'};
texts(strcmp(Stype, 'controlFlow.end')) = {'end'};
texts(strcmp(Stype, 'controlFlow.break')) = {'break'};
texts(strcmp(Stype, 'controlFlow.continue')) = {'continue'};
ind = find(ismember(Stype, {'function'}));
[val, indF] = ismember({S(ind).source}, {F.source}); % val equal to false indicates there's no comment (or perhaps no function)
texts(ind(val)) = {F(indF(val)).comment};
texts([S.implicit]) = strcat({'(implicit) '}, texts([S.implicit])); % indicate which statements are implicit
d = strcat(d, {blanks(indentCommentText)}, texts(:));
end
if verbose
disp('MATL program listing:')
end
disp(' ')
for n=1:numel(d)
disp(d{n});
end
disp(' ')
end