| 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 |
| 48 |
typedef struct stackitem_struct |
typedef struct stackitem_struct |
| 49 |
{ |
{ |
| 50 |
value *item; /* The value on the stack */ |
value *item; /* The value on the stack */ |
| 51 |
|
/* (This is never NULL) */ |
| 52 |
struct stackitem_struct *next; /* Next item */ |
struct stackitem_struct *next; /* Next item */ |
| 53 |
} stackitem; |
} stackitem; |
| 54 |
|
|
| 100 |
} |
} |
| 101 |
free(val); /* Free the actual list value */ |
free(val); /* Free the actual list value */ |
| 102 |
break; |
break; |
| 103 |
default: |
case integer: |
| 104 |
|
case func: |
| 105 |
|
case symb: |
| 106 |
break; |
break; |
| 107 |
} |
} |
| 108 |
} |
} |
| 199 |
} |
} |
| 200 |
|
|
| 201 |
/* Mangle a symbol name to a valid C identifier name */ |
/* Mangle a symbol name to a valid C identifier name */ |
| 202 |
char *mangle_(const char *old_string){ |
char *mangle_str(const char *old_string){ |
| 203 |
char validchars[] |
char validchars[] |
| 204 |
="0123456789abcdef"; |
="0123456789abcdef"; |
| 205 |
char *new_string, *current; |
char *new_string, *current; |
| 208 |
strcpy(new_string, "sx_"); /* Stack eXternal */ |
strcpy(new_string, "sx_"); /* Stack eXternal */ |
| 209 |
current=new_string+3; |
current=new_string+3; |
| 210 |
while(old_string[0] != '\0'){ |
while(old_string[0] != '\0'){ |
| 211 |
current[0]=validchars[old_string[0]/16]; |
current[0]=validchars[(unsigned char)(old_string[0])/16]; |
| 212 |
current[1]=validchars[old_string[0]%16]; |
current[1]=validchars[(unsigned char)(old_string[0])%16]; |
| 213 |
current+=2; |
current+=2; |
| 214 |
old_string++; |
old_string++; |
| 215 |
} |
} |
| 234 |
return; |
return; |
| 235 |
} |
} |
| 236 |
|
|
| 237 |
new_string= mangle_((const char *)(env->head->item->content.ptr)); |
new_string= mangle_str((const char *)(env->head->item->content.ptr)); |
| 238 |
|
|
| 239 |
toss(env); |
toss(env); |
| 240 |
if(env->err) return; |
if(env->err) return; |
| 297 |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
| 298 |
dlerr=dlerror(); |
dlerr=dlerror(); |
| 299 |
if(dlerr != NULL) { /* If no function was found */ |
if(dlerr != NULL) { /* If no function was found */ |
| 300 |
mangled=mangle_(in_string); |
mangled=mangle_str(in_string); |
| 301 |
funcptr= dlsym(handle, mangled); /* try mangling it */ |
funcptr= dlsym(handle, mangled); /* try mangling it */ |
| 302 |
free(mangled); |
free(mangled); |
| 303 |
dlerr=dlerror(); |
dlerr=dlerror(); |
| 361 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
| 362 |
break; |
break; |
| 363 |
case string: |
case string: |
| 364 |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
printf("%s", (char*)stack_head->item->content.ptr); |
| 365 |
break; |
break; |
| 366 |
case symb: |
case symb: |
| 367 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
| 439 |
env->head->next= temp; |
env->head->next= temp; |
| 440 |
} |
} |
| 441 |
|
|
| 442 |
|
/* Rotate the first three elements on the stack. */ |
| 443 |
|
extern void rot(environment *env) |
| 444 |
|
{ |
| 445 |
|
stackitem *temp= env->head; |
| 446 |
|
|
| 447 |
|
if(env->head==NULL || env->head->next==NULL |
| 448 |
|
|| env->head->next->next==NULL) { |
| 449 |
|
printerr("Too Few Arguments"); |
| 450 |
|
env->err=1; |
| 451 |
|
return; |
| 452 |
|
} |
| 453 |
|
|
| 454 |
|
env->head= env->head->next->next; |
| 455 |
|
temp->next->next= env->head->next; |
| 456 |
|
env->head->next= temp; |
| 457 |
|
} |
| 458 |
|
|
| 459 |
/* Recall a value from a symbol, if bound */ |
/* Recall a value from a symbol, if bound */ |
| 460 |
extern void rcl(environment *env) |
extern void rcl(environment *env) |
| 461 |
{ |
{ |
| 559 |
free(temp_string); |
free(temp_string); |
| 560 |
break; |
break; |
| 561 |
|
|
| 562 |
default: |
case integer: |
| 563 |
|
break; |
| 564 |
} |
} |
| 565 |
} |
} |
| 566 |
|
|
| 966 |
if(env->err) return; |
if(env->err) return; |
| 967 |
push_int(&(env->head), a+b); |
push_int(&(env->head), a+b); |
| 968 |
} |
} |
| 969 |
|
|
| 970 |
|
/* Return copy of a value */ |
| 971 |
|
value *copy_val(value *old_value){ |
| 972 |
|
stackitem *old_item, *new_item, *prev_item; |
| 973 |
|
|
| 974 |
|
value *new_value=malloc(sizeof(value)); |
| 975 |
|
|
| 976 |
|
new_value->type=old_value->type; |
| 977 |
|
new_value->refcount=0; /* This is increased if/when this |
| 978 |
|
value is referenced somewhere, like |
| 979 |
|
in a stack item or a variable */ |
| 980 |
|
switch(old_value->type){ |
| 981 |
|
case integer: |
| 982 |
|
new_value->content.val=old_value->content.val; |
| 983 |
|
break; |
| 984 |
|
case string: |
| 985 |
|
(char *)(new_value->content.ptr) |
| 986 |
|
= strdup((char *)(old_value->content.ptr)); |
| 987 |
|
break; |
| 988 |
|
case func: |
| 989 |
|
case symb: |
| 990 |
|
new_value->content.ptr=old_value->content.ptr; |
| 991 |
|
break; |
| 992 |
|
case list: |
| 993 |
|
new_value->content.ptr=NULL; |
| 994 |
|
|
| 995 |
|
prev_item=NULL; |
| 996 |
|
old_item=(stackitem *)(old_value->content.ptr); |
| 997 |
|
|
| 998 |
|
while(old_item != NULL) { /* While list is not empty */ |
| 999 |
|
new_item= malloc(sizeof(stackitem)); |
| 1000 |
|
new_item->item=copy_val(old_item->item); /* recurse */ |
| 1001 |
|
new_item->next=NULL; |
| 1002 |
|
if(prev_item != NULL) /* If this wasn't the first item */ |
| 1003 |
|
prev_item->next=new_item; /* point the previous item to the |
| 1004 |
|
new item */ |
| 1005 |
|
else |
| 1006 |
|
new_value->content.ptr=new_item; |
| 1007 |
|
old_item=old_item->next; |
| 1008 |
|
prev_item=new_item; |
| 1009 |
|
} |
| 1010 |
|
break; |
| 1011 |
|
} |
| 1012 |
|
return new_value; |
| 1013 |
|
} |
| 1014 |
|
|
| 1015 |
|
/* duplicates an item on the stack */ |
| 1016 |
|
extern void dup(environment *env) { |
| 1017 |
|
if((env->head)==NULL) { |
| 1018 |
|
printerr("Too Few Arguments"); |
| 1019 |
|
env->err=1; |
| 1020 |
|
return; |
| 1021 |
|
} |
| 1022 |
|
push_val(&(env->head), copy_val(env->head->item)); |
| 1023 |
|
} |
| 1024 |
|
|
| 1025 |
|
/* If-Then */ |
| 1026 |
|
extern void sx_6966(environment *env) { |
| 1027 |
|
|
| 1028 |
|
int truth; |
| 1029 |
|
|
| 1030 |
|
if((env->head)==NULL || env->head->next==NULL) { |
| 1031 |
|
printerr("Too Few Arguments"); |
| 1032 |
|
env->err=1; |
| 1033 |
|
return; |
| 1034 |
|
} |
| 1035 |
|
|
| 1036 |
|
if(env->head->next->item->type != integer) { |
| 1037 |
|
printerr("Bad Argument Type"); |
| 1038 |
|
env->err=2; |
| 1039 |
|
return; |
| 1040 |
|
} |
| 1041 |
|
|
| 1042 |
|
swap(env); |
| 1043 |
|
if(env->err) return; |
| 1044 |
|
|
| 1045 |
|
truth=env->head->item->content.val; |
| 1046 |
|
|
| 1047 |
|
toss(env); |
| 1048 |
|
if(env->err) return; |
| 1049 |
|
|
| 1050 |
|
if(truth) |
| 1051 |
|
eval(env); |
| 1052 |
|
else |
| 1053 |
|
toss(env); |
| 1054 |
|
} |
| 1055 |
|
|
| 1056 |
|
/* If-Then-Else */ |
| 1057 |
|
extern void ifelse(environment *env) { |
| 1058 |
|
|
| 1059 |
|
int truth; |
| 1060 |
|
|
| 1061 |
|
if((env->head)==NULL || env->head->next==NULL |
| 1062 |
|
|| env->head->next->next==NULL) { |
| 1063 |
|
printerr("Too Few Arguments"); |
| 1064 |
|
env->err=1; |
| 1065 |
|
return; |
| 1066 |
|
} |
| 1067 |
|
|
| 1068 |
|
if(env->head->next->next->item->type != integer) { |
| 1069 |
|
printerr("Bad Argument Type"); |
| 1070 |
|
env->err=2; |
| 1071 |
|
return; |
| 1072 |
|
} |
| 1073 |
|
|
| 1074 |
|
rot(env); |
| 1075 |
|
if(env->err) return; |
| 1076 |
|
|
| 1077 |
|
truth=env->head->item->content.val; |
| 1078 |
|
|
| 1079 |
|
toss(env); |
| 1080 |
|
if(env->err) return; |
| 1081 |
|
|
| 1082 |
|
if(!truth) |
| 1083 |
|
swap(env); |
| 1084 |
|
if(env->err) return; |
| 1085 |
|
|
| 1086 |
|
toss(env); |
| 1087 |
|
if(env->err) return; |
| 1088 |
|
|
| 1089 |
|
eval(env); |
| 1090 |
|
} |
| 1091 |
|
|
| 1092 |
|
/* while */ |
| 1093 |
|
extern void sx_7768696c65(environment *env) { |
| 1094 |
|
|
| 1095 |
|
int truth; |
| 1096 |
|
|
| 1097 |
|
if((env->head)==NULL || env->head->next==NULL) { |
| 1098 |
|
printerr("Too Few Arguments"); |
| 1099 |
|
env->err=1; |
| 1100 |
|
return; |
| 1101 |
|
} |
| 1102 |
|
|
| 1103 |
|
do { |
| 1104 |
|
swap(env); if(env->err) return; |
| 1105 |
|
dup(env); if(env->err) return; |
| 1106 |
|
eval(env); if(env->err) return; |
| 1107 |
|
|
| 1108 |
|
if(env->head->item->type != integer) { |
| 1109 |
|
printerr("Bad Argument Type"); |
| 1110 |
|
env->err=2; |
| 1111 |
|
return; |
| 1112 |
|
} |
| 1113 |
|
|
| 1114 |
|
truth= env->head->item->content.val; |
| 1115 |
|
|
| 1116 |
|
toss(env); if(env->err) return; |
| 1117 |
|
swap(env); if(env->err) return; |
| 1118 |
|
|
| 1119 |
|
if(truth) { |
| 1120 |
|
dup(env); |
| 1121 |
|
eval(env); |
| 1122 |
|
} else { |
| 1123 |
|
toss(env); |
| 1124 |
|
toss(env); |
| 1125 |
|
} |
| 1126 |
|
|
| 1127 |
|
} while(truth); |
| 1128 |
|
} |