summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luatruthtable/luatruthtable.sty
blob: 7f57bbe2541705b426c4ec21df1ce48f16b99841 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
% luaset package
% version 1.1
% Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt.
%Authors: Chetan Shirore and Dr. Ajit Kumar

\ProvidesPackage{luatruthtable}[1.1]
\RequirePackage{xkeyval}
\RequirePackage{amsmath}
\RequirePackage{luacode}
\begin{luacode*}
local function toBinary(x,y)
    y = y or math.max(1, select(2, math.frexp(x)))
    local res = {}
    for i = y, 1, -1 do
        res[i] = math.fmod(x, 2)
        x = math.floor((x - res[i]) / 2)
    end
    return res
end

local function _not(a)
  if a ==0 then return 1
    else return 0 end
end

local lognot = {}

local not_mt= {
  __mul = function(a,b)
      return _not(b)

  end
}

setmetatable(lognot, not_mt)
_G.lognot = lognot

local function _and(a, b)
  if a == 1 and b == 1 then return 1
    else return 0 end
end

local tmp1 = {}
local logand = {}
local v1 = 0

local and_mt= {
  __mul = function(a,b)
    if b==logand then
    	v1 = v1 + 1 
      tmp1[v1]=a
      return tmp1
    elseif a == tmp1 then
      local w1 = _and(tmp1[v1], b)
      v1 = v1 - 1
		return w1
    end
  end
}

setmetatable(tmp1, and_mt)
setmetatable(logand, and_mt)
_G.logand = logand

local function _or(a, b)
  if a == 0 and b == 0 then return 0
    else return 1 end
end

local tmp2 = {}
local logor = {}
local v2 = 0

local or_mt= {
  __mul = function(a,b)
    if b==logor then
    	 v2 = v2 + 1 
      tmp2[v2] = a
      return tmp2
    elseif a == tmp2 then
      local w2 =  _or(tmp2[v2], b)
      v2 = v2 - 1
		return w2
    end
  end
}

setmetatable(tmp2, or_mt)
setmetatable(logor, or_mt)
_G.logor = logor

local function _imp(a, b)
  if a == 1 and b == 0 then return 0
    else return 1 end
end

local tmp3 = {}
local imp = {}
local v3 = 0

local imp_mt= {
  __mul = function(a,b)
    if b==imp then
		 v3 = v3 + 1
      tmp3[v3]=a
      return tmp3
    elseif a == tmp3 then
      local w3 =  _imp(tmp3[v3], b)
      v3 = v3 - 1
		return w3
    end
  end
}

setmetatable(tmp3, imp_mt)
setmetatable(imp, imp_mt)
_G.imp = imp

local function _iff(a, b)
  if a == b then return 1
    else return 0 end
end

local tmp4 = {}
local iff = {}
local v4 = 0

local iff_mt= {
  __mul = function(a,b)
    if b==iff then
     v4 = v4 + 1 
      tmp4[v4]=a
      return tmp4
    elseif a == tmp4 then
      local w4 = _iff(tmp4[v4], b)
      v4 = v4 - 1
		return w4
    end
  end
}

setmetatable(tmp4, iff_mt)
setmetatable(iff, iff_mt)
_G.iff = iff

local function _xor(a, b)
  if a ~= b then return 1
    else return 0 end
end

local tmp5 = {}
local logxor = {}
local v5 = 0

local xor_mt= {
  __mul = function(a,b)
    if b==logxor then
     v5 = v5 + 1 
      tmp5[v5]=a
      return tmp5
    elseif a == tmp5 then
      local w5 = _xor(tmp5[v5], b)
      v5 = v5 - 1
		return w5
    end
  end
}

setmetatable(tmp5, xor_mt)
setmetatable(logxor, xor_mt)
_G.logxor = logxor

local function _nand(a, b)
  if a ==1 and b == 1 then return 0
    else return 1 end
end

local tmp6 = {}
local lognand = {}
local v6 = 0

local nand_mt= {
  __mul = function(a,b)
    if b==lognand then
		 v6 = v6 + 1 
      tmp6[v6]=a
      return tmp6
    elseif a == tmp6 then
      local w6 =  _nand(tmp6[v6], b)
      v6 = v6 - 1
		return w6
    end
  end
}

setmetatable(tmp6, nand_mt)
setmetatable(lognand, nand_mt)
_G.lognand = lognand

local function _nor(a, b)
  if a ==0 and b == 0 then return 1
    else return 0 end
end

local tmp7 = {}
local lognor = {}
local v7 = 0

local nor_mt= {
  __mul = function(a,b)
    if b==lognor then
	   v7 = v7 + 1 
      tmp7[v7]=a
      return tmp7
    elseif a == tmp7 then
      local w7 = _nor(tmp7[v7], b)
		 v7 = v7 - 1
		return w7
    end
  end
}

setmetatable(tmp7, nor_mt)
setmetatable(lognor, nor_mt)
_G.lognor = lognor

local function _xnor(a, b)
  if a == b then return 1
    else return 0 end
end

local tmp8 = {}
local logxnor = {}
local v8 = 0

local xnor_mt= {
  __mul = function(a,b)
    if b==logxnor then
      v8 = v8 + 1 
      tmp8[v8]=a
      return tmp8
    elseif a == tmp8 then
     local w8 =  _xnor(tmp8[v8], b)
     v8 = v8 - 1
		return w8
    end
  end
}

setmetatable(tmp8, xnor_mt)
setmetatable(logxnor, xnor_mt)
_G.logxnor = logxnor

function truthTable(str0,str,trtext,fltext)
local eval=""
local names={lognot=lognot,logand=logand,logor=logor, imp=imp, 
  iff=iff, logxor=logxor,lognand=lognand,lognor=lognor, logxnor=logxnor}
local vars={}
local expr={}
local countexp = 1
local countvars =1
local res=""
local sep=" & "
trtext = trtext or "$T$"
fltext = fltext or "$F$"
for variables in string.gmatch(str0, '([^,]+)') do
  vars[countvars] = variables
 countvars = countvars + 1
end

for subexp in string.gmatch(str, '([^,]+)') do
    expr[countexp] = subexp
    countexp = countexp + 1
end

local n =#vars
for i =1,2^n do
  itr = toBinary(i,n)

  for j=1, #itr do
    names[vars[j]] = itr[j]
end

    for k = 1, #expr do
      res= res ..sep.. load("return " .. expr[k],nil,"t",names)()
      end
if i~=2^n then
eval = eval .. table.concat(toBinary(i,n)," & ") .. res .."\\\\"
else
eval = eval .. table.concat(toBinary(i,n)," & ") .. res
end
 res=""
end

if trtext:gsub("%s+", "") =="0" or trtext:gsub("%s+", "") == "$0$"  
or fltext:gsub("%s+", "") =="1" or fltext:gsub("%s+", "") == "$1$" 
then
eval=eval
else
eval=eval:gsub(0,fltext):gsub(1,trtext)
end
return eval
end

\end{luacode*}

% ========= KEY DEFINITIONS =========
\define@key{luatruthtable}{trtext}{\def\luatrtbl@trtext{#1}}
\define@key{luatruthtable}{fltext}{\def\luatrtbl@fltext{#1}}

% ========= KEY DEFAULTS =========
\setkeys{luatruthtable}{trtext=$T$,fltext=$F$}%
% ========= Defining Command =========
\newcommand{\luaTruthTable}[3][]{%
  \setkeys{luatruthtable}{#1}
  \directlua{tex.sprint(truthTable(\luastringN{#2},\luastringN{#3},'\luatrtbl@trtext','\luatrtbl@fltext'))}} %

\endinput