Debian Codegraph @

texmf-dist/tex/lualatex/truthtable/truthtable.sty

raw
1 % truthtable.sty
2 %% Copyright 2021 D. Flück
3 %
4 % This work may be distributed and/or modified under the
5 % conditions of the LaTeX Project Public License, either version 1.3
6 % of this license or (at your option) any later version.
7 % The latest version of this license is in
8 % http://www.latex-project.org/lppl.txt
9 % and version 1.3 or later is part of all distributions of LaTeX
10 % version 2005/12/01 or later.
11 %
12 % This work has the LPPL maintenance status “author-maintained”.
13 %
14 % The Current Maintainer of this work is D. Flück.
15 %
16 % This work consists of the file truthtable.sty.
17 \NeedsTeXFormat{LaTeX2e}[1994/06/01]
18 \ProvidesPackage{truthtable}[2023/09/16 0.1.0 Package for generating truth tables automatically using LuaTeX]
19
20 \ProcessOptions\relax
21 \@ifpackageloaded{luacode}{
22 \PackageWarningNoLine{truthtable}{Package luacode was already loaded}
23 }{
24 \RequirePackage{luacode}
25 }
26
27 \begin{luacode*}
28
29 function Impl(a,b)
30 return (not a or b);
31 end
32
33 function Equiv(a,b)
34 return ((a and b) or ((not a) and (not b)));
35 end
36
37 function Xor(a,b)
38 return ((a or b) and (not (a and b)));
39 end
40
41 function Nand(a,b)
42 return (not (a and b));
43 end
44
45 function ComputeRows(header)
46 return 2^header
47 end
48
49 function Split(s, delimiter)
50 local result = {};
51 for match in (s..delimiter):gmatch("(.-)"..delimiter) do
52 table.insert(result, match);
53 end
54 return result;
55 end
56
57 function EvaluateFormula(formula)
58
59 local parsedFormula = "function res() return( " .. string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(formula, " ", ""),">>","Impl"),"__","Equiv"),"<>","Equiv"),"%^","Xor"),"!&","Nand"),"!","not "),"&" ," and "),"|"," or "),";",",") .. " ) end";
60
61 chunk = load(parsedFormula);
62 chunk();
63 local result = res();
64 return result;
65 end
66
67 function toBits(num)
68 local t = "" -- will contain the bits
69 while num>0 do
70 local rest = math.fmod(num,2)
71 if (rest == 1) then
72 t = "1" .. t
73 else
74 t = "0" .. t
75 end
76
77 num=(num-rest)/2
78 end
79 return t;
80 end
81
82 function printTruthValue(expr, dTrue, dFalse)
83
84 local returnVal = ""
85
86 if (expr) then
87 returnVal = dTrue;
88 else
89 returnVal = dFalse;
90 end
91
92 return returnVal;
93 end
94
95 function parse(commaSepVariables, commaSepDisplayVariables, commaSepResultRows, commaSepResultDisplayRows, displayTrue, displayFalse, order)
96
97 print("\n\ntruthtable v0.1.0\n")
98
99 local vrbls = Split(commaSepVariables, ",");
100 local numberOfColumns = #(vrbls);
101 local rows = ComputeRows(numberOfColumns);
102 local dVrbls = Split(commaSepDisplayVariables, ",");
103 local resRows = Split(commaSepResultRows, ",");
104 local dResRows = Split(commaSepResultDisplayRows, ",");
105
106 local dHeader = string.gsub(commaSepDisplayVariables, ",", " & ") .. " & " .. string.gsub(commaSepResultDisplayRows, ",", " & ") .. [[ \\ \hline]];
107
108 if (#(dVrbls) ~= #(vrbls)) then
109 print("Error: The number of variables does not match the number of display variables.");
110 return
111 end
112
113 if (#(dResRows) ~= #(resRows)) then
114 print("Error: The number of statements does not match the number of display statements.");
115 return
116 end
117
118 tex.print(dHeader);
119
120
121 local startVal;
122 local endVal;
123 local stepVal;
124
125 if order == "asc" then
126 startVal = 0;
127 endVal = rows - 1;
128 stepVal = 1;
129 else
130 startVal = rows - 1;
131 endVal = 0;
132 stepVal = -1;
133 end
134
135 for i = startVal,endVal,stepVal
136 do
137 local bitString = toBits(i);
138
139 while #bitString < numberOfColumns do
140 bitString = "0" .. bitString
141 end
142
143 local wVrbls = commaSepVariables;
144 local wCommaSepRows = commaSepResultRows
145 for ii = 1,numberOfColumns
146 do
147 wVrbls = string.gsub(wVrbls, vrbls[ii], (string.sub(bitString,ii,ii) == "1" ) and "+" or "-" )
148 wCommaSepRows = string.gsub(wCommaSepRows, vrbls[ii], (string.sub(bitString,ii,ii) == "1" ) and "+" or "-" )
149 end
150
151 local aWVrbls = Split(string.gsub(string.gsub(wVrbls, "+", "true"),"-", "false"), ",");
152
153 local aWCommaSepRows = Split(string.gsub(string.gsub(wCommaSepRows, "+", "true"),"-", "false"), ",");
154
155 local row = "";
156
157 for c = 1,#(aWVrbls)
158 do
159 row = row .. printTruthValue(EvaluateFormula(aWVrbls[c]), displayTrue, displayFalse) .. " & ";
160 end
161
162 for c = 1,#(aWCommaSepRows)
163 do
164 row = row .. printTruthValue(EvaluateFormula(aWCommaSepRows[c]), displayTrue, displayFalse) .. " & ";
165 end
166
167 row = string.sub(row, 1, #row - 2) .. [[\\]];
168
169 tex.print(row);
170 end
171 end
172
173 \end{luacode*}
174
175 \newcommand{\truthtable}[6]{
176 \luadirect{parse("#1", "\luaescapestring{#2}", "\luaescapestring{#3}", "\luaescapestring{#4}", "\luaescapestring{#5}","\luaescapestring{#6}", "\luaescapestring{desc}")}
177 }
178
179 \newcommand{\truthtableasc}[6]{
180 \luadirect{parse("#1", "\luaescapestring{#2}", "\luaescapestring{#3}", "\luaescapestring{#4}", "\luaescapestring{#5}","\luaescapestring{#6}", "\luaescapestring{asc}")}
181 }
182
183 \endinput