Commit fdecf616 authored by Colin Eles's avatar Colin Eles
Browse files

fixed up eml code generation procedure, more efficient and easier to read now

git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/trunk/TableTool@5878 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent c455f5c2
Loading
Loading
Loading
Loading
+127 −2
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ classdef EMLGenerator < handle
                % 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',obj.datatype,obj.data.Grid0.Cells(1).result_text)];
                code = [code sprintf('output=%s(%s);\n',obj.datatype,char(obj.data.Grid0.Cells(1).result_text))];
        
        end
        
@@ -90,7 +90,8 @@ classdef EMLGenerator < handle
        function code = generate_eml_code(obj)
            code = [];
            code = [code obj.generate_preamble];
            code = [code obj.generate_conditional(obj.data.Grid1,obj.data.Grid2,0)];
            code = [code obj.generate_eml2(obj.data.Grid1,obj.data.Grid2,0)];
            %code = [code obj.generate_conditional(obj.data.Grid1,obj.data.Grid2,0)];
            
        end
        
@@ -433,6 +434,130 @@ classdef EMLGenerator < handle
            
        end
        
        
        
        %% generate_eml_horizontal
        function code = generate_eml_horizontal2(obj,g1,g2_cell,depth)
            space = '';
            for i=1:depth
                space = [space sprintf('  ')];
            end
        code = [];

            elsecell = [];
            for i=1:size(g1.cells,2)
                g1cond = g1.cells(i).cond_text;
                % 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(i);
                    found = 1;
                    continue
                end
                if (i == 1)
                    code = [code sprintf('%sif(%s)\n',space,strtrim(char(g1.cells(i).cond_text)))];
                else
                    code = [code sprintf('%selseif(%s)\n',space,strtrim(char(g1.cells(i).cond_text)))];
                end
                resultcell = obj.data.Grid0.search_return(g1.cells(i),g2_cell);
                if(~isempty(resultcell))
                     code = [code sprintf('%soutput = %s(%s);\n',[space '  '],obj.datatype,strtrim(char(resultcell.result_text)))];
                                      
                  
                else
                end
            end
            if(~isempty(elsecell))
                 resultcell = obj.data.Grid0.search_return(elsecell,g2_cell);
                code = [code sprintf('%selse\n%soutput = %s(%s);\n',space,[space '  '],obj.datatype,strtrim(char(resultcell.result_text)))];

            end
            code = [code sprintf('%send\n',space)];

       
            
     
        end
        
        %% genereate_pvs
        function code = generate_eml2(obj,g1,g2,depth)
             space = '';
            for i=1:depth
                space = [space sprintf('  ')];
            end
          g1cond1 = g1.cells(1).cond_text;
          g2cond1 = g2.cells(1).cond_text;
          code = [];
          %1D horizontal
          if(~isempty(g1cond1) && isempty(g2cond1))
            code = [code obj.generate_eml_horizontal2(g1,g2.cells(1),depth)];
          % something in vertical column
          else

            elsecell = [];
            for i=1:size(g2.cells,2)
                g2cond = g2.cells(i).cond_text;
                % 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(g2cond,'otherwise'))
                    elsecell = g2.cells(i);
                    found = 1;
                    continue
                end
                if (i == 1)
                    code = [code sprintf('%sif(%s)\n',space,(strtrim(char(g2.cells(i).cond_text))))];
                else
                    code = [code sprintf('%selseif(%s)\n',space,(strtrim(char(g2.cells(i).cond_text))))];
                end

                if(~isempty(g2.cells(i).subgrid))
                    code = [code obj.generate_eml2(g1,g2.cells(i).subgrid,depth+1)];
                else
                    if (~isempty(g1cond1))
                         code = [code obj.generate_eml_horizontal2(g1,g2.cells(i),depth+1)];
                    else    
                
                        resultcell = obj.data.Grid0.search_return(g1.cells(1),g2.cells(i));
                        if(~isempty(resultcell))
                                code = [code sprintf('%soutput = %s(%s);\n',[space '  '],obj.datatype,strtrim(char(resultcell.result_text)))];

                            
                        else
                        end
                    end
                end
              
            end
           if(~isempty(elsecell))
                 %resultcell = obj.Grid0.search_return(elsecell,elsecell);
                code = [code sprintf('%selse\n',space)];

                if(~isempty(elsecell.subgrid))
                    code = [code obj.generate_pvs(g1,elsecell.subgrid,depth) sprintf('\n')];
                else
                    if (~isempty(g1cond1))
                         code = [code obj.generate_eml_horizontal2(g1,elsecell,depth) sprintf('\n')];
                    else    
                
                        resultcell = obj.data.Grid0.search_return(g1.cells(1),elsecell);
                        if(~isempty(resultcell))
                             code = [code sprintf('%soutput = %s(%s);\n',[space '  '],obj.datatype,strtrim(char(resultcell.result_text)))];
                                
                        end
                    end
                end
                
           end

         code = [code sprintf('%send\n',space)];

          end
        end
        
    end
    
end