From 9e2846f7e236a160a91eb47fe0e550953e8416ea Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Sat, 18 Nov 2023 01:05:55 -0600 Subject: [PATCH 1/2] Zap/Preserve now ignores idents working nearby --- lua/wire/client/hlzasm/hc_syntax.lua | 27 ++++++++++--- lua/wire/client/hlzasm/hc_tokenizer.lua | 52 +++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/lua/wire/client/hlzasm/hc_syntax.lua b/lua/wire/client/hlzasm/hc_syntax.lua index a3cf5d5..0bf0151 100644 --- a/lua/wire/client/hlzasm/hc_syntax.lua +++ b/lua/wire/client/hlzasm/hc_syntax.lua @@ -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] @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lua/wire/client/hlzasm/hc_tokenizer.lua b/lua/wire/client/hlzasm/hc_tokenizer.lua index 8c984b8..8562f40 100644 --- a/lua/wire/client/hlzasm/hc_tokenizer.lua +++ b/lua/wire/client/hlzasm/hc_tokenizer.lua @@ -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 @@ -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 @@ -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 From b2dff2c4749c6cc5f3005706c1e3a27663ef5c27 Mon Sep 17 00:00:00 2001 From: DerelictDrone Date: Sun, 19 Nov 2023 16:10:50 -0600 Subject: [PATCH 2/2] Include registers with operators in checks --- lua/wire/client/hlzasm/hc_syntax.lua | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/lua/wire/client/hlzasm/hc_syntax.lua b/lua/wire/client/hlzasm/hc_syntax.lua index 0bf0151..085284f 100644 --- a/lua/wire/client/hlzasm/hc_syntax.lua +++ b/lua/wire/client/hlzasm/hc_syntax.lua @@ -826,15 +826,15 @@ function HCOMP:Statement() local TOKEN,TOKENSET = self.TOKEN,self.TOKENSET local tokenType = self.TokenType if self.BlockDepth > 0 then while self:MatchToken(TOKEN.REGISTER) or self:MatchToken(TOKEN.IDENT) do + -- 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 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 @@ -847,18 +847,10 @@ function HCOMP:Statement() local TOKEN,TOKENSET = self.TOKEN,self.TOKENSET self:Error("Cannot zap ranges using register variables") end else - -- 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") + if tokenType == TOKEN.PRESERVE then + self:Error("Trying to preserve a variable") end + self:Error("Trying to zap a non register variable") end end if self.TokenType == TOKEN.REGISTER then