25 |
typedef stackitem* hashtbl[HASHTBLSIZE]; |
typedef stackitem* hashtbl[HASHTBLSIZE]; |
26 |
typedef void (*funcp)(stackitem**); |
typedef void (*funcp)(stackitem**); |
27 |
|
|
28 |
|
|
29 |
|
/* Initiates a newly created hash table. */ |
30 |
void init_hashtbl(hashtbl out_hash) |
void init_hashtbl(hashtbl out_hash) |
31 |
{ |
{ |
32 |
long i; |
long i; |
35 |
out_hash[i]= NULL; |
out_hash[i]= NULL; |
36 |
} |
} |
37 |
|
|
38 |
|
/* Returns a pointer to an element in the hash table. */ |
39 |
stackitem** hash(hashtbl in_hashtbl, const char* in_string) |
stackitem** hash(hashtbl in_hashtbl, const char* in_string) |
40 |
{ |
{ |
41 |
long i= 0; |
long i= 0; |
64 |
} |
} |
65 |
} |
} |
66 |
|
|
67 |
|
/* Generic push function. */ |
68 |
int push(stackitem** stack_head, stackitem* in_item) |
int push(stackitem** stack_head, stackitem* in_item) |
69 |
{ |
{ |
70 |
in_item->next= *stack_head; |
in_item->next= *stack_head; |
72 |
return 1; |
return 1; |
73 |
} |
} |
74 |
|
|
75 |
|
/* Push a value on the stack. */ |
76 |
int push_val(stackitem** stack_head, int in_val) |
int push_val(stackitem** stack_head, int in_val) |
77 |
{ |
{ |
78 |
stackitem* new_item= malloc(sizeof(stackitem)); |
stackitem* new_item= malloc(sizeof(stackitem)); |
83 |
return 1; |
return 1; |
84 |
} |
} |
85 |
|
|
86 |
|
/* Copy a string onto the stack. */ |
87 |
int push_cstring(stackitem** stack_head, const char* in_string) |
int push_cstring(stackitem** stack_head, const char* in_string) |
88 |
{ |
{ |
89 |
stackitem* new_item= malloc(sizeof(stackitem)); |
stackitem* new_item= malloc(sizeof(stackitem)); |
95 |
return 1; |
return 1; |
96 |
} |
} |
97 |
|
|
98 |
|
/* Create a new hash entry. */ |
99 |
int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id) |
int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id) |
100 |
{ |
{ |
101 |
in_item->id= malloc(strlen(id)+1); |
in_item->id= malloc(strlen(id)+1); |
106 |
return 1; |
return 1; |
107 |
} |
} |
108 |
|
|
109 |
|
/* Define a function a new function in the hash table. */ |
110 |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
111 |
{ |
{ |
112 |
stackitem* temp= malloc(sizeof(stackitem)); |
stackitem* temp= malloc(sizeof(stackitem)); |
117 |
mk_hashentry(in_hashtbl, temp, id); |
mk_hashentry(in_hashtbl, temp, id); |
118 |
} |
} |
119 |
|
|
120 |
|
/* Define a new symbol in the hash table. */ |
121 |
void def_sym(hashtbl in_hashtbl, const char* id) |
void def_sym(hashtbl in_hashtbl, const char* id) |
122 |
{ |
{ |
123 |
stackitem* temp= malloc(sizeof(stackitem)); |
stackitem* temp= malloc(sizeof(stackitem)); |
127 |
mk_hashentry(in_hashtbl, temp, id); |
mk_hashentry(in_hashtbl, temp, id); |
128 |
} |
} |
129 |
|
|
130 |
|
/* Push a reference to an entry in the hash table onto the stack. */ |
131 |
int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string) |
int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string) |
132 |
{ |
{ |
133 |
static void* handle= NULL; |
static void* handle= NULL; |
155 |
return 1; |
return 1; |
156 |
} |
} |
157 |
|
|
158 |
|
/* Discard the top element of the stack. */ |
159 |
extern void toss(stackitem** stack_head) |
extern void toss(stackitem** stack_head) |
160 |
{ |
{ |
161 |
stackitem* temp= *stack_head; |
stackitem* temp= *stack_head; |
170 |
free(temp); |
free(temp); |
171 |
} |
} |
172 |
|
|
173 |
|
/* Print newline. */ |
174 |
extern void nl() |
extern void nl() |
175 |
{ |
{ |
176 |
printf("\n"); |
printf("\n"); |
177 |
} |
} |
178 |
|
|
179 |
|
/* Prints the top element of the stack. */ |
180 |
void print_(stackitem** stack_head) |
void print_(stackitem** stack_head) |
181 |
{ |
{ |
182 |
if((*stack_head)==NULL) |
if((*stack_head)==NULL) |
199 |
} |
} |
200 |
} |
} |
201 |
|
|
202 |
|
/* Prints the top element of the stack and then discards it. */ |
203 |
extern void print(stackitem** stack_head) |
extern void print(stackitem** stack_head) |
204 |
{ |
{ |
205 |
print_(stack_head); |
print_(stack_head); |
206 |
toss(stack_head); |
toss(stack_head); |
207 |
} |
} |
208 |
|
|
209 |
/* print_stack(stack); */ |
/* Only to be called by function printstack. */ |
210 |
void print_st(stackitem* stack_head, long counter) |
void print_st(stackitem* stack_head, long counter) |
211 |
{ |
{ |
212 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
217 |
nl(); |
nl(); |
218 |
} |
} |
219 |
|
|
220 |
|
/* Prints the stack. */ |
221 |
extern void printstack(stackitem** stack_head) |
extern void printstack(stackitem** stack_head) |
222 |
{ |
{ |
223 |
if(*stack_head != NULL) { |
if(*stack_head != NULL) { |
226 |
} |
} |
227 |
} |
} |
228 |
|
|
229 |
|
/* If the top element is a reference, determine if it's a reference to a |
230 |
|
function, and if it is execute and pop the reference and execute the |
231 |
|
function. */ |
232 |
extern void eval(stackitem** stack_head) |
extern void eval(stackitem** stack_head) |
233 |
{ |
{ |
234 |
funcp in_func; |
funcp in_func; |
244 |
} |
} |
245 |
} |
} |
246 |
|
|
247 |
|
/* Parse input. */ |
248 |
int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line) |
int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line) |
249 |
{ |
{ |
250 |
char *temp, *rest; |
char *temp, *rest; |
259 |
push_cstring(stack_head, temp); |
push_cstring(stack_head, temp); |
260 |
else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1) |
else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1) |
261 |
push_val(stack_head, itemp); |
push_val(stack_head, itemp); |
262 |
else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1) |
else if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest)) >= 1) { |
263 |
|
temp[1]= '\0'; push_ref(stack_head, in_hash, temp); |
264 |
|
} else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1) |
265 |
push_ref(stack_head, in_hash, temp); |
push_ref(stack_head, in_hash, temp); |
266 |
else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1) |
else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1) |
267 |
if(*temp==';') |
if(*temp==';') |
280 |
return 1; |
return 1; |
281 |
} |
} |
282 |
|
|
283 |
|
/* Make a list. */ |
284 |
extern void pack(stackitem** stack_head) |
extern void pack(stackitem** stack_head) |
285 |
{ |
{ |
286 |
void* delimiter; |
void* delimiter; |
311 |
push(stack_head, pack); |
push(stack_head, pack); |
312 |
} |
} |
313 |
|
|
314 |
|
/* Push elements of the list on the stack. */ |
315 |
extern void expand(stackitem** stack_head) |
extern void expand(stackitem** stack_head) |
316 |
{ |
{ |
317 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
329 |
*stack_head= new_head; |
*stack_head= new_head; |
330 |
} |
} |
331 |
|
|
332 |
|
/* Swap the two top elements on the stack. */ |
333 |
extern void swap(stackitem** stack_head) |
extern void swap(stackitem** stack_head) |
334 |
{ |
{ |
335 |
stackitem* temp= (*stack_head); |
stackitem* temp= (*stack_head); |
342 |
(*stack_head)->next= temp; |
(*stack_head)->next= temp; |
343 |
} |
} |
344 |
|
|
345 |
|
/* Compares two elements by reference. */ |
346 |
extern void eq(stackitem** stack_head) |
extern void eq(stackitem** stack_head) |
347 |
{ |
{ |
348 |
void *left, *right; |
void *left, *right; |
352 |
return; |
return; |
353 |
|
|
354 |
left= (*stack_head)->content.ptr; |
left= (*stack_head)->content.ptr; |
355 |
right= (*stack_head)->next->content.ptr; |
swap(stack_head); |
356 |
|
right= (*stack_head)->content.ptr; |
357 |
result= (left==right); |
result= (left==right); |
358 |
|
|
359 |
toss(stack_head); toss(stack_head); |
toss(stack_head); toss(stack_head); |
360 |
push_val(stack_head, (left==right)); |
push_val(stack_head, (left==right)); |
361 |
} |
} |
362 |
|
|
363 |
|
/* Negates the top element on the stack. */ |
364 |
extern void not(stackitem** stack_head) |
extern void not(stackitem** stack_head) |
365 |
{ |
{ |
366 |
int value; |
int value; |
373 |
push_val(stack_head, !value); |
push_val(stack_head, !value); |
374 |
} |
} |
375 |
|
|
376 |
|
/* Compares the two top elements on the stack and return 0 if they're the |
377 |
|
same. */ |
378 |
extern void neq(stackitem** stack_head) |
extern void neq(stackitem** stack_head) |
379 |
{ |
{ |
380 |
eq(stack_head); |
eq(stack_head); |
381 |
not(stack_head); |
not(stack_head); |
382 |
} |
} |
383 |
|
|
384 |
|
/* Give a symbol some content. */ |
385 |
|
extern void def(stackitem** stack_head) |
386 |
|
{ |
387 |
|
stackitem *temp, *value; |
388 |
|
|
389 |
|
if(*stack_head==NULL || (*stack_head)->next==NULL |
390 |
|
|| (*stack_head)->type!=ref) |
391 |
|
return; |
392 |
|
|
393 |
|
temp= (*stack_head)->content.ptr; |
394 |
|
value= (*stack_head)->next; |
395 |
|
temp->content= value->content; |
396 |
|
value->content.ptr=NULL; |
397 |
|
temp->type= value->type; |
398 |
|
|
399 |
|
toss(stack_head); toss(stack_head); |
400 |
|
} |
401 |
|
|
402 |
|
/* Quit stack. */ |
403 |
extern void quit() |
extern void quit() |
404 |
{ |
{ |
405 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |