files used by m2sci module moved into this module kernel
Vincent Couvert [Thu, 15 Mar 2007 13:42:40 +0000 (13:42 +0000)]
scilab/modules/m2sci/macros/kernel/changevarname.sci [new file with mode: 0644]
scilab/modules/m2sci/macros/kernel/old2newinstr.sci [new file with mode: 0644]

diff --git a/scilab/modules/m2sci/macros/kernel/changevarname.sci b/scilab/modules/m2sci/macros/kernel/changevarname.sci
new file mode 100644 (file)
index 0000000..686e3bf
--- /dev/null
@@ -0,0 +1,62 @@
+function changevarname(fun,oldname,newname)
+// This function replaces the variable (named oldname) of a file by a new name (which is newname). It uses the old2newinstr function
+// INPUTS:
+// -fun: a string, the name (with the whole path) of the modifed file
+// -oldname: a string, the old name of the variable
+// -newname: a string, the new name of the variable after modification 
+//
+// Author: F.Belahcene
+// Copyright INRIA
+
+
+if MSDOS
+       sep="\";
+else
+       sep="/";
+end
+
+sep_ind=strindex(fun,sep);
+dot_ind=strindex(fun,".");
+
+// funname is the function name (without the path and the extension)
+funname=part(fun,sep_ind($)+1:dot_ind($)-1);
+
+if sep_ind<>[]
+       funsave=part(fun,1:sep_ind($))+'save_'+part(fun,sep_ind($)+1:dot_ind($)-1)+".sci";
+else
+       funsave='save_'+part(fun,sep_ind($)+1:dot_ind($)-1)+".sci";
+end
+
+// Compilation
+getf(fun);
+var=who('get');
+indvar=find(var==funname);
+funvect=var(indvar(1):-1:1);
+txt=mgetl(fun);
+mputl(txt,funsave);
+
+txt=[]
+for j=1:size(funvect,1)
+//Get the tree of the function
+       execstr("tree=macr2tree("+funvect(j)+")");
+// Change oldname into newname in the tree 
+       if tree.name==oldname then
+               tree.name=newname;
+       end
+       for i=1:size(tree.inputs)
+               tree.inputs(i)=old2newinstr(tree.inputs(i),oldname,newname);
+       end
+       for i=1:size(tree.outputs)
+               tree.outputs(i)=old2newinstr(tree.outputs(i),oldname,newname);
+       end
+       for i=1:size(tree.statements)
+               tree.statements(i)=old2newinstr(tree.statements(i),oldname,newname);
+       end
+
+// Get the matching code of the tree after modification
+       txt=[txt; tree2code(tree,%T)];
+end
+
+// Replace the old code by the new code (which is txt) in the file fun  
+mputl(txt,fun);
+endfunction
\ No newline at end of file
diff --git a/scilab/modules/m2sci/macros/kernel/old2newinstr.sci b/scilab/modules/m2sci/macros/kernel/old2newinstr.sci
new file mode 100644 (file)
index 0000000..f9f7807
--- /dev/null
@@ -0,0 +1,88 @@
+function instr=old2newinstr(instr,oldname,newname)
+// This function replaces by a new name (given in a input argument:newname) in a Scilab instruction all the variables names and functions names matching to a given name in a input argument:oldname
+// INPUTS:
+// -instr: Scilab instruction
+// -oldname: a string, matching to the name variable which must be replaced
+// -newname: a string, matching to the new variable name 
+// OUTPUT:
+// -instr: Scilab instruction after modification
+//
+// Author: F.Belahcene
+// Copyright INRIA
+
+// VARIABLE tlist //
+// If the variable name matches to the oldname argument then replace the variable name by the newname argument
+if typeof(instr)=="variable" then
+       if instr.name==oldname then
+               instr.name=newname
+       end
+// FUNCALL tlist //
+// If the function name matches to oldname argument then replace the function name by newname argument
+elseif typeof(instr)=="funcall"
+       if instr.name==oldname then
+               instr.name=newname
+       end
+       // ex: function return has not rhs: return.rhs is not a list  
+       if typeof(instr.rhs)=="list" then
+               for i=1:size(instr.rhs)
+                       instr.rhs(i)=old2newinstr(instr.rhs(i),oldname,newname)
+               end
+       end
+// OPERATION tlist//
+elseif typeof(instr)=="operation" then
+       for i=1:size(instr.operands)
+               instr.operands(i)=old2newinstr(instr.operands(i),oldname,newname)
+       end
+// IF-THEN-ELSE instruction //
+elseif typeof(instr)=="ifthenelse" then
+       instr.expression=old2newinstr(instr.expression,oldname,newname)
+       for i=1:size(instr.then)
+               instr.then(i)=old2newinstr(instr.then(i),oldname,newname)
+       end
+       for i=1:size(instr.elseifs)
+               for k=1:size(instr.elseifs(i).then)
+                       instr.elseifs(i).then(k)=old2newinstr((instr.elseifs(i).then(k)),oldname,newname) 
+               end
+       end  
+       for i=1:size(instr.else)
+               instr.else(i)=old2newinstr((instr.else(i)),oldname,newname)
+       end
+// SELECT-CASE instruction //  
+elseif typeof(instr)=="selectcase" then
+       instr.expression=old2newinstr(instr.expression,oldname,newname)
+       for i=1:size(instr.cases)
+               for j=1:size(instr.cases(i).then)
+                       instr.cases(i).then(j)=old2newinstr((instr.cases(i).then(j)),oldname,newname)
+               end   
+       end
+       for i=1:size(instr.else)
+               instr.else(i)=old2newinstr(instr.else(i),oldname,newname)
+       end
+// WHILE instruction //        
+elseif typeof(instr)=="while" then
+       instr.expression=old2newinstr(instr.expression,oldname,newname)
+       for i=1:size(instr.statements)
+               instr.statements(i)=old2newinstr(instr.statements(i),oldname,newname)
+       end     
+// TRY-CATCH instruction //    
+elseif typeof(instr)=="trycatch"
+       for i=1:size(instr.trystat)
+               instr.trystat(i)=old2newinstr(instr.trystat(i),oldname,newname)
+       end
+       for i=1:size(instr.catchstat)
+               instr.catchstat(i)=old2newinstr(instr.catchstat(i),oldname,newname)
+       end
+// FOR instruction //  
+elseif typeof(instr)=="for" then
+       instr.expression=old2newinstr(instr.expression,oldname,newname)
+       for i=1:size(instr.statements)
+               instr.statements(i)=old2newinstr(instr.statements(i),oldname,newname)
+       end
+// EQUAL instruction //
+elseif typeof(instr)=="equal" then
+       instr.expression=old2newinstr(instr.expression,oldname,newname)
+       for i=1:size(instr.lhs)
+               instr.lhs(i)=old2newinstr(instr.lhs(i),oldname,newname)
+       end
+end
+endfunction
\ No newline at end of file