| 1 |
<!DOCTYPE html> |
| 2 |
<html><head> |
| 3 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
| 4 |
<title>highlight.f90</title> |
| 5 |
<meta name="generator" content="KF5::SyntaxHighlighting - Definition (Fortran (Free Format)) - Theme (Breeze Light)"/> |
| 6 |
</head><body style="background-color:#ffffff;color:#1f1c1b"><pre> |
| 7 |
<span style="color:#898887;">! This file is an example to test the syntax highlighting file fortran-free.xml</span> |
| 8 |
<span style="color:#898887;">! (for fortran, free format)</span> |
| 9 |
|
| 10 |
<span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> |
| 11 |
<span style="color:#898887;">! THIS IS AN EXAMPLE OF A MODULE !</span> |
| 12 |
<span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> |
| 13 |
<span style="font-weight:bold;">module</span> module_example |
| 14 |
|
| 15 |
<span style="color:#898887;">! use 'implicit none' when you want all variables to be declared</span> |
| 16 |
<span style="font-weight:bold;">implicit</span> <span style="font-weight:bold;">none</span> |
| 17 |
|
| 18 |
<span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> |
| 19 |
<span style="color:#898887;">! PUBLICS AND PRIVATES</span> |
| 20 |
|
| 21 |
<span style="color:#898887;">! In fortran 90 you can define your own operator</span> |
| 22 |
<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>) |
| 23 |
<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> |
| 24 |
<span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> factorial |
| 25 |
<span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> example_fn |
| 26 |
|
| 27 |
<span style="color:#0057ae;">private</span> <span style="color:#0057ae;">::</span> point3d_add |
| 28 |
<span style="color:#0057ae;">private</span> <span style="color:#0057ae;">::</span> point3d_norm |
| 29 |
|
| 30 |
<span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> |
| 31 |
<span style="color:#898887;">! USER-DEFINED TYPES...</span> |
| 32 |
|
| 33 |
<span style="color:#898887;">! This is a definition to use in declarations of real variables,</span> |
| 34 |
<span style="color:#898887;">! parameters, etc.</span> |
| 35 |
<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>) |
| 36 |
|
| 37 |
<span style="color:#898887;">! This is a user-defined type</span> |
| 38 |
<span style="color:#0057ae;">type</span>, <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> point3d |
| 39 |
<span style="color:#0057ae;">real(kind=kr)</span> <span style="color:#0057ae;">::</span> x, y, z |
| 40 |
<span style="color:#0057ae;">end type</span> point3d |
| 41 |
|
| 42 |
<span style="color:#898887;">! This type is useless: it is only an example of type definition!</span> |
| 43 |
<span style="color:#0057ae;">type</span>, <span style="color:#0057ae;">public</span> <span style="color:#0057ae;">::</span> example_type |
| 44 |
<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> |
| 45 |
<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> |
| 46 |
r1, r2 <span style="color:#898887;">! <-- this is the final part of the previous line</span> |
| 47 |
<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 |
| 48 |
<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 |
| 49 |
<span style="color:#0057ae;">end type</span> example_type |
| 50 |
|
| 51 |
<span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> |
| 52 |
<span style="color:#898887;">! INTERFACES...</span> |
| 53 |
|
| 54 |
<span style="color:#898887;">! Interface for the norm of a 3-D vector</span> |
| 55 |
<span style="font-weight:bold;">interface</span> <span style="font-weight:bold;">operator</span>(<span style="color:#ca60ca;font-weight:bold;">.norm.</span>) |
| 56 |
<span style="font-weight:bold;">module procedure</span> point3d_norm |
| 57 |
<span style="font-weight:bold;">end interface</span> |
| 58 |
|
| 59 |
<span style="color:#898887;">! Interface for the operator '+'</span> |
| 60 |
<span style="font-weight:bold;">interface</span> <span style="font-weight:bold;">operator</span>(<span style="font-weight:bold;">+</span>) |
| 61 |
<span style="font-weight:bold;">module procedure</span> point3d_add |
| 62 |
<span style="font-weight:bold;">end interface</span> |
| 63 |
|
| 64 |
<span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> |
| 65 |
<span style="color:#898887;">! SOME DECLARATIONS...</span> |
| 66 |
|
| 67 |
<span style="color:#898887;">! A real number can be declared with the following line:</span> |
| 68 |
<span style="color:#0057ae;">real(kind=kr)</span> <span style="color:#0057ae;">::</span> real_var1 |
| 69 |
<span style="color:#898887;">! But if you are not interested on the precision of floating point numbers,</span> |
| 70 |
<span style="color:#898887;">! you can use simply:</span> |
| 71 |
<span style="color:#0057ae;">real</span> <span style="color:#0057ae;">::</span> real_var2 |
| 72 |
|
| 73 |
<span style="color:#898887;">! An array can be declared in two ways:</span> |
| 74 |
<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 |
| 75 |
<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>) |
| 76 |
|
| 77 |
<span style="color:#898887;">! This is a string with fixed lenght</span> |
| 78 |
<span style="color:#0057ae;">character(len=10)</span> <span style="color:#0057ae;">::</span> str_var |
| 79 |
|
| 80 |
<span style="color:#898887;">! This is an allocatable array, which can be a target of a pointer</span> |
| 81 |
<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> |
| 82 |
many_examples |
| 83 |
|
| 84 |
<span style="color:#898887;">! Fortran 90 hasn't got its own preprocessor, it uses the C preprocessor!</span> |
| 85 |
<span style="color:#006e28;">#ifdef XXX</span> |
| 86 |
|
| 87 |
<span style="color:#006e28;">#endif</span> |
| 88 |
|
| 89 |
<span style="font-weight:bold;">contains</span> |
| 90 |
|
| 91 |
|
| 92 |
<span style="color:#898887;">! The sum of two points</span> |
| 93 |
<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) |
| 94 |
<span style="color:#0057ae;">type(point3d)</span> <span style="color:#0057ae;">::</span> rs |
| 95 |
<span style="color:#0057ae;">type(point3d)</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> a, b |
| 96 |
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 |
| 97 |
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 |
| 98 |
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 |
| 99 |
<span style="font-weight:bold;">end function</span> point3d_add |
| 100 |
|
| 101 |
|
| 102 |
<span style="color:#898887;">! The norm of a point</span> |
| 103 |
<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) |
| 104 |
<span style="color:#0057ae;">real(kind=kr)</span> <span style="color:#0057ae;">::</span> rs |
| 105 |
<span style="color:#0057ae;">type(point3d)</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> a |
| 106 |
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) |
| 107 |
<span style="font-weight:bold;">end function</span> point3d_norm |
| 108 |
|
| 109 |
|
| 110 |
<span style="color:#898887;">! A simple recursive function</span> |
| 111 |
<span style="font-weight:bold;">recursive</span> <span style="font-weight:bold;">function</span> factorial(i) <span style="font-weight:bold;">result</span> (rs) |
| 112 |
<span style="color:#0057ae;">integer</span> <span style="color:#0057ae;">::</span> rs |
| 113 |
<span style="color:#0057ae;">integer</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> i |
| 114 |
<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> |
| 115 |
rs <span style="font-weight:bold;">=</span> <span style="color:#b08000;">1</span> |
| 116 |
<span style="font-weight:bold;">else</span> |
| 117 |
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>) |
| 118 |
<span style="font-weight:bold;">end if</span> |
| 119 |
<span style="font-weight:bold;">end function</span> factorial |
| 120 |
|
| 121 |
|
| 122 |
<span style="color:#898887;">! This is a useless function</span> |
| 123 |
<span style="font-weight:bold;">subroutine</span> example_fn(int_arg, real_arg, str_arg) |
| 124 |
<span style="color:#0057ae;">integer</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> int_arg |
| 125 |
<span style="color:#0057ae;">real(kind=kr)</span>, <span style="color:#0057ae;">intent(out)</span> <span style="color:#0057ae;">::</span> real_arg |
| 126 |
<span style="color:#0057ae;">character(len=*)</span>, <span style="color:#0057ae;">intent(in)</span> <span style="color:#0057ae;">::</span> str_arg |
| 127 |
|
| 128 |
<span style="color:#0057ae;">type(example_type)</span>, <span style="color:#0057ae;">pointer</span> <span style="color:#0057ae;">::</span> p |
| 129 |
<span style="color:#0057ae;">integer</span> <span style="color:#0057ae;">::</span> n, i, j |
| 130 |
<span style="color:#0057ae;">logical</span> <span style="color:#0057ae;">::</span> flag |
| 131 |
|
| 132 |
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> |
| 133 |
<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> |
| 134 |
<span style="color:#644a9b;">print</span> <span style="font-weight:bold;">*</span>, <span style="color:#bf0303;">"blabla"</span> |
| 135 |
<span style="font-weight:bold;">end if</span> |
| 136 |
|
| 137 |
<span style="color:#898887;">! Examples of inquiry functions: allocated, lbound, ubound.</span> |
| 138 |
<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> |
| 139 |
<span style="font-weight:bold;">allocate</span>( many_examples(<span style="color:#b08000;">10</span>) ) |
| 140 |
<span style="font-weight:bold;">end if</span> |
| 141 |
<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>) |
| 142 |
<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>) |
| 143 |
|
| 144 |
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> |
| 145 |
|
| 146 |
<span style="color:#898887;">! A strange way to calculate i*i: add the first i odd numbers</span> |
| 147 |
i <span style="font-weight:bold;">=</span> <span style="color:#b08000;">6</span> |
| 148 |
j <span style="font-weight:bold;">=</span> <span style="color:#b08000;">0</span> |
| 149 |
<span style="font-weight:bold;">do</span> n <span style="font-weight:bold;">=</span> <span style="color:#b08000;">1</span>, i |
| 150 |
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>) |
| 151 |
<span style="font-weight:bold;">end do</span> |
| 152 |
<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 |
| 153 |
|
| 154 |
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> |
| 155 |
<span style="color:#898887;">! it is unable to distinguish between this and a definition like:</span> |
| 156 |
<span style="color:#898887;">! real(kind=kr) :: a</span> |
| 157 |
<span style="font-weight:bold;">deallocate</span>( many_examples ) |
| 158 |
<span style="font-weight:bold;">end subroutine</span> example_fn |
| 159 |
|
| 160 |
<span style="font-weight:bold;">end module</span> module_example |
| 161 |
|
| 162 |
|
| 163 |
<span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> |
| 164 |
<span style="color:#898887;">! THIS IS THE MAIN PROGRAM !</span> |
| 165 |
<span style="color:#898887;">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</span> |
| 166 |
<span style="font-weight:bold;">program</span> example |
| 167 |
<span style="font-weight:bold;">use</span> module_example |
| 168 |
|
| 169 |
<span style="color:#898887;">! this is another example of use of the 'implicit' keyword</span> |
| 170 |
<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) |
| 171 |
|
| 172 |
<span style="color:#0057ae;">real(kind=kr)</span> <span style="color:#0057ae;">::</span> var_out |
| 173 |
|
| 174 |
<span style="color:#0057ae;">type(point3d)</span> <span style="color:#0057ae;">::</span> <span style="font-weight:bold;">&</span> |
| 175 |
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> |
| 176 |
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>) |
| 177 |
|
| 178 |
<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) |
| 179 |
<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>) |
| 180 |
|
| 181 |
<span style="font-weight:bold;">call</span> example_fn(<span style="color:#b08000;">1</span>, var_out, <span style="color:#bf0303;">"hello!"</span>) |
| 182 |
|
| 183 |
<span style="font-weight:bold;">end program</span> example |
| 184 |
</pre></body></html> |