-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.lua
62 lines (57 loc) · 1.48 KB
/
stack.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
--[[
stack.lua by Koji Iigura, 2017.
Global variables.
DS : data stack.
RS : return stack.
ES : environment stack
Copyright (c) 2017 Koji Iigura
Released under the MIT license
http://opensource.org/licenses/mit-license.php
]]
local dataStack,returnStack,environmentStack={},{},{}
local makePush =function(t) return function(v) table.insert(t,v) end end
local makePop =function(inTable,inStackName)
return function()
if #inTable<=0 then
error("@: "..inStackName.." is empty.")
else
return table.remove(inTable)
end
end
end
local makeEmpty=function(t)
return function()
local k,v
for k,v in pairs(t) do t[k]=nil end
end
end
local makeReadTOS=function(t)
return function()
return t[#t]
end
end
local makeNumOfElements=function(t)
return function() return #t end
end
local makeGetBody=function(t)
return function() return t end
end
local makeStack=function(inStackBody,inStackName)
return {
Push=makePush(inStackBody),
Pop=makePop(inStackBody,inStackName),
Empty=makeEmpty(inStackBody),
ReadTOS=makeReadTOS(inStackBody),
NumOfElements=makeNumOfElements(inStackBody),
GetBody=makeGetBody(inStackBody),
}
end
DS=makeStack(dataStack, "DataStack")
RS=makeStack(returnStack, "ReturnStack")
ES=makeStack(environmentStack,"EnvironmentStack")
Push=DS.Push
Pop=DS.Pop
ReadTOS=DS.ReadTOS
NumOfElements=DS.NumOfElements
GetBody=DS.GetBody
Empty=DS.Empty