Skip to content

Commit

Permalink
Support data indexing clks
Browse files Browse the repository at this point in the history
* Fix and operation (was being called incorrectly)
* Remove all assignment operators
* Support mutable data type clks
* Remove all != operators (now redundant, uses == operator internally.)
* Port wirelink index setters getters to new system
  • Loading branch information
Vurv78 committed Mar 21, 2023
1 parent d1d457e commit 7e991ee
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 240 deletions.
38 changes: 30 additions & 8 deletions lua/entities/gmod_wire_expression2/base/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,9 @@ local CompileVisitors = {

-- It can have indices, it already exists
if #indices > 0 then
local setter = table.remove(indices)
local setter, id = table.remove(indices), existing.scope:Depth()
stmts[i] = function(state)
return state.Scope[var]
return state.Scopes[id][var]
end

for j, index in ipairs(indices) do
Expand Down Expand Up @@ -923,17 +923,39 @@ local CompileVisitors = {
self:Assert(existing.type == value_ty, "Cannot assign type (" .. value_ty .. ") to variable of type (" .. existing.type .. ")", trace)

local id = existing.scope:Depth()
stmts[i] = function(state) ---@param state RuntimeContext
state.Scopes[id][var] = value(state)
if id == 0 then
stmts[i] = function(state) ---@param state RuntimeContext
local val = value(state)
state.GlobalScope[var] = val
state.GlobalScope.vclk[var] = true

if state.GlobalScope.lookup[val] then
state.GlobalScope.lookup[val][var] = true
else
state.GlobalScope.lookup[val] = { [var] = true }
end
end
else
stmts[i] = function(state) ---@param state RuntimeContext
state.Scopes[id][var] = value(state)
end
end
end
else
-- Cannot have indices.
self:Assert(#indices == 0, "Variable (" .. var .. ") does not exist", trace)
self.scope:DeclVar(var, { type = value_ty, initialized = true, trace_if_unused = trace })
self.global_scope:DeclVar(var, { type = value_ty, initialized = true, trace_if_unused = trace })

stmts[i] = function(state) ---@param state RuntimeContext
state.Scope[var] = value(state)
local val = value(state)
state.GlobalScope[var] = val
state.GlobalScope.vclk[var] = true

if state.GlobalScope.lookup[val] then
state.GlobalScope.lookup[val][var] = true
else
state.GlobalScope.lookup[val] = { [var] = true }
end
end
end
end
Expand Down Expand Up @@ -1177,11 +1199,11 @@ local CompileVisitors = {
local largs_lhs = { [1] = {}, [2] = { lhs }, [3] = { lhs_ty } }
local largs_rhs = { [1] = {}, [2] = { rhs }, [3] = { rhs_ty } }
return function(state)
return (op_lhs(lhs(state, largs_lhs)) ~= 0 and op_rhs(rhs(state, largs_rhs)) ~= 0) and 1 or 0
return (op_lhs(state, lhs(state, largs_lhs)) ~= 0 and op_rhs(state, rhs(state, largs_rhs)) ~= 0) and 1 or 0
end
else
return function(state)
return (op_lhs(lhs(state)) ~= 0 and op_rhs(rhs(state)) ~= 0) and 1 or 0
return (op_lhs(state, lhs(state)) ~= 0 and op_rhs(state, rhs(state)) ~= 0) and 1 or 0
end, "n"
end
end
Expand Down
15 changes: 0 additions & 15 deletions lua/entities/gmod_wire_expression2/core/angle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,6 @@ e2function angle ang(vector rv1)
return Angle(rv1[1], rv1[2], rv1[3])
end


registerOperator("ass", "a", "a", function(self, args)
local lhs, op2, scope = args[2], args[3], args[4]
local rhs = op2[1](self, op2)

local Scope = self.Scopes[scope]
local lookup = Scope.lookup
if !lookup then lookup = {} Scope.lookup = lookup end
if lookup[rhs] then lookup[rhs][lhs] = true else lookup[rhs] = {[lhs] = true} end

Scope[lhs] = rhs
Scope.vclk[lhs] = true
return rhs
end)

registerOperator("is", "a", "n", function(state, a)
return a:IsZero() and 1 or 0
end, 1, nil, { legacy = false })
Expand Down
19 changes: 0 additions & 19 deletions lua/entities/gmod_wire_expression2/core/array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,6 @@ registerOperator( "kvarray", "", "r", function( self, args )
return ret
end)

--------------------------------------------------------------------------------
-- = operator
--------------------------------------------------------------------------------
registerOperator("ass", "r", "r", function(self, args)
local lhs, op2, scope = args[2], args[3], args[4]
local rhs = op2[1](self, op2)

local Scope = self.Scopes[scope]
local lookup = Scope.lookup
if !lookup then lookup = {} Scope.lookup = lookup end
if lookup[rhs] then lookup[rhs][lhs] = true else lookup[rhs] = {[lhs] = true} end

Scope[lhs] = rhs
Scope.vclk[lhs] = true
return rhs
end)



--------------------------------------------------------------------------------
-- IS operator
--------------------------------------------------------------------------------
Expand Down
5 changes: 0 additions & 5 deletions lua/entities/gmod_wire_expression2/core/bone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ e2function number operator==(bone lhs, bone rhs)
if lhs == rhs then return 1 else return 0 end
end

--- B != B
e2function number operator!=(bone lhs, bone rhs)
if lhs ~= rhs then return 1 else return 0 end
end

--[[************************************************************************]]--
__e2setcost(3)

Expand Down
21 changes: 0 additions & 21 deletions lua/entities/gmod_wire_expression2/core/complex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,6 @@ e2function number operator==(number lhs, complex rhs)
else return 0 end
end

e2function number operator!=(complex lhs, complex rhs)
if abs(lhs[1]-rhs[1])>delta or
abs(lhs[2]-rhs[2])>delta then
return 1
else return 0 end
end

e2function number operator!=(complex lhs, number rhs)
if abs(lhs[1]-rhs)>delta or
abs(lhs[2])>delta then
return 1
else return 0 end
end

e2function number operator!=(number lhs, complex rhs)
if abs(lhs-rhs[1])>delta or
abs(rhs[2])>delta then
return 1
else return 0 end
end

/******************************************************************************/

e2function complex operator_neg(complex z)
Expand Down
2 changes: 1 addition & 1 deletion lua/entities/gmod_wire_expression2/core/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end)
__e2setcost(0) -- cascaded

registerOperator("is", "n", "n", function(state, num)
return num ~= 0 and 1 or 0
return (num ~= 0) and 1 or 0
end, 1, nil, { legacy = false })

--------------------------------------------------------------------------------
Expand Down
12 changes: 0 additions & 12 deletions lua/entities/gmod_wire_expression2/core/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ end

__e2setcost(5) -- temporary

registerOperator("ass", "e", "e", function(self, args)
local op1, op2, scope = args[2], args[3], args[4]
local rv2 = op2[1](self, op2)
self.Scopes[scope][op1] = rv2
self.Scopes[scope].vclk[op1] = true
return rv2
end)

/******************************************************************************/

e2function number operator_is(entity ent)
Expand All @@ -81,10 +73,6 @@ e2function number operator==(entity lhs, entity rhs)
if lhs == rhs then return 1 else return 0 end
end

e2function number operator!=(entity lhs, entity rhs)
if lhs ~= rhs then return 1 else return 0 end
end

/******************************************************************************/

e2function entity entity(id)
Expand Down
14 changes: 0 additions & 14 deletions lua/entities/gmod_wire_expression2/core/globalvars.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ registerType( "gtable", "xgt", {},

__e2setcost(1)

registerOperator("ass", "xgt", "xgt", function(self, args)
local lhs, op2, scope = args[2], args[3], args[4]
local rhs = op2[1](self, op2)

local Scope = self.Scopes[scope]
local lookup = Scope.lookup
if !lookup then lookup = {} Scope.lookup = lookup end
if lookup[rhs] then lookup[rhs][lhs] = true else lookup[rhs] = {[lhs] = true} end

Scope[lhs] = rhs
Scope.vclk[lhs] = true
return rhs
end)

e2function number operator_is( gtable tbl )
return istable(tbl) and 1 or 0
end
Expand Down
28 changes: 0 additions & 28 deletions lua/entities/gmod_wire_expression2/core/matrix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ e2function number operator==(matrix2 rv1, matrix2 rv2)
then return 1 else return 0 end
end

e2function number operator!=(matrix2 rv1, matrix2 rv2)
if rv1[1] - rv2[1] > delta and rv2[1] - rv1[1] > delta and
rv1[2] - rv2[2] > delta and rv2[2] - rv1[2] > delta and
rv1[3] - rv2[3] > delta and rv2[3] - rv1[3] > delta and
rv1[4] - rv2[4] > delta and rv2[4] - rv1[4] > delta
then return 1 else return 0 end
end

/******************************************************************************/
// Basic operations

Expand Down Expand Up @@ -1056,26 +1048,6 @@ e2function number operator==(matrix4 rv1, matrix4 rv2)
then return 1 else return 0 end
end

e2function number operator!=(matrix4 rv1, matrix4 rv2)
if rv1[1] - rv2[1] > delta and rv2[1] - rv1[1] > delta and
rv1[2] - rv2[2] > delta and rv2[2] - rv1[2] > delta and
rv1[3] - rv2[3] > delta and rv2[3] - rv1[3] > delta and
rv1[4] - rv2[4] > delta and rv2[4] - rv1[4] > delta and
rv1[5] - rv2[5] > delta and rv2[5] - rv1[5] > delta and
rv1[6] - rv2[6] > delta and rv2[6] - rv1[6] > delta and
rv1[7] - rv2[7] > delta and rv2[7] - rv1[7] > delta and
rv1[8] - rv2[8] > delta and rv2[8] - rv1[8] > delta and
rv1[9] - rv2[9] > delta and rv2[9] - rv1[9] > delta and
rv1[10] - rv2[10] > delta and rv2[10] - rv1[10] > delta and
rv1[11] - rv2[11] > delta and rv2[11] - rv1[11] > delta and
rv1[12] - rv2[12] > delta and rv2[12] - rv1[12] > delta and
rv1[13] - rv2[13] > delta and rv2[13] - rv1[13] > delta and
rv1[14] - rv2[14] > delta and rv2[14] - rv1[14] > delta and
rv1[15] - rv2[15] > delta and rv2[15] - rv1[15] > delta and
rv1[16] - rv2[16] > delta and rv2[16] - rv1[16] > delta
then return 1 else return 0 end
end

/******************************************************************************/
// Basic operations

Expand Down
23 changes: 0 additions & 23 deletions lua/entities/gmod_wire_expression2/core/quaternion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,6 @@ end

__e2setcost(2)

registerOperator("ass", "q", "q", function(self, args)
local lhs, op2, scope = args[2], args[3], args[4]
local rhs = op2[1](self, op2)

local Scope = self.Scopes[scope]
local lookup = Scope.lookup
if !lookup then lookup = {} Scope.lookup = lookup end
if lookup[rhs] then lookup[rhs][lhs] = true else lookup[rhs] = {[lhs] = true} end

Scope[lhs] = rhs
Scope.vclk[lhs] = true
return rhs
end)

/******************************************************************************/
// TODO: define division as multiplication with (1/x), or is it not useful?

Expand Down Expand Up @@ -474,15 +460,6 @@ e2function number operator==(quaternion lhs, quaternion rhs)
then return 1 else return 0 end
end

e2function number operator!=(quaternion lhs, quaternion rhs)
local rvd1, rvd2, rvd3, rvd4 = lhs[1] - rhs[1], lhs[2] - rhs[2], lhs[3] - rhs[3], lhs[4] - rhs[4]
if rvd1 > delta or rvd1 < -delta or
rvd2 > delta or rvd2 < -delta or
rvd3 > delta or rvd3 < -delta or
rvd4 > delta or rvd4 < -delta
then return 1 else return 0 end
end

/******************************************************************************/

__e2setcost(4)
Expand Down
22 changes: 1 addition & 21 deletions lua/entities/gmod_wire_expression2/core/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,22 +252,6 @@ end
-- Operators
--------------------------------------------------------------------------------

__e2setcost(5)

registerOperator("ass", "t", "t", function(self, args)
local lhs, op2, scope = args[2], args[3], args[4]
local rhs = op2[1](self, op2)

local Scope = self.Scopes[scope]
local lookup = Scope.lookup
if not lookup then lookup = {} Scope.lookup = lookup end
if lookup[rhs] then lookup[rhs][lhs] = true else lookup[rhs] = {[lhs] = true} end

Scope[lhs] = rhs
Scope.vclk[lhs] = true
return rhs
end)

__e2setcost(1)

e2function number operator_is( table tbl )
Expand All @@ -278,10 +262,6 @@ e2function number operator==( table rv1, table rv2 )
return (rv1 == rv2) and 1 or 0
end

e2function number operator!=( table rv1, table rv2 )
return (rv1 ~= rv2) and 1 or 0
end

__e2setcost(nil)

registerOperator( "kvtable", "", "t", function( self, args )
Expand Down Expand Up @@ -1253,7 +1233,7 @@ end)
--------------------------------------------------------------------------------

-- these postexecute and construct hooks handle changes to both tables and arrays.
registerCallback("postexecute", function(self)
registerCallback("postexecute", function(self) --- @param self RuntimeContext
local Scope = self.GlobalScope
local vclk, lookup = Scope.vclk, Scope.lookup

Expand Down
16 changes: 0 additions & 16 deletions lua/entities/gmod_wire_expression2/core/vector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,6 @@ end

--------------------------------------------------------------------------------

registerOperator("ass", "v", "v", function(self, args)
local lhs, op2, scope = args[2], args[3], args[4]
local rhs = op2[1](self, op2)

local Scope = self.Scopes[scope]
local lookup = Scope.lookup
if !lookup then lookup = {} Scope.lookup = lookup end
if lookup[rhs] then lookup[rhs][lhs] = true else lookup[rhs] = {[lhs] = true} end

Scope[lhs] = rhs
Scope.vclk[lhs] = true
return rhs
end)

--------------------------------------------------------------------------------

e2function number vector:operator_is()
if this[1] > delta or -this[1] > delta or
this[2] > delta or -this[2] > delta or
Expand Down
Loading

0 comments on commit 7e991ee

Please sign in to comment.