Jump to content

Module:LogicUtils

From Logic World Wiki
Revision as of 01:52, 7 September 2025 by Felipe (talk | contribs)

Documentation for this module may be created at Module:LogicUtils/doc

local p = {}

function parse_io(kind, html, str)
	local count = tonumber(str)
	local names = {}

	if count == nil then
		-- str is a list of IO names
		names = mw.text.split(str, "%s")
	else
		-- str is a number
		for i=1,count do
			names[i] = string.format("%s %i", kind, i)
		end
	end

	for i, v in ipairs(names) do
		html = html.."<th> "..v.."</th>"
	end
	
	return html
end

p.truth_table = function(frame, args)
	local args = args or frame:getParent().args
	local html = "<table class=\"wikitable\"><tr>"

	html = parse_io("Input", html, args.inputs)
	html = parse_io("Output", html, args.outputs)
	html = html.."</tr>"
	
	if args.caption ~= nil then
		html = html.."<caption>"..args.caption.."</caption>"	
	end

	local i = 1
	while args[i] ~= nil do
		mw.log(args[i])
		html = html.."<tr>"
		for token in string.gmatch(args[i], "[^%s]+") do
			if token == "0" then
				html = html.."<td style=\"color:red; font-weight:bold\">0</td>"
			elseif token == "1" then
				html = html.."<td style=\"color:green; font-weight:bold\">1</td>"
			else
				html = html.."<td>"..token.."</td>"
			end
		end
		html = html.."</tr>"
		
		i = i + 1
	end

	html = html.."</table>"	
	return html
end

return p