1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | tos = 0
|
24 |
|
25 |
|
26 | nil_symbols = 0
|
27 | push = (p) ->
|
28 | if !p?
|
29 | debugger
|
30 | if p.isZero?
|
31 | debugger
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | if p == symbol(NIL)
|
37 | nil_symbols++
|
38 | if DEBUG then console.log "pushing symbol(NIL) #" + nil_symbols
|
39 |
|
40 |
|
41 | if (tos >= frame)
|
42 | stop("stack overflow")
|
43 | stack[tos++] = p
|
44 |
|
45 |
|
46 |
|
47 | moveTos = (stackPos) ->
|
48 | if tos <= stackPos
|
49 |
|
50 |
|
51 | tos = stackPos
|
52 | return
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | while tos > stackPos
|
59 | stack[tos] = null
|
60 | tos--
|
61 | return
|
62 |
|
63 | top = ->
|
64 | stack[tos-1]
|
65 |
|
66 | pop = ->
|
67 |
|
68 |
|
69 | if (tos == 0)
|
70 | debugger
|
71 | stop("stack underflow")
|
72 | if !stack[tos-1]?
|
73 | debugger
|
74 | elementToBeReturned = stack[--tos]
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | stack[tos] = null
|
82 |
|
83 | return elementToBeReturned
|
84 |
|
85 |
|
86 | push_frame = (n) ->
|
87 | i = 0
|
88 | frame -= n
|
89 | if (frame < tos)
|
90 | debugger
|
91 | stop("frame overflow, circular reference?")
|
92 | for i in [0...n]
|
93 | stack[frame + i] = symbol(NIL)
|
94 |
|
95 |
|
96 | pop_frame = (n) ->
|
97 | frame += n
|
98 | if (frame > TOS)
|
99 | stop("frame underflow")
|
100 |
|
101 | save = ->
|
102 | frame -= 10
|
103 | if (frame < tos)
|
104 | debugger
|
105 | stop("frame overflow, circular reference?")
|
106 | stack[frame + 0] = p0
|
107 | stack[frame + 1] = p1
|
108 | stack[frame + 2] = p2
|
109 | stack[frame + 3] = p3
|
110 | stack[frame + 4] = p4
|
111 | stack[frame + 5] = p5
|
112 | stack[frame + 6] = p6
|
113 | stack[frame + 7] = p7
|
114 | stack[frame + 8] = p8
|
115 | stack[frame + 9] = p9
|
116 |
|
117 | restore = ->
|
118 | if (frame > TOS - 10)
|
119 | stop("frame underflow")
|
120 | p0 = stack[frame + 0]
|
121 | p1 = stack[frame + 1]
|
122 | p2 = stack[frame + 2]
|
123 | p3 = stack[frame + 3]
|
124 | p4 = stack[frame + 4]
|
125 | p5 = stack[frame + 5]
|
126 | p6 = stack[frame + 6]
|
127 | p7 = stack[frame + 7]
|
128 | p8 = stack[frame + 8]
|
129 | p9 = stack[frame + 9]
|
130 | frame += 10
|
131 |
|
132 |
|
133 |
|
134 | swap = ->
|
135 |
|
136 |
|
137 | p = pop()
|
138 | q = pop()
|
139 | push(p)
|
140 | push(q)
|
141 |
|
142 |
|
143 |
|
144 | dupl = ->
|
145 |
|
146 | p = pop()
|
147 | push(p)
|
148 | push(p)
|
149 |
|
150 | $.dupl = dupl
|
151 | $.swap = swap
|
152 | $.restore = restore
|
153 | $.save = save
|
154 | $.push = push
|
155 | $.pop = pop
|