summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/truthtable/truthtable.sty
blob: 94403911ad2019ccccc9cc8015808c872ecc13b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
% truthtable.sty
%% Copyright 2021 D. Flück
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status “author-maintained”.
% 
% The Current Maintainer of this work is D. Flück.
% 
% This work consists of the file truthtable.sty.
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{truthtable}[2021/10/01 0.0.1 Package for generating truth tables automatically using LuaTeX]

\ProcessOptions\relax
\@ifpackageloaded{luacode}{
	\PackageWarningNoLine{truthtable}{Package luacode was already loaded}
}{
	\RequirePackage{luacode}
}

\begin{luacode*}

function Impl(a,b)
	return (not a or b);
end

function Equiv(a,b)
	return ((a and b) or ((not a) and (not b)));
end

function ComputeRows(header)
	return 2^header
end

function Split(s, delimiter)
    local result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end

function EvaluateFormula(formula)
	local parsedFormula = "function res() return( " .. string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(formula, " ", ""),">>","Impl"),"__","Equiv"),"!","not "),"&" ," and "),"|"," or "),";",",") .. " ) end";

	chunk = load(parsedFormula);
	chunk();
	local result =  res();
	return result;
end

function toBits(num)
    local t = "" -- will contain the bits
    while num>0 do
        local rest = math.fmod(num,2)
        if (rest == 1) then
			t =  "1" .. t
		else
			t = "0" .. t 
		end
		
        num=(num-rest)/2
    end
    return t;
end

function printTruthValue(expr, dTrue, dFalse)

	local returnVal = ""

	if (expr) then
		returnVal = dTrue;
	else
		returnVal = dFalse;
	end

	return returnVal;
end

function parse(commaSepVariables, commaSepDisplayVariables, commaSepResultRows, commaSepResultDisplayRows, displayTrue, displayFalse)
	local vrbls = Split(commaSepVariables, ",");
	local numberOfColumns = #(vrbls);
	local rows = ComputeRows(numberOfColumns);
	local dVrbls = Split(commaSepDisplayVariables, ",");
	local resRows = Split(commaSepResultRows, ",");
	local dResRows = Split(commaSepResultDisplayRows, ",");

	local dHeader = string.gsub(commaSepDisplayVariables, ",", " & ") .. " & " .. string.gsub(commaSepResultDisplayRows, ",", " & ") .. " \\\\ \\hline";

	if (not (numberOfColumns == #(vrbls) and numberOfColumns == #(dVrbls))) then
		return("error");
	end

	local tableContent = dHeader;

	for i = (rows - 1),0,-1
	do
		local bitString = toBits(i);

		while #bitString < numberOfColumns do
			bitString = "0" .. bitString
		end

		local wVrbls = commaSepVariables;
		local wCommaSepRows = commaSepResultRows
		for ii = 1,numberOfColumns
		do
			wVrbls = string.gsub(wVrbls, vrbls[ii], (string.sub(bitString,ii,ii) == "1" ) and "+" or "-" )
			wCommaSepRows = string.gsub(wCommaSepRows, vrbls[ii], (string.sub(bitString,ii,ii) == "1" ) and "+" or "-" )
		end

		local aWVrbls = Split(string.gsub(string.gsub(wVrbls, "+", "true"),"-", "false"), ",");

		local aWCommaSepRows = Split(string.gsub(string.gsub(wCommaSepRows, "+", "true"),"-", "false"), ",");

		local row = "";

		for c = 1,#(aWVrbls)
		do
			row = row .. printTruthValue(EvaluateFormula(aWVrbls[c]), displayTrue, displayFalse) .. " & ";
		end

		for c = 1,#(aWCommaSepRows)
		do
			row = row .. printTruthValue(EvaluateFormula(aWCommaSepRows[c]), displayTrue, displayFalse) .. " & ";
		end

		row = string.sub(row, 1, #row - 2) .. "\\\\"

		tableContent = tableContent .. "\n" .. row
	end
	
	tex.print(tableContent);
end

\end{luacode*}

\newcommand{\truthtable}[6]{
	\luadirect{parse("#1", "\luaescapestring{#2}", "\luaescapestring{#3}", "\luaescapestring{#4}", "\luaescapestring{#5}","\luaescapestring{#6}")}
}

\endinput