Newer
Older
%% generate_preamble
% generates the preamble for the eml code, the preamble is
% everything before the first if statement, this is seperated
% out to make the code simpler.
% inputs:
% object:EMLGenerator - current object
% outputs:
% code:string - string of eml code
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function code = generate_preamble(object)
Colin Eles
committed
code = [];
function_name = EMLGenerator.parse_inputs(object.data.function_name);
%generate input list
parsed_input = EMLGenerator.parse_inputs(object.data.function_inputs);
input = [];
for i= 1:size(parsed_input,2)
input = [input char(parsed_input{i}(1))];
if i ~= size(parsed_input,2)
Colin Eles
committed
output = [];
if (object.data.multi_mode == 1)
for i=1:size(object.data.Grid1.cells,2)
parsed_output = EMLGenerator.parse_inputs(strtrim(char(object.data.Grid1.cells(i).cond_text)));
output = [output char(parsed_output{1}(1))];
if i ~= size(object.data.Grid1.cells,2)
end
end
else
output = 'output';
end
Colin Eles
committed
Colin Eles
committed
exception_flag = object.data.settings.except;
if (exception_flag)
code = sprintf('function [%s, exception] = %s(%s)\n%s\n',output,char(function_name{1}(1)),input,'%%#eml');
else
code = sprintf('function [%s] = %s(%s)\n%s\n',output,char(function_name{1}(1)),input,'%%#eml');
end
% simulink forces you to have an output for all execution paths
% since it can't compute completness and disjointness we need
% to have a default value, if the user builds the table
% properly the default value will never be used. since
% different types might have a different default value, the
% temporary solution is just to use one of the outputs from our
% table, we will use the first cell because it is
% guaranteed to
% be filled in, regardless of the dimensionality of the table.
Colin Eles
committed
if (object.data.multi_mode == 1)
for i=1:size(object.data.Grid1.cells,2)
parsed_output = EMLGenerator.parse_inputs(strtrim(char(object.data.Grid1.cells(i).cond_text)));
output_str = char(parsed_output{1}(1));
code = [code sprintf('%s=%s;\n',output_str,EMLGenerator.type_convert(output_str,object.datatype,char(object.data.Grid0.Cells(i).result_text)))];
end
else
test = 'output';
output_string = EMLGenerator.type_convert(test,object.datatype,char(object.data.Grid0.Cells(1).result_text) );
code = [code sprintf('output=%s;\n', output_string )];
Colin Eles
committed
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
end
if (exception_flag)
% generate NaN exceptions
nan_string = '';
% genereate constraint exception
cons_string = '';
for i = 1:size(parsed_input,2)
% for each input consider if a NaN
nan_string = [nan_string 'isnan(' char(parsed_input{i}(1)) ')'];
if i ~= size(parsed_input,2)
nan_string = [nan_string '||'];
end
if size(parsed_input{i},2) == 2
str = regexp(parsed_input{i}(2),'(?<=\|).*(?=})','match','once');
cons_string = [cons_string sprintf('if ~(%s) \n exception = true;\nreturn;\nelse\nexception = false;\nend\n',char(str))];
end
end
code = [code sprintf('if (%s) \n exception = true;\nreturn;\nelse\nexception = false;\nend\n',nan_string)];
code = [code cons_string];