Última actividad 1751820751

Revisión baa1264ac82657718175b89fec69b8197c6ee4fc

prelude.lua Sin formato Playground
1local function push(self, value)
2 self.n = self.n + 1
3 table.insert(self, value)
4end
5
6local function pop(self)
7 local value = self[self.n]
8 self[self.n] = nil
9 self.n = self.n - 1
10 return value
11end
12
13local function peek(self)
14 return self[self.n]
15end
16
17local function create_stack(object, name)
18 object[name] = { n = 0 , push = push, pop = pop, peek = peek }
19end
20
21LOBBY = {}
22create_stack(LOBBY, "lobby")
23LOBBY["lobby"]:push(LOBBY)
24
25function PUSH(stack, value)
26 if not LOBBY[stack] then create_stack(LOBBY, stack) end
27 LOBBY[stack]:push(value)
28end
29
30function PEEK(stack)
31 assert(LOBBY[stack], "Stack underflow")
32 assert(LOBBY[stack].n > 0, "Stack underflow")
33 return LOBBY[stack]:peek()
34end
35
36function POP(stack)
37 assert(LOBBY[stack], "Stack underflow")
38 assert(LOBBY[stack].n > 0, "Stack underflow")
39 return LOBBY[stack]:pop()
40end
41
42function PUSHTO(object, stack, value)
43 if not object[stack] then
44 create_stack(object, stack)
45 end
46 object[stack]:push(value)
47end
48
49function POPFROM(object, stack)
50 assert(object[stack], "Stack underflow in external object.")
51 assert(object[stack].n > 0, "Stack underflow in external object.")
52 return object[stack]:pop()
53end
54
55function _become()
56 LOBBY = POP('')
57end
58
59function _me()
60 PUSH('', LOBBY)
61end
62
63function _object()
64 PUSH('', {})
65end
66