Skip to content

Commit 5483f30

Browse files
authored
Merge pull request #24 from siravan/main
piecewise rewritten, math constants added to tagmap, warning solved!
2 parents 0b85c73 + 23a274a commit 5483f30

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/maps.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,12 @@ tagmap = Dict{String,Function}(
119119
"math" => x -> map(parse_node, elements(x)),
120120
"vector" => x -> map(parse_node, elements(x)),
121121
"lambda" => parse_lambda,
122+
123+
# MathML defined constants
124+
"pi" => x -> π,
125+
"exponentiale" => x -> ℯ,
126+
"notanumber" => x -> NaN,
127+
"infinity" => x -> Inf,
128+
"true" => x -> true,
129+
"false" => x -> false,
122130
)

src/parse.jl

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ where type ∈ ["e-notation", "rational", "complex-cartesian", "complex-polar"]
4646
"""
4747
function parse_cn_w_sep(node)
4848
# node = clean_attributes(node)
49-
txts = findall("text()", node)
49+
# txts = findall("text()", node)
50+
txts = [n for n in eachnode(node) if istext(n)]
5051
length(txts) != 2 && error("stop, collaborate, and listen!, problem with <cn>")
5152
x1, x2 = map(x -> Meta.parse(x.content), txts)
5253
t = node["type"]
@@ -76,32 +77,24 @@ end
7677
########## Parse piecewise ###################################################
7778

7879
function parse_piecewise(node)
79-
return process_pieces(elements(node))
80-
end
80+
ns = elements(node)
81+
pieces = filter(x -> nodename(x) == "piece", ns)
82+
others = filter(x -> nodename(x) == "otherwise", ns)
8183

82-
function process_pieces(pieces)
83-
if length(pieces) == 1
84-
return process_piece(pieces[1])
84+
if length(others) > 0
85+
otherwise = parse_node(firstelement(others[1]))
8586
else
86-
return process_piece(pieces[1], process_pieces(pieces[2:end]))
87+
otherwise = 0.0
8788
end
88-
end
8989

90-
function process_piece(node)
91-
if nodename(node) != "otherwise"
92-
@warn "expect an otherwise"
93-
else
94-
return parse_node(firstelement(node))
95-
end
90+
return process_pieces(pieces, otherwise)
9691
end
9792

98-
function process_piece(node, otherwise)
99-
if nodename(node) == "otherwise"
100-
return parse_node(firstelement(node))
101-
elseif nodename(node) == "piece"
102-
c = parse_node.(elements(node))
103-
return IfElse.ifelse(c[2] > 0.5, c[1], otherwise)
104-
end
93+
function process_pieces(pieces, otherwise)
94+
node = pieces[1]
95+
c = parse_node.(elements(node))
96+
return IfElse.ifelse(c[2] > 0.5, c[1],
97+
length(pieces)==1 ? otherwise : process_pieces(pieces[2:end], otherwise))
10598
end
10699

107100
"""

0 commit comments

Comments
 (0)