Debian Codegraph @

terps/alan3/stack.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 STACK.C
4
5 Stack Handling for Alan interpreter
6
7 \*----------------------------------------------------------------------*/
8
9 #include "stack.h"
10
11 #include "types.h"
12 #include "syserr.h"
13 #include "memory.h"
14 #include "options.h"
15
16 /* ABSTRACT TYPE */
17
18 /* TODO: Maybe convert this to interpreter stack and keep this as a
19 generic auto-growing stack... */
20
21 typedef struct StackStructure {
22 Aword *stack; /* Array that can take Awords */
23 int stackSize;
24 int stackp;
25 int framePointer;
26 } StackStructure;
27
28
29 /* PRIVATE DATA */
30
31
32
33 /*======================================================================*/
34 Stack createStack(int size)
35 {
36 StackStructure *theStack = NEW(StackStructure);
37
38 theStack->stack = allocate(size*sizeof(Aptr));
39 theStack->stackSize = size;
40 theStack->framePointer = -1;
41
42 return theStack;
43 }
44
45
46 /*======================================================================*/
47 void deleteStack(Stack theStack)
48 {
49 if (theStack == NULL)
50 syserr("deleting a NULL stack");
51
52 deallocate(theStack->stack);
53 deallocate(theStack);
54 }
55
56
57 /*======================================================================*/
58 int stackDepth(Stack theStack) {
59 return theStack->stackp;
60 }
61
62
63 /*======================================================================*/
64 void dumpStack(Stack theStack)
65 {
66 int i;
67
68 if (theStack == NULL)
69 syserr("NULL stack not supported anymore");
70
71 printf("[");
72 for (i = 0; i < theStack->stackp; i++)
73 printf("%ld ", (unsigned long) theStack->stack[i]);
74 printf("]");
75 if (!traceInstructionOption && !tracePushOption)
76 printf("\n");
77 }
78
79
80 /*======================================================================*/
81 void push(Stack theStack, Aptr i)
82 {
83 if (theStack == NULL)
84 syserr("NULL stack not supported anymore");
85
86 if (theStack->stackp == theStack->stackSize)
87 syserr("Out of stack space.");
88 theStack->stack[(theStack->stackp)++] = i;
89 }
90
91
92 /*======================================================================*/
93 Aptr pop(Stack theStack)
94 {
95 if (theStack == NULL)
96 syserr("NULL stack not supported anymore");
97
98 if (theStack->stackp == 0)
99 syserr("Stack underflow.");
100 return theStack->stack[--(theStack->stackp)];
101 }
102
103
104 /*======================================================================*/
105 Aptr top(Stack theStack)
106 {
107 if (theStack == NULL)
108 syserr("NULL stack not supported anymore");
109
110 return theStack->stack[theStack->stackp-1];
111 }
112
113
114 /* The AMACHINE Block Frames */
115
116 /*======================================================================*/
117 void newFrame(Stack theStack, Aint noOfLocals)
118 {
119 int n;
120
121 if (theStack == NULL)
122 syserr("NULL stack not supported anymore");
123
124 push(theStack, theStack->framePointer);
125 theStack->framePointer = theStack->stackp;
126 for (n = 0; n < noOfLocals; n++)
127 push(theStack, 0);
128 }
129
130
131 /*======================================================================*/
132 /* Local variables are numbered 1 and up and stored on their index-1 */
133 Aptr getLocal(Stack theStack, Aint framesBelow, Aint variableNumber)
134 {
135 int frame;
136 int frameCount;
137
138 if (variableNumber < 1)
139 syserr("Reading a non-existing block-local variable.");
140
141 if (theStack == NULL)
142 syserr("NULL stack not supported anymore");
143
144 frame = theStack->framePointer;
145
146 if (framesBelow != 0)
147 for (frameCount = framesBelow; frameCount != 0; frameCount--)
148 frame = theStack->stack[frame-1];
149
150 return theStack->stack[frame + variableNumber-1];
151 }
152
153
154 /*======================================================================*/
155 void setLocal(Stack theStack, Aint framesBelow, Aint variableNumber, Aptr value)
156 {
157 int frame;
158 int frameCount;
159
160 if (variableNumber < 1)
161 syserr("Writing a non-existing block-local variable.");
162
163 if (theStack == NULL)
164 syserr("NULL stack not supported anymore");
165
166 frame = theStack->framePointer;
167 if (framesBelow != 0)
168 for (frameCount = framesBelow; frameCount != 0; frameCount--)
169 frame = theStack->stack[frame-1];
170
171 theStack->stack[frame + variableNumber-1] = value;
172 }
173
174 /*======================================================================*/
175 void endFrame(Stack theStack)
176 {
177 if (theStack == NULL)
178 syserr("NULL stack not supported anymore");
179
180 theStack->stackp = theStack->framePointer;
181 theStack->framePointer = pop(theStack);
182 }