comparison src/SbpOperators/readoperator.jl @ 831:760c11e81fd4 operator_storage_array_of_table

Introduce parse_tuple and parse_scalar and replace all external calls to parse_rational
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 12 Jan 2022 15:32:58 +0100
parents 21ab60cc0a5c
children fc2ac236dd73
comparison
equal deleted inserted replaced
830:21ab60cc0a5c 831:760c11e81fd4
2 2
3 export read_stencil_set 3 export read_stencil_set
4 export get_stencil_set 4 export get_stencil_set
5 5
6 export parse_stencil 6 export parse_stencil
7 export parse_rational 7 export parse_scalar
8 export parse_tuple
8 9
9 export sbp_operators_path 10 export sbp_operators_path
10 11
11 # The read_stencil_set and get_stencil_set functions return the freshly parsed 12 # The read_stencil_set and get_stencil_set functions return the freshly parsed
12 # toml. The generic code in these functions can't be expected to know anyhting 13 # toml. The generic code in these functions can't be expected to know anyhting
30 31
31 # TODO: Docs for readoperator.jl 32 # TODO: Docs for readoperator.jl
32 # Parsing as rationals is intentional, allows preserving exactness, which can be lowered using converts or promotions later. 33 # Parsing as rationals is intentional, allows preserving exactness, which can be lowered using converts or promotions later.
33 # TODO: readoperator.jl file name? 34 # TODO: readoperator.jl file name?
34 # TODO: Remove references to toml for dict-input arguments 35 # TODO: Remove references to toml for dict-input arguments
36 # TODO: Documetning the format: Allows representing rationals as strings
35 37
36 """ 38 """
37 read_stencil_set(fn; filters) 39 read_stencil_set(fn; filters)
38 40
39 Picks out a stencil set from the given toml file based on some filters. 41 Picks out a stencil set from the given toml file based on some filters.
107 if !(toml["c"] isa Int) 109 if !(toml["c"] isa Int)
108 throw(ArgumentError("the center of a stencil must be specified as an integer.")) 110 throw(ArgumentError("the center of a stencil must be specified as an integer."))
109 end 111 end
110 end 112 end
111 113
114
115 function parse_scalar(toml)
116 try
117 return parse_rational(toml)
118 catch e
119 throw(ArgumentError("must be a number or a string representing a number."))
120 end
121 end
122
123 function parse_tuple(toml)
124 if !(toml isa Array)
125 throw(ArgumentError("argument must be an array"))
126 end
127 return Tuple(parse_scalar.(toml))
128 end
129
112 function parse_rational(toml) 130 function parse_rational(toml)
113 if toml isa String 131 if toml isa String
114 expr = Meta.parse(replace(toml, "/"=>"//")) 132 expr = Meta.parse(replace(toml, "/"=>"//"))
115 return eval(:(Rational($expr))) 133 return eval(:(Rational($expr)))
116 else 134 else