forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.f
375 lines (257 loc) · 7.69 KB
/
main.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
!mostly focused on differences from C
!my goal in this is the minimal knowledge for scientific computation
!i also interested in c interfacing
!#sources
!good stanford intro: <http://www.stanford.edu/class/me200c/tutorial_77/>
!#function
!there are two types: functions and **subr_incroutines**
integer function func_incr( i )
integer i
!func args are passed by value
i = 1
!there can be a return value:
func_incr = i + 1
return
end
subroutine subr_incr( i, r, n, is )
!subr_incroutine args are passed by reference
!declaration:
integer i, n, is(n)
real r
!real,optional :: iopt
!execution
i = i + 1
r = r + 1.0
is(1) = is(1) + 1
!iopt = 0.0
!if( present( iopt ) ) iopt = 1.0
call assert( size(is) == n )
!subr_incroutine cannot return a value:
!return 1
return
end
subroutine assert( b )
logical b
if ( .not. b) then
write(*,*) 'ASSERT FAILED'
stop 1
endif
end
!must always have a program:
program somename
!#declarations
!#data types
logical b, b1, b2
integer i, i1, i2
!for file io
character(256) :: arg
integer u
!looks like a linux file handle
!you need to declare the ype of functions again:
integer func_incr
!integer i = 1
!ERROR
!cannot init on declare
!must use param instead
!integer I
!ERROR
!vars are case insensitive
real r, r1, r2, r3
real(8) d, d2
!double precision:
double precision d3, d4
character c, c1, c2
!constants:
integer parami
real paramr
parameter ( parami = 3, paramr = 1.0 )
!#array
integer is2(2)
integer is10(10)
integer isrm55(-5:5)
!is Range Minus 5 to 5
!BAD
!confusing!!
integer is11
dimension is11(11)
!BAD
!bad golfing
!hard to see is3 is an array
!#multidimensional
integer is2x3(2,3)
integer is2x2(2,2)
character cs3(3)
!#whitespace
!- multiple whitespace is ignored
!- all whitespace is treated equally
!- indentation is not forced
!- first five characters of each line are reserved for labels
!only then can you write statements
!- non-comment lines have a maxium length!
!^^^^
!!!!!
!#comments
!!: bang anywhere or first char
!c: first char
!#execution
!compile error
!can only declare before first execution statement:
i = 2
!real r2
!#constants
b = .TRUE.
b = .true.
b = .FALSE.
i = 1
r = 2.0
r = 2.0e1
!bad: r is a single precision variable
r = 2.0d1
!compile error:
!r = 2.
!needs `2.0`
!d is for explcit double precision
d1 = 2.0
d3 = 2.0d0
c = 'a'
c = "a"
!c = "\n"
c = "'"
c = ''''
c = """"
!compile error:
!parami = 2
!#array
is10(1) = 1
is10(10) = 1
!is10(0) = 1
!compile time error
!starts at 1!!
!is10(11) = 1
!compile time error
!TODO confirm: at compile time because no dynamic memory
isrm55(-5) = 1
isrm55(5) = 1
!#multidimensional
is2x3(1,3) = 1
!physical memory column major:
is2x2(1,1) = 1
is2x2(1,2) = 2
is2x2(2,1) = 3
is2x2(2,2) = 4
write(*,*) is2x2
!outputs 1 3 2 4
!#string
!cs3 = "01"
!size function
!f90
call assert( size(is2) == 2 )
!#logic
b = .true. .AND. .false.
b = .true. .and. .false.
b = .true. .or. .false.
b = .not. .true.
!#arithmetic
!#arithmetic
i = 2**3
!exponentiation
!#compare
call assert( 1 < 2 )
call assert( 1 .lt. 2 )
call assert( 1 == 1 )
call assert( 1 .eq. 1 )
call assert( 1 /= 2 )
call assert( 1 .ne. 2 )
!#branch
!#if
!#single line
if ( .true. ) r = 1.0
!#multiline
if ( .true. ) then
r = 1.0
elseif ( .true. ) then
r = 2.0
else
r = 3.0
endif
!#label
!labels must be integers
! on five first chars of line
!must be part of some statement
!10
!20
!convention only: cosecutive multiples of ten
!11
!#for
do 30 i = 1, 5
write(*,*) 'i =', i
30 continue
!10 is any unique label
do 40 i = 1, 5, 2
write(*,*) 'i =', i
40 continue
!#function call
!#function
!call assert( func_incr(1) == 2 )
i = 1
i2 = func_incr( i )
call assert( i == 1 )
call assert( i2 == 2 )
!#subroutine
i = 1
r = 1.0
is10(1) = 1
call subr_incr( i, r, 10, is10 )
call assert( i == 2 )
call assert( r == 2.0 )
call assert( is10(1) == 2 )
!compile error:
!call subr_incr( i, i )
!type checked
!#dynamic memory
!not present?!
!#stdlib
!unlike c, in fortran you don't need to include files,
!you just try to link to them directly
!therefore, the compiler won't be able to tell if the
!function call signatures are ok.
!#intrinsics
!buil-in functions
!#math
is2(1) = -1
is2(2) = -2
call assert( abs(-1) == 1 )
call assert( abs(-1.0) == 1.0 )
write (*,*) abs(is2)
!min
!max
!sqrt
!sin
!cos
!tan
!atan
!exp
!log
call get_command_argument(0, arg)
write(*,*) arg
!#IO
!#stdout
is2(1) = 1
is2(2) = 2
write(*,*) 'stdout', 1, 1.0, .true., is2
!#stdin
!read (*,*) r
!#file IO
!TODO
is2(1) = 1
is2(2) = 2
!open( u, file='tmp.tmp' )
!write( u, * ) 'file', 1, 1.0, .true., is2
!close( u )
!#stop
!ends the program
!0 exit status:
stop
!other exit status:
!stop 1
end