Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update helper functions #17

Merged
merged 3 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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