11 |
|
|
12 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 65536 |
13 |
|
|
14 |
typedef struct stack_item |
/* First, define some types. */ |
15 |
{ |
|
16 |
|
/* A value of some type */ |
17 |
|
typedef struct { |
18 |
enum { |
enum { |
19 |
value, /* Integer */ |
integer, |
20 |
string, |
string, |
|
ref, /* Reference (to an element in the |
|
|
hash table) */ |
|
21 |
func, /* Function pointer */ |
func, /* Function pointer */ |
22 |
symbol, |
symb, |
23 |
list |
list |
24 |
} type; /* Type of stack element */ |
} type; /* Type of stack element */ |
25 |
|
|
26 |
union { |
union { |
27 |
void* ptr; /* Pointer to the content */ |
void *ptr; /* Pointer to the content */ |
28 |
int val; /* ...or an integer */ |
int val; /* ...or an integer */ |
29 |
} content; /* Stores a pointer or an integer */ |
} content; /* Stores a pointer or an integer */ |
30 |
|
|
31 |
char* id; /* Symbol name */ |
int refcount; /* Reference counter */ |
32 |
struct stack_item* next; /* Next element */ |
|
33 |
} stackitem; |
} value; |
34 |
|
|
35 |
|
/* A symbol with a name and possible value */ |
36 |
|
/* (These do not need reference counters, they are kept unique by |
37 |
|
hashing.) */ |
38 |
|
typedef struct symbol_struct { |
39 |
|
char *id; /* Symbol name */ |
40 |
|
value *val; /* The value (if any) bound to it */ |
41 |
|
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
42 |
|
} symbol; /* symbol is a kind of stack item. */ |
43 |
|
|
44 |
|
/* A type for a hash table for symbols */ |
45 |
|
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
46 |
|
|
47 |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
/* An item (value) on a stack */ |
48 |
typedef void (*funcp)(stackitem**); /* funcp is a pointer to a |
typedef struct stackitem_struct |
49 |
void function (stackitem **) */ |
{ |
50 |
|
value *item; /* The value on the stack */ |
51 |
|
struct stackitem_struct *next; /* Next item */ |
52 |
|
} stackitem; |
53 |
|
|
54 |
|
/* An environment; gives access to the stack and a hash table of |
55 |
|
defined symbols */ |
56 |
|
typedef struct { |
57 |
|
stackitem *head; /* Head of the stack */ |
58 |
|
hashtbl symbols; /* Hash table of all variable bindings */ |
59 |
|
int err; /* Error flag */ |
60 |
|
} environment; |
61 |
|
|
62 |
|
/* A type for pointers to external functions */ |
63 |
|
typedef void (*funcp)(environment *); /* funcp is a pointer to a void |
64 |
|
function (environment *) */ |
65 |
|
|
66 |
/* Initialize a newly created hash table. */ |
/* Initialize a newly created environment */ |
67 |
void init_hashtbl(hashtbl out_hash) |
void init_env(environment *env) |
68 |
{ |
{ |
69 |
long i; |
long i; |
70 |
|
|
71 |
|
env->err=0; |
72 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
73 |
out_hash[i]= NULL; |
env->symbols[i]= NULL; |
74 |
} |
} |
75 |
|
|
76 |
/* Returns a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
77 |
stackitem** hash(hashtbl in_hashtbl, const char* in_string) |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
78 |
{ |
{ |
79 |
long i= 0; |
long i= 0; |
80 |
unsigned long out_hash= 0; |
unsigned long out_hash= 0; |
81 |
char key= '\0'; |
char key= '\0'; |
82 |
stackitem** position; |
symbol **position; |
83 |
|
|
84 |
while(1){ /* Hash in_string */ |
while(1){ /* Hash in_string */ |
85 |
key= in_string[i++]; |
key= in_string[i++]; |
103 |
} |
} |
104 |
|
|
105 |
/* Generic push function. */ |
/* Generic push function. */ |
106 |
int push(stackitem** stack_head, stackitem* in_item) |
void push(stackitem** stack_head, stackitem* in_item) |
107 |
{ |
{ |
108 |
in_item->next= *stack_head; |
in_item->next= *stack_head; |
109 |
*stack_head= in_item; |
*stack_head= in_item; |
|
return 1; |
|
110 |
} |
} |
111 |
|
|
112 |
/* Push a value on the stack. */ |
/* Push a value onto the stack */ |
113 |
int push_val(stackitem** stack_head, int in_val) |
void push_val(stackitem **stack_head, value *val) |
114 |
{ |
{ |
115 |
stackitem* new_item= malloc(sizeof(stackitem)); |
stackitem *new_item= malloc(sizeof(stackitem)); |
116 |
assert(new_item != NULL); |
new_item->item= val; |
117 |
new_item->content.val= in_val; |
val->refcount++; |
|
new_item->type= value; |
|
|
|
|
118 |
push(stack_head, new_item); |
push(stack_head, new_item); |
|
return 1; |
|
119 |
} |
} |
120 |
|
|
121 |
/* Copy a string onto the stack. */ |
/* Push an integer onto the stack. */ |
122 |
int push_cstring(stackitem** stack_head, const char* in_string) |
void push_int(stackitem **stack_head, int in_val) |
123 |
{ |
{ |
124 |
stackitem* new_item= malloc(sizeof(stackitem)); |
value *new_value= malloc(sizeof(value)); |
125 |
new_item->content.ptr= malloc(strlen(in_string)+1); |
stackitem *new_item= malloc(sizeof(stackitem)); |
126 |
strcpy(new_item->content.ptr, in_string); |
new_item->item= new_value; |
127 |
new_item->type= string; |
|
128 |
|
new_value->content.val= in_val; |
129 |
|
new_value->type= integer; |
130 |
|
new_value->refcount=1; |
131 |
|
|
132 |
push(stack_head, new_item); |
push(stack_head, new_item); |
|
return 1; |
|
|
} |
|
|
|
|
|
/* Create a new hash entry. */ |
|
|
int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id) |
|
|
{ |
|
|
in_item->id= malloc(strlen(id)+1); |
|
|
|
|
|
strcpy(in_item->id, id); |
|
|
push(hash(in_hashtbl, id), in_item); |
|
|
|
|
|
return 1; |
|
133 |
} |
} |
134 |
|
|
135 |
/* Define a new function in the hash table. */ |
/* Copy a string onto the stack. */ |
136 |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
void push_cstring(stackitem **stack_head, const char *in_string) |
137 |
{ |
{ |
138 |
stackitem* temp= malloc(sizeof(stackitem)); |
value *new_value= malloc(sizeof(value)); |
139 |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
140 |
temp->type= func; |
new_item->item=new_value; |
141 |
temp->content.ptr= in_func; |
|
142 |
|
new_value->content.ptr= malloc(strlen(in_string)+1); |
143 |
mk_hashentry(in_hashtbl, temp, id); |
strcpy(new_value->content.ptr, in_string); |
144 |
} |
new_value->type= string; |
145 |
|
new_value->refcount=1; |
146 |
|
|
147 |
/* Define a new symbol in the hash table. */ |
push(stack_head, new_item); |
|
void def_sym(hashtbl in_hashtbl, const char* id) |
|
|
{ |
|
|
stackitem* temp= malloc(sizeof(stackitem)); |
|
|
|
|
|
temp->type= symbol; |
|
|
mk_hashentry(in_hashtbl, temp, id); |
|
148 |
} |
} |
149 |
|
|
150 |
/* Push a reference to an entry in the hash table onto the stack. */ |
/* Push a symbol onto the stack. */ |
151 |
int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string) |
void push_sym(environment *env, const char *in_string) |
152 |
{ |
{ |
153 |
static void* handle= NULL; |
stackitem *new_item; /* The new stack item */ |
154 |
void* symbol; |
/* ...which will contain... */ |
155 |
|
value *new_value; /* A new symbol value */ |
156 |
|
/* ...which might point to... */ |
157 |
|
symbol **new_symbol; /* (if needed) A new actual symbol */ |
158 |
|
/* ...which, if possible, will be bound to... */ |
159 |
|
value *new_fvalue; /* (if needed) A new function value */ |
160 |
|
/* ...which will point to... */ |
161 |
|
void *funcptr; /* A function pointer */ |
162 |
|
|
163 |
|
static void *handle= NULL; /* Dynamic linker handle */ |
164 |
|
|
165 |
|
/* Create a new stack item containing a new value */ |
166 |
|
new_item= malloc(sizeof(stackitem)); |
167 |
|
new_value= malloc(sizeof(value)); |
168 |
|
new_item->item=new_value; |
169 |
|
|
170 |
|
/* The new value is a symbol */ |
171 |
|
new_value->type= symb; |
172 |
|
new_value->refcount= 1; |
173 |
|
|
174 |
|
/* Look up the symbol name in the hash table */ |
175 |
|
new_symbol= hash(env->symbols, in_string); |
176 |
|
new_value->content.ptr= *new_symbol; |
177 |
|
|
178 |
|
if(*new_symbol==NULL) { /* If symbol was undefined */ |
179 |
|
|
180 |
|
/* Create a new symbol */ |
181 |
|
(*new_symbol)= malloc(sizeof(symbol)); |
182 |
|
(*new_symbol)->val= NULL; /* undefined value */ |
183 |
|
(*new_symbol)->next= NULL; |
184 |
|
(*new_symbol)->id= malloc(strlen(in_string)+1); |
185 |
|
strcpy((*new_symbol)->id, in_string); |
186 |
|
|
187 |
stackitem* new_item= malloc(sizeof(stackitem)); |
/* Intern the new symbol in the hash table */ |
188 |
new_item->content.ptr= *hash(in_hash, in_string); |
new_value->content.ptr= *new_symbol; |
|
new_item->type= ref; |
|
189 |
|
|
190 |
if(new_item->content.ptr==NULL) { /* If hash entry empty */ |
/* Try to load the symbol name as an external function, to see if |
191 |
|
we should bind the symbol to a new function pointer value */ |
192 |
if(handle==NULL) /* If no handle */ |
if(handle==NULL) /* If no handle */ |
193 |
handle= dlopen(NULL, RTLD_LAZY); |
handle= dlopen(NULL, RTLD_LAZY); |
194 |
|
|
195 |
symbol= dlsym(handle, in_string); /* Get function pointer */ |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
196 |
if(dlerror()==NULL) /* If existing function pointer */ |
if(dlerror()==NULL) { /* If a function was found */ |
197 |
def_func(in_hash, symbol, in_string); /* Store function pointer */ |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
198 |
else |
new_fvalue->type=func; /* The new value is a function pointer */ |
199 |
def_sym(in_hash, in_string); /* Make symbol */ |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
200 |
|
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
201 |
new_item->content.ptr= *hash(in_hash, in_string); /* The new reference |
function value */ |
202 |
shouldn't point at |
new_fvalue->refcount= 1; |
203 |
NULL */ |
} |
|
new_item->type= ref; |
|
204 |
} |
} |
205 |
|
push(&(env->head), new_item); |
|
push(stack_head, new_item); |
|
|
return 1; |
|
206 |
} |
} |
207 |
|
|
208 |
void printerr(const char* in_string) { |
void printerr(const char* in_string) { |
209 |
fprintf(stderr, "Err: %s\n", in_string); |
fprintf(stderr, "Err: %s\n", in_string); |
210 |
} |
} |
211 |
|
|
212 |
|
/* Throw away a value */ |
213 |
|
void free_val(value *val){ |
214 |
|
stackitem *item, *temp; |
215 |
|
|
216 |
|
val->refcount--; /* Decrease the reference count */ |
217 |
|
if(val->refcount == 0){ |
218 |
|
switch (val->type){ /* and free the contents if necessary */ |
219 |
|
case string: |
220 |
|
free(val->content.ptr); |
221 |
|
break; |
222 |
|
case list: /* lists needs to be freed recursively */ |
223 |
|
item=val->content.ptr; |
224 |
|
while(item != NULL) { /* for all stack items */ |
225 |
|
free_val(item->item); /* free the value */ |
226 |
|
temp=item->next; /* save next ptr */ |
227 |
|
free(item); /* free the stackitem */ |
228 |
|
item=temp; /* go to next stackitem */ |
229 |
|
} |
230 |
|
free(val); /* Free the actual list value */ |
231 |
|
break; |
232 |
|
default: |
233 |
|
break; |
234 |
|
} |
235 |
|
} |
236 |
|
} |
237 |
|
|
238 |
/* Discard the top element of the stack. */ |
/* Discard the top element of the stack. */ |
239 |
extern void toss(stackitem** stack_head) |
extern void toss(environment *env) |
240 |
{ |
{ |
241 |
stackitem* temp= *stack_head; |
stackitem *temp= env->head; |
242 |
|
|
243 |
if((*stack_head)==NULL) { |
if((env->head)==NULL) { |
244 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
245 |
|
env->err=1; |
246 |
return; |
return; |
247 |
} |
} |
248 |
|
|
249 |
if((*stack_head)->type==string) |
free_val(env->head->item); /* Free the value */ |
250 |
free((*stack_head)->content.ptr); |
env->head= env->head->next; /* Remove the top stack item */ |
251 |
|
free(temp); /* Free the old top stack item */ |
|
*stack_head= (*stack_head)->next; |
|
|
free(temp); |
|
252 |
} |
} |
253 |
|
|
254 |
/* Print newline. */ |
/* Print newline. */ |
257 |
printf("\n"); |
printf("\n"); |
258 |
} |
} |
259 |
|
|
260 |
/* Prints the top element of the stack. */ |
/* Gets the type of a value */ |
261 |
extern void print_(stackitem** stack_head) |
extern void type(environment *env){ |
262 |
{ |
int typenum; |
263 |
stackitem* temp= *stack_head; |
|
264 |
|
if((env->head)==NULL) { |
265 |
if(temp==NULL) { |
printerr("Too Few Arguments"); |
266 |
printerr("Stack empty"); |
env->err=1; |
267 |
return; |
return; |
268 |
} |
} |
269 |
|
typenum=env->head->item->type; |
270 |
|
toss(env); |
271 |
|
switch(typenum){ |
272 |
|
case integer: |
273 |
|
push_sym(env, "integer"); |
274 |
|
break; |
275 |
|
case string: |
276 |
|
push_sym(env, "string"); |
277 |
|
break; |
278 |
|
case symb: |
279 |
|
push_sym(env, "symbol"); |
280 |
|
break; |
281 |
|
case func: |
282 |
|
push_sym(env, "function"); |
283 |
|
break; |
284 |
|
case list: |
285 |
|
push_sym(env, "list"); |
286 |
|
break; |
287 |
|
default: |
288 |
|
push_sym(env, "unknown"); |
289 |
|
break; |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
while(temp->type==ref) |
/* Prints the top element of the stack. */ |
294 |
temp= temp->content.ptr; |
void print_h(stackitem *stack_head) |
295 |
|
{ |
296 |
switch(temp->type) { |
switch(stack_head->item->type) { |
297 |
case value: |
case integer: |
298 |
printf("%d", temp->content.val); |
printf("%d", stack_head->item->content.val); |
299 |
break; |
break; |
300 |
case string: |
case string: |
301 |
printf("\"%s\"", (char*)temp->content.ptr); |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
302 |
|
break; |
303 |
|
case symb: |
304 |
|
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
305 |
break; |
break; |
|
case symbol: |
|
306 |
case func: |
case func: |
307 |
printf("%s", temp->id); |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
308 |
|
break; |
309 |
|
case list: |
310 |
|
/* A list is just a stack, so make stack_head point to it */ |
311 |
|
stack_head=(stackitem *)(stack_head->item->content.ptr); |
312 |
|
printf("[ "); |
313 |
|
while(stack_head != NULL) { |
314 |
|
print_h(stack_head); |
315 |
|
printf(" "); |
316 |
|
stack_head=stack_head->next; |
317 |
|
} |
318 |
|
printf("]"); |
319 |
break; |
break; |
320 |
default: |
default: |
321 |
printf("%p", temp->content.ptr); |
printf("#<unknown %p>", (stack_head->item->content.ptr)); |
322 |
break; |
break; |
323 |
} |
} |
324 |
} |
} |
325 |
|
|
326 |
|
extern void print_(environment *env) { |
327 |
|
if(env->head==NULL) { |
328 |
|
printerr("Too Few Arguments"); |
329 |
|
env->err=1; |
330 |
|
return; |
331 |
|
} |
332 |
|
print_h(env->head); |
333 |
|
} |
334 |
|
|
335 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
336 |
extern void print(stackitem** stack_head) |
extern void print(environment *env) |
337 |
{ |
{ |
338 |
print_(stack_head); |
print_(env); |
339 |
toss(stack_head); |
if(env->err) return; |
340 |
|
toss(env); |
341 |
} |
} |
342 |
|
|
343 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
344 |
void print_st(stackitem* stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
345 |
{ |
{ |
346 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
347 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
|
|
|
348 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
349 |
print_(&stack_head); |
print_h(stack_head); |
350 |
nl(); |
nl(); |
351 |
} |
} |
352 |
|
|
353 |
|
|
354 |
|
|
355 |
/* Prints the stack. */ |
/* Prints the stack. */ |
356 |
extern void printstack(stackitem** stack_head) |
extern void printstack(environment *env) |
357 |
{ |
{ |
358 |
if(*stack_head != NULL) { |
if(env->head == NULL) { |
359 |
print_st(*stack_head, 1); |
return; |
|
nl(); |
|
|
} else { |
|
|
printerr("Stack empty"); |
|
360 |
} |
} |
361 |
|
print_st(env->head, 1); |
362 |
|
nl(); |
363 |
} |
} |
364 |
|
|
365 |
/* If the top element is a reference, determine if it's a reference to a |
/* Swap the two top elements on the stack. */ |
366 |
function, and if it is, toss the reference and execute the function. */ |
extern void swap(environment *env) |
|
extern void eval(stackitem** stack_head) |
|
367 |
{ |
{ |
368 |
funcp in_func; |
stackitem *temp= env->head; |
369 |
stackitem* temp= *stack_head; |
|
370 |
|
if((env->head)==NULL) { |
371 |
|
printerr("Too Few Arguments"); |
372 |
|
env->err=1; |
373 |
|
return; |
374 |
|
} |
375 |
|
|
376 |
|
if(env->head->next==NULL) { |
377 |
|
printerr("Too Few Arguments"); |
378 |
|
env->err=1; |
379 |
|
return; |
380 |
|
} |
381 |
|
|
382 |
|
env->head= env->head->next; |
383 |
|
temp->next= env->head->next; |
384 |
|
env->head->next= temp; |
385 |
|
} |
386 |
|
|
387 |
|
stackitem* copy(stackitem* in_item) |
388 |
|
{ |
389 |
|
stackitem *out_item= malloc(sizeof(stackitem)); |
390 |
|
|
391 |
|
memcpy(out_item, in_item, sizeof(stackitem)); |
392 |
|
out_item->next= NULL; |
393 |
|
|
394 |
if(temp==NULL) { |
return out_item; |
395 |
printerr("Stack empty"); |
} |
396 |
|
|
397 |
|
/* Recall a value from a symbol, if bound */ |
398 |
|
extern void rcl(environment *env) |
399 |
|
{ |
400 |
|
value *val; |
401 |
|
|
402 |
|
if(env->head == NULL) { |
403 |
|
printerr("Too Few Arguments"); |
404 |
|
env->err=1; |
405 |
return; |
return; |
406 |
} |
} |
407 |
|
|
408 |
while(temp->type==ref) |
if(env->head->item->type!=symb) { |
409 |
temp= temp->content.ptr; |
printerr("Bad Argument Type"); |
410 |
|
env->err=2; |
411 |
|
return; |
412 |
|
} |
413 |
|
|
414 |
if(temp->type==func) { |
val=((symbol *)(env->head->item->content.ptr))->val; |
415 |
in_func= (funcp)(temp->content.ptr); |
if(val == NULL){ |
416 |
toss(stack_head); |
printerr("Unbound Variable"); |
417 |
(*in_func)(stack_head); |
env->err=3; |
418 |
return; |
return; |
419 |
} |
} |
420 |
|
toss(env); /* toss the symbol */ |
421 |
printerr("Couldn't evaluate"); |
if(env->err) return; |
422 |
|
push_val(&(env->head), val); /* Return its bound value */ |
423 |
|
} |
424 |
|
|
425 |
|
/* If the top element is a symbol, determine if it's bound to a |
426 |
|
function value, and if it is, toss the symbol and execute the |
427 |
|
function. */ |
428 |
|
extern void eval(environment *env) |
429 |
|
{ |
430 |
|
funcp in_func; |
431 |
|
if(env->head==NULL) { |
432 |
|
printerr("Too Few Arguments"); |
433 |
|
env->err=1; |
434 |
|
return; |
435 |
|
} |
436 |
|
|
437 |
|
/* if it's a symbol */ |
438 |
|
if(env->head->item->type==symb) { |
439 |
|
|
440 |
|
rcl(env); /* get its contents */ |
441 |
|
if(env->err) return; |
442 |
|
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
443 |
|
eval(env); /* evaluate the value */ |
444 |
|
return; |
445 |
|
} |
446 |
|
} |
447 |
|
|
448 |
|
/* If it's a lone function value, run it */ |
449 |
|
if(env->head->item->type==func) { |
450 |
|
in_func= (funcp)(env->head->item->content.ptr); |
451 |
|
toss(env); |
452 |
|
if(env->err) return; |
453 |
|
(*in_func)(env); |
454 |
|
} |
455 |
|
} |
456 |
|
|
457 |
|
/* Reverse a list */ |
458 |
|
extern void rev(environment *env){ |
459 |
|
stackitem *old_head, *new_head, *item; |
460 |
|
|
461 |
|
if((env->head)==NULL) { |
462 |
|
printerr("Too Few Arguments"); |
463 |
|
env->err=1; |
464 |
|
return; |
465 |
|
} |
466 |
|
|
467 |
|
if(env->head->item->type!=list) { |
468 |
|
printerr("Bad Argument Type"); |
469 |
|
env->err=2; |
470 |
|
return; |
471 |
|
} |
472 |
|
|
473 |
|
old_head=(stackitem *)(env->head->item->content.ptr); |
474 |
|
new_head=NULL; |
475 |
|
while(old_head != NULL){ |
476 |
|
item=old_head; |
477 |
|
old_head=old_head->next; |
478 |
|
item->next=new_head; |
479 |
|
new_head=item; |
480 |
|
} |
481 |
|
env->head->item->content.ptr=new_head; |
482 |
} |
} |
483 |
|
|
484 |
/* Make a list. */ |
/* Make a list. */ |
485 |
extern void pack(stackitem** stack_head) |
extern void pack(environment *env) |
486 |
{ |
{ |
487 |
void* delimiter; |
void* delimiter; |
488 |
stackitem *iterator, *temp, *pack; |
stackitem *iterator, *temp; |
489 |
|
value *pack; |
490 |
|
|
491 |
delimiter= (*stack_head)->content.ptr; /* Get delimiter */ |
delimiter= env->head->item->content.ptr; /* Get delimiter */ |
492 |
toss(stack_head); |
toss(env); |
493 |
|
|
494 |
iterator= *stack_head; |
iterator= env->head; |
495 |
|
|
496 |
if(iterator==NULL || iterator->content.ptr==delimiter) { |
if(iterator==NULL || iterator->item->content.ptr==delimiter) { |
497 |
temp= NULL; |
temp= NULL; |
498 |
toss(stack_head); |
toss(env); |
499 |
} else { |
} else { |
500 |
/* Search for first delimiter */ |
/* Search for first delimiter */ |
501 |
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
while(iterator->next!=NULL |
502 |
|
&& iterator->next->item->content.ptr!=delimiter) |
503 |
iterator= iterator->next; |
iterator= iterator->next; |
504 |
|
|
505 |
/* Extract list */ |
/* Extract list */ |
506 |
temp= *stack_head; |
temp= env->head; |
507 |
*stack_head= iterator->next; |
env->head= iterator->next; |
508 |
iterator->next= NULL; |
iterator->next= NULL; |
509 |
|
|
510 |
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
if(env->head!=NULL) |
511 |
toss(stack_head); |
toss(env); |
512 |
} |
} |
513 |
|
|
514 |
/* Push list */ |
/* Push list */ |
515 |
pack= malloc(sizeof(stackitem)); |
pack= malloc(sizeof(value)); |
516 |
pack->type= list; |
pack->type= list; |
517 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
518 |
|
pack->refcount= 1; |
519 |
|
|
520 |
push(stack_head, pack); |
temp= malloc(sizeof(stackitem)); |
521 |
|
temp->item= pack; |
522 |
|
|
523 |
|
push(&(env->head), temp); |
524 |
|
rev(env); |
525 |
} |
} |
526 |
|
|
527 |
/* Parse input. */ |
/* Parse input. */ |
528 |
int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line) |
void stack_read(environment *env, char *in_line) |
529 |
{ |
{ |
530 |
char *temp, *rest; |
char *temp, *rest; |
531 |
int itemp; |
int itemp; |
539 |
do { |
do { |
540 |
/* If string */ |
/* If string */ |
541 |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
542 |
push_cstring(stack_head, temp); |
push_cstring(&(env->head), temp); |
543 |
break; |
break; |
544 |
} |
} |
545 |
/* If value */ |
/* If integer */ |
546 |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
547 |
push_val(stack_head, itemp); |
push_int(&(env->head), itemp); |
548 |
break; |
break; |
549 |
} |
} |
550 |
/* Escape ';' with '\' */ |
/* Escape ';' with '\' */ |
551 |
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
552 |
temp[1]= '\0'; |
temp[1]= '\0'; |
553 |
push_ref(stack_head, in_hash, temp); |
push_sym(env, temp); |
554 |
break; |
break; |
555 |
} |
} |
556 |
/* If symbol */ |
/* If symbol */ |
557 |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
558 |
push_ref(stack_head, in_hash, temp); |
push_sym(env, temp); |
559 |
break; |
break; |
560 |
} |
} |
561 |
/* If single char */ |
/* If single char */ |
562 |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
563 |
if(*temp==';') { |
if(*temp==';') { |
564 |
if(!non_eval_flag) { |
if(!non_eval_flag) { |
565 |
eval(stack_head); /* Evaluate top element */ |
eval(env); /* Evaluate top element */ |
566 |
break; |
break; |
567 |
} |
} |
568 |
|
|
569 |
push_ref(stack_head, in_hash, ";"); |
push_sym(env, ";"); |
570 |
break; |
break; |
571 |
} |
} |
572 |
|
|
573 |
if(*temp==']') { |
if(*temp==']') { |
574 |
push_ref(stack_head, in_hash, "["); |
push_sym(env, "["); |
575 |
pack(stack_head); |
pack(env); |
576 |
if(non_eval_flag!=0) |
if(non_eval_flag!=0) |
577 |
non_eval_flag--; |
non_eval_flag--; |
578 |
break; |
break; |
579 |
} |
} |
580 |
|
|
581 |
if(*temp=='[') { |
if(*temp=='[') { |
582 |
push_ref(stack_head, in_hash, "["); |
push_sym(env, "["); |
583 |
non_eval_flag++; |
non_eval_flag++; |
584 |
break; |
break; |
585 |
} |
} |
586 |
} |
} |
587 |
} while(0); |
} while(0); |
588 |
|
|
|
|
|
589 |
free(temp); |
free(temp); |
590 |
|
|
591 |
if(convert<2) { |
if(convert<2) { |
592 |
free(rest); |
free(rest); |
593 |
return 0; |
return; |
594 |
} |
} |
595 |
|
|
596 |
stack_read(stack_head, in_hash, rest); |
stack_read(env, rest); |
597 |
|
|
598 |
free(rest); |
free(rest); |
|
return 1; |
|
599 |
} |
} |
600 |
|
|
601 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
602 |
extern void expand(stackitem** stack_head) |
extern void expand(environment *env) |
603 |
{ |
{ |
604 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
605 |
|
|
606 |
/* Is top element a list? */ |
/* Is top element a list? */ |
607 |
if((*stack_head)==NULL || (*stack_head)->type!=list) { |
if(env->head==NULL) { |
608 |
printerr("Stack empty or not a list"); |
printerr("Too Few Arguments"); |
609 |
|
env->err=1; |
610 |
|
return; |
611 |
|
} |
612 |
|
if(env->head->item->type!=list) { |
613 |
|
printerr("Bad Argument Type"); |
614 |
|
env->err=2; |
615 |
return; |
return; |
616 |
} |
} |
617 |
|
|
618 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
619 |
new_head= temp= (*stack_head)->content.ptr; |
new_head= temp= env->head->item->content.ptr; |
|
toss(stack_head); |
|
620 |
|
|
621 |
if(temp==NULL) |
env->head->item->refcount++; |
622 |
return; |
toss(env); |
623 |
|
|
624 |
/* Search the end of the list */ |
/* Find the end of the list */ |
625 |
while(temp->next!=NULL) |
while(temp->next!=NULL) |
626 |
temp= temp->next; |
temp= temp->next; |
627 |
|
|
628 |
/* Connect the the tail of the list with the old stack head */ |
/* Connect the tail of the list with the old stack head */ |
629 |
temp->next= *stack_head; |
temp->next= env->head; |
630 |
*stack_head= new_head; /* ...and voila! */ |
env->head= new_head; /* ...and voila! */ |
|
} |
|
|
|
|
|
/* Swap the two top elements on the stack. */ |
|
|
extern void swap(stackitem** stack_head) |
|
|
{ |
|
|
stackitem* temp= (*stack_head); |
|
|
|
|
|
if((*stack_head)==NULL) { |
|
|
printerr("Stack empty"); |
|
|
return; |
|
|
} |
|
|
|
|
|
if((*stack_head)->next==NULL) |
|
|
return; |
|
631 |
|
|
632 |
*stack_head= (*stack_head)->next; |
} |
|
temp->next= (*stack_head)->next; |
|
|
(*stack_head)->next= temp; |
|
|
} |
|
633 |
|
|
634 |
/* Compares two elements by reference. */ |
/* Compares two elements by reference. */ |
635 |
extern void eq(stackitem** stack_head) |
extern void eq(environment *env) |
636 |
{ |
{ |
637 |
void *left, *right; |
void *left, *right; |
638 |
int result; |
int result; |
639 |
|
|
640 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
641 |
printerr("Not enough elements to compare"); |
printerr("Too Few Arguments"); |
642 |
|
env->err=1; |
643 |
return; |
return; |
644 |
} |
} |
645 |
|
|
646 |
left= (*stack_head)->content.ptr; |
left= env->head->item->content.ptr; |
647 |
swap(stack_head); |
swap(env); |
648 |
right= (*stack_head)->content.ptr; |
right= env->head->item->content.ptr; |
649 |
result= (left==right); |
result= (left==right); |
650 |
|
|
651 |
toss(stack_head); toss(stack_head); |
toss(env); toss(env); |
652 |
push_val(stack_head, (left==right)); |
push_int(&(env->head), result); |
653 |
} |
} |
654 |
|
|
655 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
656 |
extern void not(stackitem** stack_head) |
extern void not(environment *env) |
657 |
{ |
{ |
658 |
int value; |
int val; |
659 |
|
|
660 |
|
if((env->head)==NULL) { |
661 |
|
printerr("Too Few Arguments"); |
662 |
|
env->err=1; |
663 |
|
return; |
664 |
|
} |
665 |
|
|
666 |
if((*stack_head)==NULL || (*stack_head)->type!=value) { |
if(env->head->item->type!=integer) { |
667 |
printerr("Stack empty or element is not a value"); |
printerr("Bad Argument Type"); |
668 |
|
env->err=2; |
669 |
return; |
return; |
670 |
} |
} |
671 |
|
|
672 |
value= (*stack_head)->content.val; |
val= env->head->item->content.val; |
673 |
toss(stack_head); |
toss(env); |
674 |
push_val(stack_head, !value); |
push_int(&(env->head), !val); |
675 |
} |
} |
676 |
|
|
677 |
/* Compares the two top elements on the stack and return 0 if they're the |
/* Compares the two top elements on the stack and return 0 if they're the |
678 |
same. */ |
same. */ |
679 |
extern void neq(stackitem** stack_head) |
extern void neq(environment *env) |
680 |
{ |
{ |
681 |
eq(stack_head); |
eq(env); |
682 |
not(stack_head); |
not(env); |
683 |
} |
} |
684 |
|
|
685 |
/* Give a symbol some content. */ |
/* Give a symbol some content. */ |
686 |
extern void def(stackitem** stack_head) |
extern void def(environment *env) |
687 |
{ |
{ |
688 |
stackitem *temp, *value; |
symbol *sym; |
689 |
|
|
690 |
if(*stack_head==NULL || (*stack_head)->next==NULL |
/* Needs two values on the stack, the top one must be a symbol */ |
691 |
|| (*stack_head)->type!=ref) { |
if(env->head==NULL || env->head->next==NULL) { |
692 |
printerr("Define what?"); |
printerr("Too Few Arguments"); |
693 |
|
env->err=1; |
694 |
return; |
return; |
695 |
} |
} |
696 |
|
|
697 |
temp= (*stack_head)->content.ptr; |
if(env->head->item->type!=symb) { |
698 |
value= (*stack_head)->next; |
printerr("Bad Argument Type"); |
699 |
temp->content= value->content; |
env->err=2; |
700 |
value->content.ptr=NULL; |
return; |
701 |
temp->type= value->type; |
} |
702 |
|
|
703 |
|
/* long names are a pain */ |
704 |
|
sym=env->head->item->content.ptr; |
705 |
|
|
706 |
|
/* if the symbol was bound to something else, throw it away */ |
707 |
|
if(sym->val != NULL) |
708 |
|
free_val(sym->val); |
709 |
|
|
710 |
toss(stack_head); toss(stack_head); |
/* Bind the symbol to the value */ |
711 |
|
sym->val= env->head->next->item; |
712 |
|
sym->val->refcount++; /* Increase the reference counter */ |
713 |
|
|
714 |
|
toss(env); toss(env); |
715 |
} |
} |
716 |
|
|
717 |
/* Quit stack. */ |
/* Quit stack. */ |
718 |
extern void quit() |
extern void quit(environment *env) |
719 |
{ |
{ |
720 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
721 |
} |
} |
722 |
|
|
723 |
/* Clear stack */ |
/* Clear stack */ |
724 |
extern void clear(stackitem** stack_head) |
extern void clear(environment *env) |
725 |
{ |
{ |
726 |
while(*stack_head!=NULL) |
while(env->head!=NULL) |
727 |
toss(stack_head); |
toss(env); |
728 |
|
} |
729 |
|
|
730 |
|
/* List all defined words */ |
731 |
|
extern void words(environment *env) |
732 |
|
{ |
733 |
|
symbol *temp; |
734 |
|
int i; |
735 |
|
|
736 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
737 |
|
temp= env->symbols[i]; |
738 |
|
while(temp!=NULL) { |
739 |
|
printf("%s\n", temp->id); |
740 |
|
temp= temp->next; |
741 |
|
} |
742 |
|
} |
743 |
|
} |
744 |
|
|
745 |
|
/* Forgets a symbol (remove it from the hash table) */ |
746 |
|
extern void forget(environment *env) |
747 |
|
{ |
748 |
|
char* sym_id; |
749 |
|
stackitem *stack_head= env->head; |
750 |
|
symbol **hash_entry, *temp; |
751 |
|
|
752 |
|
if(stack_head==NULL) { |
753 |
|
printerr("Too Few Arguments"); |
754 |
|
env->err=1; |
755 |
|
return; |
756 |
|
} |
757 |
|
|
758 |
|
if(stack_head->item->type!=symb) { |
759 |
|
printerr("Bad Argument Type"); |
760 |
|
env->err=2; |
761 |
|
return; |
762 |
|
} |
763 |
|
|
764 |
|
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
765 |
|
toss(env); |
766 |
|
|
767 |
|
hash_entry= hash(env->symbols, sym_id); |
768 |
|
temp= *hash_entry; |
769 |
|
*hash_entry= (*hash_entry)->next; |
770 |
|
|
771 |
|
if(temp->val!=NULL) { |
772 |
|
free_val(temp->val); |
773 |
|
} |
774 |
|
free(temp->id); |
775 |
|
free(temp); |
776 |
|
} |
777 |
|
|
778 |
|
/* Returns the current error number to the stack */ |
779 |
|
extern void errn(environment *env){ |
780 |
|
push_int(&(env->head), env->err); |
781 |
} |
} |
782 |
|
|
783 |
int main() |
int main() |
784 |
{ |
{ |
785 |
stackitem* s= NULL; |
environment myenv; |
|
hashtbl myhash; |
|
786 |
char in_string[100]; |
char in_string[100]; |
787 |
|
|
788 |
init_hashtbl(myhash); |
init_env(&myenv); |
789 |
|
|
790 |
printf("okidok\n "); |
printf("okidok\n "); |
791 |
|
|
792 |
while(fgets(in_string, 100, stdin) != NULL) { |
while(fgets(in_string, 100, stdin) != NULL) { |
793 |
stack_read(&s, myhash, in_string); |
stack_read(&myenv, in_string); |
794 |
|
if(myenv.err) { |
795 |
|
printf("(error %d) ", myenv.err); |
796 |
|
myenv.err=0; |
797 |
|
} |
798 |
printf("okidok\n "); |
printf("okidok\n "); |
799 |
} |
} |
800 |
|
|
801 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
802 |
} |
} |
|
|
|
|
/* Local Variables: */ |
|
|
/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ |
|
|
/* End: */ |
|