From 0a9aa7a7d736e7d8ba23cf82b66de4212155e263 Mon Sep 17 00:00:00 2001
From: Redo <A509DCFC@cock.li>
Date: Tue, 1 Nov 2022 11:25:09 -0600
Subject: [PATCH] add scoped defines

---
 assembler-8608.lua | 49 ++++++++++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 17 deletions(-)

diff --git a/assembler-8608.lua b/assembler-8608.lua
index b3d4c98..9112500 100644
--- a/assembler-8608.lua
+++ b/assembler-8608.lua
@@ -113,10 +113,11 @@ local function assembleInstruction(line, state, instrs, validWords)
 	end
 end
 local directiveFunctions = {
-	fn    = function(state, fn) state.fileName = fn end,
-	ln    = function(state, ln) state.lineNum = tonumber(ln) end,
-	org   = function(state, addr) state.curAddr = decodeNumber(addr) end,
-	align = function(state, alns) local aln = decodeNumber(alns); if state.curAddr % aln ~= 0 then state.curAddr = state.curAddr + (aln - state.curAddr%aln) end end,
+	fn     = function(state, fn) state.fileName = fn end,
+	ln     = function(state, ln) state.lineNum = tonumber(ln) end,
+	org    = function(state, addr) state.curAddr = decodeNumber(addr) end,
+	align  = function(state, alns) local aln = decodeNumber(alns); if state.curAddr % aln ~= 0 then state.curAddr = state.curAddr + (aln - state.curAddr%aln) end end,
+	define = true,
 }
 local function assembleCode(code, instrs)
 	local validWords = validWordsFromInstrs(instrs)
@@ -178,7 +179,31 @@ local function evaluateExpression(expr)
 	return eval
 end
 local function preprocessCode(code)
-	code = "\n"..code.."\n"
+	local curscope = ""
+	local codet = {}
+	local wordt = {}
+	local lastword = ""
+	local function addword(word)
+		lastword = word
+		if word:sub(1, 1)=="." and not directiveFunctions[word:sub(2, #word)] then word = curscope..word end
+		table.insert(codet, word)
+	end
+	for i = 1, #code do
+		local c = code:sub(i, i)
+		if c:find("[%.a-zA-Z0-9_]") then table.insert(wordt, c)
+		else
+			if #wordt>0 then
+				addword(table.concat(wordt))
+				wordt = {}
+			end
+			if c==":" and lastword:sub(1, 1)~="." and not lastword:find("_BRACE_") then
+				curscope = lastword
+			end
+			table.insert(codet, c)
+		end
+	end
+	
+	code = "\n"..table.concat(codet).."\n"
 	
 	local funcmacros = {}
 	code = code:gsub(".define ([%.a-zA-Z0-9_]+)%(([^%)]+)%) ([^\n]+)", function(name, args, repl)
@@ -249,16 +274,6 @@ local function fixCode(code)
 	code = code:gsub("%*", " %* ")
 	code = code:gsub("\n[ \t\r\n]*", "\n")
 	code = code:gsub(" +", " ")
-	local curScope = ""
-	code = code:gsub("(%.?)([a-zA-Z_][a-zA-Z0-9_%.]*)(:?)", function(dot, name, colon)
-		if directiveFunctions[name] then return dot..name..colon end
-		if dot=="." then
-			assert(curScope~="", "scoped label before any unscoped label: "..name)
-			name = curScope.."."..name
-		end
-		if colon==":" and not name:find("^_BRACE_") then curScope = name:match("^[^%.]+") end
-		return name..colon
-	end)
 	
 	return code
 end
@@ -447,7 +462,7 @@ local function printMemory(mem)
 	local numreps = 0
 	local function closereps(base)
 		if numreps~=0 then
-			print("(repeated "..numreps.." more times, up to "..string.format("%04X", base+16)..")")
+			print("(repeated "..numreps.." more times, up to "..string.format("%04X", base+15)..")")
 			numreps = 0
 		end
 	end
@@ -469,7 +484,7 @@ local function printMemory(mem)
 		if nonempty then
 			local l = table.concat(line)
 			if l~=lastline or base~=lastbase+16 then
-				closereps(base)
+				closereps(base-16)
 				if base ~= lastbase+16 then print("...") end
 				print(string.format("%04X", base).." |"..l)
 			else