1 |
/* printf, sscanf, fgets, fprintf */ |
/* printf, sscanf, fgets, fprintf, fopen, perror */ |
2 |
#include <stdio.h> |
#include <stdio.h> |
3 |
/* exit, EXIT_SUCCESS, malloc, free */ |
/* exit, EXIT_SUCCESS, malloc, free */ |
4 |
#include <stdlib.h> |
#include <stdlib.h> |
8 |
#include <dlfcn.h> |
#include <dlfcn.h> |
9 |
/* strcmp, strcpy, strlen, strcat, strdup */ |
/* strcmp, strcpy, strlen, strcat, strdup */ |
10 |
#include <string.h> |
#include <string.h> |
11 |
|
/* getopt, STDIN_FILENO, STDOUT_FILENO */ |
12 |
|
#include <unistd.h> |
13 |
|
/* EX_NOINPUT, EX_USAGE */ |
14 |
|
#include <sysexits.h> |
15 |
|
/* mtrace, muntrace */ |
16 |
|
#include <mcheck.h> |
17 |
|
|
18 |
#define HASHTBLSIZE 2048 |
#define HASHTBLSIZE 2048 |
19 |
|
|
64 |
stackitem *head; /* Head of the stack */ |
stackitem *head; /* Head of the stack */ |
65 |
hashtbl symbols; /* Hash table of all variable bindings */ |
hashtbl symbols; /* Hash table of all variable bindings */ |
66 |
int err; /* Error flag */ |
int err; /* Error flag */ |
|
int non_eval_flag; |
|
67 |
char *in_string; /* Input pending to be read */ |
char *in_string; /* Input pending to be read */ |
68 |
char *free_string; /* Free this string when all input is |
char *free_string; /* Free this string when all input is |
69 |
read from in_string */ |
read from in_string */ |
70 |
|
FILE *inputstream; /* stdin or a file, most likely */ |
71 |
|
int interactive; /* print prompts, stack, etc */ |
72 |
} environment; |
} environment; |
73 |
|
|
74 |
/* A type for pointers to external functions */ |
/* A type for pointers to external functions */ |
80 |
{ |
{ |
81 |
int i; |
int i; |
82 |
|
|
83 |
env->in_string= NULL; |
env->head= NULL; |
|
env->err= 0; |
|
|
env->non_eval_flag= 0; |
|
84 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
85 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
86 |
|
env->err= 0; |
87 |
|
env->in_string= NULL; |
88 |
|
env->free_string= NULL; |
89 |
|
env->inputstream= stdin; |
90 |
|
env->interactive= 1; |
91 |
} |
} |
92 |
|
|
93 |
void printerr(const char* in_string) { |
void printerr(const char* in_string) { |
112 |
free(item); /* free the stackitem */ |
free(item); /* free the stackitem */ |
113 |
item=temp; /* go to next stackitem */ |
item=temp; /* go to next stackitem */ |
114 |
} |
} |
|
free(val); /* Free the actual list value */ |
|
115 |
break; |
break; |
116 |
case integer: |
case integer: |
117 |
case func: |
case func: |
118 |
|
break; |
119 |
case symb: |
case symb: |
120 |
|
free(((symbol*)(val->content.ptr))->id); |
121 |
|
if(((symbol*)(val->content.ptr))->val!=NULL) |
122 |
|
free_val(((symbol*)(val->content.ptr))->val); |
123 |
|
free(val->content.ptr); |
124 |
break; |
break; |
125 |
} |
} |
126 |
|
free(val); /* Free the actual value structure */ |
127 |
} |
} |
128 |
} |
} |
129 |
|
|
189 |
|
|
190 |
new_value->content.val= in_val; |
new_value->content.val= in_val; |
191 |
new_value->type= integer; |
new_value->type= integer; |
192 |
new_value->refcount=1; |
new_value->refcount= 0; |
193 |
|
|
194 |
push_val(env, new_value); |
push_val(env, new_value); |
195 |
} |
} |
202 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(strlen(in_string)+1); |
203 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
204 |
new_value->type= string; |
new_value->type= string; |
205 |
new_value->refcount=1; |
new_value->refcount= 0; |
206 |
|
|
207 |
push_val(env, new_value); |
push_val(env, new_value); |
208 |
} |
} |
228 |
} |
} |
229 |
|
|
230 |
extern void mangle(environment *env){ |
extern void mangle(environment *env){ |
|
value *new_value; |
|
231 |
char *new_string; |
char *new_string; |
232 |
|
|
233 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
247 |
toss(env); |
toss(env); |
248 |
if(env->err) return; |
if(env->err) return; |
249 |
|
|
250 |
new_value= malloc(sizeof(value)); |
push_cstring(env, new_string); |
|
new_value->content.ptr= new_string; |
|
|
new_value->type= string; |
|
|
new_value->refcount=1; |
|
|
|
|
|
push_val(env, new_value); |
|
251 |
} |
} |
252 |
|
|
253 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
349 |
} |
} |
350 |
|
|
351 |
/* Prints the top element of the stack. */ |
/* Prints the top element of the stack. */ |
352 |
void print_h(stackitem *stack_head) |
void print_h(stackitem *stack_head, int noquote) |
353 |
{ |
{ |
354 |
switch(stack_head->item->type) { |
switch(stack_head->item->type) { |
355 |
case integer: |
case integer: |
356 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
357 |
break; |
break; |
358 |
case string: |
case string: |
359 |
printf("%s", (char*)stack_head->item->content.ptr); |
if(noquote) |
360 |
|
printf("%s", (char*)stack_head->item->content.ptr); |
361 |
|
else |
362 |
|
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
363 |
break; |
break; |
364 |
case symb: |
case symb: |
365 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
372 |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
stack_head=(stackitem *)(stack_head->item->content.ptr); |
373 |
printf("[ "); |
printf("[ "); |
374 |
while(stack_head != NULL) { |
while(stack_head != NULL) { |
375 |
print_h(stack_head); |
print_h(stack_head, noquote); |
376 |
printf(" "); |
printf(" "); |
377 |
stack_head=stack_head->next; |
stack_head=stack_head->next; |
378 |
} |
} |
387 |
env->err=1; |
env->err=1; |
388 |
return; |
return; |
389 |
} |
} |
390 |
print_h(env->head); |
print_h(env->head, 0); |
391 |
|
nl(); |
392 |
} |
} |
393 |
|
|
394 |
/* Prints the top element of the stack and then discards it. */ |
/* Prints the top element of the stack and then discards it. */ |
399 |
toss(env); |
toss(env); |
400 |
} |
} |
401 |
|
|
402 |
|
extern void princ_(environment *env) { |
403 |
|
if(env->head==NULL) { |
404 |
|
printerr("Too Few Arguments"); |
405 |
|
env->err=1; |
406 |
|
return; |
407 |
|
} |
408 |
|
print_h(env->head, 1); |
409 |
|
} |
410 |
|
|
411 |
|
/* Prints the top element of the stack and then discards it. */ |
412 |
|
extern void princ(environment *env) |
413 |
|
{ |
414 |
|
princ_(env); |
415 |
|
if(env->err) return; |
416 |
|
toss(env); |
417 |
|
} |
418 |
|
|
419 |
/* Only to be called by function printstack. */ |
/* Only to be called by function printstack. */ |
420 |
void print_st(stackitem *stack_head, long counter) |
void print_st(stackitem *stack_head, long counter) |
421 |
{ |
{ |
422 |
if(stack_head->next != NULL) |
if(stack_head->next != NULL) |
423 |
print_st(stack_head->next, counter+1); |
print_st(stack_head->next, counter+1); |
424 |
printf("%ld: ", counter); |
printf("%ld: ", counter); |
425 |
print_h(stack_head); |
print_h(stack_head, 0); |
426 |
nl(); |
nl(); |
427 |
} |
} |
428 |
|
|
430 |
extern void printstack(environment *env) |
extern void printstack(environment *env) |
431 |
{ |
{ |
432 |
if(env->head == NULL) { |
if(env->head == NULL) { |
433 |
|
printf("Stack Empty\n"); |
434 |
return; |
return; |
435 |
} |
} |
436 |
print_st(env->head, 1); |
print_st(env->head, 1); |
|
nl(); |
|
437 |
} |
} |
438 |
|
|
439 |
/* Swap the two top elements on the stack. */ |
/* Swap the two top elements on the stack. */ |
506 |
value* temp_val; |
value* temp_val; |
507 |
stackitem* iterator; |
stackitem* iterator; |
508 |
|
|
509 |
|
eval_start: |
510 |
|
|
511 |
if(env->head==NULL) { |
if(env->head==NULL) { |
512 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
513 |
env->err=1; |
env->err=1; |
514 |
return; |
return; |
515 |
} |
} |
516 |
|
|
|
eval_start: |
|
|
|
|
517 |
switch(env->head->item->type) { |
switch(env->head->item->type) { |
518 |
/* if it's a symbol */ |
/* if it's a symbol */ |
519 |
case symb: |
case symb: |
621 |
pack= malloc(sizeof(value)); |
pack= malloc(sizeof(value)); |
622 |
pack->type= list; |
pack->type= list; |
623 |
pack->content.ptr= temp; |
pack->content.ptr= temp; |
624 |
pack->refcount= 1; |
pack->refcount= 0; |
625 |
|
|
626 |
push_val(env, pack); |
push_val(env, pack); |
627 |
rev(env); |
rev(env); |
750 |
|
|
751 |
extern void clear(environment *); |
extern void clear(environment *); |
752 |
void forget_sym(symbol **); |
void forget_sym(symbol **); |
753 |
|
extern void words(environment *); |
754 |
|
|
755 |
/* Quit stack. */ |
/* Quit stack. */ |
756 |
extern void quit(environment *env) |
extern void quit(environment *env) |
758 |
long i; |
long i; |
759 |
|
|
760 |
clear(env); |
clear(env); |
761 |
|
|
762 |
if (env->err) return; |
if (env->err) return; |
763 |
for(i= 0; i<HASHTBLSIZE; i++) { |
for(i= 0; i<HASHTBLSIZE; i++) { |
764 |
if (env->symbols[i]!= NULL) { |
while(env->symbols[i]!= NULL) { |
765 |
forget_sym(&(env->symbols[i])); |
forget_sym(&(env->symbols[i])); |
|
env->symbols[i]= NULL; |
|
766 |
} |
} |
767 |
|
env->symbols[i]= NULL; |
768 |
} |
} |
769 |
|
|
770 |
|
if(env->free_string!=NULL) |
771 |
|
free(env->free_string); |
772 |
|
|
773 |
|
muntrace(); |
774 |
|
|
775 |
exit(EXIT_SUCCESS); |
exit(EXIT_SUCCESS); |
776 |
} |
} |
777 |
|
|
840 |
push_int(env, env->err); |
push_int(env, env->err); |
841 |
} |
} |
842 |
|
|
843 |
extern void read(environment*); |
extern void sx_72656164(environment*); |
844 |
|
|
845 |
int main() |
int main(int argc, char **argv) |
846 |
{ |
{ |
847 |
environment myenv; |
environment myenv; |
848 |
|
|
849 |
|
int c; /* getopt option character */ |
850 |
|
|
851 |
|
mtrace(); |
852 |
|
|
853 |
init_env(&myenv); |
init_env(&myenv); |
854 |
|
|
855 |
|
myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO); |
856 |
|
|
857 |
|
while ((c = getopt (argc, argv, "i")) != -1) |
858 |
|
switch (c) |
859 |
|
{ |
860 |
|
case 'i': |
861 |
|
myenv.interactive = 1; |
862 |
|
break; |
863 |
|
case '?': |
864 |
|
fprintf (stderr, |
865 |
|
"Unknown option character `\\x%x'.\n", |
866 |
|
optopt); |
867 |
|
return EX_USAGE; |
868 |
|
default: |
869 |
|
abort (); |
870 |
|
} |
871 |
|
|
872 |
|
if (optind < argc) { |
873 |
|
myenv.interactive = 0; |
874 |
|
myenv.inputstream= fopen(argv[optind], "r"); |
875 |
|
if(myenv.inputstream== NULL) { |
876 |
|
perror(argv[0]); |
877 |
|
exit (EX_NOINPUT); |
878 |
|
} |
879 |
|
} |
880 |
|
|
881 |
while(1) { |
while(1) { |
882 |
if(myenv.in_string==NULL) |
if(myenv.in_string==NULL && myenv.interactive) { |
883 |
|
nl(); |
884 |
printstack(&myenv); |
printstack(&myenv); |
885 |
read(&myenv); |
printf("> "); |
886 |
|
} |
887 |
|
sx_72656164(&myenv); |
888 |
if(myenv.err) { |
if(myenv.err) { |
889 |
printf("(error %d) ", myenv.err); |
printf("(error %d) ", myenv.err); |
890 |
|
if (myenv.err==4) |
891 |
|
return EX_NOINPUT; |
892 |
myenv.err=0; |
myenv.err=0; |
893 |
} else if(myenv.head!=NULL |
} else if(myenv.head!=NULL |
894 |
&& myenv.head->item->type==symb |
&& myenv.head->item->type==symb |
1054 |
return new_value; |
return new_value; |
1055 |
} |
} |
1056 |
|
|
1057 |
/* duplicates an item on the stack */ |
/* "dup"; duplicates an item on the stack */ |
1058 |
extern void dup(environment *env) { |
extern void sx_647570(environment *env) { |
1059 |
if((env->head)==NULL) { |
if((env->head)==NULL) { |
1060 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
1061 |
env->err=1; |
env->err=1; |
1177 |
free_val(loop); |
free_val(loop); |
1178 |
} |
} |
1179 |
|
|
1180 |
/* For-loop */ |
/* "for"; For-loop */ |
1181 |
extern void sx_666f72(environment *env) { |
extern void sx_666f72(environment *env) { |
1182 |
|
|
1183 |
value *loop, *foo; |
value *loop, *foo; |
1253 |
|
|
1254 |
temp_val= malloc(sizeof(value)); |
temp_val= malloc(sizeof(value)); |
1255 |
temp_val->content.ptr= env->head; |
temp_val->content.ptr= env->head; |
1256 |
temp_val->refcount= 1; |
temp_val->refcount= 0; |
1257 |
temp_val->type= list; |
temp_val->type= list; |
1258 |
env->head= temp_head; |
env->head= temp_head; |
1259 |
push_val(env, temp_val); |
push_val(env, temp_val); |
1263 |
extern void readline(environment *env) { |
extern void readline(environment *env) { |
1264 |
char in_string[101]; |
char in_string[101]; |
1265 |
|
|
1266 |
fgets(in_string, 100, stdin); |
if(fgets(in_string, 100, env->inputstream)==NULL) |
1267 |
push_cstring(env, in_string); |
push_cstring(env, ""); |
1268 |
|
else |
1269 |
|
push_cstring(env, in_string); |
1270 |
} |
} |
1271 |
|
|
1272 |
/* Read a value and place on stack */ |
/* "read"; Read a value and place on stack */ |
1273 |
extern void read(environment *env) { |
extern void sx_72656164(environment *env) { |
1274 |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
1275 |
const char strform[]= "\"%[^\"]\"%n"; |
const char strform[]= "\"%[^\"]\"%n"; |
1276 |
const char intform[]= "%i%n"; |
const char intform[]= "%i%n"; |
1281 |
|
|
1282 |
int itemp, readlength= -1; |
int itemp, readlength= -1; |
1283 |
static int depth= 0; |
static int depth= 0; |
1284 |
char *rest, *match; |
char *match; |
1285 |
size_t inlength; |
size_t inlength; |
1286 |
|
|
1287 |
if(env->in_string==NULL) { |
if(env->in_string==NULL) { |
1288 |
|
if(depth > 0 && env->interactive) { |
1289 |
|
printf("]> "); |
1290 |
|
} |
1291 |
readline(env); if(env->err) return; |
readline(env); if(env->err) return; |
1292 |
|
|
1293 |
|
if(((char *)(env->head->item->content.ptr))[0]=='\0'){ |
1294 |
|
env->err= 4; /* EOF */ |
1295 |
|
return; |
1296 |
|
} |
1297 |
|
|
1298 |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
env->in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1299 |
env->free_string= env->in_string; /* Save the original pointer */ |
env->free_string= env->in_string; /* Save the original pointer */ |
1303 |
|
|
1304 |
inlength= strlen(env->in_string)+1; |
inlength= strlen(env->in_string)+1; |
1305 |
match= malloc(inlength); |
match= malloc(inlength); |
|
rest= malloc(inlength); |
|
1306 |
|
|
1307 |
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
if(sscanf(env->in_string, blankform, &readlength)!=EOF |
1308 |
&& readlength != -1) { |
&& readlength != -1) { |
1330 |
} else { |
} else { |
1331 |
free(env->free_string); |
free(env->free_string); |
1332 |
env->in_string = env->free_string = NULL; |
env->in_string = env->free_string = NULL; |
|
free(match); |
|
1333 |
} |
} |
1334 |
if ( env->in_string != NULL) { |
if ( env->in_string != NULL) { |
1335 |
env->in_string += readlength; |
env->in_string += readlength; |
1336 |
} |
} |
1337 |
|
|
1338 |
|
free(match); |
1339 |
|
|
1340 |
if(depth) |
if(depth) |
1341 |
return read(env); |
return sx_72656164(env); |
1342 |
} |
} |