Skip to content
check_matlab_syntax_condition.m 2.56 KiB
Newer Older
        %% check_matlab_syntax
        %   this function will check that a string is valid matlab syntax
        %   for the context that it is in. we need to build up the
        %   expression which involves initiallizeing the input variables to
        %   a dummy value and wraping an if statement around conditionals
        % inputs:
        %   object:GUI - current GUI object
        %   string:string - string to be checked
        %   result:boolean - false if the string is a conditoin true if
        %       the string is an output statement
        % outputs:
        %   error:string - string containing the error message, empty if
        %   there is no error
        function error = check_matlab_syntax_condition(object,string,result)
           % split the list of inputs to get inputs seperatly
           
           parsed_input = EMLGenerator.parse_inputs(get(object.function_inputs_control,'string'));
           check_string = [];
           
           % initialize inputs to zero
           % functions are assumed to be total, so 0 is just for 
           % convienence
           for i=1:size(parsed_input,2)
                % set to zero
                check_string = [check_string sprintf('%s=0;\n',char(parsed_input{i}(1)))];
                
                   
           end
           if ~result
                % the string is a condition we need to evaulate it as it
                % will be used in the code, as in order for an if statement
                % to be valid the condition must evaulate to a numeric
                % value not a structure or something else. we need to have
                % the 1 in there as a dummy output, which is necesary for
                % an if statement to be valid in matlab.
                check_string = [check_string 'if (' string sprintf(')\n1;\nend')];
           else
               % the string is an output statement we can just evaulate the
               % string on its own.
               check_string = [check_string string ';' sprintf('\n')];
           end
           % attempt to evaluate the string catch the error.
           try
               eval(check_string);
               error = [];
           catch exception
               error = exception.message;
                % attempt to make one of the common errors slightly less
                % cryptic.
               if(strcmp(exception.identifier,'MATLAB:m_invalid_lhs_of_assignment'))
                   error = [error sprintf('\nTo check equality use ==')];
               end
           end
        end