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 |
|
/* An item (value) on a stack */ |
48 |
|
typedef struct stackitem_struct |
49 |
|
{ |
50 |
|
value *item; /* The value on the stack */ |
51 |
|
struct stackitem_struct *next; /* Next item */ |
52 |
|
} stackitem; |
53 |
|
|
54 |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
/* An environment; gives access to the stack and a hash table of |
55 |
typedef void (*funcp)(stackitem**); /* funcp is a pointer to a |
defined symbols */ |
56 |
void function (stackitem **) */ |
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; |
|
133 |
} |
} |
134 |
|
|
135 |
/* Create a new hash entry. */ |
/* Copy a string onto the stack. */ |
136 |
int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id) |
void push_cstring(stackitem **stack_head, const char *in_string) |
|
{ |
|
|
in_item->id= malloc(strlen(id)+1); |
|
|
|
|
|
strcpy(in_item->id, id); |
|
|
push(hash(in_hashtbl, id), in_item); |
|
|
|
|
|
return 1; |
|
|
} |
|
|
|
|
|
/* Define a new function in the hash table. */ |
|
|
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
|
137 |
{ |
{ |
138 |
stackitem* temp= malloc(sizeof(stackitem)); |
value *new_value= malloc(sizeof(value)); |
139 |
|
stackitem *new_item= malloc(sizeof(stackitem)); |
140 |
|
new_item->item=new_value; |
141 |
|
|
142 |
|
new_value->content.ptr= malloc(strlen(in_string)+1); |
143 |
|
strcpy(new_value->content.ptr, in_string); |
144 |
|
new_value->type= string; |
145 |
|
new_value->refcount=1; |
146 |
|
|
147 |
temp->type= func; |
push(stack_head, new_item); |
|
temp->content.ptr= in_func; |
|
|
|
|
|
mk_hashentry(in_hashtbl, temp, id); |
|
|
} |
|
|
|
|
|
/* Define a new symbol in the hash table. */ |
|
|
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 |
|
case list: /* lists needs to be freed recursively */ |
222 |
|
item=val->content.ptr; |
223 |
|
while(item != NULL) { /* for all stack items */ |
224 |
|
free_val(item->item); /* free the value */ |
225 |
|
temp=item->next; /* save next ptr */ |
226 |
|
free(item); /* free the stackitem */ |
227 |
|
item=temp; /* go to next stackitem */ |
228 |
|
} |
229 |
|
free(val); /* Free the actual list value */ |
230 |
|
break; |
231 |
|
default: |
232 |
|
break; |
233 |
|
} |
234 |
|
} |
235 |
|
} |
236 |
|
|
237 |
/* Discard the top element of the stack. */ |
/* Discard the top element of the stack. */ |
238 |
extern void toss(stackitem** stack_head) |
extern void toss(environment *env) |
239 |
{ |
{ |
240 |
stackitem* temp= *stack_head; |
stackitem *temp= env->head; |
241 |
|
|
242 |
if((*stack_head)==NULL) { |
if((env->head)==NULL) { |
243 |
printerr("Stack empty"); |
printerr("Stack empty"); |
244 |
|
env->err=1; |
245 |
return; |
return; |
246 |
} |
} |
247 |
|
|
248 |
if((*stack_head)->type==string) |
free_val(env->head->item); /* Free the value */ |
249 |
free((*stack_head)->content.ptr); |
env->head= env->head->next; /* Remove the top stack item */ |
250 |
|
free(temp); /* Free the old top stack item */ |
|
*stack_head= (*stack_head)->next; |
|
|
free(temp); |
|
251 |
} |
} |
252 |
|
|
253 |
/* Print newline. */ |
/* Print newline. */ |
257 |
} |
} |
258 |
|
|
259 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
260 |
extern void print_(stackitem** stack_head) |
void print_h(stackitem *stack_head) |
261 |
{ |
{ |
262 |
stackitem* temp= *stack_head; |
switch(stack_head->item->type) { |
263 |
|
case integer: |
264 |
if(temp==NULL) { |
printf("%d", stack_head->item->content.val); |
|
printerr("Stack empty"); |
|
|
return; |
|
|
} |
|
|
|
|
|
while(temp->type==ref) { |
|
|
temp= temp->content.ptr; |
|
|
|
|
|
if(temp->type!=ref) { |
|
|
printf("ref-> %s", temp->id); |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
switch(temp->type) { |
|
|
case value: |
|
|
printf("%d", temp->content.val); |
|
265 |
break; |
break; |
266 |
case string: |
case string: |
267 |
printf("\"%s\"", (char*)temp->content.ptr); |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
268 |
|
break; |
269 |
|
case symb: |
270 |
|
printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); |
271 |
break; |
break; |
|
case symbol: |
|
272 |
case func: |
case func: |
273 |
printf("%s", temp->id); |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
274 |
|
break; |
275 |
|
case list: |
276 |
|
printf("#<list %p>", (funcp)(stack_head->item->content.ptr)); |
277 |
break; |
break; |
278 |
default: |
default: |
279 |
printf("%p", temp->content.ptr); |
printf("#<unknown %p>", (funcp)(stack_head->item->content.ptr)); |
280 |
break; |
break; |
281 |
} |
} |
282 |
} |
} |
283 |
|
|
284 |
|
extern void print_(environment *env) { |
285 |
|
if(env->head==NULL) { |
286 |
|
printerr("Stack empty"); |
287 |
|
env->err=1; |
288 |
|
return; |
289 |
|
} |
290 |
|
print_h(env->head); |
291 |
|
} |
292 |
|
|
293 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
294 |
extern void print(stackitem** stack_head) |
extern void print(environment *env) |
295 |
{ |
{ |
296 |
print_(stack_head); |
print_(env); |
297 |
toss(stack_head); |
if(env->err) return; |
298 |
|
toss(env); |
299 |
} |
} |
300 |
|
|
301 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
302 |
void print_st(stackitem* stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
303 |
{ |
{ |
304 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
305 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
|
|
|
306 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
307 |
print_(&stack_head); |
print_h(stack_head); |
308 |
nl(); |
nl(); |
309 |
} |
} |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
/* Prints the stack. */ |
/* Prints the stack. */ |
314 |
extern void printstack(stackitem** stack_head) |
extern void printstack(environment *env) |
315 |
{ |
{ |
316 |
if(*stack_head != NULL) { |
if(env->head == NULL) { |
|
print_st(*stack_head, 1); |
|
|
nl(); |
|
|
} else { |
|
317 |
printerr("Stack empty"); |
printerr("Stack empty"); |
318 |
|
env->err=1; |
319 |
|
return; |
320 |
} |
} |
321 |
|
print_st(env->head, 1); |
322 |
|
nl(); |
323 |
} |
} |
324 |
|
|
325 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
326 |
extern void swap(stackitem** stack_head) |
extern void swap(environment *env) |
327 |
{ |
{ |
328 |
stackitem* temp= (*stack_head); |
stackitem *temp= env->head; |
329 |
|
|
330 |
if((*stack_head)==NULL) { |
if((env->head)==NULL) { |
331 |
printerr("Stack empty"); |
printerr("Stack empty"); |
332 |
|
env->err=1; |
333 |
return; |
return; |
334 |
} |
} |
335 |
|
|
336 |
if((*stack_head)->next==NULL) |
if(env->head->next==NULL) { |
337 |
|
printerr("Not enough arguments"); |
338 |
|
env->err=1; |
339 |
return; |
return; |
340 |
|
} |
341 |
|
|
342 |
*stack_head= (*stack_head)->next; |
env->head= env->head->next; |
343 |
temp->next= (*stack_head)->next; |
temp->next= env->head->next; |
344 |
(*stack_head)->next= temp; |
env->head->next= temp; |
345 |
} |
} |
346 |
|
|
347 |
stackitem* copy(stackitem* in_item) |
stackitem* copy(stackitem* in_item) |
348 |
{ |
{ |
349 |
stackitem* out_item= malloc(sizeof(stackitem)); |
stackitem *out_item= malloc(sizeof(stackitem)); |
350 |
|
|
351 |
memcpy(out_item, in_item, sizeof(stackitem)); |
memcpy(out_item, in_item, sizeof(stackitem)); |
352 |
out_item->next= NULL; |
out_item->next= NULL; |
354 |
return out_item; |
return out_item; |
355 |
} |
} |
356 |
|
|
357 |
|
/* Recall a value from a symbol, if bound */ |
358 |
/* If the top element is a reference, determine if it's a reference to a |
extern void rcl(environment *env) |
|
function, and if it is, toss the reference and execute the function. */ |
|
|
extern void eval(stackitem** stack_head) |
|
359 |
{ |
{ |
360 |
funcp in_func; |
value *val; |
|
stackitem* temp= *stack_head; |
|
361 |
|
|
362 |
if(temp==NULL) { |
if(env->head == NULL) { |
363 |
printerr("Stack empty"); |
printerr("Stack empty"); |
364 |
|
env->err=1; |
365 |
return; |
return; |
366 |
} |
} |
367 |
|
|
368 |
while(temp->type==ref) |
if(env->head->item->type!=symb) { |
369 |
temp= temp->content.ptr; |
printerr("Not a symbol"); |
370 |
|
env->err=1; |
371 |
|
return; |
372 |
|
} |
373 |
|
|
374 |
if(temp->type==func) { |
val=((symbol *)(env->head->item->content.ptr))->val; |
375 |
in_func= (funcp)(temp->content.ptr); |
if(val == NULL){ |
376 |
toss(stack_head); |
printerr("Unbound variable"); |
377 |
(*in_func)(stack_head); |
env->err=1; |
378 |
return; |
return; |
379 |
} |
} |
380 |
|
toss(env); /* toss the symbol */ |
381 |
|
if(env->err) return; |
382 |
|
push_val(&(env->head), val); /* Return its bound value */ |
383 |
|
} |
384 |
|
|
385 |
|
/* If the top element is a symbol, determine if it's bound to a |
386 |
|
function value, and if it is, toss the symbol and execute the |
387 |
|
function. */ |
388 |
|
extern void eval(environment *env) |
389 |
|
{ |
390 |
|
funcp in_func; |
391 |
|
if(env->head==NULL) { |
392 |
|
printerr("Stack empty"); |
393 |
|
env->err=1; |
394 |
|
return; |
395 |
|
} |
396 |
|
|
397 |
|
/* if it's a symbol */ |
398 |
|
if(env->head->item->type==symb) { |
399 |
|
|
400 |
|
rcl(env); /* get its contents */ |
401 |
|
if(env->err) return; |
402 |
|
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
403 |
|
eval(env); /* evaluate the value */ |
404 |
|
return; |
405 |
|
} |
406 |
|
} |
407 |
|
|
408 |
push(stack_head, copy(temp)); |
/* If it's a lone function value, run it */ |
409 |
swap(stack_head); |
if(env->head->item->type==func) { |
410 |
toss(stack_head); |
in_func= (funcp)(env->head->item->content.ptr); |
411 |
|
toss(env); |
412 |
|
if(env->err) return; |
413 |
|
(*in_func)(env); |
414 |
|
} |
415 |
} |
} |
416 |
|
|
417 |
/* Make a list. */ |
/* Make a list. */ |
418 |
extern void pack(stackitem** stack_head) |
extern void pack(environment *env) |
419 |
{ |
{ |
420 |
void* delimiter; |
void* delimiter; |
421 |
stackitem *iterator, *temp, *pack; |
stackitem *iterator, *temp; |
422 |
|
value *pack; |
423 |
|
|
424 |
delimiter= (*stack_head)->content.ptr; /* Get delimiter */ |
delimiter= env->head->item->content.ptr; /* Get delimiter */ |
425 |
toss(stack_head); |
toss(env); |
426 |
|
|
427 |
iterator= *stack_head; |
iterator= env->head; |
428 |
|
|
429 |
if(iterator==NULL || iterator->content.ptr==delimiter) { |
if(iterator==NULL || iterator->item->content.ptr==delimiter) { |
430 |
temp= NULL; |
temp= NULL; |
431 |
toss(stack_head); |
toss(env); |
432 |
} else { |
} else { |
433 |
/* Search for first delimiter */ |
/* Search for first delimiter */ |
434 |
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
while(iterator->next!=NULL |
435 |
|
&& iterator->next->item->content.ptr!=delimiter) |
436 |
iterator= iterator->next; |
iterator= iterator->next; |
437 |
|
|
438 |
/* Extract list */ |
/* Extract list */ |
439 |
temp= *stack_head; |
temp= env->head; |
440 |
*stack_head= iterator->next; |
env->head= iterator->next; |
441 |
iterator->next= NULL; |
iterator->next= NULL; |
442 |
|
|
443 |
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
if(env->head!=NULL) |
444 |
toss(stack_head); |
toss(env); |
445 |
} |
} |
446 |
|
|
447 |
/* Push list */ |
/* Push list */ |
448 |
pack= malloc(sizeof(stackitem)); |
pack= malloc(sizeof(value)); |
449 |
pack->type= list; |
pack->type= list; |
450 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
451 |
|
pack->refcount= 1; |
452 |
|
|
453 |
push(stack_head, pack); |
temp= malloc(sizeof(stackitem)); |
454 |
|
temp->item= pack; |
455 |
|
|
456 |
|
push(&(env->head), temp); |
457 |
} |
} |
458 |
|
|
459 |
/* Parse input. */ |
/* Parse input. */ |
460 |
int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line) |
int stack_read(environment *env, char *in_line) |
461 |
{ |
{ |
462 |
char *temp, *rest; |
char *temp, *rest; |
463 |
int itemp; |
int itemp; |
471 |
do { |
do { |
472 |
/* If string */ |
/* If string */ |
473 |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
474 |
push_cstring(stack_head, temp); |
push_cstring(&(env->head), temp); |
475 |
break; |
break; |
476 |
} |
} |
477 |
/* If value */ |
/* If integer */ |
478 |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
479 |
push_val(stack_head, itemp); |
push_int(&(env->head), itemp); |
480 |
break; |
break; |
481 |
} |
} |
482 |
/* Escape ';' with '\' */ |
/* Escape ';' with '\' */ |
483 |
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
484 |
temp[1]= '\0'; |
temp[1]= '\0'; |
485 |
push_ref(stack_head, in_hash, temp); |
push_sym(env, temp); |
486 |
break; |
break; |
487 |
} |
} |
488 |
/* If symbol */ |
/* If symbol */ |
489 |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { |
490 |
push_ref(stack_head, in_hash, temp); |
push_sym(env, temp); |
491 |
break; |
break; |
492 |
} |
} |
493 |
/* If single char */ |
/* If single char */ |
494 |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { |
495 |
if(*temp==';') { |
if(*temp==';') { |
496 |
if(!non_eval_flag) { |
if(!non_eval_flag) { |
497 |
eval(stack_head); /* Evaluate top element */ |
eval(env); /* Evaluate top element */ |
498 |
break; |
break; |
499 |
} |
} |
500 |
|
|
501 |
push_ref(stack_head, in_hash, ";"); |
push_sym(env, ";"); |
502 |
break; |
break; |
503 |
} |
} |
504 |
|
|
505 |
if(*temp==']') { |
if(*temp==']') { |
506 |
push_ref(stack_head, in_hash, "["); |
push_sym(env, "["); |
507 |
pack(stack_head); |
pack(env); |
508 |
if(non_eval_flag!=0) |
if(non_eval_flag!=0) |
509 |
non_eval_flag--; |
non_eval_flag--; |
510 |
break; |
break; |
511 |
} |
} |
512 |
|
|
513 |
if(*temp=='[') { |
if(*temp=='[') { |
514 |
push_ref(stack_head, in_hash, "["); |
push_sym(env, "["); |
515 |
non_eval_flag++; |
non_eval_flag++; |
516 |
break; |
break; |
517 |
} |
} |
526 |
return 0; |
return 0; |
527 |
} |
} |
528 |
|
|
529 |
stack_read(stack_head, in_hash, rest); |
stack_read(env, rest); |
530 |
|
|
531 |
free(rest); |
free(rest); |
532 |
return 1; |
return 1; |
533 |
} |
} |
534 |
|
|
535 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
536 |
extern void expand(stackitem** stack_head) |
extern void expand(environment *env) |
537 |
{ |
{ |
538 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
539 |
|
|
540 |
/* Is top element a list? */ |
/* Is top element a list? */ |
541 |
if((*stack_head)==NULL || (*stack_head)->type!=list) { |
if(env->head==NULL || env->head->item->type!=list) { |
542 |
printerr("Stack empty or not a list"); |
printerr("Stack empty or not a list"); |
543 |
|
env->err=1; |
544 |
return; |
return; |
545 |
} |
} |
546 |
|
|
547 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
548 |
new_head= temp= (*stack_head)->content.ptr; |
new_head= temp= env->head->item->content.ptr; |
|
toss(stack_head); |
|
549 |
|
|
550 |
if(temp==NULL) |
env->head->item->refcount++; |
551 |
return; |
toss(env); |
552 |
|
|
553 |
/* Search the end of the list */ |
/* Find the end of the list */ |
554 |
while(temp->next!=NULL) |
while(temp->next!=NULL) |
555 |
temp= temp->next; |
temp= temp->next; |
556 |
|
|
557 |
/* Connect the the tail of the list with the old stack head */ |
/* Connect the tail of the list with the old stack head */ |
558 |
temp->next= *stack_head; |
temp->next= env->head; |
559 |
*stack_head= new_head; /* ...and voila! */ |
env->head= new_head; /* ...and voila! */ |
560 |
|
|
561 |
} |
} |
562 |
|
|
563 |
/* Compares two elements by reference. */ |
/* Compares two elements by reference. */ |
564 |
extern void eq(stackitem** stack_head) |
extern void eq(environment *env) |
565 |
{ |
{ |
566 |
void *left, *right; |
void *left, *right; |
567 |
int result; |
int result; |
568 |
|
|
569 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) { |
if((env->head)==NULL || env->head->next==NULL) { |
570 |
printerr("Not enough elements to compare"); |
printerr("Not enough elements to compare"); |
571 |
|
env->err=1; |
572 |
return; |
return; |
573 |
} |
} |
574 |
|
|
575 |
left= (*stack_head)->content.ptr; |
left= env->head->item->content.ptr; |
576 |
swap(stack_head); |
swap(env); |
577 |
right= (*stack_head)->content.ptr; |
right= env->head->item->content.ptr; |
578 |
result= (left==right); |
result= (left==right); |
579 |
|
|
580 |
toss(stack_head); toss(stack_head); |
toss(env); toss(env); |
581 |
push_val(stack_head, (left==right)); |
push_int(&(env->head), result); |
582 |
} |
} |
583 |
|
|
584 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
585 |
extern void not(stackitem** stack_head) |
extern void not(environment *env) |
586 |
{ |
{ |
587 |
int value; |
int val; |
588 |
|
|
589 |
if((*stack_head)==NULL || (*stack_head)->type!=value) { |
if((env->head)==NULL || env->head->item->type!=integer) { |
590 |
printerr("Stack empty or element is not a value"); |
printerr("Stack empty or element is not a integer"); |
591 |
|
env->err=1; |
592 |
return; |
return; |
593 |
} |
} |
594 |
|
|
595 |
value= (*stack_head)->content.val; |
val= env->head->item->content.val; |
596 |
toss(stack_head); |
toss(env); |
597 |
push_val(stack_head, !value); |
push_int(&(env->head), !val); |
598 |
} |
} |
599 |
|
|
600 |
/* 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 |
601 |
same. */ |
same. */ |
602 |
extern void neq(stackitem** stack_head) |
extern void neq(environment *env) |
603 |
{ |
{ |
604 |
eq(stack_head); |
eq(env); |
605 |
not(stack_head); |
not(env); |
606 |
} |
} |
607 |
|
|
608 |
/* Give a symbol some content. */ |
/* Give a symbol some content. */ |
609 |
extern void def(stackitem** stack_head) |
extern void def(environment *env) |
610 |
{ |
{ |
611 |
stackitem *temp, *value; |
symbol *sym; |
612 |
|
|
613 |
if(*stack_head==NULL || (*stack_head)->next==NULL |
/* Needs two values on the stack, the top one must be a symbol */ |
614 |
|| (*stack_head)->type!=ref) { |
if(env->head==NULL || env->head->next==NULL |
615 |
|
|| env->head->item->type!=symb) { |
616 |
printerr("Define what?"); |
printerr("Define what?"); |
617 |
|
env->err=1; |
618 |
return; |
return; |
619 |
} |
} |
620 |
|
|
621 |
temp= (*stack_head)->content.ptr; |
/* long names are a pain */ |
622 |
value= (*stack_head)->next; |
sym=env->head->item->content.ptr; |
623 |
temp->content= value->content; |
|
624 |
value->content.ptr=NULL; |
/* if the symbol was bound to something else, throw it away */ |
625 |
temp->type= value->type; |
if(sym->val != NULL) |
626 |
|
free_val(sym->val); |
627 |
|
|
628 |
|
/* Bind the symbol to the value */ |
629 |
|
sym->val= env->head->next->item; |
630 |
|
sym->val->refcount++; /* Increase the reference counter */ |
631 |
|
|
632 |
toss(stack_head); toss(stack_head); |
toss(env); toss(env); |
633 |
} |
} |
634 |
|
|
635 |
/* Quit stack. */ |
/* Quit stack. */ |
636 |
extern void quit() |
extern void quit(environment *env) |
637 |
{ |
{ |
638 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
639 |
} |
} |
640 |
|
|
641 |
/* Clear stack */ |
/* Clear stack */ |
642 |
extern void clear(stackitem** stack_head) |
extern void clear(environment *env) |
643 |
{ |
{ |
644 |
while(*stack_head!=NULL) |
while(env->head!=NULL) |
645 |
toss(stack_head); |
toss(env); |
646 |
|
} |
647 |
|
|
648 |
|
/* List all defined words */ |
649 |
|
extern void words(environment *env) |
650 |
|
{ |
651 |
|
symbol *temp; |
652 |
|
int i; |
653 |
|
|
654 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
655 |
|
temp= env->symbols[i]; |
656 |
|
while(temp!=NULL) { |
657 |
|
printf("%s\n", temp->id); |
658 |
|
temp= temp->next; |
659 |
|
} |
660 |
|
} |
661 |
} |
} |
662 |
|
|
663 |
|
/* Forgets a symbol (remove it from the hash table) */ |
664 |
|
extern void forget(environment *env) |
665 |
|
{ |
666 |
|
char* sym_id; |
667 |
|
stackitem *stack_head= env->head; |
668 |
|
symbol **hash_entry, *temp; |
669 |
|
|
670 |
|
if(stack_head==NULL || stack_head->item->type!=symb) { |
671 |
|
printerr("Stack empty or not a symbol"); |
672 |
|
return; |
673 |
|
} |
674 |
|
|
675 |
|
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
676 |
|
toss(env); |
677 |
|
|
678 |
|
hash_entry= hash(env->symbols, sym_id); |
679 |
|
temp= *hash_entry; |
680 |
|
*hash_entry= (*hash_entry)->next; |
681 |
|
|
682 |
|
if(temp->val!=NULL) { |
683 |
|
free_val(temp->val); |
684 |
|
} |
685 |
|
free(temp->id); |
686 |
|
free(temp); |
687 |
|
} |
688 |
|
|
689 |
int main() |
int main() |
690 |
{ |
{ |
691 |
stackitem* s= NULL; |
environment myenv; |
|
hashtbl myhash; |
|
692 |
char in_string[100]; |
char in_string[100]; |
693 |
|
|
694 |
init_hashtbl(myhash); |
init_env(&myenv); |
695 |
|
|
696 |
printf("okidok\n "); |
printf("okidok\n "); |
697 |
|
|
698 |
while(fgets(in_string, 100, stdin) != NULL) { |
while(fgets(in_string, 100, stdin) != NULL) { |
699 |
stack_read(&s, myhash, in_string); |
stack_read(&myenv, in_string); |
700 |
|
if(myenv.err) { |
701 |
|
printf("(error %d) ", myenv.err); |
702 |
|
myenv.err=0; |
703 |
|
} |
704 |
printf("okidok\n "); |
printf("okidok\n "); |
705 |
} |
} |
706 |
|
|
707 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
708 |
} |
} |
|
|
|
|
/* Local Variables: */ |
|
|
/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ |
|
|
/* End: */ |
|