Skip to content

Commit

Permalink
syntaxstring messy but working example
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-milella committed Jul 22, 2023
1 parent b525ba3 commit 0d8e79b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 39 deletions.
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ ThreadSafeDicts = "4239201d-c60e-5e0a-9702-85d713665ba7"
[compat]
DataStructures = "0.18"
Dictionaries = "0.3"
PrettyTables = "2.2"
Graphs = "1.8"
IterTools = "1"
Lazy = "0.15"
PrettyTables = "2.2"
Reexport = "1"
Revise = "3"
SoleBase = "0.11"
StatsBase = "0.34"
ThreadSafeDicts = "0.1.0"
julia = "1"
Expand Down
70 changes: 33 additions & 37 deletions src/general.jl
Original file line number Diff line number Diff line change
Expand Up @@ -651,72 +651,68 @@ function syntaxstring(
parentheses_at_propositions = !remove_redundant_parentheses,
kwargs...
)
lpar = "("
rpar = ")"

ch_kwargs = merge((; kwargs...), (;
function_notation = function_notation,
remove_redundant_parentheses = remove_redundant_parentheses,
parentheses_at_propositions = parentheses_at_propositions,
))

function _canavoid_newscope(t::SyntaxTree; fnotation::Bool=false)
# Avoid printing the subtree rooted in `t` if this does not generate ambiguities.

# For example, consider parsetree("a ∧ b ∧ c → d ∧ e ∧ f → g ∧ h ∧ i");
# naive output: (a ∧ (b ∧ c)) → ((d ∧ (e ∧ f)) → (g ∧ (h ∧ i)))
# improved output: (a ∧ b ∧ c) → ((d ∧ e ∧ f) → (g ∧ h ∧ i))

# Consider parsetree("¬p∧q→(¬s∧¬z)"; function_notation = true);
# naive output: "→(∧(¬(p), q), ∧(¬(s), ¬(z)))"
# improved output: "→(∧(¬p, q), ∧(¬s, ¬z))"
function _infix_syntaxstring(t::SyntaxTree, ch::SyntaxTree; relation::Symbol=:left, kwargs...)
tok = token(t)
chtok = token(ch)
lpar, rpar = "", ""

tarity = arity(token(t))
if (fnotation == true && tarity > 1)
# In this case, I don't want parentheses just in ¬(p) and (p) cases
return false
if !remove_redundant_parentheses
lpar, rpar = "(", ")"
end

for c in children(t)
carity = arity(token(c))
if carity <= tarity
# Suspiciously, this condition is enough!
return true
if arity(chtok) == 0
if parentheses_at_propositions
return "($(syntaxstring(ch; kwargs...)))"
else
return "$(lpar)$(syntaxstring(ch; kwargs...))$(rpar)"
end
end

return false
# Conditions are explicited, at the moment, to make them more comprehensible
if !(
(iscommutative(tok) && tok == chtok) ||
(Base.operator_precedence(tok) == Base.operator_precedence(chtok) && relation == :left)
)
lpar, rpar = "(", ")"
end

return "$(lpar)$(syntaxstring(ch; kwargs...))$(rpar)"
end

tok = token(t)
if arity(tok) == 0
syntaxstring(tok; ch_kwargs...)
elseif arity(tok) == 2 && !function_notation

if (remove_redundant_parentheses &&
_canavoid_newscope(t; fnotation=function_notation))
lpar, rpar = "", ""
if parentheses_at_propositions
return "($(syntaxstring(tok; ch_kwargs...)))"
else
return "$(syntaxstring(tok; ch_kwargs...))"
end
elseif arity(tok) == 2 && !function_notation
# Previous idea
# f = ch->arity(token(ch)) == 0 ?
# "$(syntaxstring(ch; ch_kwargs...))" :
# "$(lpar)$(syntaxstring(ch; ch_kwargs...))$(rpar)"
# "$(f(children(t)[1])) $(syntaxstring(tok; ch_kwargs...)) $(f(children(t)[2]))"

f = ch->arity(token(ch)) == 0 ?
"$(syntaxstring(ch; ch_kwargs...))" :
"$(lpar)$(syntaxstring(ch; ch_kwargs...))$(rpar)"
# Infix notation for binary operator
"$(f(children(t)[1])) $(syntaxstring(tok; ch_kwargs...)) $(f(children(t)[2]))"
"$(_infix_syntaxstring(t, children(t)[1]; relation=:left, ch_kwargs...)) $(syntaxstring(tok; ch_kwargs...)) $(_infix_syntaxstring(t, children(t)[2]; relation=:right, ch_kwargs...))"
else
if (remove_redundant_parentheses &&
_canavoid_newscope(t; fnotation=function_notation))
lpar, rpar = "(", ")"
if (remove_redundant_parentheses && arity(tok) != 2)
lpar, rpar = "", ""
end

# Function notation for higher arity operator
length(children(t)) == 0 ?
syntaxstring(tok; ch_kwargs...) :
syntaxstring(tok; ch_kwargs...) *
"$(lpar)" *
join([syntaxstring(c; ch_kwargs...) for c in children(t)], ", ") *
"$(rpar)"
# "$(syntaxstring(tok; kwargs...))(" * join(map((c)->("($(syntaxstring(c; kwargs...)))"), children(t)), ",") * ")"
end
end

Expand Down

0 comments on commit 0d8e79b

Please sign in to comment.