Skip to content

Commit

Permalink
Merge pull request #17 from eliascarv/macro-funcs
Browse files Browse the repository at this point in the history
Update helper functions
  • Loading branch information
eliascarv authored May 17, 2022
2 parents 1f7cf1d + afe865a commit 8d3d0f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
(x::Bool, y::Bool) = x || y
¬(x::Bool) = !x

_propname(x) = throw(ArgumentError("$x is not a valid proposition name."))
_propname(x::Symbol) = x
_propname(x::Any) = throw(ArgumentError("$x is not a valid proposition name."))

function _propnames!(props::Vector{Symbol}, expr::Expr)
if expr.head == :call
args = expr.args[2:end]
else
args = expr.args
end

for arg in args
b = expr.head == :call ? 2 : 1
for arg in expr.args[b:end]
if arg isa Expr
_propnames!(props, arg)
else
Expand Down Expand Up @@ -56,9 +51,9 @@ else
function exprname(expr::Expr)
str = string(expr)
str = replace(str,
"&&" => "", "&" => "",
"||" => "", "|" => "",
"!" => "¬", "~" => "¬"
r"&{2}|&" => "",
r"\|{2}|\|" => "",
r"!|~" => "¬"
)
Symbol(str)
end
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ using TruthTables: TruthTable
@test Tables.getcolumn(tt, Symbol("¬y")) == Bool[0, 1, 0, 1]
@test Tables.getcolumn(tt, Symbol("¬x ∧ ¬y")) == Bool[0, 0, 0, 1]
@test Tables.getcolumn(tt, Symbol("¬(x ∨ y) <--> ¬x ∧ ¬y")) == Bool[1, 1, 1, 1]

# helper functions
expr = :(p && q --> r)
@test TruthTables.propnames(expr) == [:p, :q, :r]
@test TruthTables.getsubexprs(expr) == [:(p && q), :(p && q --> r)]
@test TruthTables.exprname(expr) == Symbol("p ∧ q --> r")

@test_throws ArgumentError TruthTables._propname(1)
@test_throws ArgumentError TruthTables._kwarg(:(full => true))
end

@testset "TruthTable show" begin
Expand Down

0 comments on commit 8d3d0f7

Please sign in to comment.