File indexing completed on 2024-05-19 15:23:13
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 Light)"/> 0006 </head><body style="background-color:#ffffff;color:#1f1c1b"><pre> 0007 <span style="color:#898887;">! This file is an example to test the syntax highlighting file fortran-free.xml</span> 0008 <span style="color:#898887;">! (for fortran, free format)</span> 0009 0010 <span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> 0011 <span style="color:#898887;">! THIS IS AN EXAMPLE OF A MODULE !</span> 0012 <span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> 0013 <span style="font-weight:bold;">module</span> module_example 0014 0015 <span style="color:#898887;">! 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:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> 0019 <span style="color:#898887;">! PUBLICS AND PRIVATES</span> 0020 0021 <span style="color:#898887;">! In fortran 90 you can define your own operator</span> 0022 <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> <span style="font-weight:bold;">operator</span>(<span style="color:#ca60ca;font-weight:bold;">.norm.</span>) 0023 <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> <span style="font-weight:bold;">operator</span>(<span style="font-weight:bold;">+</span>) <span style="color:#898887;">! <-- you can also overload the usual operators</span> 0024 <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> factorial 0025 <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> example_fn 0026 0027 <span style="color:#0057ae;">private</span> <span style="color:#0057ae;">::</span> point3d_add 0028 <span style="color:#0057ae;">private</span> <span style="color:#0057ae;">::</span> point3d_norm 0029 0030 <span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> 0031 <span style="color:#898887;">! USER-DEFINED TYPES...</span> 0032 0033 <span style="color:#898887;">! This is a definition to use in declarations of real variables,</span> 0034 <span style="color:#898887;">! parameters, etc.</span> 0035 <span style="color:#0057ae;">integer</span>, <span style="color:#0057ae;">parameter</span>, <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> kr <span style="font-weight:bold;">=</span> <span style="color:#644a9b;font-weight:bold;">selected_real_kind</span>(<span style="color:#b08000;">10</span>) 0036 0037 <span style="color:#898887;">! This is a user-defined type</span> 0038 <span style="color:#0057ae;">type</span>, <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> point3d 0039 <span style="color:#0057ae;">real(kind=kr)</span> <span style="color:#0057ae;">::</span> x, y, z 0040 <span style="color:#0057ae;">end type</span> point3d 0041 0042 <span style="color:#898887;">! This type is useless: it is only an example of type definition!</span> 0043 <span style="color:#0057ae;">type</span>, <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> example_type 0044 <span style="color:#0057ae;">complex(kind=kr)</span> <span style="color:#0057ae;">::</span> c <span style="color:#898887;">! <-- a complex number (two reals of kind kr)!</span> 0045 <span style="color:#0057ae;">real</span>, <span style="color:#0057ae;">dimension(-10:10)</span> <span style="color:#0057ae;">::</span> <span style="font-weight:bold;">&</span> <span style="color:#898887;">! <-- this line does not end here!</span> 0046 r1, r2 <span style="color:#898887;">! <-- this is the final part of the previous line</span> 0047 <span style="color:#0057ae;">real</span>, <span style="color:#0057ae;">pointer</span>, <span style="color:#0057ae;">dimension(:)</span> <span style="color:#0057ae;">::</span> pointer_to_array_of_real 0048 <span style="color:#0057ae;">real</span>, <span style="color:#0057ae;">dimension(:)</span>, <span style="color:#0057ae;">pointer</span> <span style="color:#0057ae;">::</span> array_of_pointer_to_real 0049 <span style="color:#0057ae;">end type</span> example_type 0050 0051 <span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> 0052 <span style="color:#898887;">! INTERFACES...</span> 0053 0054 <span style="color:#898887;">! 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:#ca60ca;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:#898887;">! 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:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> 0065 <span style="color:#898887;">! SOME DECLARATIONS...</span> 0066 0067 <span style="color:#898887;">! A real number can be declared with the following line:</span> 0068 <span style="color:#0057ae;">real(kind=kr)</span> <span style="color:#0057ae;">::</span> real_var1 0069 <span style="color:#898887;">! But if you are not interested on the precision of floating point numbers,</span> 0070 <span style="color:#898887;">! you can use simply:</span> 0071 <span style="color:#0057ae;">real</span> <span style="color:#0057ae;">::</span> real_var2 0072 0073 <span style="color:#898887;">! An array can be declared in two ways:</span> 0074 <span style="color:#0057ae;">real(kind=kr)</span>, <span style="color:#0057ae;">dimension(1:10, -4:5)</span>, <span style="color:#0057ae;">private</span> <span style="color:#0057ae;">::</span> a, b, c 0075 <span style="color:#0057ae;">real(kind=kr)</span>, <span style="color:#0057ae;">private</span> <span style="color:#0057ae;">::</span> d(<span style="color:#b08000;">1</span>:<span style="color:#b08000;">10</span>, <span style="font-weight:bold;">-</span><span style="color:#b08000;">4</span>:<span style="color:#b08000;">5</span>) 0076 0077 <span style="color:#898887;">! This is a string with fixed lenght</span> 0078 <span style="color:#0057ae;">character(len=10)</span> <span style="color:#0057ae;">::</span> str_var 0079 0080 <span style="color:#898887;">! This is an allocatable array, which can be a target of a pointer</span> 0081 <span style="color:#0057ae;">type(example_type)</span>, <span style="color:#0057ae;">private</span>, <span style="color:#0057ae;">dimension(:)</span>, <span style="color:#0057ae;">allocatable</span>, <span style="color:#0057ae;">target</span> <span style="color:#0057ae;">::</span> <span style="font-weight:bold;">&</span> 0082 many_examples 0083 0084 <span style="color:#898887;">! Fortran 90 hasn't got its own preprocessor, it uses the C preprocessor!</span> 0085 <span style="color:#006e28;">#ifdef XXX</span> 0086 0087 <span style="color:#006e28;">#endif</span> 0088 0089 <span style="font-weight:bold;">contains</span> 0090 0091 0092 <span style="color:#898887;">! 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:#0057ae;">type(point3d)</span> <span style="color:#0057ae;">::</span> rs 0095 <span style="color:#0057ae;">type(point3d)</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> a, b 0096 rs<span style="color:#ca60ca;font-weight:bold;">%</span>x <span style="font-weight:bold;">=</span> a<span style="color:#ca60ca;font-weight:bold;">%</span>x <span style="font-weight:bold;">+</span> b<span style="color:#ca60ca;font-weight:bold;">%</span>x 0097 rs<span style="color:#ca60ca;font-weight:bold;">%</span>y <span style="font-weight:bold;">=</span> a<span style="color:#ca60ca;font-weight:bold;">%</span>y <span style="font-weight:bold;">+</span> b<span style="color:#ca60ca;font-weight:bold;">%</span>y 0098 rs<span style="color:#ca60ca;font-weight:bold;">%</span>z <span style="font-weight:bold;">=</span> a<span style="color:#ca60ca;font-weight:bold;">%</span>z <span style="font-weight:bold;">+</span> b<span style="color:#ca60ca;font-weight:bold;">%</span>z 0099 <span style="font-weight:bold;">end function</span> point3d_add 0100 0101 0102 <span style="color:#898887;">! 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:#0057ae;">real(kind=kr)</span> <span style="color:#0057ae;">::</span> rs 0105 <span style="color:#0057ae;">type(point3d)</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> a 0106 rs <span style="font-weight:bold;">=</span> <span style="color:#644a9b;font-weight:bold;">sqrt</span>(a<span style="color:#ca60ca;font-weight:bold;">%</span>x <span style="font-weight:bold;">*</span> a<span style="color:#ca60ca;font-weight:bold;">%</span>x <span style="font-weight:bold;">+</span> a<span style="color:#ca60ca;font-weight:bold;">%</span>y <span style="font-weight:bold;">*</span> a<span style="color:#ca60ca;font-weight:bold;">%</span>y <span style="font-weight:bold;">+</span> a<span style="color:#ca60ca;font-weight:bold;">%</span>z <span style="font-weight:bold;">*</span> a<span style="color:#ca60ca;font-weight:bold;">%</span>z) 0107 <span style="font-weight:bold;">end function</span> point3d_norm 0108 0109 0110 <span style="color:#898887;">! 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:#0057ae;">integer</span> <span style="color:#0057ae;">::</span> rs 0113 <span style="color:#0057ae;">integer</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> i 0114 <span style="font-weight:bold;">if</span> ( i <span style="color:#ca60ca;font-weight:bold;"><=</span> <span style="color:#b08000;">1</span> ) <span style="font-weight:bold;">then</span> 0115 rs <span style="font-weight:bold;">=</span> <span style="color:#b08000;">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:#b08000;">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:#898887;">! 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:#0057ae;">integer</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> int_arg 0125 <span style="color:#0057ae;">real(kind=kr)</span>, <span style="color:#0057ae;">intent(out)</span> <span style="color:#0057ae;">::</span> real_arg 0126 <span style="color:#0057ae;">character(len=*)</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> str_arg 0127 0128 <span style="color:#0057ae;">type(example_type)</span>, <span style="color:#0057ae;">pointer</span> <span style="color:#0057ae;">::</span> p 0129 <span style="color:#0057ae;">integer</span> <span style="color:#0057ae;">::</span> n, i, j 0130 <span style="color:#0057ae;">logical</span> <span style="color:#0057ae;">::</span> flag 0131 0132 flag <span style="font-weight:bold;">=</span> <span style="color:#aa5500;font-weight:bold;">.true.</span> <span style="color:#898887;">! .true. is not an operator!</span> 0133 <span style="font-weight:bold;">if</span> ( flag <span style="color:#ca60ca;font-weight:bold;">.and.</span> flag ) <span style="font-weight:bold;">then</span> <span style="color:#898887;">! .and. is a pre-defined operator</span> 0134 <span style="color:#644a9b;">print</span> <span style="font-weight:bold;">*</span>, <span style="color:#bf0303;">"blabla"</span> 0135 <span style="font-weight:bold;">end if</span> 0136 0137 <span style="color:#898887;">! Examples of inquiry functions: allocated, lbound, ubound.</span> 0138 <span style="font-weight:bold;">if</span> ( <span style="color:#ca60ca;font-weight:bold;">.not.</span> <span style="color:#644a9b;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:#b08000;">10</span>) ) 0140 <span style="font-weight:bold;">end if</span> 0141 <span style="color:#644a9b;">print</span> <span style="font-weight:bold;">*</span>, <span style="color:#bf0303;">"Lower bound = "</span>, <span style="color:#644a9b;font-weight:bold;font-style:italic;">lbound</span>(many_examples, <span style="color:#b08000;">1</span>) 0142 <span style="color:#644a9b;">print</span> <span style="font-weight:bold;">*</span>, <span style="color:#bf0303;">"Upper bound = "</span>, <span style="color:#644a9b;font-weight:bold;font-style:italic;">ubound</span>(many_examples, <span style="color:#b08000;">1</span>) 0143 0144 p <span style="font-weight:bold;">=</span><span style="color:#ca60ca;font-weight:bold;">></span> many_examples(<span style="color:#b08000;">5</span>) <span style="color:#898887;">! <-- p is a pointer</span> 0145 0146 <span style="color:#898887;">! 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:#b08000;">6</span> 0148 j <span style="font-weight:bold;">=</span> <span style="color:#b08000;">0</span> 0149 <span style="font-weight:bold;">do</span> n <span style="font-weight:bold;">=</span> <span style="color:#b08000;">1</span>, i 0150 j <span style="font-weight:bold;">=</span> j <span style="font-weight:bold;">+</span> (<span style="color:#b08000;">2</span><span style="font-weight:bold;">*</span>n <span style="font-weight:bold;">-</span> <span style="color:#b08000;">1</span>) 0151 <span style="font-weight:bold;">end do</span> 0152 <span style="color:#644a9b;">print</span> <span style="font-weight:bold;">*</span>, <span style="color:#bf0303;">"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:#0057ae;">real(j)</span> <span style="color:#898887;">! <-- here the highlighting is not very good:</span> 0155 <span style="color:#898887;">! it is unable to distinguish between this and a definition like:</span> 0156 <span style="color:#898887;">! 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:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> 0164 <span style="color:#898887;">! THIS IS THE MAIN PROGRAM !</span> 0165 <span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</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:#898887;">! this is another example of use of the 'implicit' keyword</span> 0170 <span style="font-weight:bold;">implicit</span> <span style="color:#0057ae;">double precision</span> (a<span style="font-weight:bold;">-</span>h,o<span style="font-weight:bold;">-</span>z) 0171 0172 <span style="color:#0057ae;">real(kind=kr)</span> <span style="color:#0057ae;">::</span> var_out 0173 0174 <span style="color:#0057ae;">type(point3d)</span> <span style="color:#0057ae;">::</span> <span style="font-weight:bold;">&</span> 0175 a <span style="font-weight:bold;">=</span> point3d(<span style="color:#b08000;">0.0_kr</span>, <span style="color:#b08000;">1.0_kr</span>, <span style="color:#b08000;">2.0_kr</span>), <span style="font-weight:bold;">&</span> 0176 b <span style="font-weight:bold;">=</span> point3d(<span style="color:#b08000;">4.0_kr</span>, <span style="color:#b08000;">5.0_kr</span>, <span style="color:#b08000;">6.0_kr</span>) 0177 0178 <span style="color:#644a9b;">print</span> <span style="font-weight:bold;">*</span>, <span style="color:#bf0303;">"a + b = "</span>, <span style="color:#ca60ca;font-weight:bold;">.norm.</span> (a <span style="font-weight:bold;">+</span> b) 0179 <span style="color:#644a9b;">print</span> <span style="font-weight:bold;">*</span>, <span style="color:#bf0303;">"factorial of 5 = "</span>, factorial(<span style="color:#b08000;">5</span>) 0180 0181 <span style="font-weight:bold;">call</span> example_fn(<span style="color:#b08000;">1</span>, var_out, <span style="color:#bf0303;">"hello!"</span>) 0182 0183 <span style="font-weight:bold;">end program</span> example 0184 </pre></body></html>