initial commit

This commit is contained in:
Starfflame 2021-04-30 19:54:47 -05:00
commit 77831b50d9
2 changed files with 38 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.envrc
*.nix

36
stack.tl Normal file
View File

@ -0,0 +1,36 @@
local record Stack
contents: {any}
top: number
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
function add(stack: Stack)
local a: any =stack:pop()
local b: any=stack:pop()
if a is number and b is number then
local c=a+b
stack:push(c)
else
error("invalid operands for add operation!")
end
end
function dot(stack: Stack)
print(stack:pop())
end