File indexing completed on 2024-05-12 04:02:07

0001 <!DOCTYPE html>
0002 <html><head>
0003 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
0004 <title>highlight.f90</title>
0005 <meta name="generator" content="KF5::SyntaxHighlighting - Definition (Fortran (Free Format)) - Theme (Breeze Dark)"/>
0006 </head><body style="background-color:#232629;color:#cfcfc2"><pre>
0007 <span style="color:#7a7c7d">! This file is an example to test the syntax highlighting file fortran-free.xml</span>
0008 <span style="color:#7a7c7d">! (for fortran, free format)</span>
0009 
0010 <span style="color:#7a7c7d">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span>
0011 <span style="color:#7a7c7d">!                      THIS IS AN EXAMPLE OF A MODULE                          !</span>
0012 <span style="color:#7a7c7d">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span>
0013 <span style="font-weight:bold">module</span> module_example
0014 
0015   <span style="color:#7a7c7d">! use 'implicit none' when you want all variables to be declared</span>
0016   <span style="font-weight:bold">implicit</span> <span style="font-weight:bold">none</span>
0017 
0018 <span style="color:#7a7c7d">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span>
0019 <span style="color:#7a7c7d">! PUBLICS AND PRIVATES</span>
0020 
0021   <span style="color:#7a7c7d">! In fortran 90 you can define your own operator</span>
0022   <span style="color:#2980b9">public</span> <span style="color:#2980b9">::</span> <span style="font-weight:bold">operator</span>(<span style="color:#3f8058;font-weight:bold">.norm.</span>)
0023   <span style="color:#2980b9">public</span> <span style="color:#2980b9">::</span> <span style="font-weight:bold">operator</span>(<span style="font-weight:bold">+</span>) <span style="color:#7a7c7d">! &lt;-- you can also overload the usual operators</span>
0024   <span style="color:#2980b9">public</span> <span style="color:#2980b9">::</span> factorial
0025   <span style="color:#2980b9">public</span> <span style="color:#2980b9">::</span> example_fn
0026 
0027   <span style="color:#2980b9">private</span> <span style="color:#2980b9">::</span> point3d_add
0028   <span style="color:#2980b9">private</span> <span style="color:#2980b9">::</span> point3d_norm
0029 
0030 <span style="color:#7a7c7d">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span>
0031 <span style="color:#7a7c7d">! USER-DEFINED TYPES...</span>
0032 
0033   <span style="color:#7a7c7d">! This is a definition to use in declarations of real variables,</span>
0034   <span style="color:#7a7c7d">! parameters, etc.</span>
0035   <span style="color:#2980b9">integer</span>, <span style="color:#2980b9">parameter</span>, <span style="color:#2980b9">public</span> <span style="color:#2980b9">::</span> kr <span style="font-weight:bold">=</span> <span style="color:#8e44ad;font-weight:bold">selected_real_kind</span>(<span style="color:#f67400">10</span>)
0036 
0037   <span style="color:#7a7c7d">! This is a user-defined type</span>
0038   <span style="color:#2980b9">type</span>, <span style="color:#2980b9">public</span> <span style="color:#2980b9">::</span> point3d
0039     <span style="color:#2980b9">real(kind=kr)</span> <span style="color:#2980b9">::</span> x, y, z
0040   <span style="color:#2980b9">end type</span> point3d
0041 
0042   <span style="color:#7a7c7d">! This type is useless: it is only an example of type definition!</span>
0043   <span style="color:#2980b9">type</span>, <span style="color:#2980b9">public</span> <span style="color:#2980b9">::</span> example_type
0044     <span style="color:#2980b9">complex(kind=kr)</span>            <span style="color:#2980b9">::</span> c <span style="color:#7a7c7d">! &lt;-- a complex number (two reals of kind kr)!</span>
0045     <span style="color:#2980b9">real</span>, <span style="color:#2980b9">dimension(-10:10)</span>     <span style="color:#2980b9">::</span> <span style="font-weight:bold">&amp;</span> <span style="color:#7a7c7d">! &lt;-- this line does not end here!</span>
0046       r1, r2 <span style="color:#7a7c7d">! &lt;-- this is the final part of the previous line</span>
0047     <span style="color:#2980b9">real</span>, <span style="color:#2980b9">pointer</span>, <span style="color:#2980b9">dimension(:)</span> <span style="color:#2980b9">::</span> pointer_to_array_of_real
0048     <span style="color:#2980b9">real</span>, <span style="color:#2980b9">dimension(:)</span>, <span style="color:#2980b9">pointer</span> <span style="color:#2980b9">::</span> array_of_pointer_to_real
0049   <span style="color:#2980b9">end type</span> example_type
0050 
0051 <span style="color:#7a7c7d">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span>
0052 <span style="color:#7a7c7d">! INTERFACES...</span>
0053 
0054   <span style="color:#7a7c7d">! Interface for the norm of a 3-D vector</span>
0055   <span style="font-weight:bold">interface</span> <span style="font-weight:bold">operator</span>(<span style="color:#3f8058;font-weight:bold">.norm.</span>)
0056     <span style="font-weight:bold">module procedure</span> point3d_norm
0057   <span style="font-weight:bold">end interface</span>
0058 
0059   <span style="color:#7a7c7d">! Interface for the operator '+'</span>
0060   <span style="font-weight:bold">interface</span> <span style="font-weight:bold">operator</span>(<span style="font-weight:bold">+</span>)
0061     <span style="font-weight:bold">module procedure</span> point3d_add
0062   <span style="font-weight:bold">end interface</span>
0063 
0064 <span style="color:#7a7c7d">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span>
0065 <span style="color:#7a7c7d">! SOME DECLARATIONS...</span>
0066 
0067   <span style="color:#7a7c7d">! A real number can be declared with the following line:</span>
0068   <span style="color:#2980b9">real(kind=kr)</span> <span style="color:#2980b9">::</span> real_var1
0069   <span style="color:#7a7c7d">! But if you are not interested on the precision of floating point numbers,</span>
0070   <span style="color:#7a7c7d">! you can use simply:</span>
0071   <span style="color:#2980b9">real</span> <span style="color:#2980b9">::</span> real_var2
0072 
0073   <span style="color:#7a7c7d">! An array can be declared in two ways:</span>
0074   <span style="color:#2980b9">real(kind=kr)</span>, <span style="color:#2980b9">dimension(1:10, -4:5)</span>, <span style="color:#2980b9">private</span> <span style="color:#2980b9">::</span> a, b, c
0075   <span style="color:#2980b9">real(kind=kr)</span>, <span style="color:#2980b9">private</span> <span style="color:#2980b9">::</span> d(<span style="color:#f67400">1</span>:<span style="color:#f67400">10</span>, <span style="font-weight:bold">-</span><span style="color:#f67400">4</span>:<span style="color:#f67400">5</span>)
0076 
0077   <span style="color:#7a7c7d">! This is a string with fixed lenght</span>
0078   <span style="color:#2980b9">character(len=10)</span> <span style="color:#2980b9">::</span> str_var
0079 
0080   <span style="color:#7a7c7d">! This is an allocatable array, which can be a target of a pointer</span>
0081   <span style="color:#2980b9">type(example_type)</span>, <span style="color:#2980b9">private</span>, <span style="color:#2980b9">dimension(:)</span>, <span style="color:#2980b9">allocatable</span>, <span style="color:#2980b9">target</span> <span style="color:#2980b9">::</span> <span style="font-weight:bold">&amp;</span>
0082    many_examples
0083 
0084 <span style="color:#7a7c7d">! Fortran 90 hasn't got its own preprocessor, it uses the C preprocessor!</span>
0085 <span style="color:#27ae60">#ifdef XXX</span>
0086 
0087 <span style="color:#27ae60">#endif</span>
0088 
0089 <span style="font-weight:bold">contains</span>
0090 
0091 
0092   <span style="color:#7a7c7d">! The sum of two points</span>
0093   <span style="font-weight:bold">pure</span> <span style="font-weight:bold">function</span> point3d_add(a, b) <span style="font-weight:bold">result</span>(rs)
0094     <span style="color:#2980b9">type(point3d)</span> <span style="color:#2980b9">::</span> rs
0095     <span style="color:#2980b9">type(point3d)</span>, <span style="color:#2980b9">intent(in)</span> <span style="color:#2980b9">::</span> a, b
0096     rs<span style="color:#3f8058;font-weight:bold">%</span>x <span style="font-weight:bold">=</span> a<span style="color:#3f8058;font-weight:bold">%</span>x <span style="font-weight:bold">+</span> b<span style="color:#3f8058;font-weight:bold">%</span>x
0097     rs<span style="color:#3f8058;font-weight:bold">%</span>y <span style="font-weight:bold">=</span> a<span style="color:#3f8058;font-weight:bold">%</span>y <span style="font-weight:bold">+</span> b<span style="color:#3f8058;font-weight:bold">%</span>y
0098     rs<span style="color:#3f8058;font-weight:bold">%</span>z <span style="font-weight:bold">=</span> a<span style="color:#3f8058;font-weight:bold">%</span>z <span style="font-weight:bold">+</span> b<span style="color:#3f8058;font-weight:bold">%</span>z
0099   <span style="font-weight:bold">end function</span> point3d_add
0100 
0101 
0102   <span style="color:#7a7c7d">! The norm of a point</span>
0103   <span style="font-weight:bold">pure</span> <span style="font-weight:bold">function</span> point3d_norm(a) <span style="font-weight:bold">result</span>(rs)
0104     <span style="color:#2980b9">real(kind=kr)</span> <span style="color:#2980b9">::</span> rs
0105     <span style="color:#2980b9">type(point3d)</span>, <span style="color:#2980b9">intent(in)</span> <span style="color:#2980b9">::</span> a
0106     rs <span style="font-weight:bold">=</span> <span style="color:#7f8c8d;font-weight:bold">sqrt</span>(a<span style="color:#3f8058;font-weight:bold">%</span>x <span style="font-weight:bold">*</span> a<span style="color:#3f8058;font-weight:bold">%</span>x <span style="font-weight:bold">+</span> a<span style="color:#3f8058;font-weight:bold">%</span>y <span style="font-weight:bold">*</span> a<span style="color:#3f8058;font-weight:bold">%</span>y <span style="font-weight:bold">+</span> a<span style="color:#3f8058;font-weight:bold">%</span>z <span style="font-weight:bold">*</span> a<span style="color:#3f8058;font-weight:bold">%</span>z)
0107   <span style="font-weight:bold">end function</span> point3d_norm
0108 
0109 
0110   <span style="color:#7a7c7d">! A simple recursive function</span>
0111   <span style="font-weight:bold">recursive</span> <span style="font-weight:bold">function</span> factorial(i) <span style="font-weight:bold">result</span> (rs)
0112     <span style="color:#2980b9">integer</span> <span style="color:#2980b9">::</span> rs
0113     <span style="color:#2980b9">integer</span>, <span style="color:#2980b9">intent(in)</span> <span style="color:#2980b9">::</span> i
0114     <span style="font-weight:bold">if</span> ( i <span style="color:#3f8058;font-weight:bold">&lt;=</span> <span style="color:#f67400">1</span> ) <span style="font-weight:bold">then</span>
0115       rs <span style="font-weight:bold">=</span> <span style="color:#f67400">1</span>
0116     <span style="font-weight:bold">else</span>
0117       rs <span style="font-weight:bold">=</span> i <span style="font-weight:bold">*</span> factorial(i <span style="font-weight:bold">-</span> <span style="color:#f67400">1</span>)
0118     <span style="font-weight:bold">end if</span>
0119   <span style="font-weight:bold">end function</span> factorial
0120 
0121 
0122   <span style="color:#7a7c7d">! This is a useless function</span>
0123   <span style="font-weight:bold">subroutine</span> example_fn(int_arg, real_arg, str_arg)
0124     <span style="color:#2980b9">integer</span>, <span style="color:#2980b9">intent(in)</span> <span style="color:#2980b9">::</span> int_arg
0125     <span style="color:#2980b9">real(kind=kr)</span>, <span style="color:#2980b9">intent(out)</span> <span style="color:#2980b9">::</span> real_arg
0126     <span style="color:#2980b9">character(len=*)</span>, <span style="color:#2980b9">intent(in)</span> <span style="color:#2980b9">::</span> str_arg
0127 
0128     <span style="color:#2980b9">type(example_type)</span>, <span style="color:#2980b9">pointer</span> <span style="color:#2980b9">::</span> p
0129     <span style="color:#2980b9">integer</span> <span style="color:#2980b9">::</span> n, i, j
0130     <span style="color:#2980b9">logical</span> <span style="color:#2980b9">::</span> flag
0131 
0132     flag <span style="font-weight:bold">=</span> <span style="color:#27aeae;font-weight:bold">.true.</span> <span style="color:#7a7c7d">! .true. is not an operator!</span>
0133     <span style="font-weight:bold">if</span> ( flag <span style="color:#3f8058;font-weight:bold">.and.</span> flag ) <span style="font-weight:bold">then</span> <span style="color:#7a7c7d">! .and. is a pre-defined operator</span>
0134       <span style="color:#8e44ad">print</span> <span style="font-weight:bold">*</span>, <span style="color:#f44f4f">"blabla"</span>
0135     <span style="font-weight:bold">end if</span>
0136 
0137     <span style="color:#7a7c7d">! Examples of inquiry functions: allocated, lbound, ubound.</span>
0138     <span style="font-weight:bold">if</span> ( <span style="color:#3f8058;font-weight:bold">.not.</span> <span style="color:#8e44ad;font-weight:bold;font-style:italic">allocated</span>(many_examples) ) <span style="font-weight:bold">then</span>
0139       <span style="font-weight:bold">allocate</span>( many_examples(<span style="color:#f67400">10</span>) )
0140     <span style="font-weight:bold">end if</span>
0141     <span style="color:#8e44ad">print</span> <span style="font-weight:bold">*</span>, <span style="color:#f44f4f">"Lower bound = "</span>, <span style="color:#8e44ad;font-weight:bold;font-style:italic">lbound</span>(many_examples, <span style="color:#f67400">1</span>)
0142     <span style="color:#8e44ad">print</span> <span style="font-weight:bold">*</span>, <span style="color:#f44f4f">"Upper bound = "</span>, <span style="color:#8e44ad;font-weight:bold;font-style:italic">ubound</span>(many_examples, <span style="color:#f67400">1</span>)
0143 
0144     p <span style="font-weight:bold">=</span><span style="color:#3f8058;font-weight:bold">></span> many_examples(<span style="color:#f67400">5</span>) <span style="color:#7a7c7d">! &lt;-- p is a pointer</span>
0145 
0146     <span style="color:#7a7c7d">! A strange way to calculate i*i: add the first i odd numbers</span>
0147     i <span style="font-weight:bold">=</span> <span style="color:#f67400">6</span>
0148     j <span style="font-weight:bold">=</span> <span style="color:#f67400">0</span>
0149     <span style="font-weight:bold">do</span> n <span style="font-weight:bold">=</span> <span style="color:#f67400">1</span>, i
0150       j <span style="font-weight:bold">=</span> j <span style="font-weight:bold">+</span> (<span style="color:#f67400">2</span><span style="font-weight:bold">*</span>n <span style="font-weight:bold">-</span> <span style="color:#f67400">1</span>)
0151     <span style="font-weight:bold">end do</span>
0152     <span style="color:#8e44ad">print</span> <span style="font-weight:bold">*</span>, <span style="color:#f44f4f">"i*i = "</span>, i<span style="font-weight:bold">*</span>i, j
0153 
0154     real_arg <span style="font-weight:bold">=</span> <span style="color:#2980b9">real(j)</span> <span style="color:#7a7c7d">! &lt;-- here the highlighting is not very good:</span>
0155     <span style="color:#7a7c7d">! it is unable to distinguish between this and a definition like:</span>
0156     <span style="color:#7a7c7d">!  real(kind=kr) :: a</span>
0157     <span style="font-weight:bold">deallocate</span>( many_examples )
0158   <span style="font-weight:bold">end subroutine</span> example_fn
0159 
0160 <span style="font-weight:bold">end module</span> module_example
0161 
0162 
0163 <span style="color:#7a7c7d">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span>
0164 <span style="color:#7a7c7d">!                         THIS IS THE MAIN PROGRAM                             !</span>
0165 <span style="color:#7a7c7d">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span>
0166 <span style="font-weight:bold">program</span> example
0167   <span style="font-weight:bold">use</span> module_example
0168 
0169   <span style="color:#7a7c7d">! this is another example of use of the 'implicit' keyword</span>
0170   <span style="font-weight:bold">implicit</span> <span style="color:#2980b9">double precision</span> (a<span style="font-weight:bold">-</span>h,o<span style="font-weight:bold">-</span>z)
0171 
0172   <span style="color:#2980b9">real(kind=kr)</span> <span style="color:#2980b9">::</span> var_out
0173 
0174   <span style="color:#2980b9">type(point3d)</span> <span style="color:#2980b9">::</span> <span style="font-weight:bold">&amp;</span>
0175    a <span style="font-weight:bold">=</span> point3d(<span style="color:#f67400">0.0_kr</span>, <span style="color:#f67400">1.0_kr</span>, <span style="color:#f67400">2.0_kr</span>), <span style="font-weight:bold">&amp;</span>
0176    b <span style="font-weight:bold">=</span> point3d(<span style="color:#f67400">4.0_kr</span>, <span style="color:#f67400">5.0_kr</span>, <span style="color:#f67400">6.0_kr</span>)
0177 
0178   <span style="color:#8e44ad">print</span> <span style="font-weight:bold">*</span>, <span style="color:#f44f4f">"a + b = "</span>, <span style="color:#3f8058;font-weight:bold">.norm.</span> (a <span style="font-weight:bold">+</span> b)
0179   <span style="color:#8e44ad">print</span> <span style="font-weight:bold">*</span>, <span style="color:#f44f4f">"factorial of 5 = "</span>, factorial(<span style="color:#f67400">5</span>)
0180 
0181   <span style="font-weight:bold">call</span> example_fn(<span style="color:#f67400">1</span>, var_out, <span style="color:#f44f4f">"hello!"</span>)
0182 
0183 <span style="font-weight:bold">end program</span> example
0184 </pre></body></html>