Given a syntactically valid, but otherwise arbitrary, Julia expression, such as
3 - 4 > 1 & 2 + 2 == 4 | 10 - 5 > 2
or
2 + 9 - 8 * 8 ^ 7 / 2 == 8 + 8 / 1 ^ 2
...is there a convenient way to fully parenthesize the expression in a way consistent with Julia's standard parsing of it?
One approach that won't go far enough:
julia> parse("3 - 4 > 1 & 2+2 == 4 | 10 - 5 > 2")
:(3 - 4 > 1 & 2 + 2 == (4 | 10) - 5 > 2)
julia> parse("2 + 9 - 8 * 8 ^ 7 / 2 == 8 + 8 / 1 ^ 2")
:((2 + 9) - (8 * 8^7) / 2 == 8 + 8 / 1^2)
For example, for the last case, by "full parenthesized" I mean:
:(((2 + 9) - ((8 * (8 ^ 7)) / 2)) == (8 + (8 / (1 ^ 2))))
Is there something else?
You need some code to traverse the quoted expression recursively.
I have made an example here which works for infix operations like
+, -
and will fail if you use function calls like thisf(a)
Each of the Expressions has 3 fields,
head
,typ
, andargs
, but onlyhead
andargs
are usesful astyp
is mostlyAny
most of the time. You can see this using