| 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 |
/* assert */ |
/* strcmp, strcpy, strlen, strcat, strdup */ |
|
#include <assert.h> |
|
|
/* strcat */ |
|
| 10 |
#include <string.h> |
#include <string.h> |
| 11 |
|
|
| 12 |
#define HASHTBLSIZE 65536 |
#define HASHTBLSIZE 65536 |
| 75 |
env->symbols[i]= NULL; |
env->symbols[i]= NULL; |
| 76 |
} |
} |
| 77 |
|
|
| 78 |
|
void printerr(const char* in_string) { |
| 79 |
|
fprintf(stderr, "Err: %s\n", in_string); |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
/* Throw away a value */ |
| 83 |
|
void free_val(value *val){ |
| 84 |
|
stackitem *item, *temp; |
| 85 |
|
|
| 86 |
|
val->refcount--; /* Decrease the reference count */ |
| 87 |
|
if(val->refcount == 0){ |
| 88 |
|
switch (val->type){ /* and free the contents if necessary */ |
| 89 |
|
case string: |
| 90 |
|
free(val->content.ptr); |
| 91 |
|
break; |
| 92 |
|
case list: /* lists needs to be freed recursively */ |
| 93 |
|
item=val->content.ptr; |
| 94 |
|
while(item != NULL) { /* for all stack items */ |
| 95 |
|
free_val(item->item); /* free the value */ |
| 96 |
|
temp=item->next; /* save next ptr */ |
| 97 |
|
free(item); /* free the stackitem */ |
| 98 |
|
item=temp; /* go to next stackitem */ |
| 99 |
|
} |
| 100 |
|
free(val); /* Free the actual list value */ |
| 101 |
|
break; |
| 102 |
|
case integer: |
| 103 |
|
case func: |
| 104 |
|
case symb: |
| 105 |
|
break; |
| 106 |
|
} |
| 107 |
|
} |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
/* Discard the top element of the stack. */ |
| 111 |
|
extern void toss(environment *env) |
| 112 |
|
{ |
| 113 |
|
stackitem *temp= env->head; |
| 114 |
|
|
| 115 |
|
if((env->head)==NULL) { |
| 116 |
|
printerr("Too Few Arguments"); |
| 117 |
|
env->err=1; |
| 118 |
|
return; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
free_val(env->head->item); /* Free the value */ |
| 122 |
|
env->head= env->head->next; /* Remove the top stack item */ |
| 123 |
|
free(temp); /* Free the old top stack item */ |
| 124 |
|
} |
| 125 |
|
|
| 126 |
/* Returns a pointer to a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
| 127 |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
| 128 |
{ |
{ |
| 197 |
push(stack_head, new_item); |
push(stack_head, new_item); |
| 198 |
} |
} |
| 199 |
|
|
| 200 |
|
/* Mangle a symbol name to a valid C identifier name */ |
| 201 |
|
char *mangle_str(const char *old_string){ |
| 202 |
|
char validchars[] |
| 203 |
|
="0123456789abcdef"; |
| 204 |
|
char *new_string, *current; |
| 205 |
|
|
| 206 |
|
new_string=malloc((strlen(old_string)*2)+4); |
| 207 |
|
strcpy(new_string, "sx_"); /* Stack eXternal */ |
| 208 |
|
current=new_string+3; |
| 209 |
|
while(old_string[0] != '\0'){ |
| 210 |
|
current[0]=validchars[(unsigned char)(old_string[0])/16]; |
| 211 |
|
current[1]=validchars[(unsigned char)(old_string[0])%16]; |
| 212 |
|
current+=2; |
| 213 |
|
old_string++; |
| 214 |
|
} |
| 215 |
|
current[0]='\0'; |
| 216 |
|
|
| 217 |
|
return new_string; /* The caller must free() it */ |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
extern void mangle(environment *env){ |
| 221 |
|
value *new_value; |
| 222 |
|
char *new_string; |
| 223 |
|
|
| 224 |
|
if((env->head)==NULL) { |
| 225 |
|
printerr("Too Few Arguments"); |
| 226 |
|
env->err=1; |
| 227 |
|
return; |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
if(env->head->item->type!=string) { |
| 231 |
|
printerr("Bad Argument Type"); |
| 232 |
|
env->err=2; |
| 233 |
|
return; |
| 234 |
|
} |
| 235 |
|
|
| 236 |
|
new_string= mangle_str((const char *)(env->head->item->content.ptr)); |
| 237 |
|
|
| 238 |
|
toss(env); |
| 239 |
|
if(env->err) return; |
| 240 |
|
|
| 241 |
|
new_value= malloc(sizeof(value)); |
| 242 |
|
new_value->content.ptr= new_string; |
| 243 |
|
new_value->type= string; |
| 244 |
|
new_value->refcount=1; |
| 245 |
|
|
| 246 |
|
push_val(&(env->head), new_value); |
| 247 |
|
} |
| 248 |
|
|
| 249 |
/* Push a symbol onto the stack. */ |
/* Push a symbol onto the stack. */ |
| 250 |
void push_sym(environment *env, const char *in_string) |
void push_sym(environment *env, const char *in_string) |
| 251 |
{ |
{ |
| 260 |
void *funcptr; /* A function pointer */ |
void *funcptr; /* A function pointer */ |
| 261 |
|
|
| 262 |
static void *handle= NULL; /* Dynamic linker handle */ |
static void *handle= NULL; /* Dynamic linker handle */ |
| 263 |
|
const char *dlerr; /* Dynamic linker error */ |
| 264 |
|
char *mangled; /* Mangled function name */ |
| 265 |
|
|
| 266 |
/* Create a new stack item containing a new value */ |
/* Create a new stack item containing a new value */ |
| 267 |
new_item= malloc(sizeof(stackitem)); |
new_item= malloc(sizeof(stackitem)); |
| 294 |
handle= dlopen(NULL, RTLD_LAZY); |
handle= dlopen(NULL, RTLD_LAZY); |
| 295 |
|
|
| 296 |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
| 297 |
if(dlerror()==NULL) { /* If a function was found */ |
dlerr=dlerror(); |
| 298 |
|
if(dlerr != NULL) { /* If no function was found */ |
| 299 |
|
mangled=mangle_str(in_string); |
| 300 |
|
funcptr= dlsym(handle, mangled); /* try mangling it */ |
| 301 |
|
free(mangled); |
| 302 |
|
dlerr=dlerror(); |
| 303 |
|
} |
| 304 |
|
if(dlerr==NULL) { /* If a function was found */ |
| 305 |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
new_fvalue= malloc(sizeof(value)); /* Create a new value */ |
| 306 |
new_fvalue->type=func; /* The new value is a function pointer */ |
new_fvalue->type=func; /* The new value is a function pointer */ |
| 307 |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
new_fvalue->content.ptr=funcptr; /* Store function pointer */ |
| 313 |
push(&(env->head), new_item); |
push(&(env->head), new_item); |
| 314 |
} |
} |
| 315 |
|
|
|
void printerr(const char* in_string) { |
|
|
fprintf(stderr, "Err: %s\n", in_string); |
|
|
} |
|
|
|
|
|
/* Throw away a value */ |
|
|
void free_val(value *val){ |
|
|
stackitem *item, *temp; |
|
|
|
|
|
val->refcount--; /* Decrease the reference count */ |
|
|
if(val->refcount == 0){ |
|
|
switch (val->type){ /* and free the contents if necessary */ |
|
|
case string: |
|
|
free(val->content.ptr); |
|
|
break; |
|
|
case list: /* lists needs to be freed recursively */ |
|
|
item=val->content.ptr; |
|
|
while(item != NULL) { /* for all stack items */ |
|
|
free_val(item->item); /* free the value */ |
|
|
temp=item->next; /* save next ptr */ |
|
|
free(item); /* free the stackitem */ |
|
|
item=temp; /* go to next stackitem */ |
|
|
} |
|
|
free(val); /* Free the actual list value */ |
|
|
break; |
|
|
default: |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
/* Discard the top element of the stack. */ |
|
|
extern void toss(environment *env) |
|
|
{ |
|
|
stackitem *temp= env->head; |
|
|
|
|
|
if((env->head)==NULL) { |
|
|
printerr("Too Few Arguments"); |
|
|
env->err=1; |
|
|
return; |
|
|
} |
|
|
|
|
|
free_val(env->head->item); /* Free the value */ |
|
|
env->head= env->head->next; /* Remove the top stack item */ |
|
|
free(temp); /* Free the old top stack item */ |
|
|
} |
|
|
|
|
| 316 |
/* Print newline. */ |
/* Print newline. */ |
| 317 |
extern void nl() |
extern void nl() |
| 318 |
{ |
{ |
| 531 |
toss(env); |
toss(env); |
| 532 |
if(env->err) return; |
if(env->err) return; |
| 533 |
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
| 534 |
strcat(temp_string, "[ "); |
strcpy(temp_string, "[ "); |
| 535 |
strcat(temp_string, (char*)temp_val->content.ptr); |
strcat(temp_string, (char*)temp_val->content.ptr); |
| 536 |
strcat(temp_string, " ]"); |
strcat(temp_string, " ]"); |
| 537 |
stack_read(env, temp_string); |
stack_read(env, temp_string); |
| 541 |
free(temp_string); |
free(temp_string); |
| 542 |
break; |
break; |
| 543 |
|
|
| 544 |
default: |
case integer: |
| 545 |
|
break; |
| 546 |
} |
} |
| 547 |
} |
} |
| 548 |
|
|
| 902 |
quit(&myenv); |
quit(&myenv); |
| 903 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
| 904 |
} |
} |
| 905 |
|
|
| 906 |
|
/* + */ |
| 907 |
|
extern void sx_2b(environment *env) { |
| 908 |
|
int a, b; |
| 909 |
|
size_t len; |
| 910 |
|
char* new_string; |
| 911 |
|
value *a_val, *b_val; |
| 912 |
|
|
| 913 |
|
if((env->head)==NULL || env->head->next==NULL) { |
| 914 |
|
printerr("Too Few Arguments"); |
| 915 |
|
env->err=1; |
| 916 |
|
return; |
| 917 |
|
} |
| 918 |
|
|
| 919 |
|
if(env->head->item->type==string |
| 920 |
|
&& env->head->next->item->type==string) { |
| 921 |
|
a_val= env->head->item; |
| 922 |
|
b_val= env->head->next->item; |
| 923 |
|
a_val->refcount++; |
| 924 |
|
b_val->refcount++; |
| 925 |
|
toss(env); if(env->err) return; |
| 926 |
|
toss(env); if(env->err) return; |
| 927 |
|
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
| 928 |
|
new_string= malloc(len); |
| 929 |
|
strcpy(new_string, b_val->content.ptr); |
| 930 |
|
strcat(new_string, a_val->content.ptr); |
| 931 |
|
free_val(a_val); free_val(b_val); |
| 932 |
|
push_cstring(&(env->head), new_string); |
| 933 |
|
free(new_string); |
| 934 |
|
return; |
| 935 |
|
} |
| 936 |
|
|
| 937 |
|
if(env->head->item->type!=integer |
| 938 |
|
|| env->head->next->item->type!=integer) { |
| 939 |
|
printerr("Bad Argument Type"); |
| 940 |
|
env->err=2; |
| 941 |
|
return; |
| 942 |
|
} |
| 943 |
|
a=env->head->item->content.val; |
| 944 |
|
toss(env); |
| 945 |
|
if(env->err) return; |
| 946 |
|
b=env->head->item->content.val; |
| 947 |
|
toss(env); |
| 948 |
|
if(env->err) return; |
| 949 |
|
push_int(&(env->head), a+b); |
| 950 |
|
} |
| 951 |
|
|
| 952 |
|
/* Return copy of a value */ |
| 953 |
|
value *copy_val(value *old_value){ |
| 954 |
|
stackitem *old_item, *new_item, *prev_item; |
| 955 |
|
|
| 956 |
|
value *new_value=malloc(sizeof(value)); |
| 957 |
|
|
| 958 |
|
new_value->type=old_value->type; |
| 959 |
|
new_value->refcount=0; /* This is increased if/when this |
| 960 |
|
value is referenced somewhere, like |
| 961 |
|
in a stack item or a variable */ |
| 962 |
|
switch(old_value->type){ |
| 963 |
|
case integer: |
| 964 |
|
new_value->content.val=old_value->content.val; |
| 965 |
|
break; |
| 966 |
|
case string: |
| 967 |
|
(char *)(new_value->content.ptr) |
| 968 |
|
= strdup((char *)(old_value->content.ptr)); |
| 969 |
|
break; |
| 970 |
|
case func: |
| 971 |
|
case symb: |
| 972 |
|
new_value->content.ptr=old_value->content.ptr; |
| 973 |
|
break; |
| 974 |
|
case list: |
| 975 |
|
new_value->content.ptr=NULL; |
| 976 |
|
|
| 977 |
|
prev_item=NULL; |
| 978 |
|
old_item=(stackitem *)(old_value->content.ptr); |
| 979 |
|
|
| 980 |
|
while(old_item != NULL) { /* While list is not empty */ |
| 981 |
|
new_item= malloc(sizeof(stackitem)); |
| 982 |
|
new_item->item=copy_val(old_item->item); /* recurse */ |
| 983 |
|
new_item->next=NULL; |
| 984 |
|
if(prev_item != NULL) /* If this wasn't the first item */ |
| 985 |
|
prev_item->next=new_item; /* point the previous item to the |
| 986 |
|
new item */ |
| 987 |
|
else |
| 988 |
|
new_value->content.ptr=new_item; |
| 989 |
|
old_item=old_item->next; |
| 990 |
|
prev_item=new_item; |
| 991 |
|
} |
| 992 |
|
break; |
| 993 |
|
} |
| 994 |
|
return new_value; |
| 995 |
|
} |
| 996 |
|
|
| 997 |
|
/* duplicates an item on the stack */ |
| 998 |
|
extern void dup(environment *env) { |
| 999 |
|
if((env->head)==NULL) { |
| 1000 |
|
printerr("Too Few Arguments"); |
| 1001 |
|
env->err=1; |
| 1002 |
|
return; |
| 1003 |
|
} |
| 1004 |
|
push_val(&(env->head), copy_val(env->head->item)); |
| 1005 |
|
} |