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 |
} |
} |
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 |
} |
} |
347 |
case list: |
case list: |
348 |
push_sym(env, "list"); |
push_sym(env, "list"); |
349 |
break; |
break; |
|
default: |
|
|
push_sym(env, "unknown"); |
|
|
break; |
|
350 |
} |
} |
351 |
} |
} |
352 |
|
|
358 |
printf("%d", stack_head->item->content.val); |
printf("%d", stack_head->item->content.val); |
359 |
break; |
break; |
360 |
case string: |
case string: |
361 |
printf("\"%s\"", (char*)stack_head->item->content.ptr); |
printf("%s", (char*)stack_head->item->content.ptr); |
362 |
break; |
break; |
363 |
case symb: |
case symb: |
364 |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); |
377 |
} |
} |
378 |
printf("]"); |
printf("]"); |
379 |
break; |
break; |
|
default: |
|
|
printf("#<unknown %p>", (stack_head->item->content.ptr)); |
|
|
break; |
|
380 |
} |
} |
381 |
} |
} |
382 |
|
|
433 |
env->head->next= temp; |
env->head->next= temp; |
434 |
} |
} |
435 |
|
|
436 |
|
/* Rotate the first three elements on the stack. */ |
437 |
|
extern void rot(environment *env) |
438 |
|
{ |
439 |
|
stackitem *temp= env->head; |
440 |
|
|
441 |
|
if(env->head==NULL || env->head->next==NULL |
442 |
|
|| env->head->next->next==NULL) { |
443 |
|
printerr("Too Few Arguments"); |
444 |
|
env->err=1; |
445 |
|
return; |
446 |
|
} |
447 |
|
|
448 |
|
env->head= env->head->next->next; |
449 |
|
temp->next->next= env->head->next; |
450 |
|
env->head->next= temp; |
451 |
|
} |
452 |
|
|
453 |
/* Recall a value from a symbol, if bound */ |
/* Recall a value from a symbol, if bound */ |
454 |
extern void rcl(environment *env) |
extern void rcl(environment *env) |
455 |
{ |
{ |
496 |
return; |
return; |
497 |
} |
} |
498 |
|
|
499 |
|
eval_start: |
500 |
|
|
501 |
switch(env->head->item->type) { |
switch(env->head->item->type) { |
502 |
/* if it's a symbol */ |
/* if it's a symbol */ |
503 |
case symb: |
case symb: |
504 |
rcl(env); /* get its contents */ |
rcl(env); /* get its contents */ |
505 |
if(env->err) return; |
if(env->err) return; |
506 |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
if(env->head->item->type!=symb){ /* don't recurse symbols */ |
507 |
eval(env); /* evaluate the value */ |
goto eval_start; |
|
return; |
|
508 |
} |
} |
509 |
break; |
return; |
510 |
|
|
511 |
/* If it's a lone function value, run it */ |
/* If it's a lone function value, run it */ |
512 |
case func: |
case func: |
513 |
in_func= (funcp)(env->head->item->content.ptr); |
in_func= (funcp)(env->head->item->content.ptr); |
514 |
toss(env); |
toss(env); |
515 |
if(env->err) return; |
if(env->err) return; |
516 |
(*in_func)(env); |
return (*in_func)(env); |
|
break; |
|
517 |
|
|
518 |
/* If it's a list */ |
/* If it's a list */ |
519 |
case list: |
case list: |
522 |
toss(env); |
toss(env); |
523 |
if(env->err) return; |
if(env->err) return; |
524 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= (stackitem*)temp_val->content.ptr; |
525 |
while(iterator!=NULL && iterator->item!=NULL) { |
while(iterator!=NULL) { |
526 |
push_val(&(env->head), iterator->item); |
push_val(&(env->head), iterator->item); |
527 |
if(env->head->item->type==symb |
if(env->head->item->type==symb |
528 |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
529 |
toss(env); |
toss(env); |
530 |
if(env->err) return; |
if(env->err) return; |
531 |
|
if(iterator->next == NULL){ |
532 |
|
free_val(temp_val); |
533 |
|
goto eval_start; |
534 |
|
} |
535 |
eval(env); |
eval(env); |
536 |
if(env->err) return; |
if(env->err) return; |
537 |
} |
} |
538 |
iterator= iterator->next; |
iterator= iterator->next; |
539 |
} |
} |
540 |
free_val(temp_val); |
free_val(temp_val); |
541 |
break; |
return; |
542 |
|
|
543 |
/* If it's a string */ |
/* If it's a string */ |
544 |
case string: |
case string: |
548 |
if(env->err) return; |
if(env->err) return; |
549 |
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
temp_string= malloc(strlen((char*)temp_val->content.ptr)+5); |
550 |
strcpy(temp_string, "[ "); |
strcpy(temp_string, "[ "); |
551 |
strcat(temp_string, (char*)temp_val->content.ptr); |
strcpy(temp_string+2, (char*)temp_val->content.ptr); |
552 |
|
free_val(temp_val); |
553 |
strcat(temp_string, " ]"); |
strcat(temp_string, " ]"); |
554 |
stack_read(env, temp_string); |
stack_read(env, temp_string); |
|
eval(env); |
|
|
if(env->err) return; |
|
|
free_val(temp_val); |
|
555 |
free(temp_string); |
free(temp_string); |
556 |
break; |
goto eval_start; |
557 |
|
|
558 |
default: |
case integer: |
559 |
|
return; |
560 |
} |
} |
561 |
} |
} |
562 |
|
|
957 |
a=env->head->item->content.val; |
a=env->head->item->content.val; |
958 |
toss(env); |
toss(env); |
959 |
if(env->err) return; |
if(env->err) return; |
960 |
b=env->head->item->content.val; |
if(env->head->item->refcount == 1) |
961 |
|
env->head->item->content.val += a; |
962 |
|
else { |
963 |
|
b=env->head->item->content.val; |
964 |
|
toss(env); |
965 |
|
if(env->err) return; |
966 |
|
push_int(&(env->head), a+b); |
967 |
|
} |
968 |
|
} |
969 |
|
|
970 |
|
/* - */ |
971 |
|
extern void sx_2d(environment *env) { |
972 |
|
int a, b; |
973 |
|
|
974 |
|
if((env->head)==NULL || env->head->next==NULL) { |
975 |
|
printerr("Too Few Arguments"); |
976 |
|
env->err=1; |
977 |
|
return; |
978 |
|
} |
979 |
|
|
980 |
|
if(env->head->item->type!=integer |
981 |
|
|| env->head->next->item->type!=integer) { |
982 |
|
printerr("Bad Argument Type"); |
983 |
|
env->err=2; |
984 |
|
return; |
985 |
|
} |
986 |
|
a=env->head->item->content.val; |
987 |
|
toss(env); |
988 |
|
if(env->err) return; |
989 |
|
if(env->head->item->refcount == 1) |
990 |
|
env->head->item->content.val -= a; |
991 |
|
else { |
992 |
|
b=env->head->item->content.val; |
993 |
|
toss(env); |
994 |
|
if(env->err) return; |
995 |
|
push_int(&(env->head), b-a); |
996 |
|
} |
997 |
|
} |
998 |
|
|
999 |
|
/* > */ |
1000 |
|
extern void sx_3e(environment *env) { |
1001 |
|
int a, b; |
1002 |
|
|
1003 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1004 |
|
printerr("Too Few Arguments"); |
1005 |
|
env->err=1; |
1006 |
|
return; |
1007 |
|
} |
1008 |
|
|
1009 |
|
if(env->head->item->type!=integer |
1010 |
|
|| env->head->next->item->type!=integer) { |
1011 |
|
printerr("Bad Argument Type"); |
1012 |
|
env->err=2; |
1013 |
|
return; |
1014 |
|
} |
1015 |
|
a=env->head->item->content.val; |
1016 |
|
toss(env); |
1017 |
|
if(env->err) return; |
1018 |
|
if(env->head->item->refcount == 1) |
1019 |
|
env->head->item->content.val = (env->head->item->content.val > a); |
1020 |
|
else { |
1021 |
|
b=env->head->item->content.val; |
1022 |
|
toss(env); |
1023 |
|
if(env->err) return; |
1024 |
|
push_int(&(env->head), b>a); |
1025 |
|
} |
1026 |
|
} |
1027 |
|
|
1028 |
|
/* Return copy of a value */ |
1029 |
|
value *copy_val(value *old_value){ |
1030 |
|
stackitem *old_item, *new_item, *prev_item; |
1031 |
|
|
1032 |
|
value *new_value=malloc(sizeof(value)); |
1033 |
|
|
1034 |
|
new_value->type=old_value->type; |
1035 |
|
new_value->refcount=0; /* This is increased if/when this |
1036 |
|
value is referenced somewhere, like |
1037 |
|
in a stack item or a variable */ |
1038 |
|
switch(old_value->type){ |
1039 |
|
case integer: |
1040 |
|
new_value->content.val=old_value->content.val; |
1041 |
|
break; |
1042 |
|
case string: |
1043 |
|
(char *)(new_value->content.ptr) |
1044 |
|
= strdup((char *)(old_value->content.ptr)); |
1045 |
|
break; |
1046 |
|
case func: |
1047 |
|
case symb: |
1048 |
|
new_value->content.ptr=old_value->content.ptr; |
1049 |
|
break; |
1050 |
|
case list: |
1051 |
|
new_value->content.ptr=NULL; |
1052 |
|
|
1053 |
|
prev_item=NULL; |
1054 |
|
old_item=(stackitem *)(old_value->content.ptr); |
1055 |
|
|
1056 |
|
while(old_item != NULL) { /* While list is not empty */ |
1057 |
|
new_item= malloc(sizeof(stackitem)); |
1058 |
|
new_item->item=copy_val(old_item->item); /* recurse */ |
1059 |
|
new_item->next=NULL; |
1060 |
|
if(prev_item != NULL) /* If this wasn't the first item */ |
1061 |
|
prev_item->next=new_item; /* point the previous item to the |
1062 |
|
new item */ |
1063 |
|
else |
1064 |
|
new_value->content.ptr=new_item; |
1065 |
|
old_item=old_item->next; |
1066 |
|
prev_item=new_item; |
1067 |
|
} |
1068 |
|
break; |
1069 |
|
} |
1070 |
|
return new_value; |
1071 |
|
} |
1072 |
|
|
1073 |
|
/* duplicates an item on the stack */ |
1074 |
|
extern void dup(environment *env) { |
1075 |
|
if((env->head)==NULL) { |
1076 |
|
printerr("Too Few Arguments"); |
1077 |
|
env->err=1; |
1078 |
|
return; |
1079 |
|
} |
1080 |
|
push_val(&(env->head), copy_val(env->head->item)); |
1081 |
|
} |
1082 |
|
|
1083 |
|
/* "if", If-Then */ |
1084 |
|
extern void sx_6966(environment *env) { |
1085 |
|
|
1086 |
|
int truth; |
1087 |
|
|
1088 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1089 |
|
printerr("Too Few Arguments"); |
1090 |
|
env->err=1; |
1091 |
|
return; |
1092 |
|
} |
1093 |
|
|
1094 |
|
if(env->head->next->item->type != integer) { |
1095 |
|
printerr("Bad Argument Type"); |
1096 |
|
env->err=2; |
1097 |
|
return; |
1098 |
|
} |
1099 |
|
|
1100 |
|
swap(env); |
1101 |
|
if(env->err) return; |
1102 |
|
|
1103 |
|
truth=env->head->item->content.val; |
1104 |
|
|
1105 |
|
toss(env); |
1106 |
|
if(env->err) return; |
1107 |
|
|
1108 |
|
if(truth) |
1109 |
|
eval(env); |
1110 |
|
else |
1111 |
|
toss(env); |
1112 |
|
} |
1113 |
|
|
1114 |
|
/* If-Then-Else */ |
1115 |
|
extern void ifelse(environment *env) { |
1116 |
|
|
1117 |
|
int truth; |
1118 |
|
|
1119 |
|
if((env->head)==NULL || env->head->next==NULL |
1120 |
|
|| env->head->next->next==NULL) { |
1121 |
|
printerr("Too Few Arguments"); |
1122 |
|
env->err=1; |
1123 |
|
return; |
1124 |
|
} |
1125 |
|
|
1126 |
|
if(env->head->next->next->item->type != integer) { |
1127 |
|
printerr("Bad Argument Type"); |
1128 |
|
env->err=2; |
1129 |
|
return; |
1130 |
|
} |
1131 |
|
|
1132 |
|
rot(env); |
1133 |
|
if(env->err) return; |
1134 |
|
|
1135 |
|
truth=env->head->item->content.val; |
1136 |
|
|
1137 |
|
toss(env); |
1138 |
|
if(env->err) return; |
1139 |
|
|
1140 |
|
if(!truth) |
1141 |
|
swap(env); |
1142 |
|
if(env->err) return; |
1143 |
|
|
1144 |
toss(env); |
toss(env); |
1145 |
if(env->err) return; |
if(env->err) return; |
1146 |
push_int(&(env->head), a+b); |
|
1147 |
|
eval(env); |
1148 |
|
} |
1149 |
|
|
1150 |
|
/* while */ |
1151 |
|
extern void sx_7768696c65(environment *env) { |
1152 |
|
|
1153 |
|
int truth; |
1154 |
|
value *loop, *test; |
1155 |
|
|
1156 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1157 |
|
printerr("Too Few Arguments"); |
1158 |
|
env->err=1; |
1159 |
|
return; |
1160 |
|
} |
1161 |
|
|
1162 |
|
loop= env->head->item; |
1163 |
|
loop->refcount++; |
1164 |
|
toss(env); if(env->err) return; |
1165 |
|
|
1166 |
|
test= env->head->item; |
1167 |
|
test->refcount++; |
1168 |
|
toss(env); if(env->err) return; |
1169 |
|
|
1170 |
|
do { |
1171 |
|
push_val(&(env->head), test); |
1172 |
|
eval(env); |
1173 |
|
|
1174 |
|
if(env->head->item->type != integer) { |
1175 |
|
printerr("Bad Argument Type"); |
1176 |
|
env->err=2; |
1177 |
|
return; |
1178 |
|
} |
1179 |
|
|
1180 |
|
truth= env->head->item->content.val; |
1181 |
|
toss(env); if(env->err) return; |
1182 |
|
|
1183 |
|
if(truth) { |
1184 |
|
push_val(&(env->head), loop); |
1185 |
|
eval(env); |
1186 |
|
} else { |
1187 |
|
toss(env); |
1188 |
|
} |
1189 |
|
|
1190 |
|
} while(truth); |
1191 |
|
|
1192 |
|
free_val(test); |
1193 |
|
free_val(loop); |
1194 |
|
} |
1195 |
|
|
1196 |
|
/* For-loop */ |
1197 |
|
extern void sx_666f72(environment *env) { |
1198 |
|
|
1199 |
|
value *loop, *foo; |
1200 |
|
stackitem *iterator; |
1201 |
|
|
1202 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1203 |
|
printerr("Too Few Arguments"); |
1204 |
|
env->err=1; |
1205 |
|
return; |
1206 |
|
} |
1207 |
|
|
1208 |
|
if(env->head->next->item->type != list) { |
1209 |
|
printerr("Bad Argument Type"); |
1210 |
|
env->err=2; |
1211 |
|
return; |
1212 |
|
} |
1213 |
|
|
1214 |
|
loop= env->head->item; |
1215 |
|
loop->refcount++; |
1216 |
|
toss(env); if(env->err) return; |
1217 |
|
|
1218 |
|
foo= env->head->item; |
1219 |
|
foo->refcount++; |
1220 |
|
toss(env); if(env->err) return; |
1221 |
|
|
1222 |
|
iterator= foo->content.ptr; |
1223 |
|
|
1224 |
|
while(iterator!=NULL) { |
1225 |
|
push_val(&(env->head), iterator->item); |
1226 |
|
push_val(&(env->head), loop); |
1227 |
|
eval(env); if(env->err) return; |
1228 |
|
iterator= iterator->next; |
1229 |
|
} |
1230 |
|
|
1231 |
|
free_val(loop); |
1232 |
|
free_val(foo); |
1233 |
|
} |
1234 |
|
|
1235 |
|
/* 'to' */ |
1236 |
|
extern void to(environment *env) { |
1237 |
|
int i, start, ending; |
1238 |
|
|
1239 |
|
if((env->head)==NULL || env->head->next==NULL) { |
1240 |
|
printerr("Too Few Arguments"); |
1241 |
|
env->err=1; |
1242 |
|
return; |
1243 |
|
} |
1244 |
|
|
1245 |
|
if(env->head->item->type!=integer |
1246 |
|
|| env->head->next->item->type!=integer) { |
1247 |
|
printerr("Bad Argument Type"); |
1248 |
|
env->err=2; |
1249 |
|
return; |
1250 |
|
} |
1251 |
|
|
1252 |
|
ending= env->head->item->content.val; |
1253 |
|
toss(env); if(env->err) return; |
1254 |
|
start= env->head->item->content.val; |
1255 |
|
toss(env); if(env->err) return; |
1256 |
|
|
1257 |
|
push_sym(env, "["); |
1258 |
|
|
1259 |
|
if(ending>=start) { |
1260 |
|
for(i= start; i<=ending; i++) |
1261 |
|
push_int(&(env->head), i); |
1262 |
|
} else { |
1263 |
|
for(i= start; i>=ending; i--) |
1264 |
|
push_int(&(env->head), i); |
1265 |
|
} |
1266 |
|
|
1267 |
|
push_sym(env, "["); |
1268 |
|
pack(env); if(env->err) return; |
1269 |
|
} |
1270 |
|
|
1271 |
|
/* Read a string */ |
1272 |
|
extern void readline(environment *env) { |
1273 |
|
char in_string[101]; |
1274 |
|
|
1275 |
|
fgets(in_string, 100, stdin); |
1276 |
|
push_cstring(&(env->head), in_string); |
1277 |
|
} |
1278 |
|
|
1279 |
|
/* Read a value and place on stack */ |
1280 |
|
extern void read(environment *env) { |
1281 |
|
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%100c"; |
1282 |
|
const char strform[]= "\"%[^\"]\"%100c"; |
1283 |
|
const char intform[]= "%i%100c"; |
1284 |
|
const char blankform[]= "%*[ \t]%100c"; |
1285 |
|
const char ebrackform[]= "%*1[]]%100c"; |
1286 |
|
const char semicform[]= "%*1[;]%100c"; |
1287 |
|
const char bbrackform[]= "%*1[[]%100c"; |
1288 |
|
|
1289 |
|
int itemp, rerun= 0; |
1290 |
|
static int depth= 0; |
1291 |
|
char *rest, *match; |
1292 |
|
static char *in_string= NULL; |
1293 |
|
size_t inlength; |
1294 |
|
|
1295 |
|
if(in_string==NULL) { |
1296 |
|
readline(env); if(env->err) return; |
1297 |
|
|
1298 |
|
in_string= malloc(strlen(env->head->item->content.ptr)+1); |
1299 |
|
strcpy(in_string, env->head->item->content.ptr); |
1300 |
|
toss(env); if(env->err) return; |
1301 |
|
} |
1302 |
|
|
1303 |
|
inlength= strlen(in_string)+1; |
1304 |
|
match= malloc(inlength); |
1305 |
|
rest= malloc(inlength); |
1306 |
|
|
1307 |
|
if(sscanf(in_string, blankform, rest)) { |
1308 |
|
rerun= 1; |
1309 |
|
} else if(sscanf(in_string, intform, &itemp, rest) > 0) { |
1310 |
|
push_int(&(env->head), itemp); |
1311 |
|
} else if(sscanf(in_string, strform, match, rest) > 0) { |
1312 |
|
push_cstring(&(env->head), match); |
1313 |
|
} else if(sscanf(in_string, symbform, match, rest) > 0) { |
1314 |
|
push_sym(env, match); |
1315 |
|
} else if(sscanf(in_string, ebrackform, rest) > 0) { |
1316 |
|
push_sym(env, "["); |
1317 |
|
pack(env); if(env->err) return; |
1318 |
|
if(depth!=0) depth--; |
1319 |
|
} else if(sscanf(in_string, semicform, rest) > 0) { |
1320 |
|
push_sym(env, ";"); |
1321 |
|
} else if(sscanf(in_string, bbrackform, rest) > 0) { |
1322 |
|
push_sym(env, "["); |
1323 |
|
depth++; |
1324 |
|
} else { |
1325 |
|
free(rest); |
1326 |
|
rest= NULL; |
1327 |
|
rerun= 1; |
1328 |
|
} |
1329 |
|
|
1330 |
|
free(in_string); |
1331 |
|
free(match); |
1332 |
|
|
1333 |
|
in_string= rest; |
1334 |
|
|
1335 |
|
if(rerun || depth) |
1336 |
|
return read(env); |
1337 |
} |
} |