1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
13 function [scitree, crp] = mtlbtree2sci(mtlbtree,prettyprintoutput)
14 // Conversion of a Matlab function tree to Scilab (and code generation)
16 // - mtlbtree: tree (returned by macr2tree) representing Matlab function compiled code
17 // - prettyprintoutput: boolean flag for pretty printed output file if TRUE
19 // - scitree: Scilab equivalent for mtlbtree
20 // - crp: Scilab equivalent function code (function body)
22 // Global variables for M2SCI
23 global("m2sci_to_insert_b") // To insert before current instruction
24 global("m2sci_to_insert_a") // To insert after current instruction
25 global("tmpvarnb") // Index for temporary variables
26 m2sci_to_insert_b=list()
27 m2sci_to_insert_a=list()
30 if typeof(mtlbtree)<>"program" then
31 error(gettext("wrong type of input."))
35 scitree=tlist(["program","name","outputs","inputs","statements"],mtlbtree.name,mtlbtree.outputs,mtlbtree.inputs,list())
37 ninstr=1 // Index of Matlab tree
38 if batch then // defined in m2sci.sci
41 nblines=1 // Number of converted lines
44 m2sci_info(gettext("Conversion of M-tree..."),-1);
57 if scitree.name<>"" then // Not a batch file
58 for k=1:size(scitree.outputs)
59 lhsstr=[lhsstr,expression2code(scitree.outputs(k))]
61 if ~isempty(lhsstr) then
62 lhsstr="["+strcat(lhsstr,",")+"]"
67 for k=1:size(scitree.inputs)
68 rhsstr=[rhsstr,expression2code(scitree.inputs(k))]
70 if ~isempty(rhsstr) then
71 rhsstr="("+strcat(rhsstr,",")+")"
76 crp=lhsstr+" = "+scitree.name+rhsstr;
80 // Convert Matlab instruction tree to Scilab
81 while ninstr<=size(mtlbtree.statements)-3
82 //Case : sup_equal instruction
83 // Add converted tree to scitree and also inserted instructions
84 if typeof(mtlbtree.statements(ninstr))=="sup_equal"
87 for i=1:size(mtlbtree.statements(ninstr).sup_instr)
88 [converted_tree,nblines]=instruction2sci(mtlbtree.statements(ninstr).sup_instr(i),nblines);
90 sci_stat=update_instr_list(sci_stat,converted_tree);
93 scitree.statements($+1)=tlist(["sup_equal","sup_instr","nb_opr"],sci_stat,mtlbtree.statements(ninstr).nb_opr);
96 [converted_tree,nblines]=instruction2sci(mtlbtree.statements(ninstr),nblines);
98 // Add converted tree to scitree and also inserted instructions
100 scitree.statements=update_instr_list(scitree.statements,converted_tree);
102 // Generate code corresponding to scitree.statements
104 for k=1:size(scitree.statements)
105 if k<size(scitree.statements)
106 crp = cat_code(crp,instruction2code(scitree.statements(k),prettyprintoutput));
107 crp = format_txt(crp,scitree.statements(k),prettyprintoutput,scitree.statements(k+1));
111 scitree.statements=list(scitree.statements($));
113 // Disp percentage of conversion done
114 msprintf(gettext("%s line %s out of %s..."),margin, string(nblines), string(mtlbtree.nblines));
119 if scitree.statements(1)<>list("EOL") then
120 crp = cat_code(crp,instruction2code(scitree.statements(1),prettyprintoutput));
121 crp = format_txt(crp,scitree.statements(1),prettyprintoutput,list("EOL"));
124 if scitree.name<>"" then // Not a batch file
125 crp=cat_code(crp,"");
126 crp=cat_code(crp,"endfunction"); // Replace last return
127 crp=cat_code(crp,"");
130 m2sci_info(gettext("Conversion of M-tree: Done"),-1);
133 clearglobal("m2sci_to_insert_b")
134 clearglobal("m2sci_to_insert_a")
135 clearglobal("tmpvarnb")