+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2004-2006 - INRIA - Farid BELAHCENE
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-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
-
- sep=filesep();
-
- 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
- exec(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
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function [sci_clause,nblines]=clause2sci(mtlb_clause,nblines,leveltemp)
- // M2SCI function
-
- // Global variables for M2SCI
- global("m2sci_to_insert_b")
- global("varslist")
-
- // Increment level of clause indentation
- level
-
- // Temp variable used to store instructions to insert before clause
- to_insert=list()
- select typeof(mtlb_clause)
- // --- TRYCATCH ---
- case "trycatch"
- level=[level;0]
-
- // Get instructions to insert if there are
- if m2sci_to_insert_b<>list() then
- to_insert=m2sci_to_insert_b
- m2sci_to_insert_b=list()
- end
-
- // Convert try
- sci_try=list()
- level($)=level($)+1
- for k=1:size(mtlb_clause.trystat)
- if typeof(mtlb_clause.trystat(k))=="sup_equal" then
- sci_try_temp=list()
- for i=1:size(mtlb_clause.trystat(k).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.trystat(k).sup_instr(i),nblines)
- sci_try_temp=update_instr_list(sci_try_temp,instr)
- end
- sci_try($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_try_temp,mtlb_clause.trystat(k).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.trystat(k),nblines)
- sci_try=update_instr_list(sci_try,instr)
- end
- end
-
- // Convert catch
- sci_catch=list()
- level($)=level($)+1
- for k=1:size(mtlb_clause.catchstat)
- if typeof(mtlb_clause.catchstat(k))=="sup_equal" then
- sci_catch_temp=list()
- for i=1:size(mtlb_clause.catchstat(k).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.catchstat(k).sup_instr(i),nblines)
- sci_catch_temp=update_instr_list(sci_catch_temp,instr)
- end
- sci_catch($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_catch_temp,mtlb_clause.catchstat(k).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.catchstat(k),nblines)
- sci_catch=update_instr_list(sci_catch,instr)
- end
- end
-
- // Create Scilab trycatch
- sci_clause=tlist(["trycatch","trystat","catchstat"],sci_try,sci_catch)
- level($)=level($)+1
- updatevarslist("END OF CLAUSE")
-
- // --- IF ---
- case "ifthenelse"
- level=[level;0]
-
- // Convert expression
- [sci_expr]=expression2sci(mtlb_clause.expression)
-
- // Get instructions to insert if there are
- if m2sci_to_insert_b<>list() then
- to_insert=m2sci_to_insert_b
- m2sci_to_insert_b=list()
- end
-
- // Convert then statements
- sci_then=list()
- level($)=level($)+1
- for k=1:size(mtlb_clause.then)
- if typeof(mtlb_clause.then(k))=="sup_equal" then
- sci_then_temp=list()
- for i=1:size(mtlb_clause.then(k).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.then(k).sup_instr(i),nblines)
- sci_then_temp=update_instr_list(sci_then_temp,instr)
- end
- sci_then($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_then_temp,mtlb_clause.then(k).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.then(k),nblines)
- sci_then=update_instr_list(sci_then,instr)
- end
- end
-
- // Convert elseifs
- sci_elseifs=list()
- for k=1:size(mtlb_clause.elseifs)
- level($)=level($)+1
-
- // Convert expression
- [sci_exprn]=expression2sci(mtlb_clause.elseifs(k).expression)
-
- // Get instructions to insert if there are
- if m2sci_to_insert_b<>list() then
- to_insert=m2sci_to_insert_b
- m2sci_to_insert_b=list()
- end
-
- // Convert statements
- sci_stat=list()
- for l=1:size(mtlb_clause.elseifs(k).then)
- if typeof(mtlb_clause.elseifs(k).then(l))=="sup_equal" then
- sci_stat_temp=list()
- for i=1:size(mtlb_clause.elseifs(k).then(l).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.elseifs(k).then(l).sup_instr(i),nblines)
- sci_stat_temp=update_instr_list(sci_stat_temp,instr)
- end
- sci_stat($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_stat_temp,mtlb_clause.elseifs(k).then(l).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.elseifs(k).then(l),nblines)
- sci_stat=update_instr_list(sci_stat,instr)
- end
- end
- sci_elseifs($+1)=tlist(["elseif","expression","then"],sci_exprn,sci_stat)
- end
-
- // Convert else
- sci_else=list()
- if size(mtlb_clause.else)<>0 then
- level($)=level($)+1
- end
- for k=1:size(mtlb_clause.else)
- if typeof(mtlb_clause.else(k))=="sup_equal" then
- sci_else_temp=list()
- for i=1:size(mtlb_clause.else(k).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.else(k).sup_instr(i),nblines)
- sci_else_temp=update_instr_list(sci_else_temp,instr)
- end
- sci_else($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_else_temp,mtlb_clause.else(k).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.else(k),nblines)
- sci_else=update_instr_list(sci_else,instr)
- end
- end
-
- // Create Scilab ifthenelse
- sci_clause=tlist(["ifthenelse","expression","then","elseifs","else"],sci_expr,sci_then,sci_elseifs,sci_else)
- level($)=level($)+1
- updatevarslist("END OF CLAUSE")
-
- // --- SELECT ---
- case "selectcase"
- level=[level;0]
- // Convert expression
- sci_expr=list()
- [sci_expr(1)]=expression2sci(mtlb_clause.expression(1))
- for i=2:size(mtlb_clause.expression)
- sci_expr(i)=mtlb_clause.expression(i) // EOL or comment
- end
-
- // Get instructions to insert if there are
- if m2sci_to_insert_b<>list() then
- to_insert=m2sci_to_insert_b
- m2sci_to_insert_b=list()
- end
-
- // Convert cases
- sci_cases=list()
- k=0
- while k<size(mtlb_clause.cases)
- k=k+1
- level($)=level($)+1
- // Convert expression
- if typeof(mtlb_clause.cases(k).expression)=="funcall" then
- if mtlb_clause.cases(k).expression.name=="makecell" then
- // Insert new cases
- for nbcas=size(mtlb_clause.cases):-1:k+1
- mtlb_clause.cases(nbcas+size(mtlb_clause.cases(k).expression.rhs))=mtlb_clause.cases(nbcas)
- end
- for nbrhs=1:size(mtlb_clause.cases(k).expression.rhs)
- mtlb_clause.cases(nbrhs+k)=tlist(["case","expression","then"],mtlb_clause.cases(k).expression.rhs(nbrhs),mtlb_clause.cases(k).then)
- end
- mtlb_clause.cases(k)=null()
- end
- end
- [sci_exprn]=expression2sci(mtlb_clause.cases(k).expression)
- // Get instructions to insert if there are
- if m2sci_to_insert_b<>list() then
- to_insert=m2sci_to_insert_b
- m2sci_to_insert_b=list()
- end
-
- // Convert statements
- sci_stat=list()
- for l=1:size(mtlb_clause.cases(k).then)
- if typeof(mtlb_clause.cases(k).then(l))=="sup_equal" then
- sci_stat_temp=list()
- for i=1:size(mtlb_clause.cases(k).then(l).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.cases(k).then(l).sup_instr(i),nblines)
- sci_stat_temp=update_instr_list(sci_stat_temp,instr)
- end
- sci_stat($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_stat_temp,mtlb_clause.cases(k).then(l).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.cases(k).then(l),nblines)
- sci_stat=update_instr_list(sci_stat,instr)
- end
- end
- sci_cases($+1)=tlist(["case","expression","then"],sci_exprn,sci_stat)
- end
-
- // Convert else
- sci_else=list()
- if size(mtlb_clause.else)<>0 then
- level($)=level($)+1
- end
- for k=1:size(mtlb_clause.else)
- if typeof(mtlb_clause.else(k))=="sup_equal" then
- sci_else_temp=list();
- for i=1:size(mtlb_clause.else(k).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.else(k).sup_instr(i),nblines)
- sci_else_temp=update_instr_list(sci_else_temp,instr)
- end
- sci_else($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_else_temp,mtlb_clause.else(k).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.else(k),nblines)
- sci_else=update_instr_list(sci_else,instr)
- end
- end
- // Create Scilab selectcase
- sci_clause=tlist(["selectcase","expression","cases","else"],sci_expr,sci_cases,sci_else)
- level($)=level($)+1
- updatevarslist("END OF CLAUSE")
-
- // --- WHILE ---
- case "while"
- level=[level;0]
- sci_do=list()
- // Convert expression
- [sci_expr]=expression2sci(mtlb_clause.expression)
- // If there are instructions to insert, while is modified so that inserted instruction is evaluated in each loop
- if m2sci_to_insert_b<>list() then
- newif=tlist(["ifthenelse","expression","then","elseifs","else"],sci_expr,list(Funcall("break",1,list(),list())),list(),list())
- m2sci_to_insert_b($+1)=newif
- sci_expr=Cste(%T)
- sci_do=m2sci_to_insert_b
- m2sci_to_insert_b=list()
- end
-
- // Convert all do instructions
- level($)=level($)+1
- for k=1:size(mtlb_clause.statements)
- if typeof(mtlb_clause.statements(k))=="sup_equal" then
- sci_do_temp=list()
- for i=1:size(mtlb_clause.statements(k).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.statements(k).sup_instr(i),nblines)
- // If inserted instruction is an initialisation, it has to be done just one time and before loop
- l=1;
- while l <= size(m2sci_to_insert_b)
- if typeof(m2sci_to_insert_b(l))=="equal" & ..
- (and(m2sci_to_insert_b(l).expression==Cste([])) | ..
- and(m2sci_to_insert_b(l).expression==Funcall("struct",1,list(),list())) | ..
- and(m2sci_to_insert_b(l).expression==Funcall("cell",1,list(),list())) ) then
- to_insert($+1)=m2sci_to_insert_b(l)
- m2sci_to_insert_b(l)=null()
- if size(m2sci_to_insert_b)>=l & m2sci_to_insert_b(l)==list("EOL") then
- to_insert($+1)=m2sci_to_insert_b(l)
- m2sci_to_insert_b(l)=null()
- end
- else
- l=l+1;
- end
- end
- sci_do_temp=update_instr_list(sci_do_temp,instr)
- end
- sci_do($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_do_temp,mtlb_clause.statements(k).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.statements(k),nblines)
- // If inserted instruction is an initialisation, it has to be done just one time and before loop
- l=1;
- while l <= size(m2sci_to_insert_b)
- if typeof(m2sci_to_insert_b(l))=="equal" & ..
- (and(m2sci_to_insert_b(l).expression==Cste([])) | ..
- and(m2sci_to_insert_b(l).expression==Funcall("struct",1,list(),list())) | ..
- and(m2sci_to_insert_b(l).expression==Funcall("cell",1,list(),list())) ) then
- to_insert($+1)=m2sci_to_insert_b(l)
- m2sci_to_insert_b(l)=null()
- if size(m2sci_to_insert_b)>=l & m2sci_to_insert_b(l)==list("EOL") then
- to_insert($+1)=m2sci_to_insert_b(l)
- m2sci_to_insert_b(l)=null()
- end
- else
- l=l+1;
- end
- end
- sci_do=update_instr_list(sci_do,instr)
- end
- end
-
- // Create Scilab while
- sci_clause=tlist(["while","expression","statements"],sci_expr,sci_do)
- level($)=level($)+1
- updatevarslist("END OF CLAUSE")
-
- // --- FOR ---
- case "for"
- //level=[level;1]
- // Convert expression
- [sci_expr,nblines]=instruction2sci(mtlb_clause.expression,nblines)
- if typeof(sci_expr)=="equal" then
- [bval,pos]=isdefinedvar(sci_expr.lhs(1))
- if bval then
- varslist(pos).infer.dims=list(varslist(pos).infer.dims(1),1)
- end
- end
- // Get instructions to insert if there are
- if m2sci_to_insert_b<>list() then
- to_insert=m2sci_to_insert_b
- m2sci_to_insert_b=list()
- end
- sci_instr=list()
- // Convert all do instructions
- for k=1:size(mtlb_clause.statements)
- if typeof(mtlb_clause.statements(k))=="sup_equal" then
- sci_instr_temp=list()
- for i=1:size(mtlb_clause.statements(k).sup_instr)
- [instr,nblines]=instruction2sci(mtlb_clause.statements(k).sup_instr(i),nblines)
- // If inserted instruction is an initialisation, it has to be done just one time and before loop
- l=1;
- while l <= size(m2sci_to_insert_b)
- if typeof(m2sci_to_insert_b(l))=="equal" & ..
- (and(m2sci_to_insert_b(l).expression==Cste([])) | ..
- and(m2sci_to_insert_b(l).expression==Funcall("struct",1,list(),list())) | ..
- and(m2sci_to_insert_b(l).expression==Funcall("cell",1,list(),list())) ) then
- to_insert($+1)=m2sci_to_insert_b(l)
- m2sci_to_insert_b(l)=null()
- if size(m2sci_to_insert_b)>=l & m2sci_to_insert_b(l)==list("EOL") then
- to_insert($+1)=m2sci_to_insert_b(l)
- m2sci_to_insert_b(l)=null()
- end
- else
- l=l+1;
- end
- end
- sci_instr_temp=update_instr_list(sci_instr_temp,instr)
- end
- sci_instr($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_instr_temp,mtlb_clause.statements(k).nb_opr)
- else
- [instr,nblines]=instruction2sci(mtlb_clause.statements(k),nblines)
- // If inserted instruction is an initialisation, it has to be done just one time and before loop
- l=1;
- while l <= size(m2sci_to_insert_b)
- if typeof(m2sci_to_insert_b(l))=="equal" & ..
- (and(m2sci_to_insert_b(l).expression==Cste([])) | ..
- and(m2sci_to_insert_b(l).expression==Funcall("struct",1,list(),list())) | ..
- and(m2sci_to_insert_b(l).expression==Funcall("cell",1,list(),list())) ) then
- to_insert($+1)=m2sci_to_insert_b(l)
- m2sci_to_insert_b(l)=null()
- if size(m2sci_to_insert_b)>=l & m2sci_to_insert_b(l)==list("EOL") then
- to_insert($+1)=m2sci_to_insert_b(l)
- m2sci_to_insert_b(l)=null()
- end
- else
- l=l+1;
- end
- end
- sci_instr=update_instr_list(sci_instr,instr)
- end
- end
- // Create Scilab while
- sci_clause=tlist(["for","expression","statements"],sci_expr,sci_instr)
- else
- error(msprintf(gettext("unknown clause type: %s."),typeof(mtlb_clause)))
- end
- m2sci_to_insert_b=to_insert
- if m2sci_to_insert_b<>list() then
- m2sci_to_insert_b($+1)=list("EOL");
- end
-endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function tree=default_trad(tree)
- // M2SCI function
- // Create a default translation function
-
- global("mtlbref_fun") //contains the matlab reference functions which not yet converted
- global("mtlbtool_fun")//contains the matlab toolboxes functions
- global("not_mtlb_fun") // contains the not matlab functions
-
- if ~exists("mtlbref_fun") then
- mtlb_fun=[]
- end
- if ~exists("mtlbtool_fun") then
- mtlbtool_fun=[]
- end
- if ~exists("not_mtlb_fun") then
- not_mtlb_fun=[]
- end
-
- name=tree.name
-
- ispriminame=%f;
- //true if the name function is the name of scilab function primitive
- if funptr(tree.name)<>0 then
- name1="%"+tree.name
- tree.name=name1
- ispriminame=%t;
- end
- //ismtlbfun is true if the function is in a matlab toolbox, mtlbpath is the path where is the function
- [mtlbpath,ismtlbtoolfun]=mtlbtoolfun(name)
- //Matlab reference functions
- if or(name==not_yet_converted()) then
- set_infos(msprintf(gettext("Matlab function %s not yet converted, original calling sequence used."),name),2)
- if ~or(name==mtlbref_fun(:,1)) then
- mtlbref_fun($+1,1)=name
- if ispriminame then
- mtlbref_fun($,2)=msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1);
- else
- mtlbref_fun($,2)=""
- end
- end
- //Matlab toolboxes functions
- elseif ismtlbtoolfun then
- set_infos(msprintf(gettext("Matlab toolbox(es) function %s not converted, original calling sequence used"),name),2)
- if ~or(name==mtlbtool_fun(:,1)) then
- mtlbtool_fun($+1,1)=name
- if ispriminame then
- mtlbtool_fun($,2)=msprintf(gettext("Matlab toolbox(es) function %s not converted, original calling sequence used."),name,name1,mtlbpath)
- else
- mtlbtool_fun($,2)=msprintf(gettext("(Find this function in matlab/%s)."),mtlbpath)
- end
- end
- elseif isdefinedvar(Variable(tree.name,Infer())) then
- operands=list()
- operands(1)=Variable(tree.name,Infer())
- for krhs = 1:size(tree.rhs)
- operands($+1)=tree.rhs(krhs)
- end
- tree=Operation("ext",operands,tree.lhs)
- tree=operation2sci(tree)
-
- //Not matlbb function
- else
- set_infos(msprintf(gettext("Unknown function %s not converted, original calling sequence used."),name),2)
- if ~or(name==not_mtlb_fun(:,1)) then
- not_mtlb_fun($+1,1)=name
- if ispriminame then
- not_mtlb_fun($,2)=msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1);
- else
- not_mtlb_fun($,2)=""
- end
- end
- end
- if ispriminame then
- set_infos(msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1),0)
- end
- [tree]=sci_generic(tree)
-endfunction
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function [sci_instr]=equal2sci(mtlb_instr)
- // Convertion of a Matlab instruction or expression to Scilab
- // Input:
- // - mtlb_instr: Matlab instr or expression to convert
- // Output:
- // - sci_instr: Scilab equivalent for mtlb_instr
-
- // Trees to insert in converted function tree
- global("m2sci_to_insert_b")
- m2sci_to_insert_b=list()
- global("m2sci_to_insert_a")
- m2sci_to_insert_a=list()
- global("varslist")
-
- sci_instr=mtlb_instr
-
- // Add inference data to lhs
- lhslist=list()
-
- // Inference field added to each lhs argument
- // Get infos in varslist to init
- for k=1:size(mtlb_instr.lhs)
- if typeof(mtlb_instr.lhs(k))=="variable" then
- [bval,index]=isdefinedvar(mtlb_instr.lhs(k))
- if bval then
- INFER=varslist(index).infer
- else
- INFER=Infer()
- end
- lhslist($+1)=Variable(mtlb_instr.lhs(k).name,INFER)
- elseif typeof(mtlb_instr.lhs(k))=="operation" then
- if mtlb_instr.lhs(k).operator<>"ins" then
- error(msprintf(gettext("lhs cannot be a %s operation."),mtlb_instr.lhs(k).operator))
- end
-
- [bval,index]=isdefinedvar(mtlb_instr.lhs(k).operands(1))
- if bval then
- INFER=varslist(index).infer
- else
- INFER=Infer()
- end
-
- lhslist($+1)=Operation(mtlb_instr.lhs(k).operator,..
- mtlb_instr.lhs(k).operands,..
- list(Variable(mtlb_instr.lhs(k).operands(1).name,INFER)))
- else
- error(msprintf(gettext("lhs cannot be a %s."),typeof(mtlb_instr.lhs(k))))
- end
- end
-
- // Convert expression
-
- [sci_expr]=expression2sci(mtlb_instr.expression,lhslist);
-
- if sci_expr==list() then // Conversion made by inserted instructions or 'm2scideclare'
- sci_instr=list()
- else
-
- sci_instr.expression=sci_expr;
-
- // Update lhs of instruction
- select typeof(sci_instr.expression)
- case "operation" then
- sci_instr.lhs=sci_expr.out;
- case "funcall" then
- sci_instr.lhs=sci_instr.expression.lhs
- if typeof(mtlb_instr.expression)=="funcall" then
- sci_instr.lhs=sci_expr.lhs;
- end
- case "cste" then
- sci_instr.lhs=lhslist;
- sci_instr.lhs(1).dims=sci_expr.dims;
- sci_instr.lhs(1).type=sci_expr.type;
- case "variable" then
- sci_instr.lhs=lhslist;
- sci_instr.lhs(1).dims=sci_expr.dims;
- sci_instr.lhs(1).type=sci_expr.type;
- else
- error(msprintf(gettext("%s is not yet implemented."),typeof(sci_instr.expression)));
- end
-
- // If lhs are insertion operation, they also have to be converted
- for k=1:size(sci_instr.lhs)
- if typeof(sci_instr.lhs(k))=="operation" then
- sci_instr.lhs(k).operands($+1)=sci_instr.expression
- // Keep just one inference field in sci_instr.expression (if is a funcall) so that inference can be made in operation2sci()
- if typeof(sci_instr.lhs(k).operands($))=="funcall" then
- for l=1:size(sci_instr.lhs(k).operands($).lhs)
- if l<>k then
- sci_instr.lhs(k).operands($).lhs(l)=list()
- end
- end
- l=1
- while l<=size(sci_instr.lhs(k).operands($).lhs)
- if sci_instr.lhs(k).operands($).lhs(l)==list() then
- sci_instr.lhs(k).operands($).lhs(l)=null()
- else
- l=l+1
- end
- end
- // Verify that there is just one lhs kept
- if size(sci_instr.lhs(k).operands($).lhs)<>1 then pause;end
- end
- // If insertion made in an unknown variable, I add it to varslist
- inservar=sci_instr.lhs(k).operands(1)
- [bval,index]=isdefinedvar(inservar)
- if ~bval then
- // Variable added to varslist before insertion
- if funptr(inservar.name)<>0 then
- matname="%"+inservar.name
- else
- matname=inservar.name
- end
- if sci_instr.expression.vtype==Struct then
- // Variable is initialized to struct() in converted script is does not already exist
- varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Struct,Unknown)))
- //m2sci_to_insert_b($+1)=Equal(list(inservar),Funcall("struct",1,list(),list()))
- elseif sci_instr.expression.vtype==Cell then
- // Variable is initialized to cell() in converted script is does not already exist
- varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Cell,Unknown)))
- //m2sci_to_insert_b($+1)=Equal(list(inservar),Funcall("cell",1,list(),list()))
- else
- // Variable is initialized to [] in converted script is does not already exist
- varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Double,Real)))
- //m2sci_to_insert_b($+1)=Equal(list(inservar),Cste([]))
- end
- sci_instr.lhs(k).out(1).infer=varslist($).infer
- else
- sci_instr.lhs(k).out(1).infer=varslist(index).infer
- end
- [sci_instr.lhs(k)]=operation2sci(sci_instr.lhs(k))
- if typeof(sci_instr.lhs(k))=="operation" then
- if or(sci_instr.lhs(k).operands($)<>sci_instr.expression) then // Update expression if has been modified while converting lhs
- sci_instr.expression=sci_instr.lhs(k).operands($)
- end
-
- sci_instr.lhs(k).operands($)=null()
- updatevarslist(sci_instr.lhs(k).out)
- else
- // Insertion done by inserted instruction
- sci_instr=list()
- return
- end
- end
- end
- // Update varslist
- updatevarslist(sci_instr.lhs);
- end
-endfunction
// For more information, see the COPYING file which you should have received
// along with this program.
-function [sci_equiv]=funcall2sci(mtlb_expr)
- // M2SCI function
+function sci_equiv = funcall2sci(mtlb_expr)
+ // INTERNAL function called only by expression2sci()
+ //
// Convert a function call in an instruction or in an expression from Matlab to Scilab
// Input:
// - mtlb_instr: Matlab instr or expression to convert
end
endfunction
+
+// ---------------------------------------------------------------------------
+
+function tree = default_trad(tree)
+ // M2SCI private function called only within funcall2sci()
+ // Create a default translation function
+
+ global("mtlbref_fun") //contains the matlab reference functions which not yet converted
+ global("mtlbtool_fun")//contains the matlab toolboxes functions
+ global("not_mtlb_fun") // contains the not matlab functions
+
+ if ~exists("mtlbref_fun") then
+ mtlb_fun=[]
+ end
+ if ~exists("mtlbtool_fun") then
+ mtlbtool_fun=[]
+ end
+ if ~exists("not_mtlb_fun") then
+ not_mtlb_fun=[]
+ end
+
+ name=tree.name
+
+ ispriminame=%f;
+ //true if the name function is the name of scilab function primitive
+ if funptr(tree.name)<>0 then
+ name1="%"+tree.name
+ tree.name=name1
+ ispriminame=%t;
+ end
+ //ismtlbfun is true if the function is in a matlab toolbox, mtlbpath is the path where is the function
+ [mtlbpath,ismtlbtoolfun]=mtlbtoolfun(name)
+ //Matlab reference functions
+ if or(name==not_yet_converted()) then
+ set_infos(msprintf(gettext("Matlab function %s not yet converted, original calling sequence used."),name),2)
+ if ~or(name==mtlbref_fun(:,1)) then
+ mtlbref_fun($+1,1)=name
+ if ispriminame then
+ mtlbref_fun($,2)=msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1);
+ else
+ mtlbref_fun($,2)=""
+ end
+ end
+ //Matlab toolboxes functions
+ elseif ismtlbtoolfun then
+ set_infos(msprintf(gettext("Matlab toolbox(es) function %s not converted, original calling sequence used"),name),2)
+ if ~or(name==mtlbtool_fun(:,1)) then
+ mtlbtool_fun($+1,1)=name
+ if ispriminame then
+ mtlbtool_fun($,2)=msprintf(gettext("Matlab toolbox(es) function %s not converted, original calling sequence used."),name,name1,mtlbpath)
+ else
+ mtlbtool_fun($,2)=msprintf(gettext("(Find this function in matlab/%s)."),mtlbpath)
+ end
+ end
+ elseif isdefinedvar(Variable(tree.name,Infer())) then
+ operands=list()
+ operands(1)=Variable(tree.name,Infer())
+ for krhs = 1:size(tree.rhs)
+ operands($+1)=tree.rhs(krhs)
+ end
+ tree=Operation("ext",operands,tree.lhs)
+ tree=operation2sci(tree)
+
+ //Not matlbb function
+ else
+ set_infos(msprintf(gettext("Unknown function %s not converted, original calling sequence used."),name),2)
+ if ~or(name==not_mtlb_fun(:,1)) then
+ not_mtlb_fun($+1,1)=name
+ if ispriminame then
+ not_mtlb_fun($,2)=msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1);
+ else
+ not_mtlb_fun($,2)=""
+ end
+ end
+ end
+ if ispriminame then
+ set_infos(msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1),0)
+ end
+ [tree]=sci_generic(tree)
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function path = mfile_path(nam)
+ fil = nam+".m";
+ nf = length(fil)
+ path = [];
+ for k=1:size(mfiles,"*")
+ pk=mfiles(k);
+ kk=strindex(pk,["/" "\"]);
+ if kk==[]
+ kk = 0
+ end
+ if fil==part(pk,kk($)+1:length(pk)) then
+ path=pk;
+ break
+ end
+ end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function tree = sci_generic(tree)
+ // M2SCI function
+ // Generic conversion function for unknown Matlab functions
+ // Input: tree = Matlab funcall tree
+ // Output: tree = Scilab equivalent for tree
+
+ if typeof(tree)=="operation"
+ tree.out(1).dims=list(-1,-1)
+ tree.out(1).type=Type(-1,-1)
+ else
+ for i=1:size(tree.lhs)
+ tree.lhs(i).dims=list(-1,-1)
+ tree.lhs(i).type=Type(-1,-1)
+ end
+ end
+endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) ???? - INRIA - Scilab
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function [funcallname,variablename]=funcallsearch(instr,funcallname,fnamvect,variablename)
- //
- // FUNCALLSEARCH recursive function (used by "translatepaths" function)
- // Searches the functions names in each instruction of mtlbtree
- // Input-Output
- // -funcallname : a vector which contains the names of called functions if it exists a M-file having the same name in the Paths
- // -variablename : a vector which contains the names of declared variables
- // Input
- // -instr : mtlbtree instruction
- // -fnamvect : vector which contains all M-files names found in the Paths
-
- // case : ifthenelse instruction
- if typeof(instr) == "ifthenelse" then
- [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
- for i=1:size(instr.then)
- [funcallname,variablename]=funcallsearch((instr.then(i)),funcallname,fnamvect,variablename)
- end
- for i=1:size(instr.elseifs)
- for k=1:size(instr.elseifs(i).then)
- [funcallname,variablename]=funcallsearch((instr.elseifs(i).then(k)),funcallname,fnamvect,variablename)
- end
- end
- for i=1:size(instr.else)
- [funcallname,variablename]=funcallsearch((instr.else(i)),funcallname,fnamvect,variablename)
- end
- // case : selectcase instruction
- elseif typeof(instr) == "selectcase" then
- [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
-
- for i=1:size(instr.cases)
- [funcallname,variablename]=funcallsearch((instr.cases(i).expression),funcallname,fnamvect,variablename)
- for j=1:size(instr.cases(i).then)
- [funcallname,variablename]=funcallsearch((instr.cases(i).then(j)),funcallname,fnamvect,variablename)
- end
- end
- for i=1:size(instr.else)
- [funcallname,variablename]=funcallsearch(instr.else(i),funcallname,fnamvect,variablename)
- end
- // case : while instruction
- elseif typeof(instr) == "while" then
- [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
- for i=1:size(instr.statements)
- [funcallname,variablename]=funcallsearch(instr.statements(i),funcallname,fnamvect,variablename)
- end
- // case : for instruction
- elseif typeof(instr) == "for" then
- [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
- for i=1:size(instr.statements)
- [funcallname,variablename]=funcallsearch(instr.statements(i),funcallname,fnamvect,variablename)
- end
- // case : cste instruction
- elseif typeof(instr)== "cste" then
- return
- // case : variable instruction
- elseif typeof(instr)=="variable"
- if find(instr.name==variablename)==[] & find(instr.name==stripblanks(part(fnamvect,1:24)))<>[] & find(instr.name==funcallname)==[] then
- funcallname=[funcallname;fnamvect(find(instr.name==stripblanks(part(fnamvect,1:24))))]
- else
- return
- end
- // case : equal instruction
- elseif typeof(instr) == "equal" then
- [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
- // case : expression is a funcall
- elseif typeof(instr) == "funcall" then
- if find(funcallname==instr.name) == [] & find(instr.name==stripblanks(part(fnamvect,1:24)))<>[] then
- if size(find(instr.name==stripblanks(part(fnamvect,1:24))),2)==1 then
- funcallname=[funcallname;fnamvect(find(instr.name==stripblanks(part(fnamvect,1:24))))]
- else
- findvect=find(instr.name==stripblanks(part(fnamvect,1:24)))
- funcallname=[funcallname;fnamvect(findvect(2))]
- st = " " + mtlbtree.name + ": " + fnamvect(findvect(1))
- for i=2:size(findvect,2)
- st = st+ " <-> " + fnamvect(findvect(i))
- end
- st = st + gettext(": The 24 first characters of the files names are equal: ");
- warning(st)
- end
- end
- // case : expression is cste
- if typeof(instr.rhs)== "constant" then
- return
- else
- for ind=1:size(instr.rhs)
- [funcallname,variablename]=funcallsearch(instr.rhs(ind),funcallname,fnamvect,variablename)
- end
- end
- // case : operation instruction
- elseif typeof(instr) == "operation" then
- for ind=1:size(instr.operands)
- [funcallname,variablename]=funcallsearch(instr.operands(ind),funcallname,fnamvect,variablename)
- end
- end
-
-endfunction
\ No newline at end of file
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) ???? - INRIA - Scilab
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function [sci_equiv]=get_unknown(varname,lhslist)
- // Handle cases where varname appear in an expression while it is not known.
-
- // nam may be:
- // - a variable created in another clause part
- // - a M-file called without args
- // - a variable created by an eval
-
- // Check clause !!!
-
- // Check if it is a Matlab function not converted yet
- if or(varname==not_yet_converted()) then
- set_infos(msprintf(gettext("Matlab function %s not yet converted."),varname),2)
- tmpvar=Variable(varname,Infer())
- sci_equiv=Funcall("mtlb",1,Rhs_tlist(tmpvar),lhslist)
- else
- // Other cases: I am not able to determine what is nam
- set_infos(msprintf(gettext("mtlb(%s) can be replaced by %s() or %s whether %s is an M-file or not."),varname,varname,varname,varname),1)
- tmpvar=Variable(varname,Infer())
- sci_equiv=Funcall("mtlb",1,Rhs_tlist(tmpvar),lhslist)
- end
-endfunction
// For more information, see the COPYING file which you should have received
// along with this program.
-function [sci_equiv]=getvar2sci(var,lhslist)
+function sci_equiv = getvar2sci(var,lhslist)
// Translate the named variable acquisition
// Global variable for M2SCI
sci_equiv=Variable(varname,Infer(varslist(index).dims,varslist(index).type))
end
endfunction
+
+// -----------------------------------------------------------------------------
+
+function sci_equiv = get_unknown(varname,lhslist)
+ // Private function called only within getvar2sci()
+ //
+ // Handle cases where varname appear in an expression while it is not known.
+
+ // nam may be:
+ // - a variable created in another clause part
+ // - a M-file called without args
+ // - a variable created by an eval
+
+ // Check clause !!!
+
+ // Check if it is a Matlab function not converted yet
+ if or(varname==not_yet_converted()) then
+ set_infos(msprintf(gettext("Matlab function %s not yet converted."),varname),2)
+ tmpvar=Variable(varname,Infer())
+ sci_equiv=Funcall("mtlb",1,Rhs_tlist(tmpvar),lhslist)
+ else
+ // Other cases: I am not able to determine what is nam
+ set_infos(msprintf(gettext("mtlb(%s) can be replaced by %s() or %s whether %s is an M-file or not."),varname,varname,varname,varname),1)
+ tmpvar=Variable(varname,Infer())
+ sci_equiv=Funcall("mtlb",1,Rhs_tlist(tmpvar),lhslist)
+ end
+endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) ???? - INRIA - Scilab
-// Copyright 2018 - Samuel GOUGEON
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function txt = i_notation(txt)
- // This function changes 1i ,... by 1*i,...
-
- // M2SCI kernel functions called :
- // - isinstring
-
- // To succeed in this work, we successively suppress occurences which can be proved not to be complex notation
- // Until we are 'sure' to have a complex notation
-
- n=size(txt,"r")
-
- I="i";J="j"
- matches=[string(0:9)+I(ones(1,10)),".i",string(0:9)+J(ones(1,10)),".j"]
- symbs=["+","-","*","/","\","(","["," ","^"," ",",",";","=","{"]
- s1=["+","-","*","/","\",",",";"," ","^",".","&","|","''","]",")","}"]
- s2=[string(0:9),"d","e","D","E","."]
-
- for k=1:n
- // Isolate a possible appended comment
- st=strindex(txt(k),[";//","//"])
- endComment = "";
- tk = txt(k) + " "
- if st<> [] then
- for stk=1:size(st,"*")
- if ~isinstring(txt(k),st(stk)) then
- endComment = part(txt(k), st(stk):$);
- tk = part(txt(k), 1:st(stk)-1)
- break
- end
- end
- end
-
- // Find possible occurence of complex notation
- kc=strindex(tk,matches)
-
- // Kill indexes which point to non complex values (e.g. : a1item...)
- for kk=size(kc,"*"):-1:1
- km=kc(kk)+2
- if find(part(tk,km)==s1)==[] then kc(kk)=[],end
- end
-
- kc=[0 kc]
-
- for kk=size(kc,"*"):-1:2
- km=kc(kk)
- num=%T
- // Reads numeric value leading complex variable
- while or(part(tk,km)==s2)
- km=km-1
- if km<=kc(kk-1)+1 then
- km=kc(kk-1);
- num=%F;
- break
- end
- end
-
- tokill=%F
- num=part(tk,km+1:kc(kk)-1)
- ke=strindex(convstr(num),["e","d"])
- kd=strindex(convstr(num),".")
-
- // Searching for invalid numeric values (more than one dot...)
- if size(ke,2)>1|size(kd,2)>1 then
- tokill=%T
- elseif size(ke,2)==1&size(kd,2)==1 then
- if ke<kd then tokill=%T,end
- end
-
- if ~tokill then
- // If char which follows supposed complex notation is not an operation symbol
- if km<>kc(kk-1) then
- if and(part(tk,km)<>symbs) then tokill=%T,end
- end
- end
-
- if ~tokill then
- km=kc(kk)
- // If supposed complex notation is not in a string
- if ~isinstring(tk,km) then
- tk=part(tk,1:km)+"*%"+part(tk,km+1:length(tk))
- end
- end
- end
- txt(k) = tk + endComment
- end
-endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) ???? - INRIA - Scilab
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function txt=infer2txt(infer)
- txt=[]
- dims=[]
- if typeof(infer)=="infer" then
- for l=1:size(infer.dims)
- dims=[dims,string(infer.dims(l))]
- end
- else
- error(gettext("Not yet implemented."))
- end
- dims=strcat(dims," ")
-
- tp=infer.type.vtype
- if tp==1 then
- tp="Double"
- elseif tp==10 then
- tp="String"
- elseif or(tp==[4,6]) then
- tp="Boolean"
- elseif tp==16 then
- tp="Struct"
- for k = 1:size(infer.contents.index)
- if typeof(infer.contents.index(k))<>"list" then
- txt=[txt;expression2code(list(infer.contents.index(k)))+infer2txt(infer.contents.data(k))]
- else
- txt=[txt;expression2code(infer.contents.index(k))+infer2txt(infer.contents.data(k))]
- end
- end
- elseif tp==17 then
- tp="Cell"
- for k = 1:size(infer.contents.index)
- if typeof(infer.contents.index(k))<>"list" then
- txt=[txt;expression2code(list(infer.contents.index(k)))+infer2txt(infer.contents.data(k))]
- else
- txt=[txt;expression2code(infer.contents.index(k))+infer2txt(infer.contents.data(k))]
- end
- end
- elseif tp==9 then
- tp="Handle"
- else
- tp="Unknown"
- end
- if infer.type.property==Real then
- prop="Real"
- elseif infer.type.property==Complex then
- prop="Complex"
- else
- prop="Unknown"
- end
- txt=["|"+dims+"|"+tp+"|"+prop;txt]
-endfunction
// For more information, see the COPYING file which you should have received
// along with this program.
-function [converted_instr,nblines]=instruction2sci(mtlb_instr,nblines)
+function [converted_instr, nblines] = instruction2sci(mtlb_instr,nblines)
// Convertion of empty lines
if mtlb_instr==list("EOL") then
end
if typeof(mtlb_instr)=="equal" then
- [converted_instr]=equal2sci(mtlb_instr)
+ [converted_instr] = equal2sci(mtlb_instr)
+
elseif or(typeof(mtlb_instr)==["ifthenelse","selectcase","for","while","trycatch"]) then
- [converted_instr,nblines]=clause2sci(mtlb_instr,nblines)
+ [converted_instr, nblines] = clause2sci(mtlb_instr, nblines)
+
elseif or(typeof(mtlb_instr)==["funcall","cste","operation","variable"]) then
- [converted_instr]=expression2sci(mtlb_instr)
+ converted_instr = expression2sci(mtlb_instr)
+
elseif typeof(mtlb_instr)=="comment" then
- [converted_instr]=mtlb_instr
+ converted_instr = mtlb_instr
else
error(gettext("unknown instruction type %s."),typeof(mtlb_instr))
end
endfunction
+
+// ---------------------------------------------------------------------------
+
+function sci_instr = equal2sci(mtlb_instr)
+ // PRIVATE INTERNAL function called only by instruction2sci()
+ //
+ // Conversion of a Matlab instruction or expression to Scilab
+ // Input:
+ // - mtlb_instr: Matlab instr or expression to convert
+ // Output:
+ // - sci_instr: Scilab equivalent for mtlb_instr
+
+ // Trees to insert in converted function tree
+ global("m2sci_to_insert_b")
+ m2sci_to_insert_b=list()
+ global("m2sci_to_insert_a")
+ m2sci_to_insert_a=list()
+ global("varslist")
+
+ sci_instr=mtlb_instr
+
+ // Add inference data to lhs
+ lhslist=list()
+
+ // Inference field added to each lhs argument
+ // Get infos in varslist to init
+ for k=1:size(mtlb_instr.lhs)
+ if typeof(mtlb_instr.lhs(k))=="variable" then
+ [bval,index]=isdefinedvar(mtlb_instr.lhs(k))
+ if bval then
+ INFER=varslist(index).infer
+ else
+ INFER=Infer()
+ end
+ lhslist($+1)=Variable(mtlb_instr.lhs(k).name,INFER)
+ elseif typeof(mtlb_instr.lhs(k))=="operation" then
+ if mtlb_instr.lhs(k).operator<>"ins" then
+ error(msprintf(gettext("lhs cannot be a %s operation."),mtlb_instr.lhs(k).operator))
+ end
+
+ [bval,index]=isdefinedvar(mtlb_instr.lhs(k).operands(1))
+ if bval then
+ INFER=varslist(index).infer
+ else
+ INFER=Infer()
+ end
+
+ lhslist($+1)=Operation(mtlb_instr.lhs(k).operator,..
+ mtlb_instr.lhs(k).operands,..
+ list(Variable(mtlb_instr.lhs(k).operands(1).name,INFER)))
+ else
+ error(msprintf(gettext("lhs cannot be a %s."),typeof(mtlb_instr.lhs(k))))
+ end
+ end
+
+ // Convert expression
+
+ [sci_expr]=expression2sci(mtlb_instr.expression,lhslist);
+
+ if sci_expr==list() then // Conversion made by inserted instructions or 'm2scideclare'
+ sci_instr=list()
+ else
+
+ sci_instr.expression=sci_expr;
+
+ // Update lhs of instruction
+ select typeof(sci_instr.expression)
+ case "operation" then
+ sci_instr.lhs=sci_expr.out;
+ case "funcall" then
+ sci_instr.lhs=sci_instr.expression.lhs
+ if typeof(mtlb_instr.expression)=="funcall" then
+ sci_instr.lhs=sci_expr.lhs;
+ end
+ case "cste" then
+ sci_instr.lhs=lhslist;
+ sci_instr.lhs(1).dims=sci_expr.dims;
+ sci_instr.lhs(1).type=sci_expr.type;
+ case "variable" then
+ sci_instr.lhs=lhslist;
+ sci_instr.lhs(1).dims=sci_expr.dims;
+ sci_instr.lhs(1).type=sci_expr.type;
+ else
+ error(msprintf(gettext("%s is not yet implemented."),typeof(sci_instr.expression)));
+ end
+
+ // If lhs are insertion operation, they also have to be converted
+ for k=1:size(sci_instr.lhs)
+ if typeof(sci_instr.lhs(k))=="operation" then
+ sci_instr.lhs(k).operands($+1)=sci_instr.expression
+ // Keep just one inference field in sci_instr.expression (if is a funcall) so that inference can be made in operation2sci()
+ if typeof(sci_instr.lhs(k).operands($))=="funcall" then
+ for l=1:size(sci_instr.lhs(k).operands($).lhs)
+ if l<>k then
+ sci_instr.lhs(k).operands($).lhs(l)=list()
+ end
+ end
+ l=1
+ while l<=size(sci_instr.lhs(k).operands($).lhs)
+ if sci_instr.lhs(k).operands($).lhs(l)==list() then
+ sci_instr.lhs(k).operands($).lhs(l)=null()
+ else
+ l=l+1
+ end
+ end
+ // Verify that there is just one lhs kept
+ if size(sci_instr.lhs(k).operands($).lhs)<>1 then pause;end
+ end
+ // If insertion made in an unknown variable, I add it to varslist
+ inservar=sci_instr.lhs(k).operands(1)
+ [bval,index]=isdefinedvar(inservar)
+ if ~bval then
+ // Variable added to varslist before insertion
+ if funptr(inservar.name)<>0 then
+ matname="%"+inservar.name
+ else
+ matname=inservar.name
+ end
+ if sci_instr.expression.vtype==Struct then
+ // Variable is initialized to struct() in converted script is does not already exist
+ varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Struct,Unknown)))
+ //m2sci_to_insert_b($+1)=Equal(list(inservar),Funcall("struct",1,list(),list()))
+ elseif sci_instr.expression.vtype==Cell then
+ // Variable is initialized to cell() in converted script is does not already exist
+ varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Cell,Unknown)))
+ //m2sci_to_insert_b($+1)=Equal(list(inservar),Funcall("cell",1,list(),list()))
+ else
+ // Variable is initialized to [] in converted script is does not already exist
+ varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Double,Real)))
+ //m2sci_to_insert_b($+1)=Equal(list(inservar),Cste([]))
+ end
+ sci_instr.lhs(k).out(1).infer=varslist($).infer
+ else
+ sci_instr.lhs(k).out(1).infer=varslist(index).infer
+ end
+ [sci_instr.lhs(k)]=operation2sci(sci_instr.lhs(k))
+ if typeof(sci_instr.lhs(k))=="operation" then
+ if or(sci_instr.lhs(k).operands($)<>sci_instr.expression) then // Update expression if has been modified while converting lhs
+ sci_instr.expression=sci_instr.lhs(k).operands($)
+ end
+
+ sci_instr.lhs(k).operands($)=null()
+ updatevarslist(sci_instr.lhs(k).out)
+ else
+ // Insertion done by inserted instruction
+ sci_instr=list()
+ return
+ end
+ end
+ end
+ // Update varslist
+ updatevarslist(sci_instr.lhs);
+ end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function [sci_clause, nblines] = clause2sci(mtlb_clause, nblines, leveltemp)
+ // M2SCI function
+
+ // Global variables for M2SCI
+ global("m2sci_to_insert_b")
+ global("varslist")
+
+ // Increment level of clause indentation
+ level
+
+ // Temp variable used to store instructions to insert before clause
+ to_insert=list()
+ select typeof(mtlb_clause)
+ // --- TRYCATCH ---
+ case "trycatch"
+ level=[level;0]
+
+ // Get instructions to insert if there are
+ if m2sci_to_insert_b<>list() then
+ to_insert=m2sci_to_insert_b
+ m2sci_to_insert_b=list()
+ end
+
+ // Convert try
+ sci_try=list()
+ level($)=level($)+1
+ for k=1:size(mtlb_clause.trystat)
+ if typeof(mtlb_clause.trystat(k))=="sup_equal" then
+ sci_try_temp=list()
+ for i=1:size(mtlb_clause.trystat(k).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.trystat(k).sup_instr(i),nblines)
+ sci_try_temp=update_instr_list(sci_try_temp,instr)
+ end
+ sci_try($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_try_temp,mtlb_clause.trystat(k).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.trystat(k),nblines)
+ sci_try=update_instr_list(sci_try,instr)
+ end
+ end
+
+ // Convert catch
+ sci_catch=list()
+ level($)=level($)+1
+ for k=1:size(mtlb_clause.catchstat)
+ if typeof(mtlb_clause.catchstat(k))=="sup_equal" then
+ sci_catch_temp=list()
+ for i=1:size(mtlb_clause.catchstat(k).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.catchstat(k).sup_instr(i),nblines)
+ sci_catch_temp=update_instr_list(sci_catch_temp,instr)
+ end
+ sci_catch($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_catch_temp,mtlb_clause.catchstat(k).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.catchstat(k),nblines)
+ sci_catch=update_instr_list(sci_catch,instr)
+ end
+ end
+
+ // Create Scilab trycatch
+ sci_clause=tlist(["trycatch","trystat","catchstat"],sci_try,sci_catch)
+ level($)=level($)+1
+ updatevarslist("END OF CLAUSE")
+
+ // --- IF ---
+ case "ifthenelse"
+ level=[level;0]
+
+ // Convert expression
+ [sci_expr]=expression2sci(mtlb_clause.expression)
+
+ // Get instructions to insert if there are
+ if m2sci_to_insert_b<>list() then
+ to_insert=m2sci_to_insert_b
+ m2sci_to_insert_b=list()
+ end
+
+ // Convert then statements
+ sci_then=list()
+ level($)=level($)+1
+ for k=1:size(mtlb_clause.then)
+ if typeof(mtlb_clause.then(k))=="sup_equal" then
+ sci_then_temp=list()
+ for i=1:size(mtlb_clause.then(k).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.then(k).sup_instr(i),nblines)
+ sci_then_temp=update_instr_list(sci_then_temp,instr)
+ end
+ sci_then($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_then_temp,mtlb_clause.then(k).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.then(k),nblines)
+ sci_then=update_instr_list(sci_then,instr)
+ end
+ end
+
+ // Convert elseifs
+ sci_elseifs=list()
+ for k=1:size(mtlb_clause.elseifs)
+ level($)=level($)+1
+
+ // Convert expression
+ [sci_exprn]=expression2sci(mtlb_clause.elseifs(k).expression)
+
+ // Get instructions to insert if there are
+ if m2sci_to_insert_b<>list() then
+ to_insert=m2sci_to_insert_b
+ m2sci_to_insert_b=list()
+ end
+
+ // Convert statements
+ sci_stat=list()
+ for l=1:size(mtlb_clause.elseifs(k).then)
+ if typeof(mtlb_clause.elseifs(k).then(l))=="sup_equal" then
+ sci_stat_temp=list()
+ for i=1:size(mtlb_clause.elseifs(k).then(l).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.elseifs(k).then(l).sup_instr(i),nblines)
+ sci_stat_temp=update_instr_list(sci_stat_temp,instr)
+ end
+ sci_stat($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_stat_temp,mtlb_clause.elseifs(k).then(l).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.elseifs(k).then(l),nblines)
+ sci_stat=update_instr_list(sci_stat,instr)
+ end
+ end
+ sci_elseifs($+1)=tlist(["elseif","expression","then"],sci_exprn,sci_stat)
+ end
+
+ // Convert else
+ sci_else=list()
+ if size(mtlb_clause.else)<>0 then
+ level($)=level($)+1
+ end
+ for k=1:size(mtlb_clause.else)
+ if typeof(mtlb_clause.else(k))=="sup_equal" then
+ sci_else_temp=list()
+ for i=1:size(mtlb_clause.else(k).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.else(k).sup_instr(i),nblines)
+ sci_else_temp=update_instr_list(sci_else_temp,instr)
+ end
+ sci_else($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_else_temp,mtlb_clause.else(k).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.else(k),nblines)
+ sci_else=update_instr_list(sci_else,instr)
+ end
+ end
+
+ // Create Scilab ifthenelse
+ sci_clause=tlist(["ifthenelse","expression","then","elseifs","else"],sci_expr,sci_then,sci_elseifs,sci_else)
+ level($)=level($)+1
+ updatevarslist("END OF CLAUSE")
+
+ // --- SELECT ---
+ case "selectcase"
+ level=[level;0]
+ // Convert expression
+ sci_expr=list()
+ [sci_expr(1)]=expression2sci(mtlb_clause.expression(1))
+ for i=2:size(mtlb_clause.expression)
+ sci_expr(i)=mtlb_clause.expression(i) // EOL or comment
+ end
+
+ // Get instructions to insert if there are
+ if m2sci_to_insert_b<>list() then
+ to_insert=m2sci_to_insert_b
+ m2sci_to_insert_b=list()
+ end
+
+ // Convert cases
+ sci_cases=list()
+ k=0
+ while k<size(mtlb_clause.cases)
+ k=k+1
+ level($)=level($)+1
+ // Convert expression
+ if typeof(mtlb_clause.cases(k).expression)=="funcall" then
+ if mtlb_clause.cases(k).expression.name=="makecell" then
+ // Insert new cases
+ for nbcas=size(mtlb_clause.cases):-1:k+1
+ mtlb_clause.cases(nbcas+size(mtlb_clause.cases(k).expression.rhs))=mtlb_clause.cases(nbcas)
+ end
+ for nbrhs=1:size(mtlb_clause.cases(k).expression.rhs)
+ mtlb_clause.cases(nbrhs+k)=tlist(["case","expression","then"],mtlb_clause.cases(k).expression.rhs(nbrhs),mtlb_clause.cases(k).then)
+ end
+ mtlb_clause.cases(k)=null()
+ end
+ end
+ [sci_exprn]=expression2sci(mtlb_clause.cases(k).expression)
+ // Get instructions to insert if there are
+ if m2sci_to_insert_b<>list() then
+ to_insert=m2sci_to_insert_b
+ m2sci_to_insert_b=list()
+ end
+
+ // Convert statements
+ sci_stat=list()
+ for l=1:size(mtlb_clause.cases(k).then)
+ if typeof(mtlb_clause.cases(k).then(l))=="sup_equal" then
+ sci_stat_temp=list()
+ for i=1:size(mtlb_clause.cases(k).then(l).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.cases(k).then(l).sup_instr(i),nblines)
+ sci_stat_temp=update_instr_list(sci_stat_temp,instr)
+ end
+ sci_stat($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_stat_temp,mtlb_clause.cases(k).then(l).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.cases(k).then(l),nblines)
+ sci_stat=update_instr_list(sci_stat,instr)
+ end
+ end
+ sci_cases($+1)=tlist(["case","expression","then"],sci_exprn,sci_stat)
+ end
+
+ // Convert else
+ sci_else=list()
+ if size(mtlb_clause.else)<>0 then
+ level($)=level($)+1
+ end
+ for k=1:size(mtlb_clause.else)
+ if typeof(mtlb_clause.else(k))=="sup_equal" then
+ sci_else_temp=list();
+ for i=1:size(mtlb_clause.else(k).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.else(k).sup_instr(i),nblines)
+ sci_else_temp=update_instr_list(sci_else_temp,instr)
+ end
+ sci_else($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_else_temp,mtlb_clause.else(k).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.else(k),nblines)
+ sci_else=update_instr_list(sci_else,instr)
+ end
+ end
+ // Create Scilab selectcase
+ sci_clause=tlist(["selectcase","expression","cases","else"],sci_expr,sci_cases,sci_else)
+ level($)=level($)+1
+ updatevarslist("END OF CLAUSE")
+
+ // --- WHILE ---
+ case "while"
+ level=[level;0]
+ sci_do=list()
+ // Convert expression
+ [sci_expr]=expression2sci(mtlb_clause.expression)
+ // If there are instructions to insert, while is modified so that inserted instruction is evaluated in each loop
+ if m2sci_to_insert_b<>list() then
+ newif=tlist(["ifthenelse","expression","then","elseifs","else"],sci_expr,list(Funcall("break",1,list(),list())),list(),list())
+ m2sci_to_insert_b($+1)=newif
+ sci_expr=Cste(%T)
+ sci_do=m2sci_to_insert_b
+ m2sci_to_insert_b=list()
+ end
+
+ // Convert all do instructions
+ level($)=level($)+1
+ for k=1:size(mtlb_clause.statements)
+ if typeof(mtlb_clause.statements(k))=="sup_equal" then
+ sci_do_temp=list()
+ for i=1:size(mtlb_clause.statements(k).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.statements(k).sup_instr(i),nblines)
+ // If inserted instruction is an initialisation, it has to be done just one time and before loop
+ l=1;
+ while l <= size(m2sci_to_insert_b)
+ if typeof(m2sci_to_insert_b(l))=="equal" & ..
+ (and(m2sci_to_insert_b(l).expression==Cste([])) | ..
+ and(m2sci_to_insert_b(l).expression==Funcall("struct",1,list(),list())) | ..
+ and(m2sci_to_insert_b(l).expression==Funcall("cell",1,list(),list())) ) then
+ to_insert($+1)=m2sci_to_insert_b(l)
+ m2sci_to_insert_b(l)=null()
+ if size(m2sci_to_insert_b)>=l & m2sci_to_insert_b(l)==list("EOL") then
+ to_insert($+1)=m2sci_to_insert_b(l)
+ m2sci_to_insert_b(l)=null()
+ end
+ else
+ l=l+1;
+ end
+ end
+ sci_do_temp=update_instr_list(sci_do_temp,instr)
+ end
+ sci_do($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_do_temp,mtlb_clause.statements(k).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.statements(k),nblines)
+ // If inserted instruction is an initialisation, it has to be done just one time and before loop
+ l=1;
+ while l <= size(m2sci_to_insert_b)
+ if typeof(m2sci_to_insert_b(l))=="equal" & ..
+ (and(m2sci_to_insert_b(l).expression==Cste([])) | ..
+ and(m2sci_to_insert_b(l).expression==Funcall("struct",1,list(),list())) | ..
+ and(m2sci_to_insert_b(l).expression==Funcall("cell",1,list(),list())) ) then
+ to_insert($+1)=m2sci_to_insert_b(l)
+ m2sci_to_insert_b(l)=null()
+ if size(m2sci_to_insert_b)>=l & m2sci_to_insert_b(l)==list("EOL") then
+ to_insert($+1)=m2sci_to_insert_b(l)
+ m2sci_to_insert_b(l)=null()
+ end
+ else
+ l=l+1;
+ end
+ end
+ sci_do=update_instr_list(sci_do,instr)
+ end
+ end
+
+ // Create Scilab while
+ sci_clause=tlist(["while","expression","statements"],sci_expr,sci_do)
+ level($)=level($)+1
+ updatevarslist("END OF CLAUSE")
+
+ // --- FOR ---
+ case "for"
+ //level=[level;1]
+ // Convert expression
+ [sci_expr,nblines]=instruction2sci(mtlb_clause.expression,nblines)
+ if typeof(sci_expr)=="equal" then
+ [bval,pos]=isdefinedvar(sci_expr.lhs(1))
+ if bval then
+ varslist(pos).infer.dims=list(varslist(pos).infer.dims(1),1)
+ end
+ end
+ // Get instructions to insert if there are
+ if m2sci_to_insert_b<>list() then
+ to_insert=m2sci_to_insert_b
+ m2sci_to_insert_b=list()
+ end
+ sci_instr=list()
+ // Convert all do instructions
+ for k=1:size(mtlb_clause.statements)
+ if typeof(mtlb_clause.statements(k))=="sup_equal" then
+ sci_instr_temp=list()
+ for i=1:size(mtlb_clause.statements(k).sup_instr)
+ [instr,nblines]=instruction2sci(mtlb_clause.statements(k).sup_instr(i),nblines)
+ // If inserted instruction is an initialisation, it has to be done just one time and before loop
+ l=1;
+ while l <= size(m2sci_to_insert_b)
+ if typeof(m2sci_to_insert_b(l))=="equal" & ..
+ (and(m2sci_to_insert_b(l).expression==Cste([])) | ..
+ and(m2sci_to_insert_b(l).expression==Funcall("struct",1,list(),list())) | ..
+ and(m2sci_to_insert_b(l).expression==Funcall("cell",1,list(),list())) ) then
+ to_insert($+1)=m2sci_to_insert_b(l)
+ m2sci_to_insert_b(l)=null()
+ if size(m2sci_to_insert_b)>=l & m2sci_to_insert_b(l)==list("EOL") then
+ to_insert($+1)=m2sci_to_insert_b(l)
+ m2sci_to_insert_b(l)=null()
+ end
+ else
+ l=l+1;
+ end
+ end
+ sci_instr_temp=update_instr_list(sci_instr_temp,instr)
+ end
+ sci_instr($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_instr_temp,mtlb_clause.statements(k).nb_opr)
+ else
+ [instr,nblines]=instruction2sci(mtlb_clause.statements(k),nblines)
+ // If inserted instruction is an initialisation, it has to be done just one time and before loop
+ l=1;
+ while l <= size(m2sci_to_insert_b)
+ if typeof(m2sci_to_insert_b(l))=="equal" & ..
+ (and(m2sci_to_insert_b(l).expression==Cste([])) | ..
+ and(m2sci_to_insert_b(l).expression==Funcall("struct",1,list(),list())) | ..
+ and(m2sci_to_insert_b(l).expression==Funcall("cell",1,list(),list())) ) then
+ to_insert($+1)=m2sci_to_insert_b(l)
+ m2sci_to_insert_b(l)=null()
+ if size(m2sci_to_insert_b)>=l & m2sci_to_insert_b(l)==list("EOL") then
+ to_insert($+1)=m2sci_to_insert_b(l)
+ m2sci_to_insert_b(l)=null()
+ end
+ else
+ l=l+1;
+ end
+ end
+ sci_instr=update_instr_list(sci_instr,instr)
+ end
+ end
+ // Create Scilab while
+ sci_clause=tlist(["for","expression","statements"],sci_expr,sci_instr)
+ else
+ error(msprintf(gettext("unknown clause type: %s."),typeof(mtlb_clause)))
+ end
+ m2sci_to_insert_b=to_insert
+ if m2sci_to_insert_b<>list() then
+ m2sci_to_insert_b($+1)=list("EOL");
+ end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function updatevarslist(instr_lhs)
+ // PRIVATE INTERNAL function called only by equal2sci() and clause2sci()
+ // that are also private to instruction2sci()
+ //
+ // (2 functions in this file: merge_vars() at the end)
+ // Update list of M2SCI variables with converted instruction lhs
+ // Input:
+ // - instr_lhs: list of lhs of current instruction
+ // - in_a_clause: boolean value
+ // Set to 1 if instruction is in a clause
+ // In this case, type and dimensions are set to unknown if differ from those already stored in varslist
+ // (Default value is %F)
+
+ // Global variable for M2SCI
+ global("varslist")
+ if isempty(varslist)
+ varslist = list()
+ end
+ // level is declared in m2sci.sci and modified in clause2sci.sci
+ level;
+
+ rhs=argn(2)
+ if rhs==2 then
+ in_a_clause=%F
+ end
+
+ // Merge infered data from the last two parts of clause which are above the current part
+ // if we are in the third part of clause (current), then : merge the first and second part of clause
+ // when end of conversion of a clause : merge infered data from the last two parts of clause
+ levelsize=size(level,1)
+ changepartclause=%F
+
+ for i=size(varslist):-1:1
+ if size(varslist(i).level,1)==levelsize then
+ varlevel=varslist(i).level
+ if varlevel($)<>level($)
+ changepartclause=%T
+ else
+ changepartclause=%F
+ break
+ end
+ end
+ end
+ if changepartclause | instr_lhs=="END OF CLAUSE" then
+ index=[] // Search variables from two part above current part clause
+ for k=size(varslist):-1:1
+ if size(varslist(k).level,1)==levelsize then
+ varlevel=varslist(k).level
+ if and(varlevel(1:$-1)==level(1:$-1)) & varlevel($)==level($)-2 then
+ index=[index;k]
+ end
+ end
+ end
+ if index<>[] then // Found variables from the second part above current part of a clause
+ for k=1:size(index,1)
+ boolmerge =%F
+ for i=size(varslist):-1:1 // Search variables from the first part above current part of a clause, and which have the same name than variables from the second part above current part of a clause
+ varlevel=varslist(i).level
+ if varslist(i).matname==varslist(index(k)).matname & and(varlevel(1:$-1)==level(1:$-1)) & varlevel($)==level($)-1 then
+ boolmerge =%T // Found the same variable name from the last two parts above the current part : Merge
+ merge_vars(index(k),varslist(i))
+ varslist(i)=null()
+ break
+ end
+ end
+ if ~boolmerge then
+ varslist(index(k)).level=[level(1:$-1);level($)-1]
+ end
+ end
+ end
+ end
+
+ // Special case when end of conversion of a clause
+ // Merge infered data from clause and those from level-1
+ if instr_lhs=="END OF CLAUSE" then // Search variables in the last part of a clause (above end of conversion of a clause)
+ index=[] //
+ for k=size(varslist):-1:1 // Search variables from level-1 which have the same name than variables from the last part of current level
+ varlevel=varslist(k).level
+ if varlevel==[level(1:$-1);level($)-1] then
+ index=[index;k]
+ end
+ end
+ if index<>[] then
+ for j=1:size(index,1)
+ boolmerge=%F
+ for k=size(varslist):-1:1 //
+ varlevel=varslist(k).level
+ if varslist(k).matname==varslist(index(j)).matname & and(varlevel==level(1:$-1)) then
+ boolmerge=%T // Found variables from level-1 which have the same name than variables from the last part of current level : Merge
+ index_lower_level=k
+ merge_vars(index_lower_level,varslist(index(j)))
+ varslist(k).level=level(1:$-1)
+ varslist(index(j))=null()
+ break
+ end
+ end
+ if boolmerge==%F then
+ varslist(index(j)).level=level(1:$-1)
+ end
+ end
+ end
+ return
+ end
+
+ // Expression: lhs name is empty => nothing to do
+ if instr_lhs==list() then
+ return
+ end
+
+ // Remove lhs which are not variables
+ k=1
+ while k<=size(instr_lhs)
+ // Insertion operation
+ if typeof(instr_lhs(k))=="operation" then
+ instr_lhs(k)=null()
+ else
+ k=k+1
+ end
+ end
+
+ if size(instr_lhs)==0 then
+ return
+ end
+
+ // Update varslist
+ for k=1:size(instr_lhs)
+ [bval,index]=isdefinedvar(instr_lhs(k))
+ ierr=execstr("zz=instr_lhs(k).contents.index","errcatch")
+ if ierr<>0 then pause;end
+ // Remove multiple entries from contents
+ for kcont = size(instr_lhs(k).contents.index):-1:1
+ [infertlist,pos]=get_contents_infer(instr_lhs(k),instr_lhs(k).contents.index(kcont))
+ if pos<>0 & pos<>kcont then
+ instr_lhs(k).contents.index(pos)=null()
+ instr_lhs(k).contents.data(pos)=null()
+ end
+ end
+ // If variable exists for the current level in the same part of clause then update exixting variable
+ if bval
+ boolupdate=%F
+ for l=1:size(varslist)
+ if varslist(l).matname==instr_lhs(k).name & varslist(l).level==level then
+ varslist(l)=M2scivar(varslist(l).sciname,..
+ varslist(l).matname,..
+ Infer(instr_lhs(k).infer.dims,instr_lhs(k).infer.type,instr_lhs(k).infer.contents),..
+ varslist(l).level)
+ boolupdate=%T
+ break
+ end
+ end
+ // If variable exists, but not for the current level or not in the same part of clause then Update variable then create new variable
+ if ~boolupdate then
+ varslist($+1)=M2scivar(varslist(index).sciname,..
+ varslist(index).matname,..
+ instr_lhs(k).infer,..
+ level)
+ end
+ else
+ // Variable added to varslist if as a name (not done for expressions
+ if execstr("f=instr_lhs(k).name","errcatch")<>0 then pause;end;errclear();
+ if instr_lhs(k).name<>"ans" then
+ varslist($+1)=M2scivar(instr_lhs(k).name,..
+ instr_lhs(k).name,..
+ instr_lhs(k).infer,..
+ level)
+ end
+ end
+ end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function merge_vars(oldvarindex, newvar)
+ // PRIVATE INTERNAL function called only by updatevarslist() hereabove.
+ // M2SCI function
+ // Merge two variables inference properties, if different then set to Unknown
+ // Input:
+ // - oldvarindex: index of old variable in varslist
+ // - newvar: new variable to take in account to update oldvar properties
+
+ // Global variable for M2SCI
+ global("varslist")
+ oldvar=varslist(oldvarindex)
+
+ olddims=oldvar.dims
+ oldvtype=oldvar.vtype
+ oldprop=oldvar.property
+
+ newdims=newvar.dims
+ newvtype=newvar.vtype
+ newprop=newvar.property
+
+ // Verify dims
+ for l=1:min(size(newdims),size(olddims))
+ if newdims(l)<>olddims(l) then
+ newdims(l)=Unknown
+ end
+ end
+ if size(newdims)>size(olddims) then
+ for l=size(olddims):size(newdims)
+ newdims(l)=null()
+ end
+ end
+
+ // Verify vtype
+ if newvtype<>oldvtype then
+ newvtype=Unknown
+ end
+
+ // Verify property
+ if newprop<>oldprop then
+ newprop=Unknown
+ end
+
+ // Verify contents
+ for k = 1:size(newvar.contents.index)
+ olddata=get_contents_infer(oldvar,newvar.contents.index(k))
+ newdata=newvar.contents.data(k)
+
+ if or(olddata<>newdata) then
+ newvar.infer.contents.data(k)=Infer()
+ end
+ end
+
+ // Write result in varslist
+ varslist(oldvarindex)=M2scivar(oldvar.sciname,..
+ oldvar.matname,..
+ Infer(newdims,Type(newvtype,newprop),newvar.contents),..
+ newvar.level)
+endfunction
// For more information, see the COPYING file which you should have received
// along with this program.
-function funcallname=lst_funcall(fil,fnamvect)
- // LST_FUNCALL function (used by "translatepaths" function) Creates a list of vectors. The first component of each vector is the name of a M-file (found in the Paths to translate), followed by the called functions by this file
+function funcallname = lst_funcall(fil, fnamvect)
+ // Internal function called only by translatepaths()
+ //
+ // Creates a list of vectors.
+ // The first component of each vector is the name of a M-file (found in the Paths to translate),
+ // followed by the called functions by this file
// Output
// -funcallname : a list of vectors
// Input
funcallname=[fnam;funcallname]
endfunction
+
+// ---------------------------------------------------------------------------
+
+function variablename = variablesearch(instr,variablename)
+ // PRIVATE INTERNAL function called only by lst_funcall() hereabove and itself (recursive)
+ //
+ // Searches names of declared variables for each instruction of mtlbtree
+ // Output
+ // -variablename : a vector which contains the names of declared variables
+ // -instr : mtlbtree instruction
+
+ // case : ifthenelse instruction
+ if typeof(instr) == "ifthenelse" then
+ for i=1:size(instr.then)
+ [variablename]=variablesearch((instr.then(i)),variablename)
+ end
+ for i=1:size(instr.elseifs)
+ for k=1:size(instr.elseifs(i).then)
+ [variablename]=variablesearch((instr.elseifs(i).then(k)),variablename)
+ end
+ end
+ for i=1:size(instr.else)
+ [variablename]=variablesearch((instr.else(i)),variablename)
+ end
+ // case : selectcase instruction
+ elseif typeof(instr) == "selectcase" then
+ for i=1:size(instr.cases)
+ [variablename]=variablesearch(instr.cases(i).expression,variablename)
+ for j=1:size(instr.cases(i).then)
+ [variablename]=variablesearch((instr.cases(i).then(j)),variablename)
+ end
+ end
+ for i=1:size(instr.else)
+ [variablename]=variablesearch(instr.else(i),variablename)
+ end
+ // case : while instruction
+ elseif typeof(instr) == "while" then
+ for i=1:size(instr.statements)
+ [variablename]=variablesearch(instr.statements(i),variablename)
+ end
+ // case : for instruction
+ elseif typeof(instr) == "for" then
+ [variablename]=variablesearch(instr.expression,variablename)
+ for i=1:size(instr.statements)
+ [variablename]=variablesearch(instr.statements(i),variablename)
+ end
+ // case : equal instruction
+ elseif typeof(instr) == "equal" then
+ for i=1:size(instr.lhs)
+ [variablename]=variablesearch(instr.lhs(i),variablename)
+ end
+ // case : operation instruction
+ elseif typeof(instr) == "operation" then
+ if instr.operator=="ins" then
+ if find(instr.operands(1).name==variablename)==[] then
+ variablename($+1)=instr.operands(1).name
+ end
+ end
+ // case : variable instruction
+ elseif typeof(instr) == "variable" then
+ if find(instr.name==variablename)==[] & instr.name<>"ans" then
+ variablename($+1)=instr.name
+ end
+ end
+
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function [funcallname,variablename] = funcallsearch(instr,funcallname,fnamvect,variablename)
+ // PRIVATE INTERNAL function called only by lst_funcall() hereabove and itself (recursive)
+ //
+ // Searches the functions names in each instruction of mtlbtree
+ // Input-Output
+ // -funcallname : a vector which contains the names of called functions if it exists a M-file having the same name in the Paths
+ // -variablename : a vector which contains the names of declared variables
+ // Input
+ // -instr : mtlbtree instruction
+ // -fnamvect : vector which contains all M-files names found in the Paths
+
+ // case : ifthenelse instruction
+ if typeof(instr) == "ifthenelse" then
+ [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+ for i=1:size(instr.then)
+ [funcallname,variablename]=funcallsearch((instr.then(i)),funcallname,fnamvect,variablename)
+ end
+ for i=1:size(instr.elseifs)
+ for k=1:size(instr.elseifs(i).then)
+ [funcallname,variablename]=funcallsearch((instr.elseifs(i).then(k)),funcallname,fnamvect,variablename)
+ end
+ end
+ for i=1:size(instr.else)
+ [funcallname,variablename]=funcallsearch((instr.else(i)),funcallname,fnamvect,variablename)
+ end
+ // case : selectcase instruction
+ elseif typeof(instr) == "selectcase" then
+ [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+
+ for i=1:size(instr.cases)
+ [funcallname,variablename]=funcallsearch((instr.cases(i).expression),funcallname,fnamvect,variablename)
+ for j=1:size(instr.cases(i).then)
+ [funcallname,variablename]=funcallsearch((instr.cases(i).then(j)),funcallname,fnamvect,variablename)
+ end
+ end
+ for i=1:size(instr.else)
+ [funcallname,variablename]=funcallsearch(instr.else(i),funcallname,fnamvect,variablename)
+ end
+ // case : while instruction
+ elseif typeof(instr) == "while" then
+ [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+ for i=1:size(instr.statements)
+ [funcallname,variablename]=funcallsearch(instr.statements(i),funcallname,fnamvect,variablename)
+ end
+ // case : for instruction
+ elseif typeof(instr) == "for" then
+ [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+ for i=1:size(instr.statements)
+ [funcallname,variablename]=funcallsearch(instr.statements(i),funcallname,fnamvect,variablename)
+ end
+ // case : cste instruction
+ elseif typeof(instr)== "cste" then
+ return
+ // case : variable instruction
+ elseif typeof(instr)=="variable"
+ if find(instr.name==variablename)==[] & find(instr.name==stripblanks(part(fnamvect,1:24)))<>[] & find(instr.name==funcallname)==[] then
+ funcallname=[funcallname;fnamvect(find(instr.name==stripblanks(part(fnamvect,1:24))))]
+ else
+ return
+ end
+ // case : equal instruction
+ elseif typeof(instr) == "equal" then
+ [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+ // case : expression is a funcall
+ elseif typeof(instr) == "funcall" then
+ if find(funcallname==instr.name) == [] & find(instr.name==stripblanks(part(fnamvect,1:24)))<>[] then
+ if size(find(instr.name==stripblanks(part(fnamvect,1:24))),2)==1 then
+ funcallname=[funcallname;fnamvect(find(instr.name==stripblanks(part(fnamvect,1:24))))]
+ else
+ findvect=find(instr.name==stripblanks(part(fnamvect,1:24)))
+ funcallname=[funcallname;fnamvect(findvect(2))]
+ st = " " + mtlbtree.name + ": " + fnamvect(findvect(1))
+ for i=2:size(findvect,2)
+ st = st+ " <-> " + fnamvect(findvect(i))
+ end
+ st = st + gettext(": The 24 first characters of the files names are equal: ");
+ warning(st)
+ end
+ end
+ // case : expression is cste
+ if typeof(instr.rhs)== "constant" then
+ return
+ else
+ for ind=1:size(instr.rhs)
+ [funcallname,variablename]=funcallsearch(instr.rhs(ind),funcallname,fnamvect,variablename)
+ end
+ end
+ // case : operation instruction
+ elseif typeof(instr) == "operation" then
+ for ind=1:size(instr.operands)
+ [funcallname,variablename]=funcallsearch(instr.operands(ind),funcallname,fnamvect,variablename)
+ end
+ end
+
+endfunction
clearglobal varslist
clearglobal %graphics
endfunction
+
+// ---------------------------------------------------------------------------
+
+function txt = infer2txt(infer)
+ txt=[]
+ dims=[]
+ if typeof(infer)=="infer" then
+ for l=1:size(infer.dims)
+ dims=[dims,string(infer.dims(l))]
+ end
+ else
+ error(gettext("Not yet implemented."))
+ end
+ dims=strcat(dims," ")
+
+ tp=infer.type.vtype
+ if tp==1 then
+ tp="Double"
+ elseif tp==10 then
+ tp="String"
+ elseif or(tp==[4,6]) then
+ tp="Boolean"
+ elseif tp==16 then
+ tp="Struct"
+ for k = 1:size(infer.contents.index)
+ if typeof(infer.contents.index(k))<>"list" then
+ txt=[txt;expression2code(list(infer.contents.index(k)))+infer2txt(infer.contents.data(k))]
+ else
+ txt=[txt;expression2code(infer.contents.index(k))+infer2txt(infer.contents.data(k))]
+ end
+ end
+ elseif tp==17 then
+ tp="Cell"
+ for k = 1:size(infer.contents.index)
+ if typeof(infer.contents.index(k))<>"list" then
+ txt=[txt;expression2code(list(infer.contents.index(k)))+infer2txt(infer.contents.data(k))]
+ else
+ txt=[txt;expression2code(infer.contents.index(k))+infer2txt(infer.contents.data(k))]
+ end
+ end
+ elseif tp==9 then
+ tp="Handle"
+ else
+ tp="Unknown"
+ end
+ if infer.type.property==Real then
+ prop="Real"
+ elseif infer.type.property==Complex then
+ prop="Complex"
+ else
+ prop="Unknown"
+ end
+ txt=["|"+dims+"|"+tp+"|"+prop;txt]
+endfunction
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) ???? - INRIA - Scilab
// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
-//
// Copyright (C) 2012 - 2016 - Scilab Enterprises
+// Copyright 2018 - Samuel GOUGEON
//
// This file is hereby licensed under the terms of the GNU GPL v2.0,
// pursuant to article 5.3.4 of the CeCILL v.2.1.
// END of BUG 2341 fix: function prototype with no comma between output parameters names
end
endfunction
+
+// ---------------------------------------------------------------------------
+
+function txt=replace_brackets(txt)
+
+ symbs=[",",";","=",")","]","("]
+ // This file will be use to deal with cells...
+ for k=1:size(txt,"r")
+
+ // select-case
+ if strindex(txt(k),"case")<>[] then
+ txt(k)=strsubst(strsubst(txt(k),"{","makecell("),"}",")")
+ else
+ tk=strsubst(txt(k)," ","")
+
+ ko=strindex(tk,"{")
+ if ko<>[] then
+ teq=strindex(tk,"=")
+
+ ///
+ if ko(1)==1 then
+ txt(k)=strsubst(txt(k),"{}","makecell()")
+ txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
+ elseif or(part(tk,ko(1)-1)==symbs) then
+ txt(k)=strsubst(txt(k),"{}","makecell()")
+ txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
+ else // Cell index
+ txt(k)=strsubst(strsubst(txt(k),"{","("),"}",").entries")
+ end
+
+ ////
+
+ for kk=2:size(ko,"*")
+ if or(part(tk,ko(kk)-1)==symbs) then // Cell creation
+ txt(k)=strsubst(txt(k),"{}","makecell()")
+ txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
+ else // Cell index
+ txt(k)=strsubst(strsubst(txt(k),"{","("),"}",").entries")
+ end
+ end
+ elseif ~isempty(strindex(txt(k),"}")) then
+ txt(k)=strsubst(txt(k),"}","]))")
+ end
+ end
+ end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function txt = replace_end_dollar(txt)
+
+ patterns = ["/(?:\(|\-|\+|\*|\:|\,)\s*end\s*(\)|(\-|\+|\*|\/|\:|\,).*?\))/"
+ "/(?:\{|\-|\+|\*|\:|\,)\s*end\s*(\}|(\-|\+|\*|\/|\:|\,).*?\})/"
+ ]'
+ for pattern = patterns
+ rows = grep(txt, pattern, "r");
+ for i = rows
+ t = txt(i);
+ [d, f, M] = regexp(t, pattern);
+ Mr = strsubst(M, "end", "$");
+ for j = 1:size(M,1)
+ t = strsubst(t, M(j), Mr(j));
+ end
+ txt(i) = t;
+ end
+ end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function txt = i_notation(txt)
+ // This function changes 1i ,... by 1*i,...
+
+ // M2SCI kernel functions called :
+ // - isinstring
+
+ // To succeed in this work, we successively suppress occurences which can be proved not to be complex notation
+ // Until we are 'sure' to have a complex notation
+
+ n=size(txt,"r")
+
+ I="i";J="j"
+ matches=[string(0:9)+I(ones(1,10)),".i",string(0:9)+J(ones(1,10)),".j"]
+ symbs=["+","-","*","/","\","(","["," ","^"," ",",",";","=","{"]
+ s1=["+","-","*","/","\",",",";"," ","^",".","&","|","''","]",")","}"]
+ s2=[string(0:9),"d","e","D","E","."]
+
+ for k=1:n
+ // Isolate a possible appended comment
+ st=strindex(txt(k),[";//","//"])
+ endComment = "";
+ tk = txt(k) + " "
+ if st<> [] then
+ for stk=1:size(st,"*")
+ if ~isinstring(txt(k),st(stk)) then
+ endComment = part(txt(k), st(stk):$);
+ tk = part(txt(k), 1:st(stk)-1)
+ break
+ end
+ end
+ end
+
+ // Find possible occurence of complex notation
+ kc=strindex(tk,matches)
+
+ // Kill indexes which point to non complex values (e.g. : a1item...)
+ for kk=size(kc,"*"):-1:1
+ km=kc(kk)+2
+ if find(part(tk,km)==s1)==[] then kc(kk)=[],end
+ end
+
+ kc=[0 kc]
+
+ for kk=size(kc,"*"):-1:2
+ km=kc(kk)
+ num=%T
+ // Reads numeric value leading complex variable
+ while or(part(tk,km)==s2)
+ km=km-1
+ if km<=kc(kk-1)+1 then
+ km=kc(kk-1);
+ num=%F;
+ break
+ end
+ end
+
+ tokill=%F
+ num=part(tk,km+1:kc(kk)-1)
+ ke=strindex(convstr(num),["e","d"])
+ kd=strindex(convstr(num),".")
+
+ // Searching for invalid numeric values (more than one dot...)
+ if size(ke,2)>1|size(kd,2)>1 then
+ tokill=%T
+ elseif size(ke,2)==1&size(kd,2)==1 then
+ if ke<kd then tokill=%T,end
+ end
+
+ if ~tokill then
+ // If char which follows supposed complex notation is not an operation symbol
+ if km<>kc(kk-1) then
+ if and(part(tk,km)<>symbs) then tokill=%T,end
+ end
+ end
+
+ if ~tokill then
+ km=kc(kk)
+ // If supposed complex notation is not in a string
+ if ~isinstring(tk,km) then
+ tk=part(tk,1:km)+"*%"+part(tk,km+1:length(tk))
+ end
+ end
+ end
+ txt(k) = tk + endComment
+ end
+endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) ???? - INRIA - Scilab
-// Copyright (C) 2018 - Samuel GOUGEON
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function path=mfile_path(nam)
- fil = nam+".m";
- nf = length(fil)
- path = [];
- for k=1:size(mfiles,"*")
- pk=mfiles(k);
- kk=strindex(pk,["/" "\"]);
- if kk==[]
- kk = 0
- end
- if fil==part(pk,kk($)+1:length(pk)) then
- path=pk;
- break
- end
- end
-endfunction
// For more information, see the COPYING file which you should have received
// along with this program.
-function [mtlbpath,ismtlb]=mtlbtoolfun(namefun)
+function [mtlbpath, ismtlb] = mtlbtoolfun(namefun)
+ // INTERNAL function called only by default_trad() (PRIVATE called only by funcall2sci(),
+ // only called by expression2sci())
//This function return true if the function namefun is in a matlab toolbox and determinates the access path
//Input:
//namefun : the name of the function
// For more information, see the COPYING file which you should have received
// along with this program.
-function [scitree,crp]=mtlbtree2sci(mtlbtree,prettyprintoutput)
+function [scitree, crp] = mtlbtree2sci(mtlbtree,prettyprintoutput)
// Conversion of a Matlab function tree to Scilab (and code generation)
// Input arguments:
// - mtlbtree: tree (returned by macr2tree) representing Matlab function compiled code
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function bval=multi_fun_file(fil,res_path,Recmode,only_double,verbose_mode,prettyprintoutput)
- // This function converts M-Files containing more than one functio
- // Inputs are the same as mfile2sci()
- // Outputs :
- // - bval: boolean value, %t if file contains more than on function, %f else
-
- sciparam();
-
- // File name
- k=strindex(fil,".")
- if k<>[]
- ke=k($)-1
- base_name=part(fil,1:ke)
- else
- ke=length(fil)
- base_name=fil
- end
- // File path
- k=strindex(fil,"/")
- if k==[] then
- file_path="./"
- else
- file_path=part(fil,1:k($))
- base_name=part(base_name,k($)+1:ke)
- end
-
- txt=mgetl(fil);
-
- kf=grep(txt,["function[","function "])
-
- if isempty(kf) then
- // Batch file
- bval=%f
- elseif size(kf,"*")==1 then
- // Only one function defined
- bval=%f
- else
- funcdecl=[]
- for kk=kf
- ind=strindex(txt(kk),["function[";"function "])
- if isacomment(txt(kk))==0 & ~isinstring(txt(kk),ind) & part(stripblanks(txt(kk),%T),1:8)=="function" then // function prototype
- funcdecl=[funcdecl kk]
- end
- end
-
- if isempty(funcdecl) then
- // "function" only exists in comments and strings
- bval=%f
- return
- elseif size(funcdecl,"*")==1 then
- bval=%f
- return
- end
-
- // Verify if the directory exists
- dirnam = ls(pathconvert(TMPDIR)+base_name);
-
- sep = filesep();
-
- if or(dirnam<>"") then
- rmdir(pathconvert(TMPDIR)+base_name,"s")
- end
- mkdir(pathconvert(TMPDIR),base_name)
- write(%io(2),msprintf(gettext(" -- File %s contains more than one function -- "),fil));
-
- bval= %t
-
- // First split file into as many files as function declared
- funcdecl=[funcdecl size(txt,"*")+1]
-
- tmpfiles=[]
- for k=1:size(funcdecl,"*")-1
- functxt=txt(funcdecl(k):funcdecl(k+1)-1)
- str= strindex(txt(funcdecl(k)),"(")
- if str==[] then
- funcname=stripblanks(part(txt(funcdecl(k)),strindex(txt(funcdecl(k)),["function[","function "])+8:length(txt(funcdecl(k)))))
- else
- funcname=stripblanks(part(txt(funcdecl(k)),strindex(txt(funcdecl(k)),["function[","function "])+8:str(1)-1))
- end
-
- keq=strindex(funcname,"=")
- if ~isempty(keq) then
- funcname=stripblanks(part(funcname,keq+1:length(funcname)))
- end
- tmpfiles=[tmpfiles;funcname]
- mputl(functxt,pathconvert(TMPDIR)+base_name+sep+tmpfiles($)+".m");
- end
-
- write(%io(2),msprintf(gettext(" -- Each function converted separately: %s -- "),strcat(tmpfiles," ")));
- write(%io(2),msprintf(gettext(" -- Temporary files put in: %s -- "),pathconvert(TMPDIR)));
-
- // Conversion of each file
-
- for k=1:size(tmpfiles,"*")
- txt=mgetl(pathconvert(TMPDIR)+base_name+sep+tmpfiles(k)+".m")
- //mfile2sci(pathconvert(TMPDIR)+tmpfiles(k)+".m",res_path,Recmode,only_double,verbose_mode,prettyprintoutput)
- end
-
- translatepaths(pathconvert(TMPDIR)+base_name,pathconvert(TMPDIR)+base_name)
-
- txt=[]
- if isfile(pathconvert(TMPDIR)+base_name+sep+"log") then
- txt=mgetl(pathconvert(TMPDIR)+base_name+sep+"log")
- end
- mputl(txt,res_path+"log");
- if isfile(pathconvert(TMPDIR)+base_name+sep+"resumelog") then
- txt=mgetl(pathconvert(TMPDIR)+base_name+sep+"resumelog")
- end
- mputl(txt,res_path+"resumelog");
-
- // Catenation of all .sci files to have only one output file
- txt=[]
- for k=1:size(tmpfiles,"*")
- txt=[txt ;"";mgetl(pathconvert(TMPDIR)+base_name+sep+tmpfiles(k)+".sci")]
- end
-
- // Delete useless .sci files
- //for k=1:size(tmpfiles,"*")
- //mdelete(res_path+tmpfiles(k)+".sci")
- //end
-
- mputl(txt,res_path+base_name+".sci");
-
- // Catenation of all .log files to have only one output file
- //if exists("logfile")==0 then
- //txt=[]
- //for k=1:size(tmpfiles,"*")
- //txt=[txt ; mgetl(pathconvert(TMPDIR)+base_name+sep+"m2sci_"+tmpfiles(k)+".log")]
- //end
-
- // Delete useless .log files
- //for k=1:size(tmpfiles,"*")
- //mdelete(pathconvert(TMPDIR)+base_name+sep+"m2sci_"+tmpfiles(k)+".log")
- //end
-
- //mputl(txt,res_path+"m2sci_"+base_name+".log");
- //end
-
- // Catenation of all resume.log files to have only one output file
- //if exists("resume_logfile")==0 then
- //txt=[]
- //for k=1:size(tmpfiles,"*")
- //txt=[txt ; mgetl(res_path+"m2sci_"+tmpfiles(k)+"_resume.log")]
- //end
-
- // Delete useless _resume.log files
- //for k=1:size(tmpfiles,"*")
- //mdelete(res_path+"m2sci_"+tmpfiles(k)+"_resume.log")
- //end
-
- //mputl(txt,res_path+"m2sci_"+base_name+"_resume.log");
- //end
-
- // Delete useless .m files
- //for k=1:size(tmpfiles,"*")
- //mdelete(pathconvert(TMPDIR)+tmpfiles(k)+".m")
- //end
-
- rmdir(pathconvert(TMPDIR)+base_name,"s")
- for k=1:size(tmpfiles,"*")
- mdelete(pathconvert(TMPDIR)+tmpfiles(k)+".tree")
- end
- end
-
-endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2004-2006 - INRIA - Farid BELAHCENE
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-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
-
- // 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
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) ???? - INRIA - Scilab
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function txt=replace_brackets(txt)
-
- symbs=[",",";","=",")","]","("]
- // This file will be use to deal with cells...
- for k=1:size(txt,"r")
-
- // select-case
- if strindex(txt(k),"case")<>[] then
- txt(k)=strsubst(strsubst(txt(k),"{","makecell("),"}",")")
- else
- tk=strsubst(txt(k)," ","")
-
- ko=strindex(tk,"{")
- if ko<>[] then
- teq=strindex(tk,"=")
-
- ///
- if ko(1)==1 then
- txt(k)=strsubst(txt(k),"{}","makecell()")
- txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
- elseif or(part(tk,ko(1)-1)==symbs) then
- txt(k)=strsubst(txt(k),"{}","makecell()")
- txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
- else // Cell index
- txt(k)=strsubst(strsubst(txt(k),"{","("),"}",").entries")
- end
-
- ////
-
- for kk=2:size(ko,"*")
- if or(part(tk,ko(kk)-1)==symbs) then // Cell creation
- txt(k)=strsubst(txt(k),"{}","makecell()")
- txt(k)=strsubst(strsubst(txt(k),"{","(makecell([cell(),"),"}","]))")
- else // Cell index
- txt(k)=strsubst(strsubst(txt(k),"{","("),"}",").entries")
- end
- end
- elseif ~isempty(strindex(txt(k),"}")) then
- txt(k)=strsubst(txt(k),"}","]))")
- end
- end
- end
-endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2019 - Samuel GOUGEON
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function txt = replace_end_dollar(txt)
-
- patterns = ["/(?:\(|\-|\+|\*|\:|\,)\s*end\s*(\)|(\-|\+|\*|\/|\:|\,).*?\))/"
- "/(?:\{|\-|\+|\*|\:|\,)\s*end\s*(\}|(\-|\+|\*|\/|\:|\,).*?\})/"
- ]'
- for pattern = patterns
- rows = grep(txt, pattern, "r");
- for i = rows
- t = txt(i);
- [d, f, M] = regexp(t, pattern);
- Mr = strsubst(M, "end", "$");
- for j = 1:size(M,1)
- t = strsubst(t, M(j), Mr(j));
- end
- txt(i) = t;
- end
- end
-endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function [tree]=sci_generic(tree)
- // M2SCI function
- // Generic conversion function for unknown Matlab functions
- // Input: tree = Matlab funcall tree
- // Output: tree = Scilab equivalent for tree
-
- if typeof(tree)=="operation"
- tree.out(1).dims=list(-1,-1)
- tree.out(1).type=Type(-1,-1)
- else
- for i=1:size(tree.lhs)
- tree.lhs(i).dims=list(-1,-1)
- tree.lhs(i).type=Type(-1,-1)
- end
- end
-endfunction
// For more information, see the COPYING file which you should have received
// along with this program.
-function []=sci_m2scideclare(def)
+function sci_m2scideclare(def)
+ // INTERNAL function called only by expression2sci()
+ //
// This function translate calls to m2scideclare
// which can be used by the user to influence translation
// by adding a comment which begins by m2scideclare
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2004-2006 - INRIA - Farid BELAHCENE
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function instr=transformtree(instr)
- //TRANSFORMTREE function
- //This function research and transform the equal instructions(if the lhs are a multi_operation and expression is a funcall)
- //of the matlab tree to a sup_equal instructions
- //sup_equal is a tlist : tlist([sup_equal,sup_instr,nb_op],sup_instr,nb_op)
- //i.e : the equal instruction [a(1),b(2:3)]=f() is replaced by
- //sup_equal, whith sup_intr list is composed to :
- //[%v1,%v2]=f()
- //a(1)=%v1
- //b(2:3)=%v2
- //and nb_op is: the number of insert operation (in this case 2)
- //Input
- //instr : instruction of matlab tree before tranformation
- //Output
- //instr : instruction of matlab tree after transformation
-
- Unknown=-1;
- // Browse all the instrucions of the matlab tree:
- if typeof(instr)=="ifthenelse" then
- for i=1:size(instr.then)
- instr.then(i)=transformtree((instr.then(i)))
- end
- for i=1:size(instr.elseifs)
- for k=1:size(instr.elseifs(i).then)
- instr.elseifs(i).then(k)=transformtree((instr.elseifs(i).then(k)))
- end
- end
- for i=1:size(instr.else)
- instr.else(i)=transformtree((instr.else(i)))
- end
- elseif typeof(instr)=="selectcase" then
- for i=1:size(instr.cases)
- for j=1:size(instr.cases(i).then)
- instr.cases(i).then(j)=transformtree((instr.cases(i).then(j)))
- end
- end
- for i=1:size(instr.else)
- instr.else(i)=transformtree(instr.else(i))
- end
- elseif typeof(instr)=="while" then
- for i=1:size(instr.statements)
- instr.statements(i)=transformtree(instr.statements(i))
- end
- elseif typeof(instr)=="for" then
- for i=1:size(instr.statements)
- instr.statements(i)=transformtree(instr.statements(i))
- end
- //instruction is an equal instruction
- elseif typeof(instr)=="equal" then
- if typeof(instr.expression)=="funcall" then //expression is a funcall
- nb_opr=0;
- for ind=1:size(instr.lhs)
- if typeof(instr.lhs(ind))=="operation" then
- nb_opr=nb_opr+1
- end
- end
- if nb_opr>1 then //more than one lhs insert operation
- sup_instr=list("");
- lhstemp=list();
- for j=1:size(instr.lhs)
- if typeof(instr.lhs(j))=="operation" then
- x=gettempvar();
- sup_instr($+1)=Equal(list(instr.lhs(j)),x);
- lhstemp(j)=x;
- else
- lhstemp(j)=instr.lhs(j)
- end
- end
- sup_instr(1)=Equal(lhstemp,instr.expression)
- //creation of the sup_equal
- instr=tlist(["sup_equal","sup_instr","nb_opr"],sup_instr,nb_opr)
- end
- end
- end
-endfunction
\ No newline at end of file
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) ???? - INRIA - Scilab
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function [transorder]=translateorder(transorder,funtxt,overfunname)
- // TRANSLATEORDER Recursive function
- // Determinates a translate order of the M-files found in Paths (used by "translatepaths" function)
- // Output-Input
- // -transorder : a vector containing the M-files names which are arranged in order to respect an priority order of translation
- // Input
- // -funtxt : a vector which contains the name of a M-file found in the Paths (its first component: funtxt(1)), and the called functions by this file (the others components : funtxt(2:$))
- // -overfunname : a vector which contains the files names being passed like argument of "translateorder" function
-
- // the file is already in the list
- if or(transorder==funtxt(1)) then
- return
- end
-
- if size(funtxt,"*")>1 then
- for i=2:size(funtxt,1)
- // the called function is already in the list
- if find(funtxt(i)==transorder)<>[] then
- continue
- // the called function is already passed in argument of "translateorder" function (a loop)
- elseif find(funtxt(i)==overfunname)<>[] then
- disp([overfunname;funtxt(i)],"loop: ")
- //return
- continue
- // order the under level, also we call translateorder (recursive)
- else
- overfunname($+1)=funtxt(1)
- execstr("[transorder]=translateorder(transorder,"+funtxt(i)+"vect,overfunname)")
- end
- end
- transorder($+1)=funtxt(1)
- else
- // no called function by the M-file, also we put the M-file name in the transorder vector
- transorder($+1)=funtxt(1)
- end
-
-endfunction
-
-
-
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function []=updatevarslist(instr_lhs)
- // (2 functions in this file: merge_vars() at the end)
- // Update list of M2SCI variables with converted instruction lhs
- // Input:
- // - instr_lhs: list of lhs of current instruction
- // - in_a_clause: boolean value
- // Set to 1 if instruction is in a clause
- // In this case, type and dimensions are set to unknown if differ from those already stored in varslist
- // (Default value is %F)
-
- // Global variable for M2SCI
- global("varslist")
- if isempty(varslist)
- varslist = list()
- end
- // level is declared in m2sci.sci and modified in clause2sci.sci
- level;
-
- rhs=argn(2)
- if rhs==2 then
- in_a_clause=%F
- end
-
- // Merge infered data from the last two parts of clause which are above the current part
- // if we are in the third part of clause (current), then : merge the first and second part of clause
- // when end of conversion of a clause : merge infered data from the last two parts of clause
- levelsize=size(level,1)
- changepartclause=%F
-
- for i=size(varslist):-1:1
- if size(varslist(i).level,1)==levelsize then
- varlevel=varslist(i).level
- if varlevel($)<>level($)
- changepartclause=%T
- else
- changepartclause=%F
- break
- end
- end
- end
- if changepartclause | instr_lhs=="END OF CLAUSE" then
- index=[] // Search variables from two part above current part clause
- for k=size(varslist):-1:1
- if size(varslist(k).level,1)==levelsize then
- varlevel=varslist(k).level
- if and(varlevel(1:$-1)==level(1:$-1)) & varlevel($)==level($)-2 then
- index=[index;k]
- end
- end
- end
- if index<>[] then // Found variables from the second part above current part of a clause
- for k=1:size(index,1)
- boolmerge =%F
- for i=size(varslist):-1:1 // Search variables from the first part above current part of a clause, and which have the same name than variables from the second part above current part of a clause
- varlevel=varslist(i).level
- if varslist(i).matname==varslist(index(k)).matname & and(varlevel(1:$-1)==level(1:$-1)) & varlevel($)==level($)-1 then
- boolmerge =%T // Found the same variable name from the last two parts above the current part : Merge
- merge_vars(index(k),varslist(i))
- varslist(i)=null()
- break
- end
- end
- if ~boolmerge then
- varslist(index(k)).level=[level(1:$-1);level($)-1]
- end
- end
- end
- end
-
- // Special case when end of conversion of a clause
- // Merge infered data from clause and those from level-1
- if instr_lhs=="END OF CLAUSE" then // Search variables in the last part of a clause (above end of conversion of a clause)
- index=[] //
- for k=size(varslist):-1:1 // Search variables from level-1 which have the same name than variables from the last part of current level
- varlevel=varslist(k).level
- if varlevel==[level(1:$-1);level($)-1] then
- index=[index;k]
- end
- end
- if index<>[] then
- for j=1:size(index,1)
- boolmerge=%F
- for k=size(varslist):-1:1 //
- varlevel=varslist(k).level
- if varslist(k).matname==varslist(index(j)).matname & and(varlevel==level(1:$-1)) then
- boolmerge=%T // Found variables from level-1 which have the same name than variables from the last part of current level : Merge
- index_lower_level=k
- merge_vars(index_lower_level,varslist(index(j)))
- varslist(k).level=level(1:$-1)
- varslist(index(j))=null()
- break
- end
- end
- if boolmerge==%F then
- varslist(index(j)).level=level(1:$-1)
- end
- end
- end
- return
- end
-
- // Expression: lhs name is empty => nothing to do
- if instr_lhs==list() then
- return
- end
-
- // Remove lhs which are not variables
- k=1
- while k<=size(instr_lhs)
- // Insertion operation
- if typeof(instr_lhs(k))=="operation" then
- instr_lhs(k)=null()
- else
- k=k+1
- end
- end
-
- if size(instr_lhs)==0 then
- return
- end
-
- // Update varslist
- for k=1:size(instr_lhs)
- [bval,index]=isdefinedvar(instr_lhs(k))
- ierr=execstr("zz=instr_lhs(k).contents.index","errcatch")
- if ierr<>0 then pause;end
- // Remove multiple entries from contents
- for kcont = size(instr_lhs(k).contents.index):-1:1
- [infertlist,pos]=get_contents_infer(instr_lhs(k),instr_lhs(k).contents.index(kcont))
- if pos<>0 & pos<>kcont then
- instr_lhs(k).contents.index(pos)=null()
- instr_lhs(k).contents.data(pos)=null()
- end
- end
- // If variable exists for the current level in the same part of clause then update exixting variable
- if bval
- boolupdate=%F
- for l=1:size(varslist)
- if varslist(l).matname==instr_lhs(k).name & varslist(l).level==level then
- varslist(l)=M2scivar(varslist(l).sciname,..
- varslist(l).matname,..
- Infer(instr_lhs(k).infer.dims,instr_lhs(k).infer.type,instr_lhs(k).infer.contents),..
- varslist(l).level)
- boolupdate=%T
- break
- end
- end
- // If variable exists, but not for the current level or not in the same part of clause then Update variable then create new variable
- if ~boolupdate then
- varslist($+1)=M2scivar(varslist(index).sciname,..
- varslist(index).matname,..
- instr_lhs(k).infer,..
- level)
- end
- else
- // Variable added to varslist if as a name (not done for expressions
- if execstr("f=instr_lhs(k).name","errcatch")<>0 then pause;end;errclear();
- if instr_lhs(k).name<>"ans" then
- varslist($+1)=M2scivar(instr_lhs(k).name,..
- instr_lhs(k).name,..
- instr_lhs(k).infer,..
- level)
- end
- end
- end
-endfunction
-
-function []=merge_vars(oldvarindex,newvar)
- // M2SCI function
- // Merge two variables inference properties, if different then set to Unknown
- // Input:
- // - oldvarindex: index of old variable in varslist
- // - newvar: new variable to take in account to update oldvar properties
-
- // Global variable for M2SCI
- global("varslist")
- oldvar=varslist(oldvarindex)
-
- olddims=oldvar.dims
- oldvtype=oldvar.vtype
- oldprop=oldvar.property
-
- newdims=newvar.dims
- newvtype=newvar.vtype
- newprop=newvar.property
-
- // Verify dims
- for l=1:min(size(newdims),size(olddims))
- if newdims(l)<>olddims(l) then
- newdims(l)=Unknown
- end
- end
- if size(newdims)>size(olddims) then
- for l=size(olddims):size(newdims)
- newdims(l)=null()
- end
- end
-
- // Verify vtype
- if newvtype<>oldvtype then
- newvtype=Unknown
- end
-
- // Verify property
- if newprop<>oldprop then
- newprop=Unknown
- end
-
- // Verify contents
- for k = 1:size(newvar.contents.index)
- olddata=get_contents_infer(oldvar,newvar.contents.index(k))
- newdata=newvar.contents.data(k)
-
- if or(olddata<>newdata) then
- newvar.infer.contents.data(k)=Infer()
- end
- end
-
- // Write result in varslist
- varslist(oldvarindex)=M2scivar(oldvar.sciname,..
- oldvar.matname,..
- Infer(newdims,Type(newvtype,newprop),newvar.contents),..
- newvar.level)
-endfunction
+++ /dev/null
-// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) ???? - INRIA - Scilab
-//
-// Copyright (C) 2012 - 2016 - Scilab Enterprises
-//
-// This file is hereby licensed under the terms of the GNU GPL v2.0,
-// pursuant to article 5.3.4 of the CeCILL v.2.1.
-// This file was originally licensed under the terms of the CeCILL v2.1,
-// and continues to be available under such terms.
-// For more information, see the COPYING file which you should have received
-// along with this program.
-
-function variablename=variablesearch(instr,variablename)
- // VARIABLESEARCH recursive function (used by "translatepaths" function)
- // Searches names of declared variables for each instruction of mtlbtree
- // Output
- // -variablename : a vector which contains the names of declared variables
- // -instr : mtlbtree instruction
-
- // case : ifthenelse instruction
- if typeof(instr) == "ifthenelse" then
- for i=1:size(instr.then)
- [variablename]=variablesearch((instr.then(i)),variablename)
- end
- for i=1:size(instr.elseifs)
- for k=1:size(instr.elseifs(i).then)
- [variablename]=variablesearch((instr.elseifs(i).then(k)),variablename)
- end
- end
- for i=1:size(instr.else)
- [variablename]=variablesearch((instr.else(i)),variablename)
- end
- // case : selectcase instruction
- elseif typeof(instr) == "selectcase" then
- for i=1:size(instr.cases)
- [variablename]=variablesearch(instr.cases(i).expression,variablename)
- for j=1:size(instr.cases(i).then)
- [variablename]=variablesearch((instr.cases(i).then(j)),variablename)
- end
- end
- for i=1:size(instr.else)
- [variablename]=variablesearch(instr.else(i),variablename)
- end
- // case : while instruction
- elseif typeof(instr) == "while" then
- for i=1:size(instr.statements)
- [variablename]=variablesearch(instr.statements(i),variablename)
- end
- // case : for instruction
- elseif typeof(instr) == "for" then
- [variablename]=variablesearch(instr.expression,variablename)
- for i=1:size(instr.statements)
- [variablename]=variablesearch(instr.statements(i),variablename)
- end
- // case : equal instruction
- elseif typeof(instr) == "equal" then
- for i=1:size(instr.lhs)
- [variablename]=variablesearch(instr.lhs(i),variablename)
- end
- // case : operation instruction
- elseif typeof(instr) == "operation" then
- if instr.operator=="ins" then
- if find(instr.operands(1).name==variablename)==[] then
- variablename($+1)=instr.operands(1).name
- end
- end
- // case : variable instruction
- elseif typeof(instr) == "variable" then
- if find(instr.name==variablename)==[] & instr.name<>"ans" then
- variablename($+1)=instr.name
- end
- end
-
-endfunction
tmptxt=txt
// Make minor changes on syntax
+ // ----------------------------
m2sci_info(gettext("Syntax modification..."),-1);
ierr=execstr("load(''"+pathconvert(TMPDIR)+fnam+ ".tree'',''txt'',''helppart'',''batch'')","errcatch","n")
if ierr<>0 | exists("txt")==0 | exists("batch")==0 & ..
endfunction
+// ---------------------------------------------------------------------------
+
function funcname = getMacroNameFromPrototype(proto)
- // Private utility function
+ // PRIVATE UTILITY FUNCTION called only by mfile2sci()
+ //
// Extraction of the macro's name
tmp = tokens(proto,["(" "=" ")"]);
if size(tmp,1)>1
// proto = "fun5";
// proto = "a = fun6"; // from bug_2341 use case
endfunction
+
+// ---------------------------------------------------------------------------
+
+function instr = transformtree(instr)
+ // PRIVATE UTILITY FUNCTION called only by mfile2sci()
+ //
+ // Copyright (C) 2004-2006 - INRIA - Farid BELAHCENE
+ //
+ //TRANSFORMTREE function
+ //This function research and transform the equal instructions(if the lhs are a multi_operation and expression is a funcall)
+ //of the matlab tree to a sup_equal instructions
+ //sup_equal is a tlist : tlist([sup_equal,sup_instr,nb_op],sup_instr,nb_op)
+ //i.e : the equal instruction [a(1),b(2:3)]=f() is replaced by
+ //sup_equal, whith sup_intr list is composed to :
+ //[%v1,%v2]=f()
+ //a(1)=%v1
+ //b(2:3)=%v2
+ //and nb_op is: the number of insert operation (in this case 2)
+ //Input
+ //instr : instruction of matlab tree before tranformation
+ //Output
+ //instr : instruction of matlab tree after transformation
+
+ Unknown=-1;
+ // Browse all the instrucions of the matlab tree:
+ if typeof(instr)=="ifthenelse" then
+ for i=1:size(instr.then)
+ instr.then(i)=transformtree((instr.then(i)))
+ end
+ for i=1:size(instr.elseifs)
+ for k=1:size(instr.elseifs(i).then)
+ instr.elseifs(i).then(k)=transformtree((instr.elseifs(i).then(k)))
+ end
+ end
+ for i=1:size(instr.else)
+ instr.else(i)=transformtree((instr.else(i)))
+ end
+ elseif typeof(instr)=="selectcase" then
+ for i=1:size(instr.cases)
+ for j=1:size(instr.cases(i).then)
+ instr.cases(i).then(j)=transformtree((instr.cases(i).then(j)))
+ end
+ end
+ for i=1:size(instr.else)
+ instr.else(i)=transformtree(instr.else(i))
+ end
+ elseif typeof(instr)=="while" then
+ for i=1:size(instr.statements)
+ instr.statements(i)=transformtree(instr.statements(i))
+ end
+ elseif typeof(instr)=="for" then
+ for i=1:size(instr.statements)
+ instr.statements(i)=transformtree(instr.statements(i))
+ end
+ //instruction is an equal instruction
+ elseif typeof(instr)=="equal" then
+ if typeof(instr.expression)=="funcall" then //expression is a funcall
+ nb_opr=0;
+ for ind=1:size(instr.lhs)
+ if typeof(instr.lhs(ind))=="operation" then
+ nb_opr=nb_opr+1
+ end
+ end
+ if nb_opr>1 then //more than one lhs insert operation
+ sup_instr=list("");
+ lhstemp=list();
+ for j=1:size(instr.lhs)
+ if typeof(instr.lhs(j))=="operation" then
+ x=gettempvar();
+ sup_instr($+1)=Equal(list(instr.lhs(j)),x);
+ lhstemp(j)=x;
+ else
+ lhstemp(j)=instr.lhs(j)
+ end
+ end
+ sup_instr(1)=Equal(lhstemp,instr.expression)
+ //creation of the sup_equal
+ instr=tlist(["sup_equal","sup_instr","nb_opr"],sup_instr,nb_opr)
+ end
+ end
+ end
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function bval = multi_fun_file(fil,res_path,Recmode,only_double,verbose_mode,prettyprintoutput)
+ // PRIVATE UTILITY FUNCTION called only by mfile2sci()
+ //
+ // This function converts M-Files containing more than one function
+ // Inputs are the same as mfile2sci()
+ // Outputs :
+ // - bval: boolean value, %t if file contains more than on function, %f else
+
+ sciparam();
+
+ // File name
+ k=strindex(fil,".")
+ if k<>[]
+ ke=k($)-1
+ base_name=part(fil,1:ke)
+ else
+ ke=length(fil)
+ base_name=fil
+ end
+ // File path
+ k=strindex(fil,"/")
+ if k==[] then
+ file_path="./"
+ else
+ file_path=part(fil,1:k($))
+ base_name=part(base_name,k($)+1:ke)
+ end
+
+ txt=mgetl(fil);
+
+ kf=grep(txt,["function[","function "])
+
+ if isempty(kf) then
+ // Batch file
+ bval=%f
+ elseif size(kf,"*")==1 then
+ // Only one function defined
+ bval=%f
+ else
+ funcdecl=[]
+ for kk=kf
+ ind=strindex(txt(kk),["function[";"function "])
+ if isacomment(txt(kk))==0 & ~isinstring(txt(kk),ind) & part(stripblanks(txt(kk),%T),1:8)=="function" then // function prototype
+ funcdecl=[funcdecl kk]
+ end
+ end
+
+ if isempty(funcdecl) then
+ // "function" only exists in comments and strings
+ bval=%f
+ return
+ elseif size(funcdecl,"*")==1 then
+ bval=%f
+ return
+ end
+
+ // Verify if the directory exists
+ dirnam = ls(pathconvert(TMPDIR)+base_name);
+
+ sep = filesep();
+
+ if or(dirnam<>"") then
+ rmdir(pathconvert(TMPDIR)+base_name,"s")
+ end
+ mkdir(pathconvert(TMPDIR),base_name)
+ write(%io(2),msprintf(gettext(" -- File %s contains more than one function -- "),fil));
+
+ bval= %t
+
+ // First split file into as many files as function declared
+ funcdecl=[funcdecl size(txt,"*")+1]
+
+ tmpfiles=[]
+ for k=1:size(funcdecl,"*")-1
+ functxt=txt(funcdecl(k):funcdecl(k+1)-1)
+ str= strindex(txt(funcdecl(k)),"(")
+ if str==[] then
+ funcname=stripblanks(part(txt(funcdecl(k)),strindex(txt(funcdecl(k)),["function[","function "])+8:length(txt(funcdecl(k)))))
+ else
+ funcname=stripblanks(part(txt(funcdecl(k)),strindex(txt(funcdecl(k)),["function[","function "])+8:str(1)-1))
+ end
+
+ keq=strindex(funcname,"=")
+ if ~isempty(keq) then
+ funcname=stripblanks(part(funcname,keq+1:length(funcname)))
+ end
+ tmpfiles=[tmpfiles;funcname]
+ mputl(functxt,pathconvert(TMPDIR)+base_name+sep+tmpfiles($)+".m");
+ end
+
+ write(%io(2),msprintf(gettext(" -- Each function converted separately: %s -- "),strcat(tmpfiles," ")));
+ write(%io(2),msprintf(gettext(" -- Temporary files put in: %s -- "),pathconvert(TMPDIR)));
+
+ // Conversion of each file
+
+ for k=1:size(tmpfiles,"*")
+ txt=mgetl(pathconvert(TMPDIR)+base_name+sep+tmpfiles(k)+".m")
+ //mfile2sci(pathconvert(TMPDIR)+tmpfiles(k)+".m",res_path,Recmode,only_double,verbose_mode,prettyprintoutput)
+ end
+
+ translatepaths(pathconvert(TMPDIR)+base_name,pathconvert(TMPDIR)+base_name)
+
+ txt=[]
+ if isfile(pathconvert(TMPDIR)+base_name+sep+"log") then
+ txt=mgetl(pathconvert(TMPDIR)+base_name+sep+"log")
+ end
+ mputl(txt,res_path+"log");
+ if isfile(pathconvert(TMPDIR)+base_name+sep+"resumelog") then
+ txt=mgetl(pathconvert(TMPDIR)+base_name+sep+"resumelog")
+ end
+ mputl(txt,res_path+"resumelog");
+
+ // Catenation of all .sci files to have only one output file
+ txt=[]
+ for k=1:size(tmpfiles,"*")
+ txt=[txt ;"";mgetl(pathconvert(TMPDIR)+base_name+sep+tmpfiles(k)+".sci")]
+ end
+
+ // Delete useless .sci files
+ //for k=1:size(tmpfiles,"*")
+ //mdelete(res_path+tmpfiles(k)+".sci")
+ //end
+
+ mputl(txt,res_path+base_name+".sci");
+
+ // Catenation of all .log files to have only one output file
+ //if exists("logfile")==0 then
+ //txt=[]
+ //for k=1:size(tmpfiles,"*")
+ //txt=[txt ; mgetl(pathconvert(TMPDIR)+base_name+sep+"m2sci_"+tmpfiles(k)+".log")]
+ //end
+
+ // Delete useless .log files
+ //for k=1:size(tmpfiles,"*")
+ //mdelete(pathconvert(TMPDIR)+base_name+sep+"m2sci_"+tmpfiles(k)+".log")
+ //end
+
+ //mputl(txt,res_path+"m2sci_"+base_name+".log");
+ //end
+
+ // Catenation of all resume.log files to have only one output file
+ //if exists("resume_logfile")==0 then
+ //txt=[]
+ //for k=1:size(tmpfiles,"*")
+ //txt=[txt ; mgetl(res_path+"m2sci_"+tmpfiles(k)+"_resume.log")]
+ //end
+
+ // Delete useless _resume.log files
+ //for k=1:size(tmpfiles,"*")
+ //mdelete(res_path+"m2sci_"+tmpfiles(k)+"_resume.log")
+ //end
+
+ //mputl(txt,res_path+"m2sci_"+base_name+"_resume.log");
+ //end
+
+ // Delete useless .m files
+ //for k=1:size(tmpfiles,"*")
+ //mdelete(pathconvert(TMPDIR)+tmpfiles(k)+".m")
+ //end
+
+ rmdir(pathconvert(TMPDIR)+base_name,"s")
+ for k=1:size(tmpfiles,"*")
+ mdelete(pathconvert(TMPDIR)+tmpfiles(k)+".tree")
+ end
+ end
+
+endfunction
mputl(loadertxt,loaderfile);
endfunction
+
+// ---------------------------------------------------------------------------
+
+function [transorder]=translateorder(transorder,funtxt,overfunname)
+ // PRIVATE UTILITY FUNCTION called only by translatepath() and itself (recursive)
+ //
+ // TRANSLATEORDER Recursive function
+ // Determinates a translate order of the M-files found in Paths (used by "translatepaths" function)
+ // Output-Input
+ // -transorder : a vector containing the M-files names which are arranged in order to respect an priority order of translation
+ // Input
+ // -funtxt : a vector which contains the name of a M-file found in the Paths (its first component: funtxt(1)), and the called functions by this file (the others components : funtxt(2:$))
+ // -overfunname : a vector which contains the files names being passed like argument of "translateorder" function
+
+ // the file is already in the list
+ if or(transorder==funtxt(1)) then
+ return
+ end
+
+ if size(funtxt,"*")>1 then
+ for i=2:size(funtxt,1)
+ // the called function is already in the list
+ if find(funtxt(i)==transorder)<>[] then
+ continue
+ // the called function is already passed in argument of "translateorder" function (a loop)
+ elseif find(funtxt(i)==overfunname)<>[] then
+ disp([overfunname;funtxt(i)],"loop: ")
+ //return
+ continue
+ // order the under level, also we call translateorder (recursive)
+ else
+ overfunname($+1)=funtxt(1)
+ execstr("[transorder]=translateorder(transorder,"+funtxt(i)+"vect,overfunname)")
+ end
+ end
+ transorder($+1)=funtxt(1)
+ else
+ // no called function by the M-file, also we put the M-file name in the transorder vector
+ transorder($+1)=funtxt(1)
+ end
+
+endfunction
// <-- Bugzilla URL -->
// http://bugzilla.scilab.org/16181
//
-// <-- Short Description -->
-// Unit tests for replacing the "end" index
-if ~isdef("m2scikernellib") then
- load("SCI/modules/m2sci/macros/kernel/lib")
-end
-txt = mgetl("SCI/modules/m2sci/tests/unit_tests/conversion/END_index_syntax.m");
+// Getting replace_end_dollar() as public:
+exec("SCI/modules/m2sci/macros/kernel/m2sci_syntax.sci",-1);
+txt = mgetl("SCI/modules/m2sci/tests/nonreg_tests/bug_16181.m");
printf("%s\n", replace_end_dollar(txt));
% end in comments
% a($:-1:2) in comments
// <-- Bugzilla URL -->
// http://bugzilla.scilab.org/16181
//
-// <-- Short Description -->
-// Unit tests for replacing the "end" index
-
-if ~isdef("m2scikernellib") then
- load("SCI/modules/m2sci/macros/kernel/lib")
-end
+// Getting replace_end_dollar() as public:
+exec("SCI/modules/m2sci/macros/kernel/m2sci_syntax.sci",-1);
txt = mgetl("SCI/modules/m2sci/tests/unit_tests/conversion/END_index_syntax.m");
printf("%s\n", replace_end_dollar(txt));