// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab // Copyright (C) INRIA - vincent COUVERT // // This file must be used under the terms of the CeCILL. // This source file is licensed as described in the file COPYING, which // you should have received as part of this distribution. The terms // are also available at // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt function C=instruction2code(I, bprettyprintformat) // Translate an instruction tlist to Scilab code (called by tree2code) // Input: // - I: instruction 'tree' // - bprettyprintformat: boolean value, if FALSE (default value), generated code is not formated else it is // Output: // - C: Scilab code corresponding to I // Default value rhs=argn(2) if rhs==1 then bprettyprintformat=%F end C=[] // ----------- // Empty lines // ----------- if I==list("EOL") then C="" return end // --------------------------------------------- // Generate code corresponding to a TRY-CATCH // --------------------------------------------- if typeof(I)=="trycatch" then //TRYCATCH C="try " [C,indent_space] = format_txt(C,I.trystat(1),bprettyprintformat); // Add EOL after while if needed and returns indent_space for k=1:size(I.trystat) C=cat_code(C,indent_space+instruction2code(I.trystat(k))) if k0 then for k=1:size(I.elseifs) C=cat_code(C,"elseif "+expression2code(I.elseifs(k).expression)+" then") [C,indent_space] = format_txt(C,I.elseifs(k).then(1),bprettyprintformat); // Add EOL after then if needed and returns indent_space for l=1:size(I.elseifs(k).then) C=cat_code(C,indent_space+instruction2code(I.elseifs(k).then(l))) if l0 then C=cat_code(C,"else") [C,indent_space] = format_txt(C,I.else(1),bprettyprintformat); // Add EOL after else if needed and returns indent_space for k=1:size(I.else) C=cat_code(C,indent_space+instruction2code(I.else(k))) if k0 then for k=1:size(I.cases) C=cat_code(C," case "+expression2code(I.cases(k).expression)+" then") [C,indent_space] = format_txt(C,I.cases(k).then(1),bprettyprintformat); // Add EOL after then if needed and returns indent_space if indent_space==" " then // indent_space is modified because indentation differs from others control instructions indent_space=" " end for l=1:size(I.cases(k).then) C=cat_code(C,indent_space+instruction2code(I.cases(k).then(l))) if l0 then C=cat_code(C," else") [C,indent_space] = format_txt(C,I.else(1),bprettyprintformat); // Add EOL after else if needed and returns indent_space if indent_space==" " then // indent_space is modified because indentation differs from others control instructions indent_space=" " end for k=1:size(I.else) C=cat_code(C,indent_space+instruction2code(I.else(k))) if k"" then C="["+strcat(lhsnames,",")+"] = "+rhs2code(I.expression) else C=rhs2code(I.expression) end end C($)=C($)+I.endsymbol //C($)=C($)+";"; return end // -------------------------------------- // Generate code corresponding to a comment // -------------------------------------- if typeof(I)=="comment" then C="//"+I.text //C = cat_code(C,"//"+I.text) return end // --------------------------------------- // Generate code corresponding to sup_equal // --------------------------------------- if typeof(I)=="sup_equal" then while typeof(I.sup_instr(1))=="equal" | I.sup_instr(1)==list("EOL") if I.sup_instr(1)==list("EOL") then //Instruction is an EOL I.sup_instr(1)=null() elseif typeof(I.sup_instr(1))=="equal" then //Instruction is acomment if typeof(I.sup_instr(1).expression)=="funcall" then break end end end //Optimize the code if all sup_intr are equal tlists and expression of this equal tlists are temporaries variables (not a function) if size(I.sup_instr)==I.nb_opr+1 then for i=size(I.sup_instr):-1:2 optim_instr=%f if typeof(I.sup_instr(i))=="equal" then if typeof(I.sup_instr(i).expression)=="variable" then j=0 while ~optim_instr & j<=size(I.sup_instr(1).lhs) j=j+1 optim_instr=I.sup_instr(i).expression.name==I.sup_instr(1).lhs(j).name end end end if optim_instr then I.sup_instr(1).lhs(j)=I.sup_instr(i).lhs(1) I.sup_instr(i)=null() end end end for i=1:size(I.sup_instr) C($+1)=instruction2code(I.sup_instr(i)) end return end // ---------------------------------------------------- // Generate code corresponding to a function definition // ---------------------------------------------------- if typeof(I)=="inline" then C = "function "+I.prototype; C = cat_code(C,I.definition) C($+1) = "endfunction"; return end // ------- // Command // ------- if and(typeof(I)<>["funcall" "variable", "comment"]) then disp("instruction2code: bug in macr2tree() !"); pause end C=expression2code(I); C($)=C($)+";" endfunction