From 1586d3a1442e9d4bdc91b3a10696c7a78adf618d Mon Sep 17 00:00:00 2001 From: Vurv <56230599+Vurv78@users.noreply.github.com> Date: Sun, 15 Oct 2023 14:05:15 -0700 Subject: [PATCH] Revert new $ behavior, deprecate $ --- data/expression2/tests/regressions/2784.txt | 11 +++++ .../tests/runtime/types/number/delta.txt | 2 +- .../gmod_wire_expression2/base/compiler.lua | 47 ++++++++++++++++--- 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 data/expression2/tests/regressions/2784.txt diff --git a/data/expression2/tests/regressions/2784.txt b/data/expression2/tests/regressions/2784.txt new file mode 100644 index 0000000000..d4caaf1a43 --- /dev/null +++ b/data/expression2/tests/regressions/2784.txt @@ -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. \ No newline at end of file diff --git a/data/expression2/tests/runtime/types/number/delta.txt b/data/expression2/tests/runtime/types/number/delta.txt index 66e43baf89..3a03024422 100644 --- a/data/expression2/tests/runtime/types/number/delta.txt +++ b/data/expression2/tests/runtime/types/number/delta.txt @@ -8,7 +8,7 @@ X = 5 assert($X == 5) -assert($X == 0) +# assert($X == 0) ( Unintentionally new behavior, :( ) X++ diff --git a/lua/entities/gmod_wire_expression2/base/compiler.lua b/lua/entities/gmod_wire_expression2/base/compiler.lua index 5ad1ebe51f..71465f49e7 100644 --- a/lua/entities/gmod_wire_expression2/base/compiler.lua +++ b/lua/entities/gmod_wire_expression2/base/compiler.lua @@ -998,7 +998,25 @@ local CompileVisitors = { 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 + else + state.GlobalScope.lookup[val] = { [var] = true } + 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 @@ -1024,7 +1042,25 @@ local CompileVisitors = { 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 @@ -1370,16 +1406,13 @@ local CompileVisitors = { 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