Skip to content

Commit

Permalink
Update array functions
Browse files Browse the repository at this point in the history
Add array:indexOf
  • Loading branch information
Denneisk committed Dec 12, 2023
1 parent 77e1423 commit 242ef38
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
9 changes: 7 additions & 2 deletions data/expression2/tests/runtime/types/array.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ assert(KV[50, number] == 200)
assert(KV[100, number] == 2000)

local Rec = 0
foreach(I, V:number = KV) {
foreach(I:number, V:number = KV) {
Rec++
assert(KV[I, number] == V)
}

assert(Rec == 1) # Only 1 because this breaks the internal ipairs impl (Shouldn't use an array like this anyway.)

A[1, vector] = vec()
assert(A[1, vector] == vec())
assert(A[1, vector] == vec())

A[2, entity] = entity()

assert(A:indexOf(entity()) == 2)
assert(A:indexOf(noentity()) == 0)
66 changes: 36 additions & 30 deletions lua/entities/gmod_wire_expression2/core/array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ end
-- Looped functions and operators
--------------------------------------------------------------------------------
registerCallback( "postinit", function()
local getf, setf
local NO_LEGACY = { legacy = false }
for k,v in pairs( wire_expression_types ) do
local name = k:lower()
if (name == "normal") then name = "number" end
if name == "normal" then name = "number" end
local nameupperfirst = upperfirst( name )
local id = v[1]
local default = v[2]
local typecheck = v[6]

if (!blocked_types[id]) then -- blocked check start
if not blocked_types[id] then -- blocked check start

Check warning on line 77 in lua/entities/gmod_wire_expression2/core/array.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'not' and '!'

--------------------------------------------------------------------------------
-- Get functions
Expand Down Expand Up @@ -113,10 +113,9 @@ registerCallback( "postinit", function()
__e2setcost(5)

registerFunction( name, "r:n", id, function(self, args)
local op1, op2 = args[2], args[3]
local array, index = op1[1](self,op1), op2[1](self,op2)
local array, index = args[2], args[3]
return getter( self, array, index )
end, nil, nil, { deprecated = true })
end, nil, nil, { legacy = false, deprecated = true })

--------------------------------------------------------------------------------
-- Set functions
Expand Down Expand Up @@ -153,10 +152,9 @@ registerCallback( "postinit", function()
end

registerFunction("set" .. nameupperfirst, "r:n"..id, id, function(self,args)
local op1, op2, op3 = args[2], args[3], args[4]
local array, index, value = op1[1](self,op1), op2[1](self,op2), op3[1](self,op3)
local array, index, value = args[1], args[2], args[3]
return setter( self, array, index, value )
end, nil, nil, { deprecated = true })
end, nil, nil, { legacy = false, deprecated = true })


--------------------------------------------------------------------------------
Expand All @@ -166,63 +164,71 @@ registerCallback( "postinit", function()
__e2setcost(7)

registerFunction( "push" .. nameupperfirst, "r:" .. id, id, function(self,args)
local op1, op2 = args[2], args[3]
local array, value = op1[1](self,op1), op2[1](self,op2)
local array, value = args[1], args[2]
return setter( self, array, #array + 1, value )
end)
end, nil, nil, NO_LEGACY)

--------------------------------------------------------------------------------
-- Insert functions
-- Inserts the value at the specified index. Subsequent values are moved up to compensate.
--------------------------------------------------------------------------------
registerFunction( "insert" .. nameupperfirst, "r:n" .. id, id, function( self, args )
local op1, op2, op3 = args[2], args[3], args[4]
local array, index, value = op1[1](self,op1), op2[1](self,op2), op3[1](self,op3)
local array, index, value = args[1], args[2], args[3]
return setter( self, array, index, value, true )
end)
end, nil, nil, NO_LEGACY)

--------------------------------------------------------------------------------
-- Pop functions
-- Removes and returns the last value in the array.
--------------------------------------------------------------------------------
registerFunction( "pop" .. nameupperfirst, "r:", id, function(self,args)
local op1 = args[2]
local array = op1[1](self,op1)
if (!array) then return fixDefault( default ) end
local array = args[1]
if not array then return fixDefault(default) end

Check warning on line 186 in lua/entities/gmod_wire_expression2/core/array.lua

View workflow job for this annotation

GitHub Actions / lint

"Syntax inconsistency"

Inconsistent use of 'not' and '!'
return getter( self, array, #array, true )
end)
end, nil, nil, NO_LEGACY)

--------------------------------------------------------------------------------
-- Unshift functions
-- Inserts the value at the beginning of the array. Subsequent values are moved up to compensate.
--------------------------------------------------------------------------------
registerFunction( "unshift" .. nameupperfirst, "r:" .. id, id, function(self,args)
local op1, op2 = args[2], args[3]
local array, value = op1[1](self,op1), op2[1](self,op2)
local array, value = args[1], args[2]
return setter( self, array, 1, value, true )
end)
end, nil, nil, NO_LEGACY)

--------------------------------------------------------------------------------
-- Shift functions
-- Removes and returns the first value of the array. Subsequent values are moved down to compensate.
--------------------------------------------------------------------------------
registerFunction( "shift" .. nameupperfirst, "r:", id, function(self,args)
local op1 = args[2]
local array = op1[1](self,op1)
if (!array) then return fixDefault( default ) end
local array = args[1]
if not array then return fixDefault(default) end
return getter( self, array, 1, true )
end)
end, nil, nil, NO_LEGACY)

--------------------------------------------------------------------------------
-- Remove functions
-- Removes and returns the specified value of the array. Subsequent values are moved down to compensate.
--------------------------------------------------------------------------------
registerFunction( "remove" .. nameupperfirst, "r:n", id, function(self,args)
local op1, op2 = args[2], args[3]
local array, index = op1[1](self,op1), op2[1](self,op2)
if (!array or !index) then return fixDefault( default ) end
local array, index = args[1], args[2]
if not array or not index then return fixDefault(default) end
return getter( self, array, index, true )
end)
end, nil, nil, NO_LEGACY)

-- indexOf

registerFunction("indexOf", "r:" .. id, "n", function(self, args)
local arr, val = args[1], args[2]
for i, j in ipairs(arr) do
if j == val then
self.prf = self.prf + i
return i
end
end
self.prf = self.prf + #arr
return 0
end, nil, { "value" }, NO_LEGACY)

--------------------------------------------------------------------------------
-- Foreach operators
Expand Down
1 change: 1 addition & 0 deletions lua/wire/client/e2descriptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,7 @@ E2Helper.Descriptions["concat(r:snn)"] = "Concatenates all values in the array,
E2Helper.Descriptions["count(r:)"] = "Returns the number of entries in the array"
E2Helper.Descriptions["exists(r:n)"] = "Returns 1 if the array contains any value at specified index"
E2Helper.Descriptions["id(r:)"] = "Returns the unique ID of the array"
E2Helper.Descriptions["indexOf"] = "Returns the index of the element in the array or 0 if it's not found"
E2Helper.Descriptions["invert(r)"] = "Inverts the array, creating a lookup table"
E2Helper.Descriptions["min(r:)"] = "Returns the smallest number in array"
E2Helper.Descriptions["max(r:)"] = "Returns the largest number in array"
Expand Down

0 comments on commit 242ef38

Please sign in to comment.