Skip to content

Instantly share code, notes, and snippets.

@zCrxtix
Last active June 1, 2022 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zCrxtix/e8a0d83ba89d07bc2cffe76289a28b45 to your computer and use it in GitHub Desktop.
Save zCrxtix/e8a0d83ba89d07bc2cffe76289a28b45 to your computer and use it in GitHub Desktop.
A stack implementation in Roblox
local rStack = {}
rStack.__index = rStack
function rStack.new()
local self = setmetatable({}, rStack)
self.Stack = {}
return self
end
function rStack:append(value : number)
table.insert(self.Stack, value)
end
function rStack:pop()
assert(#self.Stack >= 1, "Stack underflow")
local ret = self.Stack[#self.Stack]
table.remove(self.Stack)
return ret
end
function rStack:add()
assert(#self.Stack >= 2, "Stack underflow")
self:append(self:pop()+self:pop())
end
function rStack:sub()
assert(#self.Stack >= 2, "Stack underflow")
self:append(self:pop()-self:pop())
end
function rStack:mul()
assert(#self.Stack >= 2, "Stack underflow")
self:append(self:pop()*self:pop())
end
function rStack:div()
assert(#self.Stack >= 2, "Stack underflow")
self:append(self:pop()/self:pop())
end
function rStack:print()
assert(#self.Stack >= 1, "Stack underflow")
print(self:pop())
end
return rStack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment