Jump to content

Module:LogicUtils: Difference between revisions

From Logic World Wiki
No edit summary
Tag: Manual revert
No edit summary
Line 29: Line 29:
local args = args or frame:getParent().args
local args = args or frame:getParent().args
local html = "<table class=\"wikitable\"><tr>"
local html = "<table class=\"wikitable\"><tr>"
if args.caption ~= nil then
html = html.."<caption>"..args.caption.."</caption>"
end


html = parse_io("Input", html, args.inputs)
html = parse_io("Input", html, args.inputs)
Line 34: Line 38:
html = html.."</tr>"
html = html.."</tr>"
if args.caption ~= nil then
html = html.."<caption>"..args.caption.."</caption>"
end
local i = 1
local i = 1
while args[i] ~= nil do
while args[i] ~= nil do

Revision as of 10:21, 7 September 2025

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, ",")
	else
		-- str is a number
		if count == 1 then
			names[1] = kind
		else
			for i=1,count do
				names[i] = string.format("%s %i", kind, i)
			end
		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>"

	if args.caption ~= nil then
		html = html.."<caption>"..args.caption.."</caption>"	
	end

	html = parse_io("Input", html, args.inputs)
	html = parse_io("Output", html, args.outputs)
	html = html.."</tr>"
	
	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