Skip to content

Commit

Permalink
adding failsafe to break optimizer loop
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroflag committed Feb 5, 2025
1 parent 617fbf5 commit d6cd96f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 44 deletions.
4 changes: 4 additions & 0 deletions src/ast_optimizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function Optimizer:optimize_iteratively(ast)
self:log(string.format(
"Iteration: %d finished. Number of optimizations: %d",
iterations, num_of_optimizations))
if iterations > 100 then
print("Aborting optimizer. This is likely a bug.")
break
end
until num_of_optimizations == 0
return ast
end
Expand Down
62 changes: 19 additions & 43 deletions src/equinox_bundle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ function Optimizer:optimize_iteratively(ast)
self:log(string.format(
"Iteration: %d finished. Number of optimizations: %d",
iterations, num_of_optimizations))
if iterations > 100 then
print("Aborting optimizer. This is likely a bug.")
break
end
until num_of_optimizations == 0
return ast
end
Expand Down Expand Up @@ -2804,19 +2808,15 @@ end

function pop()
local size = #stack
if size == 0 then
error("Stack underflow: " .. name)
end
if size == 0 then error("Stack underflow: " .. name) end
local item = stack[size]
stack[size] = nil
if item ~= NIL then return item else return nil end
end

function pop2nd()
local n = #stack
if n < 2 then
error("Stack underflow: " .. name)
end
if n < 2 then error("Stack underflow: " .. name) end
local item = stack[n - 1]
stack[n -1] = stack[n]
stack[n] = nil
Expand All @@ -2825,79 +2825,61 @@ end

function pop3rd()
local n = #stack
if n < 3 then
error("Stack underflow: " .. name)
end
if n < 3 then error("Stack underflow: " .. name) end
local item = table.remove(stack, n - 2)
if item ~= NIL then return item else return nil end
end

function swap()
local n = #stack
if n < 2 then
error("Stack underflow: " .. name)
end
if n < 2 then error("Stack underflow: " .. name) end
stack[n], stack[n - 1] = stack[n - 1], stack[n]
end

function rot()
local n = #stack
if n < 3 then
error("Stack underflow: " .. name)
end
if n < 3 then error("Stack underflow: " .. name) end
local new_top = stack[n -2]
table.remove(stack, n - 2)
stack[n] = new_top
end

function mrot()
local n = #stack
if n < 3 then
error("Stack underflow: " .. name)
end
if n < 3 then error("Stack underflow: " .. name) end
local temp = stack[n]
stack[n] = nil
table.insert(stack, n - 2, temp)
end

function over()
local n = #stack
if n < 2 then
error("Stack underflow: " .. name)
end
if n < 2 then error("Stack underflow: " .. name) end
stack[n + 1] = stack[n - 1]
end

function tuck()
local n = #stack
if n < 2 then
error("Stack underflow: " .. name)
end
if n < 2 then error("Stack underflow: " .. name) end
table.insert(stack, n - 1, stack[n])
end

function nip()
local n = #stack
if n < 2 then
error("Stack underflow: " .. name)
end
if n < 2 then error("Stack underflow: " .. name) end
stack[n - 1] = stack[n]
stack[n] = nil
end

function dup()
local n = #stack
if n < 1 then
error("Stack underflow: " .. name)
end
if n < 1 then error("Stack underflow: " .. name) end
stack[n + 1] = stack[n]
end

function dup2()
local n = #stack
if n < 2 then
error("Stack underflow: " .. name)
end
if n < 2 then error("Stack underflow: " .. name) end
local tos1 = stack[n]
local tos2 = stack[n - 1]
stack[n + 1] = tos2
Expand All @@ -2906,17 +2888,13 @@ end

function tos()
local item = stack[#stack]
if item == nil then
error("Stack underflow: " .. name)
end
if item == nil then error("Stack underflow: " .. name) end
if item ~= NIL then return item else return nil end
end

function tos2()
local item = stack[#stack - 1]
if item == nil then
error("Stack underflow: " .. name)
end
if item == nil then error("Stack underflow: " .. name) end
if item ~= NIL then return item else return nil end
end

Expand All @@ -2936,9 +2914,7 @@ end

function pick(index)
local item = stack[#stack - index]
if item == nil then
error("Stack underflow: " .. name)
end
if item == nil then error("Stack underflow: " .. name) end
if item ~= NIL then return item else return nil end
end

Expand Down Expand Up @@ -3081,7 +3057,7 @@ return utils
end
end

__VERSION__="0.1-109"
__VERSION__="0.1-110"

local Compiler = require("compiler")
local Optimizer = require("ast_optimizer")
Expand Down
2 changes: 1 addition & 1 deletion src/version/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1-109
0.1-110

0 comments on commit d6cd96f

Please sign in to comment.