Skip to content

Commit 7a9e2ed

Browse files
committed
repro: reduce to one file
1 parent 5482147 commit 7a9e2ed

File tree

7 files changed

+166
-167
lines changed

7 files changed

+166
-167
lines changed

fpm.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

reproducer.f90

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
module julienne_string_m
2+
implicit none
3+
4+
type string_t
5+
character(len=:), allocatable :: string_
6+
contains
7+
procedure :: get_json_key
8+
generic :: operator(==) => string_t_eq_character
9+
generic :: get_json_value => get_integer
10+
procedure get_integer
11+
procedure string_t_eq_character
12+
end type
13+
14+
contains
15+
16+
elemental function get_json_key(self) result(unquoted_key)
17+
class(string_t), intent(in) :: self
18+
type(string_t) unquoted_key
19+
character(len=:), allocatable :: raw_line
20+
21+
raw_line = self%string_
22+
associate(opening_key_quotes => index(raw_line, '"'))
23+
associate(closing_key_quotes => opening_key_quotes + index(raw_line(opening_key_quotes+1:), '"'))
24+
unquoted_key = string_t(trim(raw_line(opening_key_quotes+1:closing_key_quotes-1)))
25+
end associate
26+
end associate
27+
end function
28+
29+
pure function get_integer(self, key, mold) result(value_)
30+
class(string_t), intent(in) :: self, key
31+
integer, intent(in) :: mold
32+
integer value_
33+
character(len=:), allocatable :: raw_line, string_value
34+
35+
raw_line = self%string_
36+
associate(text_after_colon => raw_line(index(raw_line, ':')+1:))
37+
associate(trailing_comma => index(text_after_colon, ','))
38+
if (trailing_comma == 0) then
39+
string_value = trim(adjustl((text_after_colon)))
40+
else
41+
string_value = trim(adjustl((text_after_colon(:trailing_comma-1))))
42+
end if
43+
read(string_value, fmt=*) value_
44+
end associate
45+
end associate
46+
end function
47+
48+
elemental function string_t_eq_character(lhs, rhs) result(lhs_eq_rhs)
49+
class(string_t), intent(in) :: lhs
50+
character(len=*), intent(in) :: rhs
51+
logical lhs_eq_rhs
52+
lhs_eq_rhs = lhs%string_ == rhs
53+
end function
54+
55+
end module
56+
module julienne_file_m
57+
use julienne_string_m, only : string_t
58+
59+
type file_t
60+
type(string_t), allocatable :: lines_(:)
61+
end type
62+
63+
interface file_t
64+
module procedure from_lines
65+
end interface
66+
67+
contains
68+
69+
type(file_t) pure function from_lines(lines)
70+
type(string_t), intent(in) :: lines(:)
71+
allocate(from_lines%lines_, source=lines) ! switching this to an assignment (from_lines%lines_ = lines) prevents the seg fault
72+
end function
73+
74+
end module
75+
module hyperparameters_m
76+
use julienne_string_m, only : string_t
77+
implicit none
78+
79+
type hyperparameters_t(k)
80+
integer, kind :: k = kind(1.)
81+
integer :: mini_batches_ = 10
82+
contains
83+
procedure to_json
84+
end type
85+
86+
interface hyperparameters_t
87+
module procedure default_real_from_json
88+
end interface
89+
90+
character(len=*), parameter :: mini_batches_key = "mini-batches"
91+
92+
contains
93+
94+
pure function default_real_from_json(lines) result(hyperparameters)
95+
type(string_t), intent(in) :: lines(:)
96+
type(hyperparameters_t) hyperparameters
97+
integer l
98+
99+
do l=1,size(lines)
100+
if (lines(l)%get_json_key() == "hyperparameters") then
101+
hyperparameters%mini_batches_ = lines(l+1)%get_json_value(string_t(mini_batches_key), mold=0)
102+
return
103+
end if
104+
end do
105+
end function
106+
107+
pure function to_json(self) result(lines)
108+
class(hyperparameters_t), intent(in) :: self
109+
type(string_t), allocatable :: lines(:)
110+
integer, parameter :: max_width= 18
111+
character(len=max_width) mini_batches_string
112+
113+
write(mini_batches_string,*) self%mini_batches_
114+
lines = [string_t('"hyperparameters": {"' // mini_batches_key // '" : ' // trim(adjustl(mini_batches_string)) // '}')]
115+
end function
116+
117+
end module
118+
module training_configuration_m
119+
use julienne_file_m, only : file_t
120+
use julienne_string_m, only : string_t
121+
use hyperparameters_m, only : hyperparameters_t
122+
implicit none
123+
124+
type, extends(file_t) :: training_configuration_t(m)
125+
integer, kind :: m = kind(1.)
126+
type(hyperparameters_t(m)) hyperparameters_
127+
end type
128+
129+
interface training_configuration_t
130+
module procedure default_real_from_components
131+
module procedure default_real_from_file
132+
end interface
133+
134+
contains
135+
136+
pure function default_real_from_components(hyperparameters) result(training_configuration)
137+
type(hyperparameters_t), intent(in) :: hyperparameters
138+
type(training_configuration_t) training_configuration
139+
training_configuration%hyperparameters_ = hyperparameters
140+
training_configuration%file_t = file_t([ string_t("{"), training_configuration%hyperparameters_%to_json(), string_t("}") ])
141+
end function
142+
143+
function default_real_from_file(file_object) result(training_configuration)
144+
type(file_t), intent(in) :: file_object
145+
type(training_configuration_t) training_configuration
146+
training_configuration%file_t = file_object
147+
training_configuration%hyperparameters_ = hyperparameters_t(training_configuration%file_t%lines_)
148+
end function
149+
150+
end module
151+
program test_suite_driver
152+
use training_configuration_m, only : training_configuration_t
153+
use hyperparameters_m, only : hyperparameters_t
154+
use julienne_file_m, only : file_t
155+
implicit none
156+
157+
type(training_configuration_t) training_configuration
158+
159+
training_configuration = training_configuration_t(hyperparameters_t())
160+
161+
block
162+
type(training_configuration_t) from_json
163+
from_json = training_configuration_t(file_t(training_configuration%file_t%lines_))
164+
end block
165+
166+
end program

src/hyperparameters_m.f90

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/julienne_file_m.f90

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/julienne_string_m.F90

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/training_configuration_m.f90

Lines changed: 0 additions & 33 deletions
This file was deleted.

test/driver.f90

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)