forked from lmendo/MATL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genFunDef.m
61 lines (56 loc) · 2.66 KB
/
genFunDef.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
function F = genFunDef(masterFileName, fileName)
%
% F = genFunDef(masterFileName, fileName)
%
% Generates function definition struct array "F" from tab-separated text
% file, and saves it in file.
%
% The tab-separated file has one or more lines for each function.
% The first line for each function contains its source code in the first
% column. If a function has the sixth column ("body") empty, the function
% is considered to be undefined, and is not included in struct array "F".
%
% Wraps function body in a cell, even if it's a single string.
% Function body in the text file may span several strings. This is defined
% using subsequent lines with "source" column empty and "body" field
% filled. In that case, this function collects all lines in a cell array of
% strings.
%
% Luis Mendo
fieldNames = {'source' 'minIn' 'maxIn' 'defIn' 'altIn' 'minOut' 'maxOut' 'defOut' 'altOut' 'consumeInputs' 'wrap' 'funInClipboard' 'allowedOnline' 'body' 'comment' 'description'};
nCol_consumeInputs = find(strcmp(fieldNames, 'consumeInputs'));
nCol_wrap = find(strcmp(fieldNames, 'wrap'));
nCol_funInClipboard = find(strcmp(fieldNames, 'funInClipboard'));
nCol_allowedOnline = find(strcmp(fieldNames, 'allowedOnline'));
nCol_body = find(strcmp(fieldNames, 'body'));
fid = fopen(masterFileName, 'r');
F = reshape(fread(fid,inf,'*char'),1,[]);
fclose(fid);
F = reshape(regexp(F, '[\r\n]+', 'split'),[],1);
ind = cellfun(@(s) ~isempty(s)&&s(1)~='%', F);
F = F(ind);
F = regexp(F, ' *\t *', 'split');
n = numel(fieldNames);
F = cellfun(@(s) [ s(1:min(n,end)) repmat({''},1,max(n-numel(s),0)) ], F, 'uniform', 0); % fill if empty columns
F = vertcat(F{:});
F = F(~cellfun(@isempty, F(:,nCol_body)),:); % remove functions that have a line in
% the file but are not actually defined. These are identified because "body"
% column is empty.
% Transform 'consumeInputs', 'wrap', 'funInClipboard' fields into logical values:
for c = [nCol_consumeInputs nCol_wrap nCol_funInClipboard nCol_allowedOnline]
for r = 1:size(F,1)
F{r,c} = logical(str2num(F{r,c})); %#ok
end
end
% Join function body lines corresponding to the same function source into
% a cell array of strings. Single lines are also wrapped into a cell array.
d = [~cellfun('isempty', F(:,1)); true];
starts = find(d(1:end-1));
ends = find(d(2:end));
for n = numel(starts):-1:1 % process from end because F will be shrunk along the way
F{starts(n),nCol_body} = F(starts(n):ends(n),nCol_body).';
F(starts(n)+1:ends(n),:) = [];
end
assert(numel(unique(F(:,1)))==numel(F(:,1)), 'MATL:compiler:internal', 'MATL internal error while reading function definition file: function names are not unique')
F = cell2struct(F, fieldNames, 2);
save(fileName, 'F')