* Bugs 16567 16586 fixed: mfile2sci() supports block %{..%}. Appended comments improved"
[scilab.git] / scilab / modules / m2sci / macros / kernel / i_notation.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) ???? - INRIA - Scilab
3 // Copyright 2018 - Samuel GOUGEON
4 //
5 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 //
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
13
14 function txt = i_notation(txt)
15     // This function changes 1i ,... by 1*i,...
16
17     // M2SCI kernel functions called :
18     //  - isinstring
19
20     // To succeed in this work, we successively suppress occurences which can be proved not to be complex notation
21     // Until we are 'sure' to have a complex notation
22
23     n=size(txt,"r")
24
25     I="i";J="j"
26     matches=[string(0:9)+I(ones(1,10)),".i",string(0:9)+J(ones(1,10)),".j"]
27     symbs=["+","-","*","/","\","(","["," ","^"," ",",",";","=","{"]
28     s1=["+","-","*","/","\",",",";"," ","^",".","&","|","''","]",")","}"]
29     s2=[string(0:9),"d","e","D","E","."]
30
31     for k=1:n
32         // Isolate a possible appended comment
33         st=strindex(txt(k),[";//","//"])
34         endComment = "";
35         tk = txt(k) + " "
36         if st<> [] then
37             for stk=1:size(st,"*")
38                 if ~isinstring(txt(k),st(stk)) then
39                     endComment = part(txt(k), st(stk):$);
40                     tk = part(txt(k), 1:st(stk)-1)
41                     break
42                 end
43             end
44         end
45
46         // Find possible occurence of complex notation
47         kc=strindex(tk,matches)
48
49         // Kill indexes which point to non complex values (e.g. : a1item...)
50         for kk=size(kc,"*"):-1:1
51             km=kc(kk)+2
52             if find(part(tk,km)==s1)==[] then kc(kk)=[],end
53         end
54
55         kc=[0 kc]
56
57         for kk=size(kc,"*"):-1:2
58             km=kc(kk)
59             num=%T
60             // Reads numeric value leading complex variable
61             while or(part(tk,km)==s2)
62                 km=km-1
63                 if km<=kc(kk-1)+1 then
64                     km=kc(kk-1);
65                     num=%F;
66                     break
67                 end
68             end
69
70             tokill=%F
71             num=part(tk,km+1:kc(kk)-1)
72             ke=strindex(convstr(num),["e","d"])
73             kd=strindex(convstr(num),".")
74
75             // Searching for invalid numeric values (more than one dot...)
76             if size(ke,2)>1|size(kd,2)>1 then
77                 tokill=%T
78             elseif size(ke,2)==1&size(kd,2)==1 then
79                 if ke<kd then tokill=%T,end
80             end
81
82             if ~tokill then
83                 // If char which follows supposed complex notation is not an operation symbol
84                 if km<>kc(kk-1) then
85                     if and(part(tk,km)<>symbs) then tokill=%T,end
86                 end
87             end
88
89             if ~tokill then
90                 km=kc(kk)
91                 // If supposed complex notation is not in a string
92                 if ~isinstring(tk,km) then
93                     tk=part(tk,1:km)+"*%"+part(tk,km+1:length(tk))
94                 end
95             end
96         end
97         txt(k) = tk + endComment
98     end
99 endfunction