Commit 103d5be9 authored by Colin Eles's avatar Colin Eles
Browse files

in process of spliting some functionality outof gui, this rev will not work properly yet

git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/trunk/TableTool@5875 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent b0a5223c
Loading
Loading
Loading
Loading

EMLGenerator.m

0 → 100644
+387 −0
Original line number Diff line number Diff line
classdef EMLGenerator < handle
    %UNTITLED Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        data = [];
    end
    
    methods
        
        function obj = EMLGenerator(data)
            obj.data = data;
        end
        
        function code = generate_preamble(obj)
                code = [];

                %generate input list
                parsed_input = object.parse_inputs(get(object.function_inputs_control,'string'));
                input = [];
                for i= 1:size(parsed_input,2)
                    input = [input char(parsed_input{i}(1))];
                    if i ~= size(parsed_input,2)
                        input = [input ',']
                    end
                end
                code = sprintf('function output = %s(%s)\n',get(object.function_name_control,'String'),input);
                % 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.
                code = [code sprintf('output=%s(%s);\n',object.output_data_type,get(object.Grid0.Cells(1).result,'String'))];
        
        end
        
        
        function code = generate_eml_code(obj)
            code = [];
            code = [code obj.generate_preamble];
            code = [code obj.generate_conditional];
            
        end
        
         %% generate_code
        %   this function will generate a string that represents the
        %   embedded matlab code. code will have proper syntax
        %   indentations.
        % inputs:
        %   obj:GUI - current GUI object
        %   g1:Grid - the grid on the top
        %   g2:Grid - the grid on the left
        %   depth:integer - the depth of the current call, incremented
        %      every recursive call, initial value should be 0. used for
        %      indenting the code.
        % outputs;
        %   error:string - string containing the error message, empty if
        %   there is no error
        function code = generate_conditional(obj,g1,g2,depth)
            space = '';
            % space is equal to 2 * depth number of ' '
            for i=1:depth
                space = [space sprintf('  ')];
            end
            code = [];
            g1cond1 = get(g1.cells(1).cond,'String');
            g2cond1 = get(g2.cells(1).cond,'String');
           
            % 1D horizontal table
            if (isempty(g2cond1) && ~isempty(g1cond1))
                found = 0;
                elsecell = [];
                for j=1:size(g1.cells,2)
                    g1cond = get(g1.cells(j).cond,'String');
                    % condition string of otherwise corresponds to an else
                    % statement, we allow this statement to be in any order
                    % in the cells of the grid, so we need to find where it
                    % is, if it exists.
                    if (strcmp(g1cond,'otherwise'))
                        elsecell = g1.cells(j);
                        found = 1;
                        continue
                    end
                    if (j == 1 || (j==2 && found == 1))
                        code = [code sprintf('%sif(%s)\n',space,strtrim(char(get(g1.cells(j).cond,'String'))))];
                    else
                        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(get(g1.cells(j).cond,'String'))))];
                    end
                    
                    cell = obj.Grid0.search_return(g1.cells(j),g2.cells(1));
                    if(~isempty(cell))
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        code = [code sprintf('%soutput = %s(%s);\n',space,obj.output_data_type,strtrim(char(get(cell.result,'String'))))];
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    else
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        code = [code sprintf('%smissing!!!\n',space)];
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    end
                    
                end
                if (~isempty(elsecell))
                    code = [code sprintf('%selse\n',space)];

                    cell = obj.Grid0.search_return(elsecell,g2.cells(1));
                    if(~isempty(cell))
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        code = [code sprintf('%soutput = %s(%s);\n',space,obj.output_data_type,strtrim(char(get(cell.result,'String'))))];
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    else
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        code = [code sprintf('%smissing!!!\n',space)];
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    end
                end
                code = [code sprintf('%send\n',space)];

            %either 2D table, 1D vertical table or empty table    
            else
                found = 0;
                elsecell = [];
                for i=1:size(g2.cells,2)
                    g2cond = get(g2.cells(i).cond,'String');
                    if (strcmp(g2cond,'otherwise'))
                        elsecell = g2.cells(i);
                        elseindex = i;
                        found = 1;
                        continue
                    end

                    if (i == 1 || (i == 2 && found == 1))
                        code = [code sprintf('%sif(%s)\n',space,strtrim(char(get(g2.cells(i).cond,'String'))))];
                    else
                        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(get(g2.cells(i).cond,'String'))))];
                    end
                    
                    if (~isempty(g2.cells(i).subgrid))
                        code = [code obj.generate_code(g1,g2.cells(i).subgrid,depth+1)];
                    else
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        found1 = 0;
                        elsecell1 = [];
                        for j=1:size(g1.cells,2)
                             g1cond = get(g1.cells(j).cond,'String');
                            if (strcmp(g1cond,'otherwise'))
                                elsecell1 = g1.cells(j);
                                found1 = 1;
                                continue
                            end
                            
                            if(~isempty(g1.cells(j).subgrid))
                                %%TODO if add more dimensions to grid1
                            else
                                if (~isempty(g1cond1))
                                    if (j == 1 || (j==2 && found1 == 1))
                                        code = [code sprintf('%sif(%s)\n',space,strtrim(char(get(g1.cells(j).cond,'String'))))];
                                    else
                                        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(get(g1.cells(j).cond,'String'))))];
                                    end
                                end
                                
                                cell = obj.Grid0.search_return(g1.cells(j),g2.cells(i))
                                if(~isempty(cell))
                                    depth = depth + 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                    code = [code sprintf('%soutput = %s(%s);\n',space,obj.output_data_type,strtrim(char(get(cell.result,'String'))))];
                                    depth = depth - 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                else
                                    depth = depth + 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                    code = [code sprintf('%smissing!!!\n',space)];
                                    depth = depth - 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                end
                                
                            end
                        end
                        if(~isempty(g1cond1))
                            if(size(g1.cells,2)>0)
                                if (~isempty(elsecell1))
                                    code = [code sprintf('%selse\n',space)];

                                    cell = obj.Grid0.search_return(elsecell1,g2.cells(i))
                                    if(~isempty(cell))
                                        depth = depth + 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                        code = [code sprintf('%soutput = %s(%s);\n',space,obj.output_data_type,strtrim(char(get(cell.result,'String'))))];
                                        depth = depth - 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                    else
                                        depth = depth + 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                        code = [code sprintf('%smissing!!!\n',space)];
                                        depth = depth - 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                    end
                                end
                                code = [code sprintf('%send\n',space)];
                            end
                        end
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                    end
                    
                end
                
                if(~isempty(elsecell))
                    code = [code sprintf('%selse\n',space)];
                    if (~isempty(g2.cells(elseindex).subgrid))
                        code = [code obj.generate_code(g1,g2.cells(elseindex).subgrid,depth+1)];
                    else
                        depth = depth + 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        found1 = 0;
                        for j=1:size(g1.cells,2)
                             g1cond = get(g1.cells(j).cond,'String');
                            if (strcmp(g1cond,'otherwise'))
                                elsecell1 = g1.cells(j);
                                found1 = 1;
                                continue
                            end
                            
                            if(~isempty(g1.cells(j).subgrid))
                                %%TODO if add more dimensions to grid1
                            else
                                if (~isempty(g1cond1))
                                    if (j == 1 || (j==2 && found1 == 1))
                                        code = [code sprintf('%sif(%s)\n',space,strtrim(char(get(g1.cells(j).cond,'String'))))];
                                    else
                                        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(get(g1.cells(j).cond,'String'))))];
                                    end
                                end
                                
                                cell = obj.Grid0.search_return(g1.cells(j),g2.cells(elseindex));
                                if(~isempty(cell))
                                    depth = depth + 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                    code = [code sprintf('%soutput = %s(%s);\n',space,obj.output_data_type,strtrim(char(get(cell.result,'String'))))]
                                    depth = depth - 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                else
                                    depth = depth + 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                    code = [code sprintf('%smissing!!!\n',space)];
                                    depth = depth - 1;
                                    space = [];
                                    for k=1:depth
                                        space = [space sprintf('  ')];
                                    end
                                end
                                
                            end
                        end
                        if(~isempty(g1cond1))
                            if(size(g1.cells,2)>0)
                                if (~isempty(elsecell1))
                                    code = [code sprintf('%selse\n',space)];

                                    cell = obj.Grid0.search_return(elsecell1,g2.cells(elseindex));
                                    if(~isempty(cell))
                                        depth = depth + 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                        code = [code sprintf('%soutput = %s(%s);\n',space,obj.output_data_type,strtrim(char(get(cell.result,'String'))))];
                                        depth = depth - 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                    else
                                        depth = depth + 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                        code = [code sprintf('%smissing!!!\n',space)];
                                        depth = depth - 1;
                                        space = [];
                                        for k=1:depth
                                            space = [space sprintf('  ')];
                                        end
                                    end
                                end
                                code = [code sprintf('%send\n',space)];
                            end
                        end
                        depth = depth - 1;
                        space = [];
                        for k=1:depth
                            space = [space sprintf('  ')];
                        end
                        
                    end
                end
                code = [code sprintf('%send\n',space)];
                
            end
            
            
            
        end
        
    end
    
end
+11 −618

File changed.

Preview size limit exceeded, changes collapsed.

TableBlock.m

0 → 100644
+252 −0

File added.

Preview size limit exceeded, changes collapsed.

+3 −3

File changed.

Contains only whitespace changes.