Difference between revisions of "Module:ASM highlight"

From ALttP Speedrunning Wiki
Jump to: navigation, search
Line 90: Line 90:
 
 
 
if addr:byte(1) == DOL then
 
if addr:byte(1) == DOL then
local adrt, ttl = FMT(addr, nil, true)
+
local addr2 = string.match(addr, '^$%X')
 +
local adrt, ttl = FMT(addr2, nil, true)
 
opandtag:wikitext(adrt)
 
opandtag:wikitext(adrt)
 
:attr('title', ttl)
 
:attr('title', ttl)

Revision as of 11:20, 9 April 2019

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,
}
	-- numbers
	--['#%$'] = NUM,

	-- addresses
	--['%$']

	-- defines
	--db = DEF, dw = DEF, dl = DEF

	-- 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

	local ret = mw.html.create('div')
	ret:addClass('asmtemplate')

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

	for _, v in ipairs(lines) do
		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')
					
					if addr:byte(1) == DOL then
						local addr2 = string.match(addr, '^$%X')
						local adrt, ttl = FMT(addr2, nil, true)
						opandtag:wikitext(adrt)
								:attr('title', ttl)
						:done()
					end
				end
			end

		end
	end

	return ret
end

return p