Skip to content

Commit

Permalink
Zap/Preserve now ignores idents working nearby
Browse files Browse the repository at this point in the history
  • Loading branch information
DerelictDrone committed Nov 18, 2023
1 parent 9a16f24 commit 9e2846f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lua/wire/client/hlzasm/hc_syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ for i=0,15 do VectorSyntax.MATRIX[i+1] = {tostring(i)} end

--------------------------------------------------------------------------------
-- Compile an opcode (called after if self:MatchToken(TOKEN.OPCODE))
function HCOMP:Opcode() local TOKEN = self.TOKEN
function HCOMP:Opcode() local TOKEN,TOKENSET = self.TOKEN,self.TOKENSET
local opcodeName = self.TokenData
local opcodeNo = self.OpcodeNumber[self.TokenData]
local operandCount = self.OperandCount[opcodeNo]
Expand Down Expand Up @@ -456,7 +456,7 @@ end

--------------------------------------------------------------------------------
-- Compile a variable/function. Returns corresponding labels
function HCOMP:DefineVariable(isFunctionParam,isForwardDecl,isRegisterDecl,isStructMember) local TOKEN = self.TOKEN
function HCOMP:DefineVariable(isFunctionParam,isForwardDecl,isRegisterDecl,isStructMember) local TOKEN,TOKENSET = self.TOKEN,self.TOKENSET
local varType,varSize,isStruct
if self:MatchToken(TOKEN.IDENT) then -- Define structure
varType = self.TokenData
Expand Down Expand Up @@ -742,7 +742,7 @@ end

--------------------------------------------------------------------------------
-- Compile a single statement
function HCOMP:Statement() local TOKEN = self.TOKEN
function HCOMP:Statement() local TOKEN,TOKENSET = self.TOKEN,self.TOKENSET
-- Parse code for absolute labels and define (LABEL:)
if self.CurrentToken == 1 then
while not(self:MatchToken(TOKEN.EOF)) do
Expand Down Expand Up @@ -828,6 +828,13 @@ function HCOMP:Statement() local TOKEN = self.TOKEN
while self:MatchToken(TOKEN.REGISTER) or self:MatchToken(TOKEN.IDENT) do
if self.TokenType == TOKEN.IDENT then
if self.RegisterIdentities[self.TokenData] then
-- Don't error on catching a variable being used near a zap/preserve
if self:MatchToken(TOKENSET.OPERATORS) then
-- move back 2 tokens and then re-parse this
self:PreviousToken()
self:PreviousToken()
return self:Statement()
end
if tokenType == TOKEN.PRESERVE then
self:Error("Trying to preserve a register variable")
end
Expand All @@ -840,10 +847,18 @@ function HCOMP:Statement() local TOKEN = self.TOKEN
self:Error("Cannot zap ranges using register variables")
end
else
if tokenType == TOKEN.PRESERVE then
self:Error("Trying to preserve a variable")
-- Don't error on catching a variable being used near a zap/preserve
if self:MatchToken(TOKEN.DCOLON) or self:MatchToken(TOKENSET.OPERATORS) then
-- move back 2 tokens and then re-parse this
self:PreviousToken()
self:PreviousToken()
return self:Statement()
else
if tokenType == TOKEN.PRESERVE then
self:Error("Trying to preserve a variable")
end
self:Error("Trying to zap a non register variable")
end
self:Error("Trying to zap a non register variable")
end
end
if self.TokenType == TOKEN.REGISTER then
Expand Down
52 changes: 52 additions & 0 deletions lua/wire/client/hlzasm/hc_tokenizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ end
--------------------------------------------------------------------------------
-- Generate table of all possible tokens
HCOMP.TOKEN = {}
HCOMP.TOKENSET = {} -- Manuallly defined sets of tokens
HCOMP.TOKEN_NAME = {}
HCOMP.TOKEN_NAME2 = {}
local IDX = 1
Expand All @@ -128,6 +129,49 @@ for tokenName,tokenData in pairs(HCOMP.TOKEN_TEXT) do
IDX = IDX + 1
end

HCOMP.TOKENSET.OPERATORS = {
HCOMP.TOKEN.LPAREN,
HCOMP.TOKEN.RPAREN,
HCOMP.TOKEN.LSUBSCR,
HCOMP.TOKEN.RSUBSCR,
HCOMP.TOKEN.TIMES,
HCOMP.TOKEN.SLASH,
HCOMP.TOKEN.MODULUS,
HCOMP.TOKEN.PLUS,
HCOMP.TOKEN.MINUS,
HCOMP.TOKEN.AND,
HCOMP.TOKEN.OR,
HCOMP.TOKEN.XOR,
HCOMP.TOKEN.POWER,
HCOMP.TOKEN.INC,
HCOMP.TOKEN.DEC,
HCOMP.TOKEN.SHL,
HCOMP.TOKEN.SHR,
HCOMP.TOKEN.EQL,
HCOMP.TOKEN.NEQ,
HCOMP.TOKEN.LEQ,
HCOMP.TOKEN.LSS,
HCOMP.TOKEN.GEQ,
HCOMP.TOKEN.GTR,
HCOMP.TOKEN.NOT,
HCOMP.TOKEN.EQUAL,
HCOMP.TOKEN.LAND,
HCOMP.TOKEN.LOR,
HCOMP.TOKEN.EQLADD,
HCOMP.TOKEN.EQLSUB,
HCOMP.TOKEN.EQLMUL,
HCOMP.TOKEN.EQLDIV,
HCOMP.TOKEN.DOT
}

HCOMP.TOKENSET.ASSIGNMENT = {
HCOMP.TOKEN.EQUAL,
HCOMP.TOKEN.EQLADD,
HCOMP.TOKEN.EQLSUB,
HCOMP.TOKEN.EQLMUL,
HCOMP.TOKEN.EQLDIV
}

-- Create lookup tables for faster parsing
HCOMP.PARSER_LOOKUP = {}
for symID,symList in pairs(HCOMP.TOKEN_TEXT) do
Expand Down Expand Up @@ -495,6 +539,14 @@ end

-- Returns true and skips a token if it matches this one
function HCOMP:MatchToken(tok)
if istable(tok) then -- Match against a table of tokens
for _,token in pairs(tok) do
if self:MatchToken(token) then
return true
end
end
return false
end
if not self.Tokens[self.CurrentToken] then
return tok == self.TOKEN.EOF
end
Expand Down

0 comments on commit 9e2846f

Please sign in to comment.