-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
449 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
add_subdirectory (Trivial) | ||
add_subdirectory (Simple) | ||
add_subdirectory (Iterators) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
add_executable (iterator.x iterator.F90) | ||
target_link_libraries (iterator.x yafyaml gftl-shared) | ||
add_dependencies (examples iterator.x) | ||
|
||
FILE (COPY iterator.yaml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
! This example does minimal error checking. The point is to | ||
! demonstrate how to use iterators. In the absence of exceptions, | ||
! error checking becomes verbose and obscures the main features. | ||
|
||
program main | ||
use yafyaml | ||
use gFTL_IntegerVector | ||
use gftl_StringIntegerMap | ||
implicit none | ||
|
||
type(Parser) :: p | ||
type(Configuration) :: config | ||
type(Configuration) :: subcfg | ||
type(Configuration) :: shape_cfg | ||
|
||
type(ConfigurationIterator) :: iter, iter2 | ||
|
||
integer :: status | ||
character(:), pointer :: key | ||
integer :: prime | ||
character(:), pointer :: shape | ||
integer :: n_edges | ||
|
||
|
||
p = Parser('core') | ||
config = p%load(FileStream('iterator.yaml')) | ||
|
||
! Outer loop over mapping | ||
iter = config%begin() | ||
do while (iter /= config%end()) | ||
! Access mapping values with key()/value() methods on iterator. | ||
key => iter%key() | ||
subcfg = iter%value() | ||
|
||
select case (key) | ||
case ('primes') | ||
|
||
! loop over primes (a yaml sequence) | ||
iter2 = subcfg%begin() | ||
do while (iter2 /= subcfg%end()) | ||
! Access sequence values with get() method on iterator | ||
prime = iter2%get() ! cast as integer | ||
print*,'another prime: ', prime | ||
call iter2%next() | ||
end do | ||
|
||
case ('shapes') | ||
|
||
! loop over shapes (a yaml mapping) | ||
iter2 = subcfg%begin() | ||
do while (iter2 /= subcfg%end()) | ||
shape => iter2%key() | ||
shape_cfg = iter2%value() | ||
n_edges = shape_cfg%at('num_edges') | ||
print*,'shape: ',shape,' has ',n_edges, 'sides' | ||
call iter2%next() | ||
end do | ||
|
||
end select | ||
|
||
|
||
call iter%next() | ||
end do | ||
|
||
end program main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
primes: | ||
- 2 | ||
- 3 | ||
- 5 | ||
- 7 | ||
shapes: | ||
triangle: | ||
num_edges: 3 | ||
edge_length: 3.12 | ||
square: | ||
num_edges: 4 | ||
edge_length: 6.8 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
add_executable (simple.x simple.F90) | ||
target_link_libraries (simple.x yafyaml gftl-shared) | ||
add_dependencies (examples simple.x) | ||
|
||
FILE (COPY simple.yaml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
program main | ||
use yafyaml | ||
use gFTL_IntegerVector | ||
use gftl_StringIntegerMap | ||
implicit none | ||
|
||
type(Parser) :: p | ||
type(Configuration) :: config | ||
type(Configuration) :: subconfig | ||
|
||
real :: x | ||
character(:), allocatable :: name | ||
logical :: flag | ||
|
||
integer, allocatable :: sequence_a(:) | ||
integer, allocatable :: sequence_b(:) | ||
|
||
integer :: v1, v2, v3 | ||
|
||
logical :: is_present | ||
integer :: status | ||
|
||
|
||
p = Parser('core') | ||
config = p%load(FileStream('simple.yaml')) | ||
|
||
x = config%at('x') | ||
|
||
if (x == 1.234) then | ||
print*,'success' | ||
else | ||
print*,'failure; expected 1.234 but found ', x | ||
end if | ||
|
||
|
||
flag = .false. | ||
flag = config%at('flag') | ||
|
||
if (flag) then | ||
print*,'success' | ||
else | ||
print*,'failure; expected .true.' | ||
end if | ||
|
||
sequence_a = config%at('sequence_a') | ||
|
||
if (all (sequence_a == [1,2,3,4])) then | ||
print*,'success' | ||
else | ||
print*,'failure in handling flow sequence; expected .true.' | ||
end if | ||
|
||
sequence_b = config%at('sequence_b') | ||
|
||
if (all (sequence_b == [1,2,3,4])) then | ||
print*,'success' | ||
else | ||
print*,'failure in handling block sequence; expected .true.' | ||
end if | ||
|
||
! Flow mapping | ||
v1 = config%at('mapping_a', 'v1') | ||
v2 = config%at('mapping_a', 'v2') | ||
|
||
if (v1 == 7 .and. v2 == 8) then | ||
print*,'success' | ||
else | ||
print*,'failure in handling flow mapping', v1, v2 | ||
end if | ||
|
||
! Block mapping | ||
v1 = config%at('mapping_b', 'v1') | ||
v2 = config%at('mapping_b', 'v2') | ||
|
||
if (v1 == 7 .and. v2 == 8) then | ||
print*,'success' | ||
else | ||
print*,'failure in handling flow mapping', v1, v2 | ||
end if | ||
|
||
call config%get(subconfig, 'mapping_b') | ||
|
||
v1 = -1 | ||
call config%get(v1, 'mapping_b', 'v1', is_present=is_present, rc=status) | ||
if (v1 == 7 .and. is_present .and. status == SUCCESS) then | ||
print*,'success' | ||
else | ||
print*,'failure in handling flow mapping', v1, v2 | ||
end if | ||
|
||
! Handle missing values | ||
call config%get(v3, 'mapping_b', 'v3', default=17, is_present=is_present, rc=status) | ||
if (v3 == 17 .and. (.not. is_present) .and. status == SUCCESS) then | ||
print*,'success' | ||
else | ||
print*,'failure in handling flow mapping' | ||
end if | ||
|
||
! error if wrong type: | ||
call config%get(flag, 'mapping_b', 'v2', is_present=is_present, rc=status) | ||
if (is_present .and. status == INCONSISTENT_TYPE) then | ||
print*,'expected failure' | ||
else | ||
print*,'should have failed, but did not' | ||
end if | ||
|
||
end program main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
# exercising the basics and non-nested sequences/mappings | ||
x: 1.234 | ||
name: Fred | ||
flag: True | ||
|
||
# flow sequence | ||
sequence_a: [ 1 , 2 , 3 , 4 ] | ||
|
||
# block sequence | ||
sequence_b: | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
|
||
# block mapping | ||
mapping_b: | ||
v1: 7 | ||
v2: 8 | ||
|
||
# flow mapping | ||
mapping_a: { v1: 7 , v2: 8 } | ||
|
||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
add_executable (trivial.x trivial.F90) | ||
target_link_libraries (trivial.x yafyaml) | ||
add_dependencies (examples trivial.x) | ||
|
||
FILE (COPY trivial.yaml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
program main | ||
use yafyaml | ||
|
||
type(Parser) :: p | ||
type(Configuration) :: config | ||
integer :: prime | ||
|
||
|
||
p = Parser('core') | ||
config = p%load(FileStream('trivial.yaml')) | ||
|
||
prime = config%at('prime') | ||
|
||
if (prime == 17) then | ||
print*,'success' | ||
else | ||
print*,'failure; expected 17 but found ', prime | ||
end if | ||
end program main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
prime: 17 | ||
... |
Oops, something went wrong.