1 |
/* printf */ |
/* printf, sscanf, fgets, fprintf */ |
2 |
#include <stdio.h> |
#include <stdio.h> |
3 |
/* EXIT_SUCCESS */ |
/* exit, EXIT_SUCCESS, malloc, free */ |
4 |
#include <stdlib.h> |
#include <stdlib.h> |
5 |
/* NULL */ |
/* NULL */ |
6 |
#include <stddef.h> |
#include <stddef.h> |
7 |
/* dlopen, dlsym, dlerror */ |
/* dlopen, dlsym, dlerror */ |
8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
9 |
|
/* strcmp, strcpy, strlen, strcat, strdup */ |
10 |
|
#include <string.h> |
11 |
|
|
12 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 2048 |
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; /* Tells what kind 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 |
|
} 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 |
|
/* (This is never NULL) */ |
52 |
|
struct stackitem_struct *next; /* Next item */ |
53 |
} stackitem; |
} stackitem; |
54 |
|
|
55 |
|
/* An environment; gives access to the stack and a hash table of |
56 |
|
defined symbols */ |
57 |
|
typedef struct { |
58 |
|
stackitem *head; /* Head of the stack */ |
59 |
|
hashtbl symbols; /* Hash table of all variable bindings */ |
60 |
|
int err; /* Error flag */ |
61 |
|
int non_eval_flag; |
62 |
|
char *in_string; /* Input pending to be read */ |
63 |
|
char *free_string; /* Free this string when all input is |
64 |
|
read from in_string */ |
65 |
|
} environment; |
66 |
|
|
67 |
|
/* A type for pointers to external functions */ |
68 |
|
typedef void (*funcp)(environment *); /* funcp is a pointer to a void |
69 |
|
function (environment *) */ |
70 |
|
|
71 |
|
/* Initialize a newly created environment */ |
72 |
|
void init_env(environment *env) |
73 |
|
{ |
74 |
|
int i; |
75 |
|
|
76 |
|
env->in_string= NULL; |
77 |
|
env->err= 0; |
78 |
|
env->non_eval_flag= 0; |
79 |
|
for(i= 0; i<HASHTBLSIZE; i++) |
80 |
|
env->symbols[i]= NULL; |
81 |
|
} |
82 |
|
|
83 |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
void printerr(const char* in_string) { |
84 |
typedef void (*funcp)(stackitem**); /* Function pointer declaration */ |
fprintf(stderr, "Err: %s\n", in_string); |
85 |
|
} |
86 |
|
|
87 |
|
/* Throw away a value */ |
88 |
|
void free_val(value *val){ |
89 |
|
stackitem *item, *temp; |
90 |
|
|
91 |
/* Initiates a newly created hash table. */ |
val->refcount--; /* Decrease the reference count */ |
92 |
void init_hashtbl(hashtbl out_hash) |
if(val->refcount == 0){ |
93 |
|
switch (val->type){ /* and free the contents if necessary */ |
94 |
|
case string: |
95 |
|
free(val->content.ptr); |
96 |
|
break; |
97 |
|
case list: /* lists needs to be freed recursively */ |
98 |
|
item=val->content.ptr; |
99 |
|
while(item != NULL) { /* for all stack items */ |
100 |
|
free_val(item->item); /* free the value */ |
101 |
|
temp=item->next; /* save next ptr */ |
102 |
|
free(item); /* free the stackitem */ |
103 |
|
item=temp; /* go to next stackitem */ |
104 |
|
} |
105 |
|
free(val); /* Free the actual list value */ |
106 |
|
break; |
107 |
|
case integer: |
108 |
|
case func: |
109 |
|
case symb: |
110 |
|
break; |
111 |
|
} |
112 |
|
} |
113 |
|
} |
114 |
|
|
115 |
|
/* Discard the top element of the stack. */ |
116 |
|
extern void toss(environment *env) |
117 |
{ |
{ |
118 |
long i; |
stackitem *temp= env->head; |
119 |
|
|
120 |
for(i= 0; i<HASHTBLSIZE; i++) |
if((env->head)==NULL) { |
121 |
out_hash[i]= NULL; |
printerr("Too Few Arguments"); |
122 |
|
env->err=1; |
123 |
|
return; |
124 |
|
} |
125 |
|
|
126 |
|
free_val(env->head->item); /* Free the value */ |
127 |
|
env->head= env->head->next; /* Remove the top stack item */ |
128 |
|
free(temp); /* Free the old top stack item */ |
129 |
} |
} |
130 |
|
|
131 |
/* Returns a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
132 |
stackitem** hash(hashtbl in_hashtbl, const char* in_string) |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
133 |
{ |
{ |
134 |
long i= 0; |
int i= 0; |
135 |
unsigned long out_hash= 0; |
unsigned int out_hash= 0; |
136 |
char key= 0; |
char key= '\0'; |
137 |
stackitem** position; |
symbol **position; |
138 |
|
|
139 |
while(1){ /* Hash in_string */ |
while(1){ /* Hash in_string */ |
140 |
key= in_string[i++]; |
key= in_string[i++]; |
146 |
out_hash= out_hash%HASHTBLSIZE; |
out_hash= out_hash%HASHTBLSIZE; |
147 |
position= &(in_hashtbl[out_hash]); |
position= &(in_hashtbl[out_hash]); |
148 |
|
|
149 |
while(1){ /* Return position if empty */ |
while(1){ |
150 |
if(*position==NULL) |
if(*position==NULL) /* If empty */ |
151 |
return position; |
return position; |
152 |
|
|
153 |
if(strcmp(in_string, (*position)->id)==0) /* Return position if match */ |
if(strcmp(in_string, (*position)->id)==0) /* If match */ |
154 |
return position; |
return position; |
155 |
|
|
156 |
position= &((*position)->next); /* Try next */ |
position= &((*position)->next); /* Try next */ |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
160 |
/* Generic push function. */ |
/* Push a value onto the stack */ |
161 |
int push(stackitem** stack_head, stackitem* in_item) |
void push_val(environment *env, value *val) |
162 |
{ |
{ |
163 |
in_item->next= *stack_head; |
stackitem *new_item= malloc(sizeof(stackitem)); |
164 |
*stack_head= in_item; |
new_item->item= val; |
165 |
return 1; |
val->refcount++; |
166 |
|
new_item->next= env->head; |
167 |
|
env->head= new_item; |
168 |
} |
} |
169 |
|
|
170 |
/* Push a value on the stack. */ |
/* Push an integer onto the stack. */ |
171 |
int push_val(stackitem** stack_head, int in_val) |
void push_int(environment *env, int in_val) |
172 |
{ |
{ |
173 |
stackitem* new_item= malloc(sizeof(stackitem)); |
value *new_value= malloc(sizeof(value)); |
174 |
new_item->content.val= in_val; |
|
175 |
new_item->type= value; |
new_value->content.val= in_val; |
176 |
|
new_value->type= integer; |
177 |
|
new_value->refcount=1; |
178 |
|
|
179 |
push(stack_head, new_item); |
push_val(env, new_value); |
|
return 1; |
|
180 |
} |
} |
181 |
|
|
182 |
/* Copy a string onto the stack. */ |
/* Copy a string onto the stack. */ |
183 |
int push_cstring(stackitem** stack_head, const char* in_string) |
void push_cstring(environment *env, const char *in_string) |
184 |
{ |
{ |
185 |
stackitem* new_item= malloc(sizeof(stackitem)); |
value *new_value= malloc(sizeof(value)); |
|
new_item->content.ptr= malloc(strlen(in_string)+1); |
|
|
strcpy(new_item->content.ptr, in_string); |
|
|
new_item->type= string; |
|
186 |
|
|
187 |
push(stack_head, new_item); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
188 |
return 1; |
strcpy(new_value->content.ptr, in_string); |
189 |
|
new_value->type= string; |
190 |
|
new_value->refcount=1; |
191 |
|
|
192 |
|
push_val(env, new_value); |
193 |
|
} |
194 |
|
|
195 |
|
/* Mangle a symbol name to a valid C identifier name */ |
196 |
|
char *mangle_str(const char *old_string){ |
197 |
|
char validchars[] |
198 |
|
="0123456789abcdef"; |
199 |
|
char *new_string, *current; |
200 |
|
|
201 |
|
new_string=malloc((strlen(old_string)*2)+4); |
202 |
|
strcpy(new_string, "sx_"); /* Stack eXternal */ |
203 |
|
current=new_string+3; |
204 |
|
while(old_string[0] != '\0'){ |
205 |
|
current[0]=validchars[(unsigned char)(old_string[0])/16]; |
206 |
|
current[1]=validchars[(unsigned char)(old_string[0])%16]; |
207 |
|
current+=2; |
208 |
|
old_string++; |
209 |
|
} |
210 |
|
current[0]='\0'; |
211 |
|
|
212 |
|
return new_string; /* The caller must free() it */ |
213 |
} |
} |
214 |
|
|
215 |
/* Create a new hash entry. */ |
extern void mangle(environment *env){ |
216 |
int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id) |
value *new_value; |
217 |
{ |
char *new_string; |
|
in_item->id= malloc(strlen(id)+1); |
|
218 |
|
|
219 |
strcpy(in_item->id, id); |
if((env->head)==NULL) { |
220 |
push(hash(in_hashtbl, id), in_item); |
printerr("Too Few Arguments"); |
221 |
|
env->err=1; |
222 |
|
return; |
223 |
|
} |
224 |
|
|
225 |
return 1; |
if(env->head->item->type!=string) { |
226 |
} |
printerr("Bad Argument Type"); |
227 |
|
env->err=2; |
228 |
|
return; |
229 |
|
} |
230 |
|
|
231 |
/* Define a function a new function in the hash table. */ |
new_string= mangle_str((const char *)(env->head->item->content.ptr)); |
|
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
|
|
{ |
|
|
stackitem* temp= malloc(sizeof(stackitem)); |
|
232 |
|
|
233 |
temp->type= func; |
toss(env); |
234 |
temp->content.ptr= in_func; |
if(env->err) return; |
235 |
|
|
236 |
mk_hashentry(in_hashtbl, temp, id); |
new_value= malloc(sizeof(value)); |
237 |
} |
new_value->content.ptr= new_string; |
238 |
|
new_value->type= string; |
239 |
|
new_value->refcount=1; |
240 |
|
|
241 |
/* Define a new symbol in the hash table. */ |
push_val(env, new_value); |
|
void def_sym(hashtbl in_hashtbl, const char* id) |
|
|
{ |
|
|
stackitem* temp= malloc(sizeof(stackitem)); |
|
|
|
|
|
temp->type= symbol; |
|
|
mk_hashentry(in_hashtbl, temp, id); |
|
242 |
} |
} |
243 |
|
|
244 |
/* Push a reference to an entry in the hash table onto the stack. */ |
/* Push a symbol onto the stack. */ |
245 |
int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string) |
void push_sym(environment *env, const char *in_string) |
246 |
{ |
{ |
247 |
static void* handle= NULL; |
value *new_value; /* A new symbol value */ |
248 |
void* symbol; |
/* ...which might point to... */ |
249 |
|
symbol **new_symbol; /* (if needed) A new actual symbol */ |
250 |
|
/* ...which, if possible, will be bound to... */ |
251 |
|
value *new_fvalue; /* (if needed) A new function value */ |
252 |
|
/* ...which will point to... */ |
253 |
|
void *funcptr; /* A function pointer */ |
254 |
|
|
255 |
stackitem* new_item= malloc(sizeof(stackitem)); |
static void *handle= NULL; /* Dynamic linker handle */ |
256 |
new_item->content.ptr= *hash(in_hash, in_string); |
const char *dlerr; /* Dynamic linker error */ |
257 |
new_item->type= ref; |
char *mangled; /* Mangled function name */ |
258 |
|
|
259 |
if(new_item->content.ptr==NULL) { /* If hash entry empty */ |
new_value= malloc(sizeof(value)); |
|
if(handle==NULL) /* If no handle */ |
|
|
handle= dlopen(NULL, RTLD_LAZY); |
|
260 |
|
|
261 |
symbol= dlsym(handle, in_string); /* Get function pointer */ |
/* The new value is a symbol */ |
262 |
if(dlerror()==NULL) /* If existing function pointer */ |
new_value->type= symb; |
263 |
def_func(in_hash, symbol, in_string); /* Store function pointer */ |
new_value->refcount= 1; |
|
else |
|
|
def_sym(in_hash, in_string); /* Make symbol */ |
|
|
|
|
|
new_item->content.ptr= *hash(in_hash, in_string); /* XXX */ |
|
|
new_item->type= ref; |
|
|
} |
|
264 |
|
|
265 |
push(stack_head, new_item); |
/* Look up the symbol name in the hash table */ |
266 |
return 1; |
new_symbol= hash(env->symbols, in_string); |
267 |
} |
new_value->content.ptr= *new_symbol; |
268 |
|
|
269 |
/* Discard the top element of the stack. */ |
if(*new_symbol==NULL) { /* If symbol was undefined */ |
|
extern void toss(stackitem** stack_head) |
|
|
{ |
|
|
stackitem* temp= *stack_head; |
|
270 |
|
|
271 |
if((*stack_head)==NULL) |
/* Create a new symbol */ |
272 |
return; |
(*new_symbol)= malloc(sizeof(symbol)); |
273 |
|
(*new_symbol)->val= NULL; /* undefined value */ |
274 |
if((*stack_head)->type==string) |
(*new_symbol)->next= NULL; |
275 |
free((*stack_head)->content.ptr); |
(*new_symbol)->id= malloc(strlen(in_string)+1); |
276 |
|
strcpy((*new_symbol)->id, in_string); |
277 |
|
|
278 |
*stack_head= (*stack_head)->next; |
/* Intern the new symbol in the hash table */ |
279 |
free(temp); |
new_value->content.ptr= *new_symbol; |
280 |
|
|
281 |
|
/* Try to load the symbol name as an external function, to see if |
282 |
|
we should bind the symbol to a new function pointer value */ |
283 |
|
if(handle==NULL) /* If no handle */ |
284 |
|
handle= dlopen(NULL, RTLD_LAZY); |
285 |
|
|
286 |
|
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
287 |
|
dlerr=dlerror(); |
288 |
|
if(dlerr != NULL) { /* If no function was found */ |
289 |
|
mangled=mangle_str(in_string); |
290 |
|
funcptr= dlsym(handle, mangled); /* try mangling it */ |
291 |
|
free(mangled); |
292 |
|
dlerr=dlerror(); |
293 |
|
} |
294 |
|
if(dlerr==NULL) { /* If a function was found */ |
295 |
|
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
296 |
|
new_fvalue->type=func; /* The new value is a function pointer */ |
297 |
|
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
298 |
|
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
299 |
|
function value */ |
300 |
|
new_fvalue->refcount= 1; |
301 |
|
} |
302 |
|
} |
303 |
|
push_val(env, new_value); |
304 |
} |
} |
305 |
|
|
306 |
/* Print newline. */ |
/* Print newline. */ |
309 |
printf("\n"); |
printf("\n"); |
310 |
} |
} |
311 |
|
|
312 |
/* Prints the top element of the stack. */ |
/* Gets the type of a value */ |
313 |
void print_(stackitem** stack_head) |
extern void type(environment *env){ |
314 |
{ |
int typenum; |
315 |
if((*stack_head)==NULL) |
|
316 |
|
if((env->head)==NULL) { |
317 |
|
printerr("Too Few Arguments"); |
318 |
|
env->err=1; |
319 |
return; |
return; |
320 |
|
} |
321 |
|
typenum=env->head->item->type; |
322 |
|
toss(env); |
323 |
|
switch(typenum){ |
324 |
|
case integer: |
325 |
|
push_sym(env, "integer"); |
326 |
|
break; |
327 |
|
case string: |
328 |
|
push_sym(env, "string"); |
329 |
|
break; |
330 |
|
case symb: |
331 |
|
push_sym(env, "symbol"); |
332 |
|
break; |
333 |
|
case func: |
334 |
|
push_sym(env, "function"); |
335 |
|
break; |
336 |
|
case list: |
337 |
|
push_sym(env, "list"); |
338 |
|
break; |
339 |
|
} |
340 |
|
} |
341 |
|
|
342 |
switch((*stack_head)->type) { |
/* Prints the top element of the stack. */ |
343 |
case value: |
void print_h(stackitem *stack_head) |
344 |
printf("%d", (*stack_head)->content.val); |
{ |
345 |
|
switch(stack_head->item->type) { |
346 |
|
case integer: |
347 |
|
printf("%d", stack_head->item->content.val); |
348 |
break; |
break; |
349 |
case string: |
case string: |
350 |
printf("%s", (char*)(*stack_head)->content.ptr); |
printf("%s", (char*)stack_head->item->content.ptr); |
351 |
break; |
break; |
352 |
case ref: |
case symb: |
353 |
printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
354 |
break; |
break; |
355 |
case symbol: |
case func: |
356 |
default: |
printf("#<function %p>", (funcp)(stack_head->item->content.ptr)); |
357 |
printf("%p", (*stack_head)->content.ptr); |
break; |
358 |
|
case list: |
359 |
|
/* A list is just a stack, so make stack_head point to it */ |
360 |
|
stack_head=(stackitem *)(stack_head->item->content.ptr); |
361 |
|
printf("[ "); |
362 |
|
while(stack_head != NULL) { |
363 |
|
print_h(stack_head); |
364 |
|
printf(" "); |
365 |
|
stack_head=stack_head->next; |
366 |
|
} |
367 |
|
printf("]"); |
368 |
break; |
break; |
369 |
} |
} |
370 |
} |
} |
371 |
|
|
372 |
|
extern void print_(environment *env) { |
373 |
|
if(env->head==NULL) { |
374 |
|
printerr("Too Few Arguments"); |
375 |
|
env->err=1; |
376 |
|
return; |
377 |
|
} |
378 |
|
print_h(env->head); |
379 |
|
} |
380 |
|
|
381 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
382 |
extern void print(stackitem** stack_head) |
extern void print(environment *env) |
383 |
{ |
{ |
384 |
print_(stack_head); |
print_(env); |
385 |
toss(stack_head); |
if(env->err) return; |
386 |
|
toss(env); |
387 |
} |
} |
388 |
|
|
389 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
390 |
void print_st(stackitem* stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
391 |
{ |
{ |
392 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
393 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
|
|
|
394 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
395 |
print_(&stack_head); |
print_h(stack_head); |
396 |
nl(); |
nl(); |
397 |
} |
} |
398 |
|
|
399 |
/* Prints the stack. */ |
/* Prints the stack. */ |
400 |
extern void printstack(stackitem** stack_head) |
extern void printstack(environment *env) |
401 |
{ |
{ |
402 |
if(*stack_head != NULL) { |
if(env->head == NULL) { |
403 |
print_st(*stack_head, 1); |
return; |
|
printf("\n"); |
|
404 |
} |
} |
405 |
|
print_st(env->head, 1); |
406 |
|
nl(); |
407 |
} |
} |
408 |
|
|
409 |
/* If the top element is a reference, determine if it's a reference to a |
/* Swap the two top elements on the stack. */ |
410 |
function, and if it is, toss the reference and execute the function. */ |
extern void swap(environment *env) |
|
extern void eval(stackitem** stack_head) |
|
411 |
{ |
{ |
412 |
funcp in_func; |
stackitem *temp= env->head; |
413 |
|
|
414 |
|
if(env->head==NULL || env->head->next==NULL) { |
415 |
|
printerr("Too Few Arguments"); |
416 |
|
env->err=1; |
417 |
|
return; |
418 |
|
} |
419 |
|
|
420 |
|
env->head= env->head->next; |
421 |
|
temp->next= env->head->next; |
422 |
|
env->head->next= temp; |
423 |
|
} |
424 |
|
|
425 |
if((*stack_head)==NULL || (*stack_head)->type!=ref) |
/* Rotate the first three elements on the stack. */ |
426 |
|
extern void rot(environment *env) |
427 |
|
{ |
428 |
|
stackitem *temp= env->head; |
429 |
|
|
430 |
|
if(env->head==NULL || env->head->next==NULL |
431 |
|
|| env->head->next->next==NULL) { |
432 |
|
printerr("Too Few Arguments"); |
433 |
|
env->err=1; |
434 |
return; |
return; |
435 |
|
} |
436 |
|
|
437 |
|
env->head= env->head->next->next; |
438 |
|
temp->next->next= env->head->next; |
439 |
|
env->head->next= temp; |
440 |
|
} |
441 |
|
|
442 |
|
/* Recall a value from a symbol, if bound */ |
443 |
|
extern void rcl(environment *env) |
444 |
|
{ |
445 |
|
value *val; |
446 |
|
|
447 |
if(((stackitem*)(*stack_head)->content.ptr)->type==func) { |
if(env->head == NULL) { |
448 |
in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; |
printerr("Too Few Arguments"); |
449 |
toss(stack_head); |
env->err=1; |
|
(*in_func)(stack_head); |
|
450 |
return; |
return; |
451 |
} |
} |
452 |
|
|
453 |
|
if(env->head->item->type!=symb) { |
454 |
|
printerr("Bad Argument Type"); |
455 |
|
env->err=2; |
456 |
|
return; |
457 |
|
} |
458 |
|
|
459 |
|
val=((symbol *)(env->head->item->content.ptr))->val; |
460 |
|
if(val == NULL){ |
461 |
|
printerr("Unbound Variable"); |
462 |
|
env->err=3; |
463 |
|
return; |
464 |
|
} |
465 |
|
toss(env); /* toss the symbol */ |
466 |
|
if(env->err) return; |
467 |
|
push_val(env, val); /* Return its bound value */ |
468 |
} |
} |
469 |
|
|
470 |
/* Parse input. */ |
/* If the top element is a symbol, determine if it's bound to a |
471 |
int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line) |
function value, and if it is, toss the symbol and execute the |
472 |
|
function. */ |
473 |
|
extern void eval(environment *env) |
474 |
{ |
{ |
475 |
char *temp, *rest; |
funcp in_func; |
476 |
int itemp; |
value* temp_val; |
477 |
size_t inlength= strlen(in_line)+1; |
stackitem* iterator; |
|
int convert= 0; |
|
478 |
|
|
479 |
temp= malloc(inlength); |
if(env->head==NULL) { |
480 |
rest= malloc(inlength); |
printerr("Too Few Arguments"); |
481 |
|
env->err=1; |
482 |
|
return; |
483 |
|
} |
484 |
|
|
485 |
do { |
eval_start: |
|
/* If string */ |
|
|
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
|
|
push_cstring(stack_head, temp); |
|
|
break; |
|
|
} |
|
|
/* If value */ |
|
|
if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { |
|
|
push_val(stack_head, itemp); |
|
|
break; |
|
|
} |
|
|
/* Escape ';' with '\' */ |
|
|
if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { |
|
|
temp[1]= '\0'; |
|
|
push_ref(stack_head, in_hash, temp); |
|
|
break; |
|
|
} |
|
|
/* If symbol */ |
|
|
if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) { |
|
|
push_ref(stack_head, in_hash, temp); |
|
|
break; |
|
|
} |
|
|
/* If ';' */ |
|
|
if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') { |
|
|
eval(stack_head); /* Evaluate top element */ |
|
|
break; |
|
|
} |
|
|
} while(0); |
|
486 |
|
|
487 |
|
switch(env->head->item->type) { |
488 |
|
/* if it's a symbol */ |
489 |
|
case symb: |
490 |
|
rcl(env); /* get its contents */ |
491 |
|
if(env->err) return; |
492 |
|
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
493 |
|
goto eval_start; |
494 |
|
} |
495 |
|
return; |
496 |
|
|
497 |
free(temp); |
/* If it's a lone function value, run it */ |
498 |
|
case func: |
499 |
|
in_func= (funcp)(env->head->item->content.ptr); |
500 |
|
toss(env); |
501 |
|
if(env->err) return; |
502 |
|
return (*in_func)(env); |
503 |
|
|
504 |
|
/* If it's a list */ |
505 |
|
case list: |
506 |
|
temp_val= env->head->item; |
507 |
|
env->head->item->refcount++; |
508 |
|
toss(env); |
509 |
|
if(env->err) return; |
510 |
|
iterator= (stackitem*)temp_val->content.ptr; |
511 |
|
while(iterator!=NULL) { |
512 |
|
push_val(env, iterator->item); |
513 |
|
if(env->head->item->type==symb |
514 |
|
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
515 |
|
toss(env); |
516 |
|
if(env->err) return; |
517 |
|
if(iterator->next == NULL){ |
518 |
|
free_val(temp_val); |
519 |
|
goto eval_start; |
520 |
|
} |
521 |
|
eval(env); |
522 |
|
if(env->err) return; |
523 |
|
} |
524 |
|
iterator= iterator->next; |
525 |
|
} |
526 |
|
free_val(temp_val); |
527 |
|
return; |
528 |
|
|
529 |
if(convert<2) { |
default: |
530 |
free(rest); |
return; |
|
return 0; |
|
531 |
} |
} |
|
|
|
|
stack_read(stack_head, in_hash, rest); |
|
|
|
|
|
free(rest); |
|
|
return 1; |
|
532 |
} |
} |
533 |
|
|
534 |
/* Make a list. */ |
/* Reverse (flip) a list */ |
535 |
extern void pack(stackitem** stack_head) |
extern void rev(environment *env){ |
536 |
{ |
stackitem *old_head, *new_head, *item; |
537 |
void* delimiter; |
|
538 |
stackitem *iterator, *temp, *pack; |
if((env->head)==NULL) { |
539 |
|
printerr("Too Few Arguments"); |
540 |
|
env->err=1; |
541 |
|
return; |
542 |
|
} |
543 |
|
|
544 |
if((*stack_head)==NULL) /* No delimiter */ |
if(env->head->item->type!=list) { |
545 |
|
printerr("Bad Argument Type"); |
546 |
|
env->err=2; |
547 |
return; |
return; |
548 |
|
} |
549 |
|
|
550 |
delimiter= (*stack_head)->content.ptr; /* Get delimiter */ |
old_head=(stackitem *)(env->head->item->content.ptr); |
551 |
toss(stack_head); |
new_head=NULL; |
552 |
|
while(old_head != NULL){ |
553 |
|
item=old_head; |
554 |
|
old_head=old_head->next; |
555 |
|
item->next=new_head; |
556 |
|
new_head=item; |
557 |
|
} |
558 |
|
env->head->item->content.ptr=new_head; |
559 |
|
} |
560 |
|
|
561 |
iterator= *stack_head; |
/* Make a list. */ |
562 |
|
extern void pack(environment *env) |
563 |
|
{ |
564 |
|
stackitem *iterator, *temp; |
565 |
|
value *pack; |
566 |
|
|
567 |
/* Search for first delimiter */ |
iterator= env->head; |
|
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
|
|
iterator= iterator->next; |
|
568 |
|
|
569 |
/* Extract list */ |
if(iterator==NULL |
570 |
temp= *stack_head; |
|| (iterator->item->type==symb |
571 |
*stack_head= iterator->next; |
&& ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { |
572 |
iterator->next= NULL; |
temp= NULL; |
573 |
|
toss(env); |
574 |
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
} else { |
575 |
toss(stack_head); |
/* Search for first delimiter */ |
576 |
|
while(iterator->next!=NULL |
577 |
|
&& (iterator->next->item->type!=symb |
578 |
|
|| ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) |
579 |
|
iterator= iterator->next; |
580 |
|
|
581 |
|
/* Extract list */ |
582 |
|
temp= env->head; |
583 |
|
env->head= iterator->next; |
584 |
|
iterator->next= NULL; |
585 |
|
|
586 |
|
if(env->head!=NULL) |
587 |
|
toss(env); |
588 |
|
} |
589 |
|
|
590 |
/* Push list */ |
/* Push list */ |
591 |
pack= malloc(sizeof(stackitem)); |
pack= malloc(sizeof(value)); |
592 |
pack->type= list; |
pack->type= list; |
593 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
594 |
|
pack->refcount= 1; |
595 |
|
|
596 |
push(stack_head, pack); |
push_val(env, pack); |
597 |
|
rev(env); |
598 |
} |
} |
599 |
|
|
600 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
601 |
extern void expand(stackitem** stack_head) |
extern void expand(environment *env) |
602 |
{ |
{ |
603 |
stackitem *temp, *new_head; |
stackitem *temp, *new_head; |
604 |
|
|
605 |
/* Is top element a list? */ |
/* Is top element a list? */ |
606 |
if((*stack_head)==NULL || (*stack_head)->type!=list) |
if(env->head==NULL) { |
607 |
|
printerr("Too Few Arguments"); |
608 |
|
env->err=1; |
609 |
|
return; |
610 |
|
} |
611 |
|
if(env->head->item->type!=list) { |
612 |
|
printerr("Bad Argument Type"); |
613 |
|
env->err=2; |
614 |
|
return; |
615 |
|
} |
616 |
|
|
617 |
|
rev(env); |
618 |
|
|
619 |
|
if(env->err) |
620 |
return; |
return; |
621 |
|
|
622 |
/* The first list element is the new stack head */ |
/* The first list element is the new stack head */ |
623 |
new_head= temp= (*stack_head)->content.ptr; |
new_head= temp= env->head->item->content.ptr; |
624 |
toss(stack_head); |
|
625 |
|
env->head->item->refcount++; |
626 |
|
toss(env); |
627 |
|
|
628 |
/* Search the end of the list */ |
/* Find the end of the list */ |
629 |
while(temp->next!=NULL) |
while(temp->next!=NULL) |
630 |
temp= temp->next; |
temp= temp->next; |
631 |
|
|
632 |
/* Connect the the tail of the list with the old stack head */ |
/* Connect the tail of the list with the old stack head */ |
633 |
temp->next= *stack_head; |
temp->next= env->head; |
634 |
*stack_head= new_head; /* ...and voila! */ |
env->head= new_head; /* ...and voila! */ |
|
} |
|
635 |
|
|
636 |
/* Swap the two top elements on the stack. */ |
} |
|
extern void swap(stackitem** stack_head) |
|
|
{ |
|
|
stackitem* temp= (*stack_head); |
|
|
|
|
|
if((*stack_head)==NULL || (*stack_head)->next==NULL) |
|
|
return; |
|
|
|
|
|
*stack_head= (*stack_head)->next; |
|
|
temp->next= (*stack_head)->next; |
|
|
(*stack_head)->next= temp; |
|
|
} |
|
637 |
|
|
638 |
/* Compares two elements by reference. */ |
/* Compares two elements by reference. */ |
639 |
extern void eq(stackitem** stack_head) |
extern void eq(environment *env) |
640 |
{ |
{ |
641 |
void *left, *right; |
void *left, *right; |
642 |
int result; |
int result; |
643 |
|
|
644 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) |
if((env->head)==NULL || env->head->next==NULL) { |
645 |
|
printerr("Too Few Arguments"); |
646 |
|
env->err=1; |
647 |
return; |
return; |
648 |
|
} |
649 |
|
|
650 |
left= (*stack_head)->content.ptr; |
left= env->head->item->content.ptr; |
651 |
swap(stack_head); |
swap(env); |
652 |
right= (*stack_head)->content.ptr; |
right= env->head->item->content.ptr; |
653 |
result= (left==right); |
result= (left==right); |
654 |
|
|
655 |
toss(stack_head); toss(stack_head); |
toss(env); toss(env); |
656 |
push_val(stack_head, (left==right)); |
push_int(env, result); |
657 |
} |
} |
658 |
|
|
659 |
/* Negates the top element on the stack. */ |
/* Negates the top element on the stack. */ |
660 |
extern void not(stackitem** stack_head) |
extern void not(environment *env) |
661 |
{ |
{ |
662 |
int value; |
int val; |
663 |
|
|
664 |
if((*stack_head)==NULL || (*stack_head)->type!=value) |
if((env->head)==NULL) { |
665 |
|
printerr("Too Few Arguments"); |
666 |
|
env->err=1; |
667 |
return; |
return; |
668 |
|
} |
669 |
|
|
670 |
value= (*stack_head)->content.val; |
if(env->head->item->type!=integer) { |
671 |
toss(stack_head); |
printerr("Bad Argument Type"); |
672 |
push_val(stack_head, !value); |
env->err=2; |
673 |
|
return; |
674 |
|
} |
675 |
|
|
676 |
|
val= env->head->item->content.val; |
677 |
|
toss(env); |
678 |
|
push_int(env, !val); |
679 |
} |
} |
680 |
|
|
681 |
/* 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 |
682 |
same. */ |
same. */ |
683 |
extern void neq(stackitem** stack_head) |
extern void neq(environment *env) |
684 |
{ |
{ |
685 |
eq(stack_head); |
eq(env); |
686 |
not(stack_head); |
not(env); |
687 |
} |
} |
688 |
|
|
689 |
/* Give a symbol some content. */ |
/* Give a symbol some content. */ |
690 |
extern void def(stackitem** stack_head) |
extern void def(environment *env) |
691 |
{ |
{ |
692 |
stackitem *temp, *value; |
symbol *sym; |
693 |
|
|
694 |
if(*stack_head==NULL || (*stack_head)->next==NULL |
/* Needs two values on the stack, the top one must be a symbol */ |
695 |
|| (*stack_head)->type!=ref) |
if(env->head==NULL || env->head->next==NULL) { |
696 |
|
printerr("Too Few Arguments"); |
697 |
|
env->err=1; |
698 |
return; |
return; |
699 |
|
} |
700 |
|
|
701 |
|
if(env->head->item->type!=symb) { |
702 |
|
printerr("Bad Argument Type"); |
703 |
|
env->err=2; |
704 |
|
return; |
705 |
|
} |
706 |
|
|
707 |
|
/* long names are a pain */ |
708 |
|
sym=env->head->item->content.ptr; |
709 |
|
|
710 |
temp= (*stack_head)->content.ptr; |
/* if the symbol was bound to something else, throw it away */ |
711 |
value= (*stack_head)->next; |
if(sym->val != NULL) |
712 |
temp->content= value->content; |
free_val(sym->val); |
713 |
value->content.ptr=NULL; |
|
714 |
temp->type= value->type; |
/* Bind the symbol to the value */ |
715 |
|
sym->val= env->head->next->item; |
716 |
|
sym->val->refcount++; /* Increase the reference counter */ |
717 |
|
|
718 |
toss(stack_head); toss(stack_head); |
toss(env); toss(env); |
719 |
} |
} |
720 |
|
|
721 |
|
extern void clear(environment *); |
722 |
|
void forget_sym(symbol **); |
723 |
|
|
724 |
/* Quit stack. */ |
/* Quit stack. */ |
725 |
extern void quit() |
extern void quit(environment *env) |
726 |
{ |
{ |
727 |
|
long i; |
728 |
|
|
729 |
|
clear(env); |
730 |
|
if (env->err) return; |
731 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
732 |
|
while(env->symbols[i]!= NULL) { |
733 |
|
forget_sym(&(env->symbols[i])); |
734 |
|
env->symbols[i]= NULL; |
735 |
|
} |
736 |
|
} |
737 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
738 |
} |
} |
739 |
|
|
740 |
|
/* Clear stack */ |
741 |
|
extern void clear(environment *env) |
742 |
|
{ |
743 |
|
while(env->head!=NULL) |
744 |
|
toss(env); |
745 |
|
} |
746 |
|
|
747 |
|
/* List all defined words */ |
748 |
|
extern void words(environment *env) |
749 |
|
{ |
750 |
|
symbol *temp; |
751 |
|
int i; |
752 |
|
|
753 |
|
for(i= 0; i<HASHTBLSIZE; i++) { |
754 |
|
temp= env->symbols[i]; |
755 |
|
while(temp!=NULL) { |
756 |
|
printf("%s\n", temp->id); |
757 |
|
temp= temp->next; |
758 |
|
} |
759 |
|
} |
760 |
|
} |
761 |
|
|
762 |
|
/* Internal forget function */ |
763 |
|
void forget_sym(symbol **hash_entry) { |
764 |
|
symbol *temp; |
765 |
|
|
766 |
|
temp= *hash_entry; |
767 |
|
*hash_entry= (*hash_entry)->next; |
768 |
|
|
769 |
|
if(temp->val!=NULL) { |
770 |
|
free_val(temp->val); |
771 |
|
} |
772 |
|
free(temp->id); |
773 |
|
free(temp); |
774 |
|
} |
775 |
|
|
776 |
|
/* Forgets a symbol (remove it from the hash table) */ |
777 |
|
extern void forget(environment *env) |
778 |
|
{ |
779 |
|
char* sym_id; |
780 |
|
stackitem *stack_head= env->head; |
781 |
|
|
782 |
|
if(stack_head==NULL) { |
783 |
|
printerr("Too Few Arguments"); |
784 |
|
env->err=1; |
785 |
|
return; |
786 |
|
} |
787 |
|
|
788 |
|
if(stack_head->item->type!=symb) { |
789 |
|
printerr("Bad Argument Type"); |
790 |
|
env->err=2; |
791 |
|
return; |
792 |
|
} |
793 |
|
|
794 |
|
sym_id= ((symbol*)(stack_head->item->content.ptr))->id; |
795 |
|
toss(env); |
796 |
|
|
797 |
|
return forget_sym(hash(env->symbols, sym_id)); |
798 |
|
} |
799 |
|
|
800 |
|
/* Returns the current error number to the stack */ |
801 |
|
extern void errn(environment *env){ |
802 |
|
push_int(env, env->err); |
803 |
|
} |
804 |
|
|
805 |
|
extern void read(environment*); |
806 |
|
|
807 |
int main() |
int main() |
808 |
{ |
{ |
809 |
stackitem* s= NULL; |
environment myenv; |
810 |
hashtbl myhash; |
|
811 |
char in_string[100]; |
init_env(&myenv); |
812 |
|
|
813 |
|
while(1) { |
814 |
|
if(myenv.in_string==NULL) |
815 |
|
printstack(&myenv); |
816 |
|
read(&myenv); |
817 |
|
if(myenv.err) { |
818 |
|
printf("(error %d) ", myenv.err); |
819 |
|
myenv.err=0; |
820 |
|
} else if(myenv.head!=NULL |
821 |
|
&& myenv.head->item->type==symb |
822 |
|
&& ((symbol*)(myenv.head->item->content.ptr))->id[0]==';') { |
823 |
|
toss(&myenv); /* No error check in main */ |
824 |
|
eval(&myenv); |
825 |
|
} |
826 |
|
} |
827 |
|
quit(&myenv); |
828 |
|
return EXIT_FAILURE; |
829 |
|
} |
830 |
|
|
831 |
init_hashtbl(myhash); |
/* + */ |
832 |
|
extern void sx_2b(environment *env) { |
833 |
|
int a, b; |
834 |
|
size_t len; |
835 |
|
char* new_string; |
836 |
|
value *a_val, *b_val; |
837 |
|
|
838 |
|
if((env->head)==NULL || env->head->next==NULL) { |
839 |
|
printerr("Too Few Arguments"); |
840 |
|
env->err=1; |
841 |
|
return; |
842 |
|
} |
843 |
|
|
844 |
printf("okidok\n "); |
if(env->head->item->type==string |
845 |
|
&& env->head->next->item->type==string) { |
846 |
|
a_val= env->head->item; |
847 |
|
b_val= env->head->next->item; |
848 |
|
a_val->refcount++; |
849 |
|
b_val->refcount++; |
850 |
|
toss(env); if(env->err) return; |
851 |
|
toss(env); if(env->err) return; |
852 |
|
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
853 |
|
new_string= malloc(len); |
854 |
|
strcpy(new_string, b_val->content.ptr); |
855 |
|
strcat(new_string, a_val->content.ptr); |
856 |
|
free_val(a_val); free_val(b_val); |
857 |
|
push_cstring(env, new_string); |
858 |
|
free(new_string); |
859 |
|
return; |
860 |
|
} |
861 |
|
|
862 |
|
if(env->head->item->type!=integer |
863 |
|
|| env->head->next->item->type!=integer) { |
864 |
|
printerr("Bad Argument Type"); |
865 |
|
env->err=2; |
866 |
|
return; |
867 |
|
} |
868 |
|
a=env->head->item->content.val; |
869 |
|
toss(env); |
870 |
|
if(env->err) return; |
871 |
|
if(env->head->item->refcount == 1) |
872 |
|
env->head->item->content.val += a; |
873 |
|
else { |
874 |
|
b=env->head->item->content.val; |
875 |
|
toss(env); |
876 |
|
if(env->err) return; |
877 |
|
push_int(env, a+b); |
878 |
|
} |
879 |
|
} |
880 |
|
|
881 |
while(fgets(in_string, 100, stdin) != NULL) { |
/* - */ |
882 |
stack_read(&s, myhash, in_string); |
extern void sx_2d(environment *env) { |
883 |
printf("okidok\n "); |
int a, b; |
884 |
|
|
885 |
|
if((env->head)==NULL || env->head->next==NULL) { |
886 |
|
printerr("Too Few Arguments"); |
887 |
|
env->err=1; |
888 |
|
return; |
889 |
|
} |
890 |
|
|
891 |
|
if(env->head->item->type!=integer |
892 |
|
|| env->head->next->item->type!=integer) { |
893 |
|
printerr("Bad Argument Type"); |
894 |
|
env->err=2; |
895 |
|
return; |
896 |
|
} |
897 |
|
a=env->head->item->content.val; |
898 |
|
toss(env); |
899 |
|
if(env->err) return; |
900 |
|
if(env->head->item->refcount == 1) |
901 |
|
env->head->item->content.val -= a; |
902 |
|
else { |
903 |
|
b=env->head->item->content.val; |
904 |
|
toss(env); |
905 |
|
if(env->err) return; |
906 |
|
push_int(env, b-a); |
907 |
} |
} |
908 |
|
} |
909 |
|
|
910 |
exit(EXIT_SUCCESS); |
/* > */ |
911 |
|
extern void sx_3e(environment *env) { |
912 |
|
int a, b; |
913 |
|
|
914 |
|
if((env->head)==NULL || env->head->next==NULL) { |
915 |
|
printerr("Too Few Arguments"); |
916 |
|
env->err=1; |
917 |
|
return; |
918 |
|
} |
919 |
|
|
920 |
|
if(env->head->item->type!=integer |
921 |
|
|| env->head->next->item->type!=integer) { |
922 |
|
printerr("Bad Argument Type"); |
923 |
|
env->err=2; |
924 |
|
return; |
925 |
|
} |
926 |
|
a=env->head->item->content.val; |
927 |
|
toss(env); |
928 |
|
if(env->err) return; |
929 |
|
if(env->head->item->refcount == 1) |
930 |
|
env->head->item->content.val = (env->head->item->content.val > a); |
931 |
|
else { |
932 |
|
b=env->head->item->content.val; |
933 |
|
toss(env); |
934 |
|
if(env->err) return; |
935 |
|
push_int(env, b>a); |
936 |
|
} |
937 |
} |
} |
938 |
|
|
939 |
/* Local Variables: */ |
/* Return copy of a value */ |
940 |
/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ |
value *copy_val(value *old_value){ |
941 |
/* End: */ |
stackitem *old_item, *new_item, *prev_item; |
942 |
|
|
943 |
|
value *new_value=malloc(sizeof(value)); |
944 |
|
|
945 |
|
new_value->type=old_value->type; |
946 |
|
new_value->refcount=0; /* This is increased if/when this |
947 |
|
value is referenced somewhere, like |
948 |
|
in a stack item or a variable */ |
949 |
|
switch(old_value->type){ |
950 |
|
case integer: |
951 |
|
new_value->content.val=old_value->content.val; |
952 |
|
break; |
953 |
|
case string: |
954 |
|
(char *)(new_value->content.ptr) |
955 |
|
= strdup((char *)(old_value->content.ptr)); |
956 |
|
break; |
957 |
|
case func: |
958 |
|
case symb: |
959 |
|
new_value->content.ptr=old_value->content.ptr; |
960 |
|
break; |
961 |
|
case list: |
962 |
|
new_value->content.ptr=NULL; |
963 |
|
|
964 |
|
prev_item=NULL; |
965 |
|
old_item=(stackitem *)(old_value->content.ptr); |
966 |
|
|
967 |
|
while(old_item != NULL) { /* While list is not empty */ |
968 |
|
new_item= malloc(sizeof(stackitem)); |
969 |
|
new_item->item=copy_val(old_item->item); /* recurse */ |
970 |
|
new_item->next=NULL; |
971 |
|
if(prev_item != NULL) /* If this wasn't the first item */ |
972 |
|
prev_item->next=new_item; /* point the previous item to the |
973 |
|
new item */ |
974 |
|
else |
975 |
|
new_value->content.ptr=new_item; |
976 |
|
old_item=old_item->next; |
977 |
|
prev_item=new_item; |
978 |
|
} |
979 |
|
break; |
980 |
|
} |
981 |
|
return new_value; |
982 |
|
} |
983 |
|
|
984 |
|
/* duplicates an item on the stack */ |
985 |
|
extern void dup(environment *env) { |
986 |
|
if((env->head)==NULL) { |
987 |
|
printerr("Too Few Arguments"); |
988 |
|
env->err=1; |
989 |
|
return; |
990 |
|
} |
991 |
|
push_val(env, copy_val(env->head->item)); |
992 |
|
} |
993 |
|
|
994 |
|
/* "if", If-Then */ |
995 |
|
extern void sx_6966(environment *env) { |
996 |
|
|
997 |
|
int truth; |
998 |
|
|
999 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1000 |
|
printerr("Too Few Arguments"); |
1001 |
|
env->err=1; |
1002 |
|
return; |
1003 |
|
} |
1004 |
|
|
1005 |
|
if(env->head->next->item->type != integer) { |
1006 |
|
printerr("Bad Argument Type"); |
1007 |
|
env->err=2; |
1008 |
|
return; |
1009 |
|
} |
1010 |
|
|
1011 |
|
swap(env); |
1012 |
|
if(env->err) return; |
1013 |
|
|
1014 |
|
truth=env->head->item->content.val; |
1015 |
|
|
1016 |
|
toss(env); |
1017 |
|
if(env->err) return; |
1018 |
|
|
1019 |
|
if(truth) |
1020 |
|
eval(env); |
1021 |
|
else |
1022 |
|
toss(env); |
1023 |
|
} |
1024 |
|
|
1025 |
|
/* If-Then-Else */ |
1026 |
|
extern void ifelse(environment *env) { |
1027 |
|
|
1028 |
|
int truth; |
1029 |
|
|
1030 |
|
if((env->head)==NULL || env->head->next==NULL |
1031 |
|
|| env->head->next->next==NULL) { |
1032 |
|
printerr("Too Few Arguments"); |
1033 |
|
env->err=1; |
1034 |
|
return; |
1035 |
|
} |
1036 |
|
|
1037 |
|
if(env->head->next->next->item->type != integer) { |
1038 |
|
printerr("Bad Argument Type"); |
1039 |
|
env->err=2; |
1040 |
|
return; |
1041 |
|
} |
1042 |
|
|
1043 |
|
rot(env); |
1044 |
|
if(env->err) return; |
1045 |
|
|
1046 |
|
truth=env->head->item->content.val; |
1047 |
|
|
1048 |
|
toss(env); |
1049 |
|
if(env->err) return; |
1050 |
|
|
1051 |
|
if(!truth) |
1052 |
|
swap(env); |
1053 |
|
if(env->err) return; |
1054 |
|
|
1055 |
|
toss(env); |
1056 |
|
if(env->err) return; |
1057 |
|
|
1058 |
|
eval(env); |
1059 |
|
} |
1060 |
|
|
1061 |
|
/* while */ |
1062 |
|
extern void sx_7768696c65(environment *env) { |
1063 |
|
|
1064 |
|
int truth; |
1065 |
|
value *loop, *test; |
1066 |
|
|
1067 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1068 |
|
printerr("Too Few Arguments"); |
1069 |
|
env->err=1; |
1070 |
|
return; |
1071 |
|
} |
1072 |
|
|
1073 |
|
loop= env->head->item; |
1074 |
|
loop->refcount++; |
1075 |
|
toss(env); if(env->err) return; |
1076 |
|
|
1077 |
|
test= env->head->item; |
1078 |
|
test->refcount++; |
1079 |
|
toss(env); if(env->err) return; |
1080 |
|
|
1081 |
|
do { |
1082 |
|
push_val(env, test); |
1083 |
|
eval(env); |
1084 |
|
|
1085 |
|
if(env->head->item->type != integer) { |
1086 |
|
printerr("Bad Argument Type"); |
1087 |
|
env->err=2; |
1088 |
|
return; |
1089 |
|
} |
1090 |
|
|
1091 |
|
truth= env->head->item->content.val; |
1092 |
|
toss(env); if(env->err) return; |
1093 |
|
|
1094 |
|
if(truth) { |
1095 |
|
push_val(env, loop); |
1096 |
|
eval(env); |
1097 |
|
} else { |
1098 |
|
toss(env); |
1099 |
|
} |
1100 |
|
|
1101 |
|
} while(truth); |
1102 |
|
|
1103 |
|
free_val(test); |
1104 |
|
free_val(loop); |
1105 |
|
} |
1106 |
|
|
1107 |
|
/* For-loop */ |
1108 |
|
extern void sx_666f72(environment *env) { |
1109 |
|
|
1110 |
|
value *loop, *foo; |
1111 |
|
stackitem *iterator; |
1112 |
|
|
1113 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1114 |
|
printerr("Too Few Arguments"); |
1115 |
|
env->err=1; |
1116 |
|
return; |
1117 |
|
} |
1118 |
|
|
1119 |
|
if(env->head->next->item->type != list) { |
1120 |
|
printerr("Bad Argument Type"); |
1121 |
|
env->err=2; |
1122 |
|
return; |
1123 |
|
} |
1124 |
|
|
1125 |
|
loop= env->head->item; |
1126 |
|
loop->refcount++; |
1127 |
|
toss(env); if(env->err) return; |
1128 |
|
|
1129 |
|
foo= env->head->item; |
1130 |
|
foo->refcount++; |
1131 |
|
toss(env); if(env->err) return; |
1132 |
|
|
1133 |
|
iterator= foo->content.ptr; |
1134 |
|
|
1135 |
|
while(iterator!=NULL) { |
1136 |
|
push_val(env, iterator->item); |
1137 |
|
push_val(env, loop); |
1138 |
|
eval(env); if(env->err) return; |
1139 |
|
iterator= iterator->next; |
1140 |
|
} |
1141 |
|
|
1142 |
|
free_val(loop); |
1143 |
|
free_val(foo); |
1144 |
|
} |
1145 |
|
|
1146 |
|
/* 'to' */ |
1147 |
|
extern void to(environment *env) { |
1148 |
|
int i, start, ending; |
1149 |
|
stackitem *temp_head; |
1150 |
|
value *temp_val; |
1151 |
|
|
1152 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1153 |
|
printerr("Too Few Arguments"); |
1154 |
|
env->err=1; |
1155 |
|
return; |
1156 |
|
} |
1157 |
|
|
1158 |
|
if(env->head->item->type!=integer |
1159 |
|
|| env->head->next->item->type!=integer) { |
1160 |
|
printerr("Bad Argument Type"); |
1161 |
|
env->err=2; |
1162 |
|
return; |
1163 |
|
} |
1164 |
|
|
1165 |
|
ending= env->head->item->content.val; |
1166 |
|
toss(env); if(env->err) return; |
1167 |
|
start= env->head->item->content.val; |
1168 |
|
toss(env); if(env->err) return; |
1169 |
|
|
1170 |
|
temp_head= env->head; |
1171 |
|
env->head= NULL; |
1172 |
|
|
1173 |
|
if(ending>=start) { |
1174 |
|
for(i= ending; i>=start; i--) |
1175 |
|
push_int(env, i); |
1176 |
|
} else { |
1177 |
|
for(i= ending; i<=start; i++) |
1178 |
|
push_int(env, i); |
1179 |
|
} |
1180 |
|
|
1181 |
|
temp_val= malloc(sizeof(value)); |
1182 |
|
temp_val->content.ptr= env->head; |
1183 |
|
temp_val->refcount= 1; |
1184 |
|
temp_val->type= list; |
1185 |
|
env->head= temp_head; |
1186 |
|
push_val(env, temp_val); |
1187 |
|
} |
1188 |
|
|
1189 |
|
/* Read a string */ |
1190 |
|
extern void readline(environment *env) { |
1191 |
|
char in_string[101]; |
1192 |
|
|
1193 |
|
fgets(in_string, 100, stdin); |
1194 |
|
push_cstring(env, in_string); |
1195 |
|
} |
1196 |
|
|
1197 |
|
/* Read a value and place on stack */ |
1198 |
|
extern void read(environment *env) { |
1199 |
|
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
1200 |
|
const char strform[]= "\"%[^\"]\"%n"; |
1201 |
|
const char intform[]= "%i%n"; |
1202 |
|
const char blankform[]= "%*[ \t]%n"; |
1203 |
|
const char ebrackform[]= "%*1[]]%n"; |
1204 |
|
const char semicform[]= "%*1[;]%n"; |
1205 |
|
const char bbrackform[]= "%*1[[]%n"; |
1206 |
|
|
1207 |
|
int itemp, readlength= -1; |
1208 |
|
static int depth= 0; |
1209 |
|
char *rest, *match; |
1210 |
|
size_t inlength; |
1211 |
|
|
1212 |
|
if(env->in_string==NULL) { |
1213 |
|
readline(env); if(env->err) return; |
1214 |
|
|
1215 |
|
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1216 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
1217 |
|
strcpy(env->in_string, env->head->item->content.ptr); |
1218 |
|
toss(env); if(env->err) return; |
1219 |
|
} |
1220 |
|
|
1221 |
|
inlength= strlen(env->in_string)+1; |
1222 |
|
match= malloc(inlength); |
1223 |
|
rest= malloc(inlength); |
1224 |
|
|
1225 |
|
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
1226 |
|
&& readlength != -1) { |
1227 |
|
; |
1228 |
|
} else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF |
1229 |
|
&& readlength != -1) { |
1230 |
|
push_int(env, itemp); |
1231 |
|
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
1232 |
|
&& readlength != -1) { |
1233 |
|
push_cstring(env, match); |
1234 |
|
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
1235 |
|
&& readlength != -1) { |
1236 |
|
push_sym(env, match); |
1237 |
|
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
1238 |
|
&& readlength != -1) { |
1239 |
|
pack(env); if(env->err) return; |
1240 |
|
if(depth != 0) depth--; |
1241 |
|
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
1242 |
|
&& readlength != -1) { |
1243 |
|
push_sym(env, ";"); |
1244 |
|
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
1245 |
|
&& readlength != -1) { |
1246 |
|
push_sym(env, "["); |
1247 |
|
depth++; |
1248 |
|
} else { |
1249 |
|
free(env->free_string); |
1250 |
|
env->in_string = env->free_string = NULL; |
1251 |
|
free(match); |
1252 |
|
} |
1253 |
|
if ( env->in_string != NULL) { |
1254 |
|
env->in_string += readlength; |
1255 |
|
} |
1256 |
|
|
1257 |
|
if(depth) |
1258 |
|
return read(env); |
1259 |
|
} |