Module:IC10: Difference between revisions
From Stationeers Wiki
More actions
Trying to get syntax highlighting working |
m replace mw.text.gsub with string.gsub |
||
| Line 19: | Line 19: | ||
-- comments | -- comments | ||
line = | line = string.gsub(line, "(#.*)", | ||
'<span style="color:#6a9955;font-style:italic;">%1</span>') | '<span style="color:#6a9955;font-style:italic;">%1</span>') | ||
-- labels | -- labels | ||
line = | line = string.gsub(line, "^(%w+:)", | ||
'<span style="color:#dcdcaa;">%1</span>') | '<span style="color:#dcdcaa;">%1</span>') | ||
-- registers r0–r15 | -- registers r0–r15 | ||
line = | line = string.gsub(line, "%f[%w](r1?[0-5])%f[%W]", | ||
'<span style="color:#4fc1ff;">%1</span>') | '<span style="color:#4fc1ff;">%1</span>') | ||
-- numbers hex | -- numbers hex | ||
line = | line = string.gsub(line, "%f[%w](%-?0x%x+)%f[%W]", | ||
'<span style="color:#b5cea8;">%1</span>') | '<span style="color:#b5cea8;">%1</span>') | ||
-- numbers decimal | -- numbers decimal | ||
line = | line = string.gsub(line, "%f[%w](%-?%d+)%f[%W]", | ||
'<span style="color:#b5cea8;">%1</span>') | '<span style="color:#b5cea8;">%1</span>') | ||
-- opcodes | -- opcodes | ||
for op,_ in pairs(opcode_lookup) do | for op,_ in pairs(opcode_lookup) do | ||
line = | line = string.gsub(line, | ||
"%f[%w]("..op..")%f[%W]", | "%f[%w]("..op..")%f[%W]", | ||
'<span style="color:#c586c0;font-weight:bold;">%1</span>') | '<span style="color:#c586c0;font-weight:bold;">%1</span>') | ||
Revision as of 05:25, 20 February 2026
local p = {}
-- List of IC10 opcodes (all strings) local opcodes = {
"add","sub","mul","div", "and","or","xor","not", "beq","bne","j","jal", "move","slt","sgt"
}
-- Build lookup table local opcode_lookup = {} for _, op in ipairs(opcodes) do
opcode_lookup[op] = true
end
local function highlight_line(line)
line = mw.text.nowiki(line)
-- comments
line = string.gsub(line, "(#.*)",
'%1')
-- labels
line = string.gsub(line, "^(%w+:)",
'%1')
-- registers r0–r15
line = string.gsub(line, "%f[%w](r1?[0-5])%f[%W]",
'%1')
-- numbers hex
line = string.gsub(line, "%f[%w](%-?0x%x+)%f[%W]",
'%1')
-- numbers decimal
line = string.gsub(line, "%f[%w](%-?%d+)%f[%W]",
'%1')
-- opcodes
for op,_ in pairs(opcode_lookup) do
line = string.gsub(line,
"%f[%w]("..op..")%f[%W]",
'%1')
end
return line
end
function p.highlight(frame)
local text = frame.args[1] or "" local lines = mw.text.split(text, "\n")
for i,line in ipairs(lines) do
lines[i] = highlight_line(line)
end
return '
'
.. table.concat(lines, "\n") ..
''
end
return p