Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert new $ behavior, deprecate $ #2794

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions data/expression2/tests/regressions/2784.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## SHOULD_PASS:EXECUTE
@persist Var:number # Need this since test runner does not initialize global variables (TODO)

Var = 5

assert($Var == 5) # 5 - 0 = 5

Var = 2

assert($Var == -3) # 2 - 5 = -3
assert($Var == -3) # Actual regression part, delta should not change if variable is not assigned.
2 changes: 1 addition & 1 deletion data/expression2/tests/runtime/types/number/delta.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ X = 5

assert($X == 5)

assert($X == 0)
# assert($X == 0) ( Unintentionally new behavior, :( )

X++

Expand Down
47 changes: 40 additions & 7 deletions lua/entities/gmod_wire_expression2/base/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@

---@param data { [1]: Token<string>, [2]: Node, [3]: Node, [4]: Node?, [5]: Node } var start stop step block
[NodeVariant.For] = function (self, trace, data)
local var, start, stop, step = data[1], self:CompileExpr(data[2]), self:CompileExpr(data[3]), data[4] and self:CompileExpr(data[4]) or data[4]

Check warning on line 391 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: step

local block = self:Scope(function(scope)
scope.data.loop = true
Expand Down Expand Up @@ -566,9 +566,9 @@

if state.__break__ then
state.__break__ = false
goto exit

Check warning on line 569 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Goto"

Don't use labels and gotos unless you're jumping out of multiple loops.
elseif state.__return__ then -- Yes this should only be checked if the switch is inside a function, but I don't care enough about the performance of switch case to add another duplicated 30 lines to the file
goto exit

Check warning on line 571 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Goto"

Don't use labels and gotos unless you're jumping out of multiple loops.
else -- Fallthrough, run every case until break found.
for j = i + 1, ncases do
cases[j][2](state)
Expand Down Expand Up @@ -998,7 +998,25 @@

local id = existing.depth
if id == 0 then
if E2Lib.IOTableTypes[value_ty] then
if self.delta_vars[var] then
if E2Lib.IOTableTypes[value_ty] then
stmts[i] = function(state, val) ---@param state RuntimeContext
state.GlobalScope["$" .. var] = state.GlobalScope[var]
state.GlobalScope[var], state.GlobalScope.vclk[var] = val, true

if state.GlobalScope.lookup[val] then
state.GlobalScope.lookup[val][var] = true

Check warning on line 1008 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
else
state.GlobalScope.lookup[val] = { [var] = true }

Check warning on line 1010 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
end
end
else
stmts[i] = function(state, val) ---@param state RuntimeContext
state.GlobalScope["$" .. var] = state.GlobalScope[var]
state.GlobalScope[var], state.GlobalScope.vclk[var] = val, true
end
end
elseif E2Lib.IOTableTypes[value_ty] then
stmts[i] = function(state, val) ---@param state RuntimeContext
state.GlobalScope[var], state.GlobalScope.vclk[var] = val, true

Expand All @@ -1024,7 +1042,25 @@
self:Assert(#indices == 0, "Variable (" .. var .. ") does not exist", trace)
self.global_scope:DeclVar(var, { type = value_ty, initialized = true, trace_if_unused = trace })

if E2Lib.IOTableTypes[value_ty] then
if self.delta_vars[var] then
if E2Lib.IOTableTypes[value_ty] then
stmts[i] = function(state, val) ---@param state RuntimeContext
state.GlobalScope["$" .. var] = state.GlobalScope[var] -- Set $Var to old value to be used in $ operator.
state.GlobalScope[var], state.GlobalScope.vclk[var] = val, 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, val) ---@param state RuntimeContext
state.GlobalScope["$" .. var] = state.GlobalScope[var] -- Set $Var to old value to be used in $ operator.
state.GlobalScope[var], state.GlobalScope.vclk[var] = val, true
end
end
elseif E2Lib.IOTableTypes[value_ty] then
stmts[i] = function(state, val) ---@param state RuntimeContext
state.GlobalScope[var], state.GlobalScope.vclk[var] = val, true

Expand Down Expand Up @@ -1370,16 +1406,13 @@
self:AssertW(var.initialized, "Use of variable [" .. var_name .. "] before initialization", trace)

if data[1] == Operator.Dlt then -- $
self:Warning("Delta operator ($) is deprecated. Recommended to handle variable differences yourself.", trace)
self:Assert(var.depth == 0, "Delta operator ($) can not be used on temporary variables", trace)
self.delta_vars[var_name] = true

local sub_op, sub_ty = self:GetOperator("sub", { var.type, var.type }, trace)

return function(state) ---@param state RuntimeContext
local current, past = state.GlobalScope[var_name], state.GlobalScope["$" .. var_name]
local diff = sub_op(state, current, past)
state.GlobalScope["$" .. var_name] = current
return diff
return sub_op(state, state.GlobalScope[var_name], state.GlobalScope["$" .. var_name])
end, sub_ty
elseif data[1] == Operator.Trg then -- ~
return function(state) ---@param state RuntimeContext
Expand Down
Loading