Module:ASM highlight

From ALttP Speedrunning Wiki
Jump to: navigation, search

Documentation for this module may be created at highlight/doc&action=edit&redlink=1 Module:ASM highlight/doc

local p = {}

local OP = 0x0000FF
local NUM = 0xFF8800
local ADDR = 0xFF0088
local DEF = 0x0044FF
local MISC = 0xFF0000
local COMMENT = 0x00FF00

local TAB = string.byte('\t')
local DOL = string.byte('$')
local HASH = string.byte('#')

local HEX = require('Module:Base convert')
local FMT = require('Module:Address')._main

local highlight = {
	-- operators
	ADC = true, AND = true, ASL = true, BCC = true, BCS = true, BEQ = true, BGE = true, BIT = true,
	BLT = true, BMI = true, BNE = true, BPL = true, BRA = true, BRK = true, BRL = true, BVC = true,
	BVS = true, CLC = true, CLD = true, CLI = true, CLV = true, CMP = true, COP = true, CPX = true,
	CPY = true, DEA = true, DEC = true, DEX = true, DEY = true, EOR = true, INA = true, INC = true,
	INX = true, INY = true, JML = true, JMP = true, JSL = true, JSR = true, LDA = true, LDX = true,
	LDY = true, LSR = true, MVN = true, MVP = true, NOP = true, ORA = true, PEA = true, PEI = true,
	PER = true, PHA = true, PHB = true, PHD = true, PHK = true, PHP = true, PHX = true, PHY = true,
	PLA = true, PLB = true, PLD = true, PLP = true, PLX = true, PLY = true, REP = true, ROL = true,
	ROR = true, RTI = true, RTL = true, RTS = true, SBC = true, SEC = true, SED = true, SEI = true,
	SEP = true, STA = true, STP = true, STX = true, STY = true, STZ = true, TAX = true, TAY = true,
	TCD = true, TCS = true, TDC = true, TRB = true, TSB = true, TSC = true, TSX = true, TXA = true, 
	TXS = true, TXY = true, TYA = true, TYX = true, WAI = true, WDM = true, XBA = true, XCE = true,
}

local expl = {
	db = true, dw = true, dl = true
}

	-- imports/etc
	--incsrc = MISC, fillbyte = MISC, fill = MISC, lorom = MISC, org = MISC

function p.main(frame)
	local args = frame:getParent().args
	local text = args.code

	return p._main(text)
end

function p._main(text)
	local ret = mw.html.create('div')
	ret:addClass('asmtemplate')

	local lines = mw.text.split(text, '\n')

	for _, v in ipairs(lines) do
		ret:tag('br')
		local line = ret:tag('span')
		local l = v

		while l:byte(1) == TAB do
			line:wikitext('\t')
			l = l:sub(2)
		end

		local commentloc = l:find(';')
		local comment = ''

		if commentloc then
			l = l:sub(1,commentloc)
			comment = l:sub(commentloc)
		end

		local lsplit = mw.text.split(l, ' : ')

		local m = #lsplit

		for i, w in ipairs(lsplit) do
			local t = mw.text.trim(w)
			t = string.gsub(t, ' *([+-><]+) *', '%1') -- removes spaces in xkas operations
			local parts = mw.text.split(w, ' ')
			local opr = mw.text.split(parts[1], '%.')

			if highlight[opr[1]] then
				local optag = line:tag('span')
						:addClass('operator')
						:wikitext(parts[1])
					:done()
				-- assume it's not an address if not an operator (for now)
				local addr = parts[2]
				if addr then
					line:wikitext(' ')
					local opandtag = line:tag('span')
					local fb = addr:byte(1)

					if fb == DOL then
						local addr2 = string.match(addr, '^%$(%x*).*')
						local adrt, ttl = FMT(addr2, nil, true)
						opandtag:wikitext(addr)
								:addClass('address')
								:attr('title', ttl)
						:done()
					elseif fb == HASH then
						local addr2 = string.match(addr, '^#%$(%x*).*')
						local indecimal = tonumber(HEX._convert(addr2, 10, 16))
						opandtag:wikitext(addr)
								:addClass('const')
								:attr('title', string.format('0x%s = %s', addr2, indecimal))
						:done()
					end
				end
			elseif expl[opr[1]] then
				local optag
			end

		end
	end

	return ret
end

return p