Home > arte3.2.0 > RAPID > interpreter > matlab2RAPID.m

matlab2RAPID

PURPOSE ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SYNOPSIS ^

function matlab2RAPID(varargin)

DESCRIPTION ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   matlab2RAPID creates the equivalent RAPID program of the matlab program

     matlab2RAPID(SinkFile_name, sinkfile_name) saves the program on a new file
   called sinkfile_name.prg in the path.  

   For example:
   Example: 
   matlab2RAPID('SourceFile.m','SinkFile.prg')

   gets the matlab instructions wtritten for simulation in SinkFile.m
   and write the equivalent RAPID instruction in sinkfile.prg


   Author: Angel Rodriguez. Universidad Miguel Hernandez de Elche. email:
   arodgre@gmail.com 

   Coauthor: Arturo Gil. Universidad Miguel Hernandez de Elche. email:
   arturo.gil@umh.es date:   24/09/2013
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0002 %   matlab2RAPID creates the equivalent RAPID program of the matlab program
0003 %
0004 %     matlab2RAPID(SinkFile_name, sinkfile_name) saves the program on a new file
0005 %   called sinkfile_name.prg in the path.
0006 %
0007 %   For example:
0008 %   Example:
0009 %   matlab2RAPID('SourceFile.m','SinkFile.prg')
0010 %
0011 %   gets the matlab instructions wtritten for simulation in SinkFile.m
0012 %   and write the equivalent RAPID instruction in sinkfile.prg
0013 %
0014 %
0015 %   Author: Angel Rodriguez. Universidad Miguel Hernandez de Elche. email:
0016 %   arodgre@gmail.com
0017 %
0018 %   Coauthor: Arturo Gil. Universidad Miguel Hernandez de Elche. email:
0019 %   arturo.gil@umh.es date:   24/09/2013
0020 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0021 
0022 
0023 function matlab2RAPID(varargin)
0024 if nargin==0
0025     [FileName,PathName] = uigetfile({'*.m','SinkFile (*.m)'},'Pick a program.m file', 'MultiSelect', 'off');
0026     Source_name=FileName;%Saves name of the file
0027     i=1;
0028     while Source_name(i)~='.'
0029         if i<6
0030         Sink_name(i)=Source_name(i);
0031         end
0032         i=i+1;
0033     end
0034     Module_name=upper(Sink_name);
0035     Sink_name=strcat(Sink_name,'.prg');
0036     cd(PathName);
0037 elseif nargin==1
0038     Source_name=varargin{1};
0039     i=1;
0040     while Source_name(i)~='.' 
0041         if i<6
0042         Sink_name(i)=Source_name(i);
0043         end
0044         i=i+1;
0045     end
0046     Sink_name=strcat(Sink_name,'.prg');
0047     %Path_name: get the path of Source_file
0048     %cd(Path_name);
0049 elseif nargin==2
0050     Source_name=varargin(1);
0051     Sink_name=varargin(2);
0052     %Path_name: get the path of Source_file
0053     %cd(Path_name);
0054 end
0055   fprintf('Converting Matlab into RAPID lenguaje...\n\n');  
0056   fprintf('The name of the RAPID program is: %s\n\n', Sink_name);
0057   SinkFile=fopen(Sink_name,'w+');
0058   a=0;
0059   SourceFile = fopen(Source_name, 'r');
0060   %include a comment block
0061   
0062   fprintf(SinkFile,'%c%c%c\nVERSION:1\nLANGUAGE:ENGLISH\n%c%c%c\n',37, 37, 37,37, 37, 37);
0063 
0064   
0065     lines=1; %Variable to control de line of the source file
0066   string=fgets(SourceFile);% fgets gets the next line and saves it in string
0067   %The first line with code must be the main function for writing the
0068   %module sentence
0069   while isempty(strfind(string,'%'))==0 | string(1)==10 | string(1)==' '
0070       if string(1)==10
0071           fprintf(SinkFile,'\n');
0072       elseif string(1)==' '
0073               if isempty(strfind(string,'%'))==0
0074                   i=strfind(strfind(string,'%'));
0075                   fprintf(SinkFile,'!%s', string(i+1:length(string)));
0076               end
0077               i=0;
0078               while string(i)==' '
0079                   i=i+1;
0080               end
0081               if string(i)==10
0082                     fprintf(SinkFile,'\n');
0083               end
0084       else
0085       fprintf(SinkFile,'!%s', string(2:length(string)));
0086       end
0087       string=fgets(SourceFile);
0088   end
0089   if isempty(strfind(string,'function'))==1
0090       fprintf(SinkFile,'MODULE %s\n',Module_name);
0091       a=1;%Var used to mantein the first sentence when is not a function declaration
0092   else
0093       Start_Name=strfind(string,' ');
0094       string=upper(string(Start_Name(1)+1:Start_Name(1)+5))
0095       fprintf(SinkFile,'MODULE %s',string);
0096   end
0097   
0098   
0099   while ~feof(SourceFile)%Reads the file until it�s ends
0100   
0101   if a~=1%If there's no function declaration first sentence is saved in string
0102   string=fgets(SourceFile);
0103   end
0104   a=0;
0105   line_array = char(string);%The string is saved as an array
0106   %%%% Each line is readed and teh key word is looked for
0107   if FindCommentsOrNewLines(line_array,SinkFile,lines,SourceFile)==1
0108   else
0109       InitialTab(line_array,SinkFile);
0110       words_array=strtokenizador(line_array,lines,SinkFile);
0111       if strfinder('function',words_array,lines)==1
0112         Start_Name=strfind(string,' ');
0113         string=string(Start_Name(1)+1:length(string)-1);
0114         contrabarra= '\';
0115         if isempty(strfind(string,'('))==1
0116             fprintf(SinkFile,'PROC %s()\n',string);
0117         else
0118             fprintf(SinkFile,'PROC %s\n',string);
0119         end
0120         if strfinder('main',words_array,lines)==1
0121             fprintf(SinkFile,'\nConfJ %cOff;\nConfL %cOff;\n',contrabarra, contrabarra);
0122         end
0123         ModifyFunction(SourceFile,SinkFile);
0124       else
0125         FindAndModify(words_array,lines,SourceFile,string,SinkFile);
0126       end
0127       lines=lines+1;
0128       clearvars words_array;
0129       clearvars line_array;
0130   end%else&if
0131   end% End of the file�s detection
0132   p=fclose(SourceFile);
0133   if p==1
0134     fprintf('An error ocurred while file was being closed\n');
0135   end
0136   
0137   fprintf(SinkFile,'\nENDMODULE');
0138   fclose(SinkFile);
0139 end
0140  
0141 function words_array=strtokenizador(line_array,lines,SinkFile)
0142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0143 %%%  Function that separates the sentence on it's words              %%%
0144 %%%  This function does not separate variables whit '_'              %%%
0145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0146 
0147   i=1;%Integer that marks the positi�n in line_array
0148   a=0;%Integer that saves the begining of each word
0149   word_number=0;%Counts the number of words in word_array
0150   InitialTab(line_array,SinkFile);
0151   while i<=length(line_array)
0152 
0153     if (line_array(i)>='A'&&line_array(i)<='Z')|(line_array(i)>='a'&&line_array(i)<='z')| line_array(i)=='_'
0154       %If it's a letter goes to the next position
0155     elseif line_array(i)>='0' & line_array(i)<='9'  
0156       %If it's a number it's necesary to Know if its a float or a word
0157       %If it�s a float has a point followed by more numbers. If the point
0158       %is detected the word it's not separed
0159       if line_array(i+1)=='.'
0160           i=i+1;          
0161       end
0162     else 
0163       if i~=a+1%If there are 2 pointsymbol together the second is ignored
0164         j=i-1;
0165         word_number=word_number+1; %there's another word
0166          while j>a
0167             words_array(lines,word_number,j-a)=line_array(j);
0168             j=j-1;
0169          end
0170       end
0171       a=i;%New word's begining
0172     end
0173     i=i+1;
0174   end
0175  end
0176 
0177 function FindAndModify(words_array,lines,SourceFile,string,SinkFile)
0178       Functions=FindAndModifyFunctions(words_array,lines,SinkFile);
0179       Cycles=FindAndModifyCycles(words_array,lines,SourceFile,string,SinkFile);
0180       Allocations=FindAndModifyAllocations(string,SinkFile);
0181       if Functions==0 & Cycles==0 & Allocations==0 & isempty(strfind(string,'else'))==1
0182         k=1;
0183         while string(k)==11 | string(k)==32
0184             k=k+1;%Instruction for clear white space and horizontal tab before the asignation
0185         end
0186         fprintf(SinkFile,'%s',string(k:length(string)));
0187       end
0188 end
0189 
0190 function finder=FindAndModifyFunctions(words_array,lines,SinkFile)
0191 finder=0;
0192 if strfinder('MoveJ',words_array,lines)==1
0193     ModifyMoveJ(words_array,lines,SinkFile)
0194     finder=1;
0195 end
0196 if strfinder('MoveC', words_array,lines)==1
0197     ModifyMoveC(words_array,lines,SinkFile);
0198     finder=1;
0199 end
0200 if strfinder('MoveL',words_array,lines)==1
0201     ModifyMoveL(words_array,lines,SinkFile);
0202     finder=1;
0203 end
0204 if strfinder('Waittime',words_array,lines)==1
0205     fprintf(SinkFile,'%s %s;\n', words_array(lines,1,:), words_array(lines,2,:));
0206     finder=1;
0207 end
0208 
0209 if strfinder('global',words_array,lines)==1%Do nothing
0210     finder=1;
0211 end
0212 if strfinder('end',words_array,lines)==1%Do nothing for function main
0213     finder=1;
0214 end
0215 if strfinder('drawrobot3d',words_array,lines)==1%Do nothing
0216     finder=1;
0217 end
0218 if strfinder('simulation',words_array,lines)==1%Do nothing
0219     finder=1;
0220 end
0221 if strfinder('main',words_array,lines)==1%Do nothing
0222      finder=1;
0223 %      if strfinder('function',words_array,lines)==1
0224 %          finder=1;
0225 %      end
0226 end
0227 
0228 end
0229 
0230 
0231 function finder=FindAndModifyCycles(words_array,lines,SourceFile,string,SinkFile)
0232 finder=0;
0233 if strfinder('for',words_array,lines)==1
0234     ModifyFor(words_array,lines,SourceFile,SinkFile);
0235     finder=1;
0236 end
0237 if strfinder('while',words_array,lines)==1
0238     ModifyWhile(words_array,lines,SourceFile,string,SinkFile);
0239     finder=1;
0240 end
0241 if strfinder('if',words_array,lines)==1
0242     ModifyIf(words_array,lines,SourceFile,string,SinkFile);
0243     finder=1;
0244 end
0245 end
0246 
0247 function ModifyMoveJ(words_array,line, SinkFile)
0248     contrabarra= '\';
0249     word(1,:)=char(words_array(line,2,:));
0250     if strncmp(word,'Offs',4)==1
0251        cad1(1,:)=char(words_array(line,4,:));
0252        cad2(1,:)=char(words_array(line,5,:));
0253        cad3(1,:)=char(words_array(line,6,:));
0254        cad1=int2str(eval(strcat(cad1,'*1000')));
0255        cad2=int2str(eval(strcat(cad2,'*1000')));
0256        cad3=int2str(eval(strcat(cad3,'*1000')));
0257        if size(words_array,2)<10
0258        %If it�s empty we will refer the targetpoint to the world's
0259        %reference system
0260        fprintf(SinkFile,'%s %s(%s,%s,%s,%s),%s,%s,%s%cWObj:=wobj0;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), cad1, cad2, cad3, words_array(line,7,:),words_array(line,8,:),words_array(line,9,:),contrabarra);
0261        else
0262        fprintf(SinkFile,'%s %s(%s,%s,%s,%s),%s,%s,%s%cWObj:=%s;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), cad1, cad2, cad3,words_array(line,7,:),words_array(line,8,:),words_array(line,9,:),contrabarra,words_array(line,10,:));  
0263        end
0264     else
0265        if size(words_array,2)<6
0266        %If it�s empty we will refer the targetpoint to the world's reference system
0267        fprintf(SinkFile,'%s %s,%s,%s,%s%cWObj:=wobj0;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), words_array(line,4,:),words_array(line,5,:),contrabarra);
0268        else
0269        fprintf(SinkFile,'%s %s,%s,%s,%s%cWObj:=%s;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), words_array(line,4,:), words_array(line,5,:),contrabarra,words_array(line,6,:));
0270        end
0271     end
0272 end
0273 
0274 function ModifyMoveC(words_array,line,SinkFile)
0275 contrabarra= '\';
0276 word(1,:)=char(words_array(line,2,:));
0277 word2(1,:)=char(words_array(line,3,:));
0278 if strncmp(word,'Offs',4)==1%If Offs is found in the first point
0279     word3(1,:)=char(words_array(line,7,:));
0280     if strncmp(word3,'Offs',4)==1% And the second targetpoint it�s also asigned with offs
0281        cad1(1,:)=char(words_array(line,4,:));
0282        cad2(1,:)=char(words_array(line,5,:));
0283        cad3(1,:)=char(words_array(line,6,:));
0284        cad4(1,:)=char(words_array(line,9,:));
0285        cad5(1,:)=char(words_array(line,10,:));
0286        cad6(1,:)=char(words_array(line,11,:));
0287        cad1=int2str(eval(strcat(cad1,'*1000')));
0288        cad2=int2str(eval(strcat(cad2,'*1000')));
0289        cad3=int2str(eval(strcat(cad3,'*1000')));
0290        cad4=int2str(eval(strcat(cad4,'*1000')));
0291        cad5=int2str(eval(strcat(cad5,'*1000')));
0292        cad6=int2str(eval(strcat(cad6,'*1000')));
0293        if size(words_array,2)<15%Wobj isn't defined
0294             fprintf(SinkFile,'%s %s(%s,%s,%s,%s),%s(%s,%s,%s,%s),%s,%s,%s%cWObj:=wobj0;\n',words_array(line,1,:),words_array(line,2,:),words_array(line,3,:),cad1,cad2,cad3,words_array(line,7,:),words_array(line,8,:),cad4,cad5,cad6,words_array(line,12,:),words_array(line,13,:),words_array(line,14,:),contrabarra );
0295         else
0296             fprintf(SinkFile,'%s %s(%s,%s,%s,%s),%s(%s,%s,%s,%s),%s,%s,%s%cWObj:=%s;\n',words_array(line,1,:),words_array(line,2,:),words_array(line,3,:),cad1,cad2,cad3,words_array(line,7,:),words_array(line,8,:),cad4,cad5,cad6,words_array(line,12,:),words_array(line,13,:),words_array(line,14,:),contrabarra,words_array(line,15,:));
0297         end
0298     else%Just the first targetpoint is asigned with offs
0299        cad1(1,:)=char(words_array(line,4,:));
0300        cad2(1,:)=char(words_array(line,5,:));
0301        cad3(1,:)=char(words_array(line,6,:));
0302        cad1=int2str(eval(strcat(cad1,'*1000')));
0303        cad2=int2str(eval(strcat(cad2,'*1000')));
0304        cad3=int2str(eval(strcat(cad3,'*1000')));
0305         if size(words_array,2)<11%Wobj isn't defined
0306             fprintf(SinkFile,'%s %s(%s,%s,%s,%s),%s,%s,%s,%s%cWObj:=wobj0;\n',words_array(line,1,:),words_array(line,2,:),words_array(line,3,:),cad1,cad2,cad3,words_array(line,7,:),words_array(line,8,:),words_array(line,9,:),words_array(line,10,:),contrabarra );
0307         else
0308             fprintf(SinkFile,'%s %s(%s,%s,%s,%s),%s,%s,%s,%s%cWObj:=%s;\n',words_array(line,1,:),words_array(line,2,:),words_array(line,3,:),cad1,cad2,cad3,words_array(line,7,:),words_array(line,8,:),words_array(line,9,:),words_array(line,10,:), contrabarra,words_array(line,11,:));
0309         end
0310     end
0311 elseif strncmp(word2,'Offs',4)==1%Just the second targetpoints is asigned with offs
0312        cad1(1,:)=char(words_array(line,5,:));
0313        cad2(1,:)=char(words_array(line,6,:));
0314        cad3(1,:)=char(words_array(line,7,:));
0315        cad1=int2str(eval(strcat(cad1,'*1000')));
0316        cad2=int2str(eval(strcat(cad2,'*1000')));
0317        cad3=int2str(eval(strcat(cad3,'*1000')));
0318     if size(words_array,2)<11%Wobj isn't defined
0319         fprintf(SinkFile,'%s %s,%s(%s,%s,%s,%s),%s,%s,%s%cWObj:=wobj0;\n',words_array(line,1,:),words_array(line,2,:),words_array(line,3,:),words_array(line,4,:),cad1,cad2,cad3,words_array(line,8,:),words_array(line,9,:),words_array(line,10,:),contrabarra );
0320     else
0321         fprintf(SinkFile,'%s %s,%s(%s,%s,%s,%s),%s,%s,%s%cWObj:=%s;\n',words_array(line,1,:),words_array(line,2,:),words_array(line,3,:),words_array(line,4,:),cad1,cad2,cad3,words_array(line,8,:),words_array(line,9,:),words_array(line,10,:), contrabarra,words_array(line,11,:));
0322     end
0323 else %Offs it�s not used
0324     if size(words_array,2)<6
0325         fprintf(SinkFile,'%s %s,%s,%s,%s,%s%cWObj:=wobj0;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), words_array(line,4,:), words_array(line,5,:),words_array(line,6,:),contrabarra);
0326     else
0327         fprintf(SinkFile,'%s %s,%s,%s,%s,%s%cWObj:=%s;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), words_array(line,4,:), words_array(line,5,:),words_array(line,6,:),contrabarra,words_array(line,6,:));
0328     end
0329 end
0330 
0331 end
0332 
0333 function ModifyMoveL(words_array,line,SinkFile)
0334     contrabarra= '\';
0335     word(1,:)=char(words_array(line,2,:));
0336     if strncmp(word,'Offs',4)==1
0337        cad1(1,:)=char(words_array(line,4,:));
0338        cad2(1,:)=char(words_array(line,5,:));
0339        cad3(1,:)=char(words_array(line,6,:));
0340        cad1=int2str(eval(strcat(cad1,'*1000')));
0341        cad2=int2str(eval(strcat(cad2,'*1000')));
0342        cad3=int2str(eval(strcat(cad3,'*1000')));
0343        if size(words_array,2)<11
0344        %If it�s empty we will refer the targetpoint to the world's reference system
0345        fprintf(SinkFile,'%s %s(%s,%s,%s,%s),%s,%s,%s%cWObj:=wobj0;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:),cad1,cad2,cad3,words_array(line,7,:),words_array(line,8,:),words_array(line,9,:),contrabarra);
0346        else
0347        fprintf(SinkFile,'%s %s(%s,%s,%s,%s),%s,%s,%s%cWObj:=%s;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), cad1,cad2,cad3,words_array(line,7,:),words_array(line,8,:),words_array(line,9,:),contrabarra,words_array(line,11,:));  
0348        end
0349     else
0350        if size(words_array,2)<8
0351        %If it�s empty we will refer the targetpoint to the world's reference system
0352        fprintf(SinkFile,'%s %s,%s,%s,%s%cWObj:=wobj0;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), words_array(line,4,:),words_array(line,5,:),contrabarra);
0353        else
0354        fprintf(SinkFile,'%s %s,%s,%s,%s%cWObj:=%s;\n',words_array(line,1,:), words_array(line,2,:), words_array(line,3,:), words_array(line,4,:), words_array(line,5,:),contrabarra,words_array(line,7,:));
0355        end
0356     end
0357 end
0358 
0359 
0360 function ModifyFunction(SourceFile,SinkFile)
0361     string = fgets(SourceFile);
0362     line_Func=1;
0363     while isempty(strfind(string,'end'))==1
0364         line_array = char(string);
0365         if FindCommentsOrNewLines(line_array,SinkFile,line_Func,SourceFile)==1
0366         else    
0367             words_array_func=strtokenizador(line_array,line_Func,SinkFile);
0368             FindAndModify(words_array_func,line_Func,SourceFile,string,SinkFile)
0369         end
0370         string=fgets(SourceFile);
0371         line_Func=line_Func+1;
0372     end
0373     fprintf(SinkFile,'ENDPROC\n');
0374 end
0375 
0376 function ModifyFor(words_array,line,SourceFile,SinkFile)
0377 
0378 if size(words_array,2)==4
0379     fprintf(SinkFile,'FOR %s FROM %s TO %s  DO\n', words_array(line,2,:), words_array(line,3,:), words_array(line,4,:));
0380 end
0381 if size(words_array,2)==5
0382     fprintf(SinkFile,'FOR %s FROM %s TO %s [STEP %s] DO\n', words_array(line,2,:), words_array(line,3,:),  words_array(line,5,:), words_array(line,4,:));
0383 end
0384 auxiliar = fgets(SourceFile);
0385 while isempty(strfind(auxiliar,'end'))==1
0386     line=line+1;
0387     line_array = char(auxiliar);
0388     if FindCommentsOrNewLines(line_array,SinkFile,line,SourceFile)==1
0389     else
0390     words_array=strtokenizador(line_array,line,SinkFile);
0391     FindAndModify(words_array,line,SourceFile,auxiliar,SinkFile);
0392     end
0393     auxiliar= fgets(SourceFile);
0394 end
0395 InitialTab(auxiliar,SinkFile);
0396 fprintf(SinkFile,'ENDFOR\n');
0397 end
0398 
0399 function ModifyWhile(words_array,line,SourceFile,string,SinkFile)
0400 
0401 Start_condition = strfind(string,' ');
0402 j=1;
0403 while uint8(string(Start_condition(1)+j))~=13 & uint8(string(Start_condition(1)+j))~=10
0404     copia(j)=string(Start_condition(1) + j);
0405     j=j+1;
0406 end
0407 fprintf(SinkFile,'WHILE %s\n',copia);
0408 auxiliar = fgets(SourceFile);
0409 while  isempty(strfind(auxiliar,'end'))==1
0410     line=line+1;
0411     line_array = char(auxiliar);
0412     if FindCommentsOrNewLines(line_array,SinkFile,line,SourceFile)==1
0413     else
0414     words_array=strtokenizador(line_array,line,SinkFile);
0415     FindAndModify(words_array,line,SourceFile,auxiliar,SinkFile);
0416     end
0417     auxiliar = fgets(SourceFile);
0418 end
0419 InitialTab(auxiliar,SinkFile);
0420 fprintf(SinkFile,'ENDWHILE\n');
0421 end
0422 
0423 function ModifyIf(words_array,line,SourceFile,string,SinkFile)
0424 
0425 Start_condition= strfind(string,' ');
0426 j=0;
0427 i=0;
0428 %while string(Start_condition+j)~='\n' %% This doesn�t work
0429 while uint8(string(Start_condition+j))~=13 & uint8(string(Start_condition+j))~=10
0430     j=j+1;
0431     i=i+1;
0432     if string(Start_condition(1) + j)=='='
0433         if string(Start_condition(1) + j + 1)=='='
0434             copia(i)=string(Start_condition(1) + j);
0435             j=j+1;
0436         else
0437             copia(i)=string(Start_condition(1) + j);
0438         end
0439     else
0440         copia(i)=string(Start_condition(1) + j);
0441     end
0442 end
0443 fprintf(SinkFile,'IF %s THEN\n',copia(1:length(copia)-1));
0444 auxiliar = fgets(SourceFile);
0445 while  isempty(strfind(auxiliar,'end'))==1
0446     clearvars copia;
0447     line=line+1;
0448     line_array = char(auxiliar);
0449     if FindCommentsOrNewLines(line_array,SinkFile,line,SourceFile)==1
0450     else
0451         words_array=strtokenizador(line_array,line,SinkFile);
0452         FindAndModify(words_array,line,SourceFile,auxiliar,SinkFile);
0453         a=0;
0454         if isempty(strfind(auxiliar,'elseif'))==0
0455             a=1;
0456             Start_condition= strfind(auxiliar,' ');
0457             j=0;
0458             i=0;
0459             while uint8(auxiliar(Start_condition+j))~=13 & uint8(auxiliar(Start_condition+j))~=10
0460                 j=j+1;
0461                 i=i+1;
0462                 if auxiliar(Start_condition(1) + j)=='='
0463                     if auxiliar(Start_condition(1) + j + 1)=='='
0464                         copia(i)=auxiliar(Start_condition(1) + j);
0465                         j=j+1;
0466                     else
0467                         copia(i)=auxiliar(Start_condition(1) + j);
0468                     end
0469                 else
0470                     copia(i)=auxiliar(Start_condition(1) + j);
0471                 end
0472             end
0473             fprintf(SinkFile,'ELSEIF %s THEN\n',copia(1:length(copia)-1));
0474         elseif isempty(strfind(auxiliar,'else'))==0 & a==0
0475             fprintf(SinkFile,'ELSE\n');
0476         end
0477         
0478     end
0479     auxiliar = fgets(SourceFile);
0480 end
0481 fprintf(SinkFile,'ENDIF\n');
0482 end
0483 
0484 function result = strfinder(string,words_array,lines)
0485 result=0;
0486 k=1;
0487 while  k<=size(words_array,2)%Recorremos el vector en busca de coincidencias
0488     j=1;
0489     while  j <= size(words_array,3) && words_array(lines,k,j)~= 0
0490         if words_array(lines,k,j)=='_'
0491             if  strcmp(word,string)==1
0492                 result=1;
0493             end
0494         end
0495         word(1,j)=char(words_array(lines,k,j));
0496         j=j+1;
0497     end
0498     if  strcmp(word,string)==1
0499         result=1;
0500     end
0501     clearvars word
0502     k=k+1;
0503 end
0504 end
0505 
0506 function finder=FindAndModifyAllocations(string,SinkFile)
0507 finder=0;
0508 if strfind(string,'=')%Control sentence!
0509     if (isempty(strfind(string,'MoveJ'))==0 | isempty(strfind(string,'MoveL'))==0 | isempty(strfind(string,'MoveC'))==0| isempty(strfind(string,'for'))==0 | isempty(strfind(string,'while'))==0 | isempty(strfind(string,'if'))==0)==0%Y no coincide con alguna funci�n
0510         %fprintf('Asignaci�n: %s\n Asig(1)=%c - Asig(2)=%c\n', string,string(1),string(2));
0511         declaration=strcat(string(1),string(2));
0512         if strcmp(declaration, 'RT')==1 
0513             fprintf(SinkFile, 'CONST robtarget ');
0514         elseif strcmp(declaration, 'TD')==1
0515             fprintf(SinkFile, 'PERS tooldata ');
0516         elseif strcmp(declaration, 'VA')==1
0517             fprintf(SinkFile, 'VAR');
0518         elseif isempty(strfind(string,'['))==1
0519             %DoNothig becuase its a normal var declaration
0520         else    
0521             fprintf('WARNING, define the variable or delete the string: \n\n %s\n', string);
0522             fprintf('Before the variable�s name include:\n TD_ for tooldata\n RT_ for robottarget\n VAR_ for variables\n');
0523             return
0524         end
0525         j=1;
0526         k=1;
0527         finder=1;
0528         while string(k)==' '
0529             k=k+1;%Instruction for clear white space and horizontal tab before the asignation
0530         end
0531         while string(k)~=10 & k< length(string)%Including ':' to the asignation.
0532             if strcmp(string(k),'=')==1
0533                 copia(j)=':';
0534                 j=j+1;
0535                 copia(j)='=';
0536                 j=j+1;
0537                 k=k+1;
0538             else
0539                 copia(j)=string(k);
0540                 j=j+1;
0541                 k=k+1;
0542             end
0543         end
0544         %If its a TargetPoint Should have a "["
0545         if strfind(copia,'[')
0546            %Looking for float numbers
0547            h=1;
0548            copia2='';
0549            while h<length(copia)
0550                 if (copia(h)>='0' & copia(h)<='9') & copia(h+1)=='.'
0551                     g=h;
0552                     final=h;
0553                     while copia(h)~=',' & copia(h)~=']'
0554                        if copia(h)~='0' & copia(h)~='.'
0555                            final=h;
0556                        end
0557                        h=h+1;
0558                     end
0559                     for f=g:1:final,
0560                     copia2(f)=copia(f);
0561                     end
0562                     copia2(f+1)=copia(h);
0563                 else
0564                   copia2(h)=copia(h);   
0565                 end
0566            h=h+1;
0567            end
0568         b=0;
0569         c=0;
0570         g=1;
0571         for f=1:1:length(copia2),
0572             if uint8(copia2(f))==0%Clear null cells in copia2
0573                 f=f+1;
0574             elseif copia2(f)==',' & b<3  & (copia2(f-1)<97 | copia2(f-1)>122) & (length(strfind(copia2,','))<3 | c==1)%We put the position in milimeters
0575                 b=b+1;
0576                 copia3(g:g+5)='*1000,';
0577                 f=f+6;
0578                 g=g+6;
0579             elseif copia2(f)==']' & b==2 & (copia2(f-1)<97 | copia2(f-1)>122) & (length(strfind(copia2,','))<3 | c==1)
0580                 b=b+1;
0581                 copia3(g:g+5)='*1000]';
0582                 f=f+6;
0583                 g=g+6;
0584                 c=0;
0585             elseif copia2(f)=='['& copia2(f+1)=='['
0586                  c=1;
0587                  copia3(g)=copia2(f);
0588                  g=g+1;
0589             else
0590                 copia3(g)=copia2(f);
0591                 g=g+1;
0592             end
0593         end
0594             copia4=clearexpression(copia3);
0595             if strfind(copia3,';')
0596             fprintf(SinkFile,'%s\n',copia4);
0597             else
0598             fprintf(SinkFile,'%s;\n',copia4);    
0599             end
0600         else
0601             if strfind(copia,';')
0602             fprintf(SinkFile,'%s\n',copia);
0603             else
0604             fprintf(SinkFile,'%s;\n',copia);    
0605             end
0606         end
0607     end
0608 end
0609 end
0610 
0611 function InitialTab(line_array,SinkFile)
0612 
0613 if line_array(1)==' ' %| line_array(1)==11 %Space or horizontal tab
0614     fprintf(SinkFile,'%c', line_array(1));
0615     n=2;
0616     while line_array(n)==' ' | line_array(n)==11
0617         fprintf(SinkFile,'%c',line_array(n));
0618         n=n+1;
0619     end
0620 end
0621 end
0622 
0623 function coment = FindCommentsOrNewLines(line_array,SinkFile,lines,SourceFile)
0624 
0625 coment=0;
0626 a=strfind(line_array,'%');
0627 if  isempty(a)==0
0628     %It's posible to have an instructi�n before the comentary
0629     control=1;
0630     i=1;
0631     while i<a
0632         if line_array(i)~=' '
0633             control=i;
0634             i=a;
0635             auxiliar=line_array(control:a-1);
0636             words_array=strtokenizador(line_array,lines,SinkFile);
0637             FindAndModify(words_array,lines,SourceFile,auxiliar,SinkFile);
0638         end
0639         i=i+1;
0640     end
0641     fprintf(SinkFile,'!%s', line_array(a+1:length(line_array)));
0642     coment=1;
0643 elseif line_array(1)==13 | line_array(1)==10 | line_array(1)=='\n' | line_array(1)==' ' %New line or carriage return
0644     i=1;
0645     while line_array(i)==' '
0646         i=i+1;
0647     end
0648     if line_array(i)==13 | line_array(i)==10 | line_array(i)=='\n'
0649     fprintf(SinkFile,'\n');
0650     coment=1;
0651     end
0652 end
0653 end
0654 
0655 function auxiliar=clearexpression(string)
0656 
0657 pos=strfind(string,'*');
0658 length(pos);
0659 for i=1:length(pos)
0660     count_back=pos(i)-1;
0661     while string(count_back)~=',' & string(count_back)~='['
0662         start=count_back;
0663         count_back=count_back-1;
0664     end
0665     count_rear=pos(i)+1;
0666     while string(count_rear)~=',' & string(count_rear)~=']'
0667         final=count_rear;
0668         count_rear=count_rear+1;
0669     end
0670     integer=eval(string(start:final));
0671     cadena=int2str(integer);
0672     a=1;
0673     for j=start:final
0674         if a<=length(cadena)
0675         string(j)=cadena(a);
0676         else
0677         string(j)=' ';
0678         end
0679         a=a+1;
0680     end    
0681 end
0682 j=1;
0683 for i=1:length(string)
0684    if string(i)==' '
0685        %DoNothing
0686    else
0687        auxiliar(j)=string(i);
0688        j=j+1;
0689    end
0690 end
0691 if strfind(auxiliar,'TD')
0692    ToF=strfind(auxiliar,'['); 
0693    j=1;
0694    for i=1:length(auxiliar)
0695         if(i==ToF(1)+1)
0696             if auxiliar(ToF(1)+1)=='1'
0697                 auxiliar2(j:j+3)='TRUE';
0698                 j=j+4;
0699             else
0700                 auxiliar2(j:j+4)='FALSE';
0701                 j=j+5;
0702             end
0703         else
0704             auxiliar2(j)=auxiliar(i);
0705             j=j+1;
0706         end
0707    end
0708    auxiliar=strcat(auxiliar2,' ');
0709 end
0710 end

Generated on Fri 03-Jan-2014 12:20:01 by m2html © 2005