29 lines
539 B
Plaintext
29 lines
539 B
Plaintext
|
|
local type Stack = record
|
|
contents: {any}
|
|
top: number
|
|
push: function(Stack, any)
|
|
pop: function(Stack)
|
|
new: function(Stack): Stack
|
|
end
|
|
function Stack:push(val: any)
|
|
self.top = self.top + 1
|
|
table.insert(self.contents,val)
|
|
end
|
|
function Stack:pop(): any
|
|
self.top = self.top -1
|
|
if self.top < 0 then
|
|
error("Stack underflow")
|
|
end
|
|
return table.remove(self.contents)
|
|
end
|
|
local stack_mt = {__index = Stack}
|
|
function Stack:new(): Stack
|
|
return setmetatable({contents = {}, top = 0} as Stack, stack_mt)
|
|
end
|
|
|
|
|
|
-- operations
|
|
|
|
return Stack
|