Jump to content

Module:Logic

Unchecked
From Wikipedia

This performs a logic calculation.

Examples

[edit]

{{#invoke:Logic|andgate|1|0}} returns 0

{{#invoke:Logic|andgate|1|1}} returns 1

{{#invoke:Logic|notgate|1}} returns 0

{{#invoke:Logic|notgate|0}} returns 1


local p = {}

function p.notgate(frame)
	local input = tonumber(frame.args[1])
	if input == 1 then
		return "0"
	elseif input == 0 then
		return "1"
	end
end

function p.andgate(frame)
	local input1 = tonumber(frame.args[1])
	local input2 = tonumber(frame.args[2])
	if input1 == 1 and input2 == 1 then
		return "1"
	else
		return "0"
	end
end

function p.orgate(frame)
	local input1 = tonumber(frame.args[1])
	local input2 = tonumber(frame.args[2])
	if input1 == 1 or input2 == 1 then
		return "1"
	else
		return "0"
	end
end

function p.nandgate(frame)
	local input1 = tonumber(frame.args[1])
	local input2 = tonumber(frame.args[2])
	if input1 == 1 and input2 == 1 then
		return "0"
	else
		return "1"
	end
end

function p.norgate(frame)
	local input1 = tonumber(frame.args[1])
	local input2 = tonumber(frame.args[2])
	if input1 == 0 and input2 == 0 then
		return "1"
	else
		return "0"
	end
end

function p.xorgate(frame)
	local input1 = tonumber(frame.args[1])
	local input2 = tonumber(frame.args[2])
	if input1 == 0 and input2 == 1 then
		return "1"
	elseif input1 == 1 and input2 == 0 then
		return "1"
	else
		return "0"
	end
end

function p.xnorgate(frame)
	local input1 = tonumber(frame.args[1])
	local input2 = tonumber(frame.args[2])
	if input1 == 0 and input2 == 1 then
		return "0"
	elseif input1 == 1 and input2 == 0 then
		return "0"
	else
		return "1"
	end
end

return p