-
Notifications
You must be signed in to change notification settings - Fork 8
/
matl.m
288 lines (271 loc) · 9.24 KB
/
matl.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
function matl(varargin)
%
% MATL main program.
%
% Requires Matlab 2015b or newer. Also works in Octave. See
% specification document for compatibility considerations.
%
% Calls parser, compiler, runner depending on input options.
%
% Luis Mendo
indentBase = 4; % number of spaces for indentation level 0. Default value
indentStep = 2; % number of spaces to add for each indentation level. Default value
indentCommentSymbol = 6; % number of spaces before comment symbol. Default value
indentCommentText = 1; % number of spaces before actual comment. Default value
pOutFile = 'MATLp.txt'; % temporary file for parsed code
cOutFile = 'MATLc.m'; % temporary file for compiled code
cOutFileNoExt = cOutFile(1:end-2); % same without extension. Needed to run file in old Matlab versions
% cOutFileNoExt = regexprep(cOutFile, '\.m$', ''); % Old Octave versions
% (before 3.8 apparently) don't recognize '\.' as an escaped dot symbol
thisDir = fileparts(mfilename('fullpath'));
funDefMasterFile = fullfile(thisDir, 'funDef.txt'); % function definition master file
funDefMatFile = fullfile(thisDir, 'funDef.mat'); % function definition processed file
preLitMasterFile = fullfile(thisDir, 'preLit.txt'); % master file that defines predefined strings with (key, value) pairs
preLitMatFile = fullfile(thisDir, 'preLit.mat'); % processed file file that defines predefined strings with (key, value) pairs
helpFile = fullfile(thisDir, 'help.mat');
matlInputPrompt = ' > ';
version = ver;
indMainName = find(ismember({version.Name}, {'MATLAB','Octave'}));
isMatlab = strcmp(version(indMainName).Name, 'MATLAB'); % 1 if Matlab, 0 if Octave
verNum = version(indMainName).Version; % version number as a string
verNum = str2double(regexp(verNum, '\.', 'split')); % version number as a vector
useTags = isMatlab && (verNum(1)>7 || (verNum(1)==7 && verNum(2)>=13)) && usejava('desktop');
if useTags
strongBegin = '<strong>';
strongEnd = '</strong>';
else
strongBegin = '';
strongEnd = '';
end
if numel(varargin)==0
options = 'r';
inputNeeded = true;
elseif numel(varargin)==1 && ~isempty(varargin{1}) && varargin{1}(1)=='-' && numel(varargin{1})>1
options = varargin{1}(2:end);
inputNeeded = ~any(options=='h'); % true unless option is h
elseif numel(varargin)==1 && ~isempty(varargin{1}) && varargin{1}(1)=='-' && numel(varargin{1})==1
options = 'r';
inputNeeded = true;
elseif numel(varargin)==1
options = 'r';
s = varargin{1};
inputNeeded = false;
elseif numel(varargin)==2 && varargin{1}(1)=='-'
options = varargin{1}(2:end);
s = varargin{2};
inputNeeded = false;
elseif numel(varargin)==2 && varargin{1}(1)~='-'
error('MATL:main', 'MATL error while processing options: two input strings have identified, but the first string does not begin with %s-%s', strongBegin, strongEnd)
else
error('MATL:main', 'MATL error while processing options: the number of inputs cannot exceed 2')
end
if any(options=='r') && any(options=='d')
error('MATL:main', 'MATL error while processing options: incompatible options %sr%s and %sd%s', strongBegin, strongEnd, strongBegin, strongEnd)
end
if any(options=='l') && any(options=='e')
error('MATL:main', 'MATL error while processing options: incompatible options %sl%s and %se%s', strongBegin, strongEnd, strongBegin, strongEnd)
end
if ~ismember(options,'plecsrdh')
options = [options 'r'];
end
if any(options=='v')
verbose = true;
else
verbose = false;
end
if any(options=='h') && any(ismember(options, ['plecrdf' '0':'9' 'A':'Z']))
error('MATL:main', 'MATL error while processing options: %sh%s is not compatible with specified options', strongBegin, strongEnd)
end
if any(options=='o')
online = true; % saffe mode, for online compiler
else
online = false;
end
if ~all(ismember(options, ['plecrdfvho' '0':'9' 'A':'Z']))
error('MATL:main', 'MATL error: unrecognized option')
end
numericOptions = options(ismember(options,['0':'9' 'A':'Z']));
numericOptions = base2dec(numericOptions(:), 36);
if numel(numericOptions)>=1
indentBase = numericOptions(1);
end
if numel(numericOptions)>=2
indentStep = numericOptions(2);
end
if numel(numericOptions)>=3
indentCommentSymbol = numericOptions(3);
end
if numel(numericOptions)>=4
indentCommentText = numericOptions(4);
end
if numel(numericOptions)>=5
error('MATL:main', 'MATL error while processing options: too many numeric options')
end
genHelpNeeded = false;
% Update funDefMatFile, if needed; or load file
fDtxt = dir(funDefMasterFile);
if ~isempty(fDtxt)
fDtxt = datenum(fDtxt.date);
else
fDtxt = -inf;
end
fDmat = dir(funDefMatFile);
if ~isempty(fDmat)
fDmat = datenum(fDmat.date);
else
fDmat = -inf;
end
if fDmat < fDtxt % regeneration of funDefMatFile required
if verbose
fprintf('Regenerating function definitions from master file ''%s''\n', funDefMasterFile)
end
F = genFunDef(funDefMasterFile, funDefMatFile); % creates `F` struct array with unique function
% definitions from funDefMasterFile, and saves it in funDefMatFile file
genHelpNeeded = true;
else % load funDef;atFile directly
if verbose
fprintf('Loading function definitions from file ''%s''\n', funDefMatFile)
end
load(funDefMatFile) % loads F variable
end
if verbose
fprintf(' %i function definitions found\n', numel(F))
end
% Update preLitFile, if needed
fDtxt = dir(preLitMasterFile);
if ~isempty(fDtxt)
fDtxt = datenum(fDtxt.date);
else
fDtxt = -inf;
end
fDmat = dir(preLitMatFile);
if ~isempty(fDmat)
fDmat = datenum(fDmat.date);
else
fDmat = -inf;
end
if fDmat < fDtxt % regeneration of preLitFile required
if verbose
fprintf('Regenerating predefined literals from master file ''%s''\n', preLitMasterFile)
end
L = genPreLit(preLitMasterFile, preLitMatFile); % creates struct `L` with keys and values
% from preLitMasterFile, and saves it in preLitFile. Returns number of keys.
genHelpNeeded = true;
else
if verbose
fprintf('Loading predefined literals from file ''%s''\n', preLitMatFile)
end
load(preLitMatFile) % loads struct arrray `L`
end
if verbose
fprintf( ' %i predefined literals found in %i functions\n', ...
sum(cellfun(@(x) numel(x.key), struct2cell(L))), numel(fieldnames(L)) )
end
% Update help file, if needed
if genHelpNeeded
if verbose
fprintf('Regenerating help file \n')
end
H = genHelp(F, L);
else
if verbose
fprintf('Loading help file ''%s''\n', helpFile)
end
load(helpFile)
end
if verbose
fprintf(' %i help entries found\n', numel(H.source))
end
% Get input, if needed
if inputNeeded
if any(options=='f')
if verbose
disp('Input filename:')
end
fprintf(matlInputPrompt); % change prompt to show we are accepting MATL content
s = input('','s');
else
s = {};
done = false;
if verbose
disp('Input program. End with blank line:')
disp(' ')
end
while ~done
fprintf(matlInputPrompt); % change prompt to show we are accepting MATL content
s{end+1} = input('','s');
done = isempty(s{end});
end
% s = strjoin(s, '\n'); % `strjoin` doesn't exist in old versions
s = sprintf('%s\n',s{:}); s = s(1:end-1);
end
end
% Read file, if needed
if any(options=='f')
fid = fopen(s,'r');
s = fread(fid);
fclose(fid);
s = char(s.');
end
% Call help, if required, and quit
if any(options=='h')
if nargin>1
matl_help(H, s, verbose, useTags)
else
matl_help(H, '', verbose, useTags)
end
return
end
% Call parser, if required
if any(ismember(options,'plecrdfv'))
if verbose
disp('Parsing program')
end
S = matl_parse(s, useTags);
if verbose
if numel(S)~=1
fprintf(' %i statements parsed\n', numel(S))
else
fprintf(' 1 statement parsed\n')
end
end
end
% Save parsed output, and display if required
if any(ismember(options,'le'))
matl_disp(S, F, indentBase, indentStep, indentCommentSymbol, indentCommentText, pOutFile, true, any(options=='e')||numel(numericOptions)>=3, any(options=='e'), verbose);
if ~verbose && any(ismember(options,'rd'))
pause
end
else
matl_disp(S, F, indentBase, indentStep, indentCommentSymbol, indentCommentText, pOutFile, false, false, false, verbose);
end
% Compile parsed program, if required
if any(ismember(options,'csrd'))
if verbose
disp('Compiling program')
end
S = matl_compile(S, F, L, pOutFile, cOutFile, verbose, isMatlab, verNum, useTags, online);
%if verbose
% disp(' Done.')
%end
end
% Run compiled program, if required
if any(options=='r')
if verbose
str = 'Press any key to run program';
disp(str)
%disp('--') %disp(repmat('-',size(str)))
pause
end
matl_run(S, pOutFile, cOutFileNoExt, [], isMatlab, useTags) % ...NoExt because a file name without extension is
% needed in old Matlab versions
end
% Run compiled program in debug mode, if required
if any(options=='d')
if verbose
disp('Press any key to run MATL program in debug mode')
pause
end
matl_run(S, pOutFile, cOutFileNoExt, [S.compileLine], isMatlab, useTags) % ...NoExt because a file name without
% extension is needed in old Matlab versions
end