Skip to content

Commit

Permalink
Add syntax highlighting to structs to be like other types. (#70)
Browse files Browse the repository at this point in the history
* Add syntax highlighting to structs to be like other types.

The code looks more unified and easier to look at instead of situations like below.

void function(orange, orange, yellow, orange)

or

(orange) char = "string";
(yellow) struct;
(orange) float = 5;

* Removed space I added on accident.
  • Loading branch information
WallopingEwe authored Dec 15, 2024
1 parent 13da808 commit 81b7aa5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lua/wire/client/hlzasm/hc_compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ function HCOMP:StartCompile(sourceCode,fileName,writeByteCallback,writeByteCalle
self.WriteByteCallback = writeByteCallback
self.WriteByteCaller = writeByteCaller

-- Refresh the keywords table.
local keywordsTable = WireTextEditor.Modes.ZCPU.keywordsTable
local keywordsTableOriginal = WireTextEditor.Modes.ZCPU.keywordsTableOriginal

for k,v in pairs(keywordsTable) do
if keywordsTableOriginal[k] == nil then
keywordsTable[k] = nil
end
end

-- Set the working directory
self.FileName = string.sub(fileName,string.find(fileName,"\\$") or 1)
if string.GetPathFromFilename then
Expand Down
1 change: 1 addition & 0 deletions lua/wire/client/hlzasm/hc_syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ function HCOMP:DefineVariable(isFunctionParam,isForwardDecl,isRegisterDecl,isStr
varType = self.TokenData
varSize = 0 -- Depends on pointer level
isStruct = true
WireTextEditor.Modes.ZCPU.keywordsTable[string.upper(varType)] = true -- Add the new struct to the keywords table.
else -- Define variable
self:ExpectToken(TOKEN.TYPE)
varType = self.TokenData
Expand Down
14 changes: 12 additions & 2 deletions lua/wire/client/text_editor/modes/zcpu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,16 @@ local keywordsList = {
"INT","FLOAT","CHAR","VOID","PRESERVE","ZAP","STRUCT","VECTOR"
}

local keywordsTable = {}
EDITOR.keywordsTable = {}
local keywordsTable = EDITOR.keywordsTable

for k,v in pairs(keywordsList) do
keywordsTable[v] = true
end

-- For checking the keywords to remove structs in the editor.
EDITOR.keywordsTableOriginal = table.Copy(keywordsTable)

-- Build lookup table for registers
local registersTable = {
EAX = true,EBX = true,ECX = true,EDX = true,ESI = true,EDI = true,
Expand Down Expand Up @@ -202,6 +207,9 @@ function EDITOR:SyntaxColorLine(row)

local isGpu = self:GetParent().EditorType == "GPU"

-- Remember the last token to prevent structs from being highlighted.
local previousToken = ""

while self.character do
local tokenname = ""
self.tokendata = ""
Expand All @@ -217,13 +225,15 @@ function EDITOR:SyntaxColorLine(row)
tokenname = "opcode"
elseif registersTable[sstr] then
tokenname = "register"
elseif keywordsTable[sstr] then
elseif keywordsTable[sstr] and (previousToken ~= "STRUCT") then -- Default to number/normal if it's declaring a struct.
tokenname = "keyword"
elseif tonumber(self.tokendata) then
tokenname = "number"
else
tokenname = "normal"
end

previousToken = sstr
elseif (self.character == "'") or (self.character == "\"") then
tokenname = "string"
local delimiter = self.character
Expand Down

0 comments on commit 81b7aa5

Please sign in to comment.