Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Truth-table/Jq/truth-table-1.jq
Normal file
25
Task/Truth-table/Jq/truth-table-1.jq
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
include "peg"; # see [[:Category:jq/peg.jq]
|
||||
|
||||
def expr:
|
||||
|
||||
def Var : parse("[A-Z][a-zA-Z0-9]*");
|
||||
|
||||
def boolean : (literal("true") // literal("false"))
|
||||
| .result[-1] |= fromjson;
|
||||
|
||||
def primary : ws
|
||||
| (Var
|
||||
// boolean
|
||||
// box(q("(") | expr | q(")"))
|
||||
)
|
||||
| ws;
|
||||
|
||||
def e3 : ws | (box(literal("not") | primary) // primary);
|
||||
def e2 : box(e3 | star(literal("and") | e3)) ;
|
||||
def e1 : box(e2 | star((literal("or") // literal("xor")) | e2)) ;
|
||||
def e0 : box(primary | literal("=>") | primary) // e1;
|
||||
|
||||
ws | e0 | ws;
|
||||
|
||||
def statement:
|
||||
{remainder: .} | expr | eos;
|
||||
15
Task/Truth-table/Jq/truth-table-2.jq
Normal file
15
Task/Truth-table/Jq/truth-table-2.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# Evaluate $Expr in the context of {A,B,....}
|
||||
def eval($Expr):
|
||||
if $Expr|type == "boolean" then $Expr
|
||||
elif $Expr|type == "string" then getpath([$Expr])
|
||||
elif $Expr|length == 1 then eval($Expr[0])
|
||||
elif $Expr|(length == 2 and first == "not") then eval($Expr[-1])|not
|
||||
elif $Expr|(length == 3 and .[1] == "or") then eval($Expr[0]) or eval($Expr[2])
|
||||
elif $Expr|(length == 3 and .[1] == "xor")
|
||||
then eval($Expr[0]) as $x
|
||||
| eval($Expr[2]) as $y
|
||||
| ($x and ($y|not)) or ($y and ($x|not))
|
||||
elif $Expr|(length == 3 and .[1] == "and") then eval($Expr[0]) and eval($Expr[2])
|
||||
elif $Expr|(length == 3 and .[1] == "=>") then (eval($Expr[0])|not) or eval($Expr[2])
|
||||
else $Expr | error
|
||||
end;
|
||||
22
Task/Truth-table/Jq/truth-table-3.jq
Normal file
22
Task/Truth-table/Jq/truth-table-3.jq
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# input: a list of strings
|
||||
# output: a stream of objects representing all possible true/false combinations
|
||||
# Each object has the keys specified in the input.
|
||||
def vars2tf:
|
||||
if length == 0 then {}
|
||||
else .[0] as $k
|
||||
| ({} | .[$k] = (true,false)) + (.[1:] | vars2tf)
|
||||
end;
|
||||
|
||||
# If the input is a string, then echo it;
|
||||
# otherwise emit T or F
|
||||
def TF:
|
||||
if type == "string" then .
|
||||
elif . then "T"
|
||||
else "F"
|
||||
end;
|
||||
|
||||
# Extract the distinct variable names from the parse tree.
|
||||
def vars: [.. | strings | select(test("^[A-Z]"))] | unique;
|
||||
|
||||
def underscore:
|
||||
., (length * "_");
|
||||
22
Task/Truth-table/Jq/truth-table-4.jq
Normal file
22
Task/Truth-table/Jq/truth-table-4.jq
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
def tests: [
|
||||
"A xor B",
|
||||
"notA",
|
||||
"A and B",
|
||||
"A and B or C",
|
||||
"A=>(notB)",
|
||||
"A=>(A => (B or A))",
|
||||
"A xor B and C"
|
||||
];
|
||||
|
||||
def tables:
|
||||
tests[] as $test
|
||||
| ($test | statement | .result)
|
||||
| . as $result
|
||||
| vars as $vars
|
||||
| ($vars + [" ", $test] | join(" ") | underscore),
|
||||
(($vars | vars2tf)
|
||||
| ( [.[], " ", eval($result) | TF] | join(" ")) ),
|
||||
""
|
||||
;
|
||||
|
||||
tables
|
||||
Loading…
Add table
Add a link
Reference in a new issue