| 1 |
/* printf */ |
/* -*- coding: utf-8; -*- */ |
| 2 |
#include <stdio.h> |
/* |
| 3 |
/* EXIT_SUCCESS */ |
stack - an interactive interpreter for a stack-based language |
| 4 |
#include <stdlib.h> |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn |
| 5 |
/* NULL */ |
|
| 6 |
#include <stddef.h> |
This program is free software; you can redistribute it and/or modify |
| 7 |
/* dlopen, dlsym, dlerror */ |
it under the terms of the GNU General Public License as published by |
| 8 |
#include <dlfcn.h> |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
|
(at your option) any later version. |
|
#define HASHTBLSIZE 65536 |
|
|
|
|
|
typedef struct stack_item |
|
|
{ |
|
|
enum { |
|
|
value, /* Integer */ |
|
|
string, |
|
|
ref, /* Reference (to an element in the hash table) */ |
|
|
func, /* Function pointer */ |
|
|
symbol, |
|
|
list |
|
|
} type; /* Tells what kind of stack element */ |
|
|
|
|
|
union { |
|
|
void* ptr; /* Pointer to the content */ |
|
|
int val; /* ...or an integer */ |
|
|
} content; /* Stores a pointer or an integer */ |
|
| 10 |
|
|
| 11 |
char* id; /* Symbol name */ |
This program is distributed in the hope that it will be useful, |
| 12 |
struct stack_item* next; /* Next element */ |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
} stackitem; |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
|
GNU General Public License for more details. |
| 15 |
|
|
| 16 |
|
You should have received a copy of the GNU General Public License |
| 17 |
|
along with this program; if not, write to the Free Software |
| 18 |
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
|
|
| 20 |
typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
Authors: Mats Alritzson <masse@fukt.bth.se> |
| 21 |
typedef void (*funcp)(stackitem**); /* Function pointer declaration */ |
Teddy Hogeborn <teddy@fukt.bth.se> |
| 22 |
|
*/ |
| 23 |
|
|
| 24 |
|
#include "stack.h" |
| 25 |
|
|
| 26 |
/* Initiates a newly created hash table. */ |
/* Initialize a newly created environment */ |
| 27 |
void init_hashtbl(hashtbl out_hash) |
void init_env(environment *env) |
| 28 |
{ |
{ |
| 29 |
long i; |
int i; |
| 30 |
|
|
| 31 |
|
env->gc_limit= 400000; |
| 32 |
|
env->gc_count= 0; |
| 33 |
|
env->gc_ref= NULL; |
| 34 |
|
|
| 35 |
|
env->head= new_val(env); |
| 36 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
| 37 |
out_hash[i]= NULL; |
env->symbols[i]= NULL; |
| 38 |
|
env->err= 0; |
| 39 |
|
env->in_string= NULL; |
| 40 |
|
env->free_string= NULL; |
| 41 |
|
env->inputstream= stdin; |
| 42 |
|
env->interactive= 1; |
| 43 |
} |
} |
| 44 |
|
|
| 45 |
/* Returns a pointer to an element in the hash table. */ |
void printerr(const char* in_string) |
|
stackitem** hash(hashtbl in_hashtbl, const char* in_string) |
|
| 46 |
{ |
{ |
| 47 |
long i= 0; |
fprintf(stderr, "Err: %s\n", in_string); |
| 48 |
unsigned long out_hash= 0; |
} |
| 49 |
char key= 0; |
|
| 50 |
stackitem** position; |
/* Returns a pointer to a pointer to an element in the hash table. */ |
| 51 |
|
symbol **hash(hashtbl in_hashtbl, const char *in_string) |
| 52 |
|
{ |
| 53 |
|
int i= 0; |
| 54 |
|
unsigned int out_hash= 0; |
| 55 |
|
char key= '\0'; |
| 56 |
|
symbol **position; |
| 57 |
|
|
| 58 |
while(1){ /* Hash in_string */ |
while(1){ /* Hash in_string */ |
| 59 |
key= in_string[i++]; |
key= in_string[i++]; |
| 65 |
out_hash= out_hash%HASHTBLSIZE; |
out_hash= out_hash%HASHTBLSIZE; |
| 66 |
position= &(in_hashtbl[out_hash]); |
position= &(in_hashtbl[out_hash]); |
| 67 |
|
|
| 68 |
while(1){ /* Return position if empty */ |
while(1){ |
| 69 |
if(*position==NULL) |
if(*position==NULL) /* If empty */ |
| 70 |
return position; |
return position; |
| 71 |
|
|
| 72 |
if(strcmp(in_string, (*position)->id)==0) /* Return position if match */ |
if(strcmp(in_string, (*position)->id)==0) /* If match */ |
| 73 |
return position; |
return position; |
| 74 |
|
|
| 75 |
position= &((*position)->next); /* Try next */ |
position= &((*position)->next); /* Try next */ |
| 76 |
} |
} |
| 77 |
} |
} |
| 78 |
|
|
| 79 |
/* Generic push function. */ |
/* Create new value */ |
| 80 |
int push(stackitem** stack_head, stackitem* in_item) |
value* new_val(environment *env) |
| 81 |
{ |
{ |
| 82 |
in_item->next= *stack_head; |
value *nval= malloc(sizeof(value)); |
| 83 |
*stack_head= in_item; |
stackitem *nitem= malloc(sizeof(stackitem)); |
| 84 |
return 1; |
|
| 85 |
|
assert(nval != NULL); |
| 86 |
|
assert(nitem != NULL); |
| 87 |
|
|
| 88 |
|
nval->content.ptr= NULL; |
| 89 |
|
nval->type= empty; |
| 90 |
|
|
| 91 |
|
nitem->item= nval; |
| 92 |
|
nitem->next= env->gc_ref; |
| 93 |
|
|
| 94 |
|
env->gc_ref= nitem; |
| 95 |
|
|
| 96 |
|
env->gc_count += sizeof(value); |
| 97 |
|
nval->gc.flag.mark= 0; |
| 98 |
|
nval->gc.flag.protect= 0; |
| 99 |
|
|
| 100 |
|
return nval; |
| 101 |
} |
} |
| 102 |
|
|
| 103 |
/* Push a value on the stack. */ |
|
| 104 |
int push_val(stackitem** stack_head, int in_val) |
/* Mark values recursively. |
| 105 |
|
Marked values are not collected by the GC. */ |
| 106 |
|
inline void gc_mark(value *val) |
| 107 |
{ |
{ |
| 108 |
stackitem* new_item= malloc(sizeof(stackitem)); |
if(val==NULL || val->gc.flag.mark) |
| 109 |
new_item->content.val= in_val; |
return; |
|
new_item->type= value; |
|
| 110 |
|
|
| 111 |
push(stack_head, new_item); |
val->gc.flag.mark= 1; |
| 112 |
return 1; |
|
| 113 |
|
if(val->type==tcons) { |
| 114 |
|
gc_mark(CAR(val)); |
| 115 |
|
gc_mark(CDR(val)); |
| 116 |
|
} |
| 117 |
} |
} |
| 118 |
|
|
| 119 |
/* Copy a string onto the stack. */ |
|
| 120 |
int push_cstring(stackitem** stack_head, const char* in_string) |
/* Start GC */ |
| 121 |
|
extern void gc_init(environment *env) |
| 122 |
{ |
{ |
| 123 |
stackitem* new_item= malloc(sizeof(stackitem)); |
stackitem *new_head= NULL, *titem; |
| 124 |
new_item->content.ptr= malloc(strlen(in_string)+1); |
symbol *tsymb; |
| 125 |
strcpy(new_item->content.ptr, in_string); |
int i; |
| 126 |
new_item->type= string; |
|
| 127 |
|
if(env->interactive) |
| 128 |
|
printf("Garbage collecting."); |
| 129 |
|
|
| 130 |
|
/* Mark values on stack */ |
| 131 |
|
gc_mark(env->head); |
| 132 |
|
|
| 133 |
|
if(env->interactive) |
| 134 |
|
printf("."); |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
/* Mark values in hashtable */ |
| 138 |
|
for(i= 0; i<HASHTBLSIZE; i++) |
| 139 |
|
for(tsymb= env->symbols[i]; tsymb!=NULL; tsymb= tsymb->next) |
| 140 |
|
if (tsymb->val != NULL) |
| 141 |
|
gc_mark(tsymb->val); |
| 142 |
|
|
| 143 |
push(stack_head, new_item); |
|
| 144 |
return 1; |
if(env->interactive) |
| 145 |
|
printf("."); |
| 146 |
|
|
| 147 |
|
env->gc_count= 0; |
| 148 |
|
|
| 149 |
|
while(env->gc_ref!=NULL) { /* Sweep unused values */ |
| 150 |
|
|
| 151 |
|
if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ |
| 152 |
|
|
| 153 |
|
/* Remove content */ |
| 154 |
|
switch(env->gc_ref->item->type){ |
| 155 |
|
case string: |
| 156 |
|
free(env->gc_ref->item->content.ptr); |
| 157 |
|
break; |
| 158 |
|
case tcons: |
| 159 |
|
free(env->gc_ref->item->content.c); |
| 160 |
|
break; |
| 161 |
|
case port: |
| 162 |
|
case empty: |
| 163 |
|
case integer: |
| 164 |
|
case tfloat: |
| 165 |
|
case func: |
| 166 |
|
case symb: |
| 167 |
|
/* Symbol strings are freed when walking the hash table */ |
| 168 |
|
break; |
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
free(env->gc_ref->item); /* Remove from gc_ref */ |
| 172 |
|
titem= env->gc_ref->next; |
| 173 |
|
free(env->gc_ref); /* Remove value */ |
| 174 |
|
env->gc_ref= titem; |
| 175 |
|
continue; |
| 176 |
|
} |
| 177 |
|
#ifdef DEBUG |
| 178 |
|
printf("Kept value (%p)", env->gc_ref->item); |
| 179 |
|
if(env->gc_ref->item->gc.flag.mark) |
| 180 |
|
printf(" (marked)"); |
| 181 |
|
if(env->gc_ref->item->gc.flag.protect) |
| 182 |
|
printf(" (protected)"); |
| 183 |
|
switch(env->gc_ref->item->type){ |
| 184 |
|
case integer: |
| 185 |
|
printf(" integer: %d", env->gc_ref->item->content.i); |
| 186 |
|
break; |
| 187 |
|
case func: |
| 188 |
|
printf(" func: %p", env->gc_ref->item->content.ptr); |
| 189 |
|
break; |
| 190 |
|
case symb: |
| 191 |
|
printf(" symb: %s", env->gc_ref->item->content.sym->id); |
| 192 |
|
break; |
| 193 |
|
case tcons: |
| 194 |
|
printf(" tcons: %p\t%p", env->gc_ref->item->content.c->car, |
| 195 |
|
env->gc_ref->item->content.c->cdr); |
| 196 |
|
break; |
| 197 |
|
default: |
| 198 |
|
printf(" <unknown %d>", (env->gc_ref->item->type)); |
| 199 |
|
} |
| 200 |
|
printf("\n"); |
| 201 |
|
#endif /* DEBUG */ |
| 202 |
|
|
| 203 |
|
/* Keep values */ |
| 204 |
|
env->gc_count += sizeof(value); |
| 205 |
|
if(env->gc_ref->item->type==string) |
| 206 |
|
env->gc_count += strlen(env->gc_ref->item->content.ptr)+1; |
| 207 |
|
|
| 208 |
|
titem= env->gc_ref->next; |
| 209 |
|
env->gc_ref->next= new_head; |
| 210 |
|
new_head= env->gc_ref; |
| 211 |
|
new_head->item->gc.flag.mark= 0; |
| 212 |
|
env->gc_ref= titem; |
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
if (env->gc_limit < env->gc_count*2) |
| 216 |
|
env->gc_limit= env->gc_count*2; |
| 217 |
|
|
| 218 |
|
env->gc_ref= new_head; |
| 219 |
|
|
| 220 |
|
if(env->interactive) |
| 221 |
|
printf("done (%d bytes still allocated)\n", env->gc_count); |
| 222 |
|
|
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
inline void gc_maybe(environment *env) |
| 226 |
|
{ |
| 227 |
|
if(env->gc_count < env->gc_limit) |
| 228 |
|
return; |
| 229 |
|
else |
| 230 |
|
return gc_init(env); |
| 231 |
} |
} |
| 232 |
|
|
| 233 |
/* Create a new hash entry. */ |
/* Protect values from GC */ |
| 234 |
int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id) |
void protect(value *val) |
| 235 |
{ |
{ |
| 236 |
in_item->id= malloc(strlen(id)+1); |
if(val==NULL || val->gc.flag.protect) |
| 237 |
|
return; |
| 238 |
|
|
| 239 |
strcpy(in_item->id, id); |
val->gc.flag.protect= 1; |
|
push(hash(in_hashtbl, id), in_item); |
|
| 240 |
|
|
| 241 |
return 1; |
if(val->type==tcons) { |
| 242 |
|
protect(CAR(val)); |
| 243 |
|
protect(CDR(val)); |
| 244 |
|
} |
| 245 |
} |
} |
| 246 |
|
|
| 247 |
/* Define a function a new function in the hash table. */ |
/* Unprotect values from GC */ |
| 248 |
void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) |
void unprotect(value *val) |
| 249 |
{ |
{ |
| 250 |
stackitem* temp= malloc(sizeof(stackitem)); |
if(val==NULL || !(val->gc.flag.protect)) |
| 251 |
|
return; |
| 252 |
|
|
| 253 |
temp->type= func; |
val->gc.flag.protect= 0; |
|
temp->content.ptr= in_func; |
|
| 254 |
|
|
| 255 |
mk_hashentry(in_hashtbl, temp, id); |
if(val->type==tcons) { |
| 256 |
|
unprotect(CAR(val)); |
| 257 |
|
unprotect(CDR(val)); |
| 258 |
|
} |
| 259 |
} |
} |
| 260 |
|
|
| 261 |
/* Define a new symbol in the hash table. */ |
/* Push a value onto the stack */ |
| 262 |
void def_sym(hashtbl in_hashtbl, const char* id) |
void push_val(environment *env, value *val) |
| 263 |
{ |
{ |
| 264 |
stackitem* temp= malloc(sizeof(stackitem)); |
value *new_value= new_val(env); |
| 265 |
|
|
| 266 |
temp->type= symbol; |
new_value->content.c= malloc(sizeof(pair)); |
| 267 |
mk_hashentry(in_hashtbl, temp, id); |
assert(new_value->content.c!=NULL); |
| 268 |
|
env->gc_count += sizeof(pair); |
| 269 |
|
new_value->type= tcons; |
| 270 |
|
CAR(new_value)= val; |
| 271 |
|
CDR(new_value)= env->head; |
| 272 |
|
env->head= new_value; |
| 273 |
} |
} |
| 274 |
|
|
| 275 |
/* Push a reference to an entry in the hash table onto the stack. */ |
/* Push an integer onto the stack */ |
| 276 |
int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string) |
void push_int(environment *env, int in_val) |
| 277 |
{ |
{ |
| 278 |
static void* handle= NULL; |
value *new_value= new_val(env); |
| 279 |
void* symbol; |
|
| 280 |
|
new_value->content.i= in_val; |
| 281 |
|
new_value->type= integer; |
| 282 |
|
|
| 283 |
stackitem* new_item= malloc(sizeof(stackitem)); |
push_val(env, new_value); |
| 284 |
new_item->content.ptr= *hash(in_hash, in_string); |
} |
|
new_item->type= ref; |
|
| 285 |
|
|
| 286 |
if(new_item->content.ptr==NULL) { /* If hash entry empty */ |
/* Push a floating point number onto the stack */ |
| 287 |
if(handle==NULL) /* If no handle */ |
void push_float(environment *env, float in_val) |
| 288 |
handle= dlopen(NULL, RTLD_LAZY); |
{ |
| 289 |
|
value *new_value= new_val(env); |
| 290 |
|
|
| 291 |
symbol= dlsym(handle, in_string); /* Get function pointer */ |
new_value->content.f= in_val; |
| 292 |
if(dlerror()==NULL) /* If existing function pointer */ |
new_value->type= tfloat; |
|
def_func(in_hash, symbol, in_string); /* Store function pointer */ |
|
|
else |
|
|
def_sym(in_hash, in_string); /* Make symbol */ |
|
|
|
|
|
new_item->content.ptr= *hash(in_hash, in_string); /* XXX */ |
|
|
new_item->type= ref; |
|
|
} |
|
| 293 |
|
|
| 294 |
push(stack_head, new_item); |
push_val(env, new_value); |
|
return 1; |
|
| 295 |
} |
} |
| 296 |
|
|
| 297 |
void printerr(const char* in_string) { |
/* Copy a string onto the stack. */ |
| 298 |
fprintf(stderr, "Err: %s\n", in_string); |
void push_cstring(environment *env, const char *in_string) |
| 299 |
|
{ |
| 300 |
|
value *new_value= new_val(env); |
| 301 |
|
int length= strlen(in_string)+1; |
| 302 |
|
|
| 303 |
|
new_value->content.ptr= malloc(length); |
| 304 |
|
assert(new_value != NULL); |
| 305 |
|
env->gc_count += length; |
| 306 |
|
strcpy(new_value->content.ptr, in_string); |
| 307 |
|
new_value->type= string; |
| 308 |
|
|
| 309 |
|
push_val(env, new_value); |
| 310 |
|
} |
| 311 |
|
|
| 312 |
|
/* Mangle a symbol name to a valid C identifier name */ |
| 313 |
|
char *mangle_str(const char *old_string) |
| 314 |
|
{ |
| 315 |
|
char validchars[]= "0123456789abcdef"; |
| 316 |
|
char *new_string, *current; |
| 317 |
|
|
| 318 |
|
new_string= malloc((strlen(old_string)*2)+4); |
| 319 |
|
assert(new_string != NULL); |
| 320 |
|
strcpy(new_string, "sx_"); /* Stack eXternal */ |
| 321 |
|
current= new_string+3; |
| 322 |
|
while(old_string[0] != '\0'){ |
| 323 |
|
current[0]= validchars[(unsigned char)(old_string[0])/16]; |
| 324 |
|
current[1]= validchars[(unsigned char)(old_string[0])%16]; |
| 325 |
|
current+= 2; |
| 326 |
|
old_string++; |
| 327 |
|
} |
| 328 |
|
current[0]= '\0'; |
| 329 |
|
|
| 330 |
|
return new_string; /* The caller must free() it */ |
| 331 |
} |
} |
| 332 |
|
|
| 333 |
/* Discard the top element of the stack. */ |
extern void mangle(environment *env) |
|
extern void toss(stackitem** stack_head) |
|
| 334 |
{ |
{ |
| 335 |
stackitem* temp= *stack_head; |
char *new_string; |
| 336 |
|
|
| 337 |
if((*stack_head)==NULL) { |
if(env->head->type==empty) { |
| 338 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
| 339 |
|
env->err= 1; |
| 340 |
return; |
return; |
| 341 |
} |
} |
|
|
|
|
if((*stack_head)->type==string) |
|
|
free((*stack_head)->content.ptr); |
|
| 342 |
|
|
| 343 |
*stack_head= (*stack_head)->next; |
if(CAR(env->head)->type!=string) { |
| 344 |
free(temp); |
printerr("Bad Argument Type"); |
| 345 |
} |
env->err= 2; |
| 346 |
|
return; |
| 347 |
|
} |
| 348 |
|
|
| 349 |
/* Print newline. */ |
new_string= |
| 350 |
extern void nl() |
mangle_str((const char *)(CAR(env->head)->content.ptr)); |
| 351 |
{ |
|
| 352 |
printf("\n"); |
toss(env); |
| 353 |
|
if(env->err) return; |
| 354 |
|
|
| 355 |
|
push_cstring(env, new_string); |
| 356 |
} |
} |
| 357 |
|
|
| 358 |
/* Prints the top element of the stack. */ |
/* Push a symbol onto the stack. */ |
| 359 |
void print_(stackitem** stack_head) |
void push_sym(environment *env, const char *in_string) |
| 360 |
{ |
{ |
| 361 |
if((*stack_head)==NULL) { |
value *new_value; /* A new symbol value */ |
| 362 |
printerr("Stack empty"); |
/* ...which might point to... */ |
| 363 |
return; |
symbol **new_symbol; /* (if needed) A new actual symbol */ |
| 364 |
|
/* ...which, if possible, will be bound to... */ |
| 365 |
|
value *new_fvalue; /* (if needed) A new function value */ |
| 366 |
|
/* ...which will point to... */ |
| 367 |
|
void *funcptr; /* A function pointer */ |
| 368 |
|
|
| 369 |
|
static void *handle= NULL; /* Dynamic linker handle */ |
| 370 |
|
const char *dlerr; /* Dynamic linker error */ |
| 371 |
|
char *mangled; /* Mangled function name */ |
| 372 |
|
|
| 373 |
|
new_value= new_val(env); |
| 374 |
|
protect(new_value); |
| 375 |
|
new_fvalue= new_val(env); |
| 376 |
|
protect(new_fvalue); |
| 377 |
|
|
| 378 |
|
/* The new value is a symbol */ |
| 379 |
|
new_value->type= symb; |
| 380 |
|
|
| 381 |
|
/* Look up the symbol name in the hash table */ |
| 382 |
|
new_symbol= hash(env->symbols, in_string); |
| 383 |
|
new_value->content.ptr= *new_symbol; |
| 384 |
|
|
| 385 |
|
if(*new_symbol==NULL) { /* If symbol was undefined */ |
| 386 |
|
|
| 387 |
|
/* Create a new symbol */ |
| 388 |
|
(*new_symbol)= malloc(sizeof(symbol)); |
| 389 |
|
assert((*new_symbol) != NULL); |
| 390 |
|
(*new_symbol)->val= NULL; /* undefined value */ |
| 391 |
|
(*new_symbol)->next= NULL; |
| 392 |
|
(*new_symbol)->id= malloc(strlen(in_string)+1); |
| 393 |
|
assert((*new_symbol)->id != NULL); |
| 394 |
|
strcpy((*new_symbol)->id, in_string); |
| 395 |
|
|
| 396 |
|
/* Intern the new symbol in the hash table */ |
| 397 |
|
new_value->content.ptr= *new_symbol; |
| 398 |
|
|
| 399 |
|
/* Try to load the symbol name as an external function, to see if |
| 400 |
|
we should bind the symbol to a new function pointer value */ |
| 401 |
|
if(handle==NULL) /* If no handle */ |
| 402 |
|
handle= dlopen(NULL, RTLD_LAZY); |
| 403 |
|
|
| 404 |
|
mangled= mangle_str(in_string); /* mangle the name */ |
| 405 |
|
funcptr= dlsym(handle, mangled); /* and try to find it */ |
| 406 |
|
|
| 407 |
|
dlerr= dlerror(); |
| 408 |
|
if(dlerr != NULL) { /* If no function was found */ |
| 409 |
|
funcptr= dlsym(handle, in_string); /* Get function pointer */ |
| 410 |
|
dlerr= dlerror(); |
| 411 |
|
} |
| 412 |
|
|
| 413 |
|
if(dlerr==NULL) { /* If a function was found */ |
| 414 |
|
new_fvalue->type= func; /* The new value is a function pointer */ |
| 415 |
|
new_fvalue->content.ptr= funcptr; /* Store function pointer */ |
| 416 |
|
(*new_symbol)->val= new_fvalue; /* Bind the symbol to the new |
| 417 |
|
function value */ |
| 418 |
|
} |
| 419 |
|
|
| 420 |
|
free(mangled); |
| 421 |
} |
} |
| 422 |
|
|
| 423 |
switch((*stack_head)->type) { |
push_val(env, new_value); |
| 424 |
case value: |
unprotect(new_value); unprotect(new_fvalue); |
| 425 |
printf("%d", (*stack_head)->content.val); |
} |
| 426 |
|
|
| 427 |
|
/* Print a value */ |
| 428 |
|
void print_val(environment *env, value *val, int noquote, stackitem *stack, FILE *stream) |
| 429 |
|
{ |
| 430 |
|
stackitem *titem, *tstack; |
| 431 |
|
int depth; |
| 432 |
|
|
| 433 |
|
switch(val->type) { |
| 434 |
|
case empty: |
| 435 |
|
if(fprintf(stream, "[]") < 0){ |
| 436 |
|
perror("print_val"); |
| 437 |
|
env->err= 5; |
| 438 |
|
return; |
| 439 |
|
} |
| 440 |
|
break; |
| 441 |
|
case integer: |
| 442 |
|
if(fprintf(stream, "%d", val->content.i) < 0){ |
| 443 |
|
perror("print_val"); |
| 444 |
|
env->err= 5; |
| 445 |
|
return; |
| 446 |
|
} |
| 447 |
|
break; |
| 448 |
|
case tfloat: |
| 449 |
|
if(fprintf(stream, "%f", val->content.f) < 0){ |
| 450 |
|
perror("print_val"); |
| 451 |
|
env->err= 5; |
| 452 |
|
return; |
| 453 |
|
} |
| 454 |
break; |
break; |
| 455 |
case string: |
case string: |
| 456 |
printf("%s", (char*)(*stack_head)->content.ptr); |
if(noquote){ |
| 457 |
|
if(fprintf(stream, "%s", (char*)(val->content.ptr)) < 0){ |
| 458 |
|
perror("print_val"); |
| 459 |
|
env->err= 5; |
| 460 |
|
return; |
| 461 |
|
} |
| 462 |
|
} else { /* quote */ |
| 463 |
|
if(fprintf(stream, "\"%s\"", (char*)(val->content.ptr)) < 0){ |
| 464 |
|
perror("print_val"); |
| 465 |
|
env->err= 5; |
| 466 |
|
return; |
| 467 |
|
} |
| 468 |
|
} |
| 469 |
|
break; |
| 470 |
|
case symb: |
| 471 |
|
if(fprintf(stream, "%s", val->content.sym->id) < 0){ |
| 472 |
|
perror("print_val"); |
| 473 |
|
env->err= 5; |
| 474 |
|
return; |
| 475 |
|
} |
| 476 |
|
break; |
| 477 |
|
case func: |
| 478 |
|
if(fprintf(stream, "#<function %p>", (funcp)(val->content.ptr)) < 0){ |
| 479 |
|
perror("print_val"); |
| 480 |
|
env->err= 5; |
| 481 |
|
return; |
| 482 |
|
} |
| 483 |
break; |
break; |
| 484 |
case ref: |
case port: |
| 485 |
printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); |
if(fprintf(stream, "#<port %p>", (funcp)(val->content.p)) < 0){ |
| 486 |
|
perror("print_val"); |
| 487 |
|
env->err= 5; |
| 488 |
|
return; |
| 489 |
|
} |
| 490 |
break; |
break; |
| 491 |
case symbol: |
case tcons: |
| 492 |
default: |
if(fprintf(stream, "[ ") < 0){ |
| 493 |
printf("%p", (*stack_head)->content.ptr); |
perror("print_val"); |
| 494 |
|
env->err= 5; |
| 495 |
|
return; |
| 496 |
|
} |
| 497 |
|
tstack= stack; |
| 498 |
|
do { |
| 499 |
|
titem=malloc(sizeof(stackitem)); |
| 500 |
|
assert(titem != NULL); |
| 501 |
|
titem->item=val; |
| 502 |
|
titem->next=tstack; |
| 503 |
|
tstack=titem; /* Put it on the stack */ |
| 504 |
|
/* Search a stack of values being printed to see if we are already |
| 505 |
|
printing this value */ |
| 506 |
|
titem=tstack; |
| 507 |
|
depth=0; |
| 508 |
|
while(titem != NULL && titem->item != CAR(val)){ |
| 509 |
|
titem=titem->next; |
| 510 |
|
depth++; |
| 511 |
|
} |
| 512 |
|
if(titem != NULL){ /* If we found it on the stack, */ |
| 513 |
|
if(fprintf(stream, "#%d#", depth) < 0){ /* print a depth reference */ |
| 514 |
|
perror("print_val"); |
| 515 |
|
env->err= 5; |
| 516 |
|
free(titem); |
| 517 |
|
return; |
| 518 |
|
} |
| 519 |
|
} else { |
| 520 |
|
print_val(env, CAR(val), noquote, tstack, stream); |
| 521 |
|
} |
| 522 |
|
val= CDR(val); |
| 523 |
|
switch(val->type){ |
| 524 |
|
case empty: |
| 525 |
|
break; |
| 526 |
|
case tcons: |
| 527 |
|
/* Search a stack of values being printed to see if we are already |
| 528 |
|
printing this value */ |
| 529 |
|
titem=tstack; |
| 530 |
|
depth=0; |
| 531 |
|
while(titem != NULL && titem->item != val){ |
| 532 |
|
titem=titem->next; |
| 533 |
|
depth++; |
| 534 |
|
} |
| 535 |
|
if(titem != NULL){ /* If we found it on the stack, */ |
| 536 |
|
if(fprintf(stream, " . #%d#", depth) < 0){ /* print a depth reference */ |
| 537 |
|
perror("print_val"); |
| 538 |
|
env->err= 5; |
| 539 |
|
goto printval_end; |
| 540 |
|
} |
| 541 |
|
} else { |
| 542 |
|
if(fprintf(stream, " ") < 0){ |
| 543 |
|
perror("print_val"); |
| 544 |
|
env->err= 5; |
| 545 |
|
goto printval_end; |
| 546 |
|
} |
| 547 |
|
} |
| 548 |
|
break; |
| 549 |
|
default: |
| 550 |
|
if(fprintf(stream, " . ") < 0){ /* Improper list */ |
| 551 |
|
perror("print_val"); |
| 552 |
|
env->err= 5; |
| 553 |
|
goto printval_end; |
| 554 |
|
} |
| 555 |
|
print_val(env, val, noquote, tstack, stream); |
| 556 |
|
} |
| 557 |
|
} while(val->type == tcons && titem == NULL); |
| 558 |
|
|
| 559 |
|
printval_end: |
| 560 |
|
|
| 561 |
|
titem=tstack; |
| 562 |
|
while(titem != stack){ |
| 563 |
|
tstack=titem->next; |
| 564 |
|
free(titem); |
| 565 |
|
titem=tstack; |
| 566 |
|
} |
| 567 |
|
|
| 568 |
|
if(! (env->err)){ |
| 569 |
|
if(fprintf(stream, " ]") < 0){ |
| 570 |
|
perror("print_val"); |
| 571 |
|
env->err= 5; |
| 572 |
|
} |
| 573 |
|
} |
| 574 |
break; |
break; |
| 575 |
} |
} |
| 576 |
} |
} |
| 577 |
|
|
| 578 |
/* Prints the top element of the stack and then discards it. */ |
/* Swap the two top elements on the stack. */ |
| 579 |
extern void print(stackitem** stack_head) |
extern void swap(environment *env) |
| 580 |
{ |
{ |
| 581 |
print_(stack_head); |
value *temp= env->head; |
| 582 |
toss(stack_head); |
|
| 583 |
} |
if(env->head->type == empty || CDR(env->head)->type == empty) { |
| 584 |
|
printerr("Too Few Arguments"); |
| 585 |
/* Only to be called by function printstack. */ |
env->err=1; |
| 586 |
void print_st(stackitem* stack_head, long counter) |
return; |
| 587 |
{ |
} |
|
if(stack_head->next != NULL) |
|
|
print_st(stack_head->next, counter+1); |
|
| 588 |
|
|
| 589 |
printf("%ld: ", counter); |
env->head= CDR(env->head); |
| 590 |
print_(&stack_head); |
CDR(temp)= CDR(env->head); |
| 591 |
nl(); |
CDR(env->head)= temp; |
| 592 |
} |
} |
| 593 |
|
|
| 594 |
/* Prints the stack. */ |
/* Recall a value from a symbol, if bound */ |
| 595 |
extern void printstack(stackitem** stack_head) |
extern void rcl(environment *env) |
| 596 |
{ |
{ |
| 597 |
if(*stack_head != NULL) { |
value *val; |
| 598 |
print_st(*stack_head, 1); |
|
| 599 |
printf("\n"); |
if(env->head->type==empty) { |
| 600 |
} else { |
printerr("Too Few Arguments"); |
| 601 |
printerr("Stack empty"); |
env->err= 1; |
| 602 |
|
return; |
| 603 |
|
} |
| 604 |
|
|
| 605 |
|
if(CAR(env->head)->type!=symb) { |
| 606 |
|
printerr("Bad Argument Type"); |
| 607 |
|
env->err= 2; |
| 608 |
|
return; |
| 609 |
} |
} |
| 610 |
|
|
| 611 |
|
val= CAR(env->head)->content.sym->val; |
| 612 |
|
if(val == NULL){ |
| 613 |
|
printerr("Unbound Variable"); |
| 614 |
|
env->err= 3; |
| 615 |
|
return; |
| 616 |
|
} |
| 617 |
|
push_val(env, val); /* Return the symbol's bound value */ |
| 618 |
|
swap(env); |
| 619 |
|
if(env->err) return; |
| 620 |
|
toss(env); /* toss the symbol */ |
| 621 |
|
if(env->err) return; |
| 622 |
} |
} |
| 623 |
|
|
| 624 |
/* If the top element is a reference, determine if it's a reference to a |
|
| 625 |
function, and if it is, toss the reference and execute the function. */ |
/* If the top element is a symbol, determine if it's bound to a |
| 626 |
extern void eval(stackitem** stack_head) |
function value, and if it is, toss the symbol and execute the |
| 627 |
|
function. */ |
| 628 |
|
extern void eval(environment *env) |
| 629 |
{ |
{ |
| 630 |
funcp in_func; |
funcp in_func; |
| 631 |
|
value* temp_val; |
| 632 |
|
value* iterator; |
| 633 |
|
|
| 634 |
|
eval_start: |
| 635 |
|
|
| 636 |
if((*stack_head)==NULL || (*stack_head)->type!=ref) { |
gc_maybe(env); |
| 637 |
printerr("Stack empty or not a reference"); |
|
| 638 |
|
if(env->head->type==empty) { |
| 639 |
|
printerr("Too Few Arguments"); |
| 640 |
|
env->err= 1; |
| 641 |
return; |
return; |
| 642 |
} |
} |
| 643 |
|
|
| 644 |
if(((stackitem*)(*stack_head)->content.ptr)->type==func) { |
switch(CAR(env->head)->type) { |
| 645 |
in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; |
/* if it's a symbol */ |
| 646 |
toss(stack_head); |
case symb: |
| 647 |
(*in_func)(stack_head); |
rcl(env); /* get its contents */ |
| 648 |
|
if(env->err) return; |
| 649 |
|
if(CAR(env->head)->type!=symb){ /* don't recurse symbols */ |
| 650 |
|
goto eval_start; |
| 651 |
|
} |
| 652 |
return; |
return; |
| 653 |
} else |
|
| 654 |
printerr("Not a function"); |
/* If it's a lone function value, run it */ |
| 655 |
|
case func: |
| 656 |
|
in_func= (funcp)(CAR(env->head)->content.ptr); |
| 657 |
|
toss(env); |
| 658 |
|
if(env->err) return; |
| 659 |
|
return in_func(env); |
| 660 |
|
|
| 661 |
|
/* If it's a list */ |
| 662 |
|
case tcons: |
| 663 |
|
temp_val= CAR(env->head); |
| 664 |
|
protect(temp_val); |
| 665 |
|
|
| 666 |
|
toss(env); if(env->err) return; |
| 667 |
|
iterator= temp_val; |
| 668 |
|
|
| 669 |
|
while(iterator->type != empty) { |
| 670 |
|
push_val(env, CAR(iterator)); |
| 671 |
|
|
| 672 |
|
if(CAR(env->head)->type==symb |
| 673 |
|
&& CAR(env->head)->content.sym->id[0]==';') { |
| 674 |
|
toss(env); |
| 675 |
|
if(env->err) return; |
| 676 |
|
|
| 677 |
|
if(CDR(iterator)->type == empty){ |
| 678 |
|
goto eval_start; |
| 679 |
|
} |
| 680 |
|
eval(env); |
| 681 |
|
if(env->err) return; |
| 682 |
|
} |
| 683 |
|
if (CDR(iterator)->type == empty || CDR(iterator)->type == tcons) |
| 684 |
|
iterator= CDR(iterator); |
| 685 |
|
else { |
| 686 |
|
printerr("Bad Argument Type"); /* Improper list */ |
| 687 |
|
env->err= 2; |
| 688 |
|
return; |
| 689 |
|
} |
| 690 |
|
} |
| 691 |
|
unprotect(temp_val); |
| 692 |
|
return; |
| 693 |
|
|
| 694 |
|
case empty: |
| 695 |
|
toss(env); |
| 696 |
|
case integer: |
| 697 |
|
case tfloat: |
| 698 |
|
case string: |
| 699 |
|
case port: |
| 700 |
|
return; |
| 701 |
|
} |
| 702 |
} |
} |
| 703 |
|
|
| 704 |
/* Parse input. */ |
/* List all defined words */ |
| 705 |
int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line) |
extern void words(environment *env) |
| 706 |
{ |
{ |
| 707 |
char *temp, *rest; |
symbol *temp; |
| 708 |
int itemp; |
int i; |
| 709 |
size_t inlength= strlen(in_line)+1; |
|
| 710 |
int convert= 0; |
for(i= 0; i<HASHTBLSIZE; i++) { |
| 711 |
|
temp= env->symbols[i]; |
| 712 |
|
while(temp!=NULL) { |
| 713 |
|
#ifdef DEBUG |
| 714 |
|
if (temp->val != NULL && temp->val->gc.flag.protect) |
| 715 |
|
printf("(protected) "); |
| 716 |
|
#endif /* DEBUG */ |
| 717 |
|
printf("%s ", temp->id); |
| 718 |
|
temp= temp->next; |
| 719 |
|
} |
| 720 |
|
} |
| 721 |
|
} |
| 722 |
|
|
| 723 |
temp= malloc(inlength); |
/* Quit stack. */ |
| 724 |
rest= malloc(inlength); |
extern void quit(environment *env) |
| 725 |
|
{ |
| 726 |
|
int i; |
| 727 |
|
|
| 728 |
do { |
while(env->head->type != empty) |
| 729 |
/* If string */ |
toss(env); |
| 730 |
if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { |
|
| 731 |
push_cstring(stack_head, temp); |
if (env->err) return; |
| 732 |
break; |
for(i= 0; i<HASHTBLSIZE; i++) { |
| 733 |
} |
while(env->symbols[i]!= NULL) { |
| 734 |
/* If value */ |
forget_sym(&(env->symbols[i])); |
|
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; |
|
| 735 |
} |
} |
| 736 |
} while(0); |
env->symbols[i]= NULL; |
| 737 |
|
} |
| 738 |
|
|
| 739 |
|
env->gc_limit= 0; |
| 740 |
|
gc_maybe(env); |
| 741 |
|
|
| 742 |
free(temp); |
words(env); |
| 743 |
|
|
| 744 |
if(convert<2) { |
if(env->free_string!=NULL) |
| 745 |
free(rest); |
free(env->free_string); |
|
return 0; |
|
|
} |
|
|
|
|
|
stack_read(stack_head, in_hash, rest); |
|
| 746 |
|
|
| 747 |
free(rest); |
#ifdef __linux__ |
| 748 |
return 1; |
muntrace(); |
| 749 |
|
#endif |
| 750 |
|
|
| 751 |
|
exit(EXIT_SUCCESS); |
| 752 |
} |
} |
| 753 |
|
|
| 754 |
/* Make a list. */ |
/* Internal forget function */ |
| 755 |
extern void pack(stackitem** stack_head) |
void forget_sym(symbol **hash_entry) |
| 756 |
{ |
{ |
| 757 |
void* delimiter; |
symbol *temp; |
| 758 |
stackitem *iterator, *temp, *pack; |
|
| 759 |
|
temp= *hash_entry; |
| 760 |
|
*hash_entry= (*hash_entry)->next; |
| 761 |
|
|
| 762 |
|
free(temp->id); |
| 763 |
|
free(temp); |
| 764 |
|
} |
| 765 |
|
|
| 766 |
|
/* Only to be called by itself function printstack. */ |
| 767 |
|
void print_st(environment *env, value *stack_head, long counter) |
| 768 |
|
{ |
| 769 |
|
if(CDR(stack_head)->type != empty) |
| 770 |
|
print_st(env, CDR(stack_head), counter+1); |
| 771 |
|
printf("%ld: ", counter); |
| 772 |
|
print_val(env, CAR(stack_head), 0, NULL, stdout); |
| 773 |
|
printf("\n"); |
| 774 |
|
} |
| 775 |
|
|
| 776 |
if((*stack_head)==NULL) { |
/* Prints the stack. */ |
| 777 |
printerr("Stack empty"); |
extern void printstack(environment *env) |
| 778 |
|
{ |
| 779 |
|
if(env->head->type == empty) { |
| 780 |
|
printf("Stack Empty\n"); |
| 781 |
return; |
return; |
| 782 |
} |
} |
| 783 |
|
|
| 784 |
delimiter= (*stack_head)->content.ptr; /* Get delimiter */ |
print_st(env, env->head, 1); |
| 785 |
toss(stack_head); |
} |
| 786 |
|
|
| 787 |
|
int main(int argc, char **argv) |
| 788 |
|
{ |
| 789 |
|
environment myenv; |
| 790 |
|
|
| 791 |
|
int c; /* getopt option character */ |
| 792 |
|
|
| 793 |
iterator= *stack_head; |
#ifdef __linux__ |
| 794 |
|
mtrace(); |
| 795 |
|
#endif |
| 796 |
|
|
| 797 |
/* Search for first delimiter */ |
init_env(&myenv); |
|
while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) |
|
|
iterator= iterator->next; |
|
| 798 |
|
|
| 799 |
/* Extract list */ |
myenv.interactive = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO); |
| 800 |
temp= *stack_head; |
|
| 801 |
*stack_head= iterator->next; |
while ((c = getopt (argc, argv, "i")) != -1) |
| 802 |
iterator->next= NULL; |
switch (c) |
| 803 |
|
{ |
| 804 |
|
case 'i': |
| 805 |
|
myenv.interactive = 1; |
| 806 |
|
break; |
| 807 |
|
case '?': |
| 808 |
|
fprintf (stderr, |
| 809 |
|
"Unknown option character '\\x%x'.\n", |
| 810 |
|
optopt); |
| 811 |
|
return EX_USAGE; |
| 812 |
|
default: |
| 813 |
|
abort (); |
| 814 |
|
} |
| 815 |
|
|
| 816 |
if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) |
if (optind < argc) { |
| 817 |
toss(stack_head); |
myenv.interactive = 0; |
| 818 |
|
myenv.inputstream= fopen(argv[optind], "r"); |
| 819 |
|
if(myenv.inputstream== NULL) { |
| 820 |
|
perror(argv[0]); |
| 821 |
|
exit (EX_NOINPUT); |
| 822 |
|
} |
| 823 |
|
} |
| 824 |
|
|
| 825 |
/* Push list */ |
if(myenv.interactive) { |
| 826 |
pack= malloc(sizeof(stackitem)); |
printf("Stack version $Revision$\n\ |
| 827 |
pack->type= list; |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn\n\ |
| 828 |
pack->content.ptr= temp; |
Stack comes with ABSOLUTELY NO WARRANTY; for details type 'warranty;'.\n\ |
| 829 |
|
This is free software, and you are welcome to redistribute it\n\ |
| 830 |
|
under certain conditions; type 'copying;' for details.\n"); |
| 831 |
|
} |
| 832 |
|
|
| 833 |
push(stack_head, pack); |
while(1) { |
| 834 |
|
if(myenv.in_string==NULL) { |
| 835 |
|
if (myenv.interactive) { |
| 836 |
|
if(myenv.err) { |
| 837 |
|
printf("(error %d)\n", myenv.err); |
| 838 |
|
myenv.err= 0; |
| 839 |
|
} |
| 840 |
|
printf("\n"); |
| 841 |
|
printstack(&myenv); |
| 842 |
|
printf("> "); |
| 843 |
|
} |
| 844 |
|
myenv.err=0; |
| 845 |
|
} |
| 846 |
|
readstream(&myenv, myenv.inputstream); |
| 847 |
|
if (myenv.err) { /* EOF or other error */ |
| 848 |
|
myenv.err=0; |
| 849 |
|
quit(&myenv); |
| 850 |
|
} else if(myenv.head->type!=empty |
| 851 |
|
&& CAR(myenv.head)->type==symb |
| 852 |
|
&& CAR(myenv.head)->content.sym->id[0] == ';') { |
| 853 |
|
toss(&myenv); if(myenv.err) continue; |
| 854 |
|
eval(&myenv); |
| 855 |
|
} else { |
| 856 |
|
gc_maybe(&myenv); |
| 857 |
|
} |
| 858 |
|
} |
| 859 |
|
quit(&myenv); |
| 860 |
|
return EXIT_FAILURE; |
| 861 |
} |
} |
| 862 |
|
|
| 863 |
/* Relocate elements of the list on the stack. */ |
/* Return copy of a value */ |
| 864 |
extern void expand(stackitem** stack_head) |
value *copy_val(environment *env, value *old_value) |
| 865 |
{ |
{ |
| 866 |
stackitem *temp, *new_head; |
value *new_value; |
| 867 |
|
|
| 868 |
/* Is top element a list? */ |
if(old_value==NULL) |
| 869 |
if((*stack_head)==NULL || (*stack_head)->type!=list) { |
return NULL; |
| 870 |
printerr("Stack empty or not a list"); |
|
| 871 |
return; |
new_value= new_val(env); |
| 872 |
|
new_value->type= old_value->type; |
| 873 |
|
|
| 874 |
|
switch(old_value->type){ |
| 875 |
|
case tfloat: |
| 876 |
|
case integer: |
| 877 |
|
case func: |
| 878 |
|
case symb: |
| 879 |
|
case empty: |
| 880 |
|
case port: |
| 881 |
|
new_value->content= old_value->content; |
| 882 |
|
break; |
| 883 |
|
case string: |
| 884 |
|
(char *)(new_value->content.ptr)= |
| 885 |
|
strdup((char *)(old_value->content.ptr)); |
| 886 |
|
break; |
| 887 |
|
case tcons: |
| 888 |
|
|
| 889 |
|
new_value->content.c= malloc(sizeof(pair)); |
| 890 |
|
assert(new_value->content.c!=NULL); |
| 891 |
|
env->gc_count += sizeof(pair); |
| 892 |
|
|
| 893 |
|
CAR(new_value)= copy_val(env, CAR(old_value)); /* recurse */ |
| 894 |
|
CDR(new_value)= copy_val(env, CDR(old_value)); /* recurse */ |
| 895 |
|
break; |
| 896 |
} |
} |
| 897 |
|
|
| 898 |
/* The first list element is the new stack head */ |
return new_value; |
| 899 |
new_head= temp= (*stack_head)->content.ptr; |
} |
|
toss(stack_head); |
|
| 900 |
|
|
| 901 |
/* Search the end of the list */ |
/* read a line from a stream; used by readline */ |
| 902 |
while(temp->next!=NULL) |
void readlinestream(environment *env, FILE *stream) |
| 903 |
temp= temp->next; |
{ |
| 904 |
|
char in_string[101]; |
| 905 |
|
|
| 906 |
/* Connect the the tail of the list with the old stack head */ |
if(fgets(in_string, 100, stream)==NULL) { |
| 907 |
temp->next= *stack_head; |
push_cstring(env, ""); |
| 908 |
*stack_head= new_head; /* ...and voila! */ |
if (! feof(stream)){ |
| 909 |
|
perror("readline"); |
| 910 |
|
env->err= 5; |
| 911 |
|
} |
| 912 |
|
} else { |
| 913 |
|
push_cstring(env, in_string); |
| 914 |
|
} |
| 915 |
} |
} |
| 916 |
|
|
| 917 |
/* Swap the two top elements on the stack. */ |
/* Reverse (flip) a list */ |
| 918 |
extern void swap(stackitem** stack_head) |
extern void rev(environment *env) |
| 919 |
{ |
{ |
| 920 |
stackitem* temp= (*stack_head); |
value *old_head, *new_head, *item; |
| 921 |
|
|
| 922 |
if((*stack_head)==NULL) { |
if(env->head->type==empty) { |
| 923 |
printerr("Stack empty"); |
printerr("Too Few Arguments"); |
| 924 |
|
env->err= 1; |
| 925 |
return; |
return; |
| 926 |
} |
} |
| 927 |
|
|
| 928 |
if((*stack_head)->next==NULL) |
if(CAR(env->head)->type==empty) |
| 929 |
|
return; /* Don't reverse an empty list */ |
| 930 |
|
|
| 931 |
|
if(CAR(env->head)->type!=tcons) { |
| 932 |
|
printerr("Bad Argument Type"); |
| 933 |
|
env->err= 2; |
| 934 |
return; |
return; |
| 935 |
|
} |
| 936 |
|
|
| 937 |
*stack_head= (*stack_head)->next; |
old_head= CAR(env->head); |
| 938 |
temp->next= (*stack_head)->next; |
new_head= new_val(env); |
| 939 |
(*stack_head)->next= temp; |
while(old_head->type != empty) { |
| 940 |
} |
item= old_head; |
| 941 |
|
old_head= CDR(old_head); |
| 942 |
|
CDR(item)= new_head; |
| 943 |
|
new_head= item; |
| 944 |
|
} |
| 945 |
|
CAR(env->head)= new_head; |
| 946 |
|
} |
| 947 |
|
|
| 948 |
/* Compares two elements by reference. */ |
/* Make a list. */ |
| 949 |
extern void eq(stackitem** stack_head) |
extern void pack(environment *env) |
| 950 |
{ |
{ |
| 951 |
void *left, *right; |
value *iterator, *temp, *ending; |
|
int result; |
|
| 952 |
|
|
| 953 |
if((*stack_head)==NULL || (*stack_head)->next==NULL) { |
ending=new_val(env); |
| 954 |
printerr("Not enough elements to compare"); |
|
| 955 |
return; |
iterator= env->head; |
| 956 |
|
if(iterator->type == empty |
| 957 |
|
|| (CAR(iterator)->type==symb |
| 958 |
|
&& CAR(iterator)->content.sym->id[0]=='[')) { |
| 959 |
|
temp= ending; |
| 960 |
|
toss(env); |
| 961 |
|
} else { |
| 962 |
|
/* Search for first delimiter */ |
| 963 |
|
while(CDR(iterator)->type != empty |
| 964 |
|
&& (CAR(CDR(iterator))->type!=symb |
| 965 |
|
|| CAR(CDR(iterator))->content.sym->id[0]!='[')) |
| 966 |
|
iterator= CDR(iterator); |
| 967 |
|
|
| 968 |
|
/* Extract list */ |
| 969 |
|
temp= env->head; |
| 970 |
|
env->head= CDR(iterator); |
| 971 |
|
CDR(iterator)= ending; |
| 972 |
|
|
| 973 |
|
if(env->head->type != empty) |
| 974 |
|
toss(env); |
| 975 |
} |
} |
| 976 |
|
|
| 977 |
left= (*stack_head)->content.ptr; |
/* Push list */ |
| 978 |
swap(stack_head); |
|
| 979 |
right= (*stack_head)->content.ptr; |
push_val(env, temp); |
| 980 |
result= (left==right); |
rev(env); |
|
|
|
|
toss(stack_head); toss(stack_head); |
|
|
push_val(stack_head, (left==right)); |
|
| 981 |
} |
} |
| 982 |
|
|
| 983 |
/* Negates the top element on the stack. */ |
/* read from a stream; used by "read" and "readport" */ |
| 984 |
extern void not(stackitem** stack_head) |
void readstream(environment *env, FILE *stream) |
| 985 |
{ |
{ |
| 986 |
int value; |
const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; |
| 987 |
|
const char strform[]= "\"%[^\"]\"%n"; |
| 988 |
|
const char intform[]= "%i%n"; |
| 989 |
|
const char fltform[]= "%f%n"; |
| 990 |
|
const char blankform[]= "%*[ \t]%n"; |
| 991 |
|
const char ebrackform[]= "]%n"; |
| 992 |
|
const char semicform[]= ";%n"; |
| 993 |
|
const char bbrackform[]= "[%n"; |
| 994 |
|
|
| 995 |
|
int itemp, readlength= -1; |
| 996 |
|
int count= -1; |
| 997 |
|
float ftemp; |
| 998 |
|
static int depth= 0; |
| 999 |
|
char *match; |
| 1000 |
|
size_t inlength; |
| 1001 |
|
|
| 1002 |
|
if(env->in_string==NULL) { |
| 1003 |
|
if(depth > 0 && env->interactive) { |
| 1004 |
|
printf("]> "); |
| 1005 |
|
} |
| 1006 |
|
readlinestream(env, env->inputstream); |
| 1007 |
|
if(env->err) return; |
| 1008 |
|
|
| 1009 |
if((*stack_head)==NULL || (*stack_head)->type!=value) { |
if(((char *)(CAR(env->head)->content.ptr))[0]=='\0'){ |
| 1010 |
printerr("Stack empty or element is not a value"); |
env->err= 4; /* "" means EOF */ |
| 1011 |
return; |
return; |
| 1012 |
|
} |
| 1013 |
|
|
| 1014 |
|
env->in_string= malloc(strlen(CAR(env->head)->content.ptr)+1); |
| 1015 |
|
assert(env->in_string != NULL); |
| 1016 |
|
env->free_string= env->in_string; /* Save the original pointer */ |
| 1017 |
|
strcpy(env->in_string, CAR(env->head)->content.ptr); |
| 1018 |
|
toss(env); if(env->err) return; |
| 1019 |
|
} |
| 1020 |
|
|
| 1021 |
|
inlength= strlen(env->in_string)+1; |
| 1022 |
|
match= malloc(inlength); |
| 1023 |
|
assert(match != NULL); |
| 1024 |
|
|
| 1025 |
|
if(sscanf(env->in_string, blankform, &readlength) != EOF |
| 1026 |
|
&& readlength != -1) { |
| 1027 |
|
; |
| 1028 |
|
} else if(sscanf(env->in_string, fltform, &ftemp, &readlength) != EOF |
| 1029 |
|
&& readlength != -1) { |
| 1030 |
|
if(sscanf(env->in_string, intform, &itemp, &count) != EOF |
| 1031 |
|
&& count==readlength) { |
| 1032 |
|
push_int(env, itemp); |
| 1033 |
|
} else { |
| 1034 |
|
push_float(env, ftemp); |
| 1035 |
|
} |
| 1036 |
|
} else if(sscanf(env->in_string, "\"\"%n", &readlength) != EOF |
| 1037 |
|
&& readlength != -1) { |
| 1038 |
|
push_cstring(env, ""); |
| 1039 |
|
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
| 1040 |
|
&& readlength != -1) { |
| 1041 |
|
push_cstring(env, match); |
| 1042 |
|
} else if(sscanf(env->in_string, symbform, match, &readlength) != EOF |
| 1043 |
|
&& readlength != -1) { |
| 1044 |
|
push_sym(env, match); |
| 1045 |
|
} else if(sscanf(env->in_string, ebrackform, &readlength) != EOF |
| 1046 |
|
&& readlength != -1) { |
| 1047 |
|
pack(env); if(env->err) return; |
| 1048 |
|
if(depth != 0) depth--; |
| 1049 |
|
} else if(sscanf(env->in_string, semicform, &readlength) != EOF |
| 1050 |
|
&& readlength != -1) { |
| 1051 |
|
push_sym(env, ";"); |
| 1052 |
|
} else if(sscanf(env->in_string, bbrackform, &readlength) != EOF |
| 1053 |
|
&& readlength != -1) { |
| 1054 |
|
push_sym(env, "["); |
| 1055 |
|
depth++; |
| 1056 |
|
} else { |
| 1057 |
|
free(env->free_string); |
| 1058 |
|
env->in_string = env->free_string = NULL; |
| 1059 |
|
} |
| 1060 |
|
if (env->in_string != NULL) { |
| 1061 |
|
env->in_string += readlength; |
| 1062 |
} |
} |
| 1063 |
|
|
| 1064 |
value= (*stack_head)->content.val; |
free(match); |
|
toss(stack_head); |
|
|
push_val(stack_head, !value); |
|
|
} |
|
| 1065 |
|
|
| 1066 |
/* Compares the two top elements on the stack and return 0 if they're the |
if(depth) |
| 1067 |
same. */ |
return readstream(env, env->inputstream); |
|
extern void neq(stackitem** stack_head) |
|
|
{ |
|
|
eq(stack_head); |
|
|
not(stack_head); |
|
| 1068 |
} |
} |
| 1069 |
|
|
| 1070 |
/* Give a symbol some content. */ |
extern void copying(environment *env) |
|
extern void def(stackitem** stack_head) |
|
| 1071 |
{ |
{ |
| 1072 |
stackitem *temp, *value; |
printf(" GNU GENERAL PUBLIC LICENSE\n\ |
| 1073 |
|
Version 2, June 1991\n\ |
| 1074 |
if(*stack_head==NULL || (*stack_head)->next==NULL |
\n\ |
| 1075 |
|| (*stack_head)->type!=ref) { |
Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n\ |
| 1076 |
printerr("Define what?"); |
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n\ |
| 1077 |
|
Everyone is permitted to copy and distribute verbatim copies\n\ |
| 1078 |
|
of this license document, but changing it is not allowed.\n\ |
| 1079 |
|
\n\ |
| 1080 |
|
Preamble\n\ |
| 1081 |
|
\n\ |
| 1082 |
|
The licenses for most software are designed to take away your\n\ |
| 1083 |
|
freedom to share and change it. By contrast, the GNU General Public\n\ |
| 1084 |
|
License is intended to guarantee your freedom to share and change free\n\ |
| 1085 |
|
software--to make sure the software is free for all its users. This\n\ |
| 1086 |
|
General Public License applies to most of the Free Software\n\ |
| 1087 |
|
Foundation's software and to any other program whose authors commit to\n\ |
| 1088 |
|
using it. (Some other Free Software Foundation software is covered by\n\ |
| 1089 |
|
the GNU Library General Public License instead.) You can apply it to\n\ |
| 1090 |
|
your programs, too.\n\ |
| 1091 |
|
\n\ |
| 1092 |
|
When we speak of free software, we are referring to freedom, not\n\ |
| 1093 |
|
price. Our General Public Licenses are designed to make sure that you\n\ |
| 1094 |
|
have the freedom to distribute copies of free software (and charge for\n\ |
| 1095 |
|
this service if you wish), that you receive source code or can get it\n\ |
| 1096 |
|
if you want it, that you can change the software or use pieces of it\n\ |
| 1097 |
|
in new free programs; and that you know you can do these things.\n\ |
| 1098 |
|
\n\ |
| 1099 |
|
To protect your rights, we need to make restrictions that forbid\n\ |
| 1100 |
|
anyone to deny you these rights or to ask you to surrender the rights.\n\ |
| 1101 |
|
These restrictions translate to certain responsibilities for you if you\n\ |
| 1102 |
|
distribute copies of the software, or if you modify it.\n\ |
| 1103 |
|
\n\ |
| 1104 |
|
For example, if you distribute copies of such a program, whether\n\ |
| 1105 |
|
gratis or for a fee, you must give the recipients all the rights that\n\ |
| 1106 |
|
you have. You must make sure that they, too, receive or can get the\n\ |
| 1107 |
|
source code. And you must show them these terms so they know their\n\ |
| 1108 |
|
rights.\n\ |
| 1109 |
|
\n\ |
| 1110 |
|
We protect your rights with two steps: (1) copyright the software, and\n\ |
| 1111 |
|
(2) offer you this license which gives you legal permission to copy,\n\ |
| 1112 |
|
distribute and/or modify the software.\n\ |
| 1113 |
|
\n\ |
| 1114 |
|
Also, for each author's protection and ours, we want to make certain\n\ |
| 1115 |
|
that everyone understands that there is no warranty for this free\n\ |
| 1116 |
|
software. If the software is modified by someone else and passed on, we\n\ |
| 1117 |
|
want its recipients to know that what they have is not the original, so\n\ |
| 1118 |
|
that any problems introduced by others will not reflect on the original\n\ |
| 1119 |
|
authors' reputations.\n\ |
| 1120 |
|
\n\ |
| 1121 |
|
Finally, any free program is threatened constantly by software\n\ |
| 1122 |
|
patents. We wish to avoid the danger that redistributors of a free\n\ |
| 1123 |
|
program will individually obtain patent licenses, in effect making the\n\ |
| 1124 |
|
program proprietary. To prevent this, we have made it clear that any\n\ |
| 1125 |
|
patent must be licensed for everyone's free use or not licensed at all.\n\ |
| 1126 |
|
\n\ |
| 1127 |
|
The precise terms and conditions for copying, distribution and\n\ |
| 1128 |
|
modification follow.\n\ |
| 1129 |
|
\n\ |
| 1130 |
|
GNU GENERAL PUBLIC LICENSE\n\ |
| 1131 |
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n\ |
| 1132 |
|
\n\ |
| 1133 |
|
0. This License applies to any program or other work which contains\n\ |
| 1134 |
|
a notice placed by the copyright holder saying it may be distributed\n\ |
| 1135 |
|
under the terms of this General Public License. The \"Program\", below,\n\ |
| 1136 |
|
refers to any such program or work, and a \"work based on the Program\"\n\ |
| 1137 |
|
means either the Program or any derivative work under copyright law:\n\ |
| 1138 |
|
that is to say, a work containing the Program or a portion of it,\n\ |
| 1139 |
|
either verbatim or with modifications and/or translated into another\n\ |
| 1140 |
|
language. (Hereinafter, translation is included without limitation in\n\ |
| 1141 |
|
the term \"modification\".) Each licensee is addressed as \"you\".\n\ |
| 1142 |
|
\n\ |
| 1143 |
|
Activities other than copying, distribution and modification are not\n\ |
| 1144 |
|
covered by this License; they are outside its scope. The act of\n\ |
| 1145 |
|
running the Program is not restricted, and the output from the Program\n\ |
| 1146 |
|
is covered only if its contents constitute a work based on the\n\ |
| 1147 |
|
Program (independent of having been made by running the Program).\n\ |
| 1148 |
|
Whether that is true depends on what the Program does.\n\ |
| 1149 |
|
\n\ |
| 1150 |
|
1. You may copy and distribute verbatim copies of the Program's\n\ |
| 1151 |
|
source code as you receive it, in any medium, provided that you\n\ |
| 1152 |
|
conspicuously and appropriately publish on each copy an appropriate\n\ |
| 1153 |
|
copyright notice and disclaimer of warranty; keep intact all the\n\ |
| 1154 |
|
notices that refer to this License and to the absence of any warranty;\n\ |
| 1155 |
|
and give any other recipients of the Program a copy of this License\n\ |
| 1156 |
|
along with the Program.\n\ |
| 1157 |
|
\n\ |
| 1158 |
|
You may charge a fee for the physical act of transferring a copy, and\n\ |
| 1159 |
|
you may at your option offer warranty protection in exchange for a fee.\n\ |
| 1160 |
|
\n\ |
| 1161 |
|
2. You may modify your copy or copies of the Program or any portion\n\ |
| 1162 |
|
of it, thus forming a work based on the Program, and copy and\n\ |
| 1163 |
|
distribute such modifications or work under the terms of Section 1\n\ |
| 1164 |
|
above, provided that you also meet all of these conditions:\n\ |
| 1165 |
|
\n\ |
| 1166 |
|
a) You must cause the modified files to carry prominent notices\n\ |
| 1167 |
|
stating that you changed the files and the date of any change.\n\ |
| 1168 |
|
\n\ |
| 1169 |
|
b) You must cause any work that you distribute or publish, that in\n\ |
| 1170 |
|
whole or in part contains or is derived from the Program or any\n\ |
| 1171 |
|
part thereof, to be licensed as a whole at no charge to all third\n\ |
| 1172 |
|
parties under the terms of this License.\n\ |
| 1173 |
|
\n\ |
| 1174 |
|
c) If the modified program normally reads commands interactively\n\ |
| 1175 |
|
when run, you must cause it, when started running for such\n\ |
| 1176 |
|
interactive use in the most ordinary way, to print or display an\n\ |
| 1177 |
|
announcement including an appropriate copyright notice and a\n\ |
| 1178 |
|
notice that there is no warranty (or else, saying that you provide\n\ |
| 1179 |
|
a warranty) and that users may redistribute the program under\n\ |
| 1180 |
|
these conditions, and telling the user how to view a copy of this\n\ |
| 1181 |
|
License. (Exception: if the Program itself is interactive but\n\ |
| 1182 |
|
does not normally print such an announcement, your work based on\n\ |
| 1183 |
|
the Program is not required to print an announcement.)\n\ |
| 1184 |
|
\n\ |
| 1185 |
|
These requirements apply to the modified work as a whole. If\n\ |
| 1186 |
|
identifiable sections of that work are not derived from the Program,\n\ |
| 1187 |
|
and can be reasonably considered independent and separate works in\n\ |
| 1188 |
|
themselves, then this License, and its terms, do not apply to those\n\ |
| 1189 |
|
sections when you distribute them as separate works. But when you\n\ |
| 1190 |
|
distribute the same sections as part of a whole which is a work based\n\ |
| 1191 |
|
on the Program, the distribution of the whole must be on the terms of\n\ |
| 1192 |
|
this License, whose permissions for other licensees extend to the\n\ |
| 1193 |
|
entire whole, and thus to each and every part regardless of who wrote it.\n\ |
| 1194 |
|
\n\ |
| 1195 |
|
Thus, it is not the intent of this section to claim rights or contest\n\ |
| 1196 |
|
your rights to work written entirely by you; rather, the intent is to\n\ |
| 1197 |
|
exercise the right to control the distribution of derivative or\n\ |
| 1198 |
|
collective works based on the Program.\n\ |
| 1199 |
|
\n\ |
| 1200 |
|
In addition, mere aggregation of another work not based on the Program\n\ |
| 1201 |
|
with the Program (or with a work based on the Program) on a volume of\n\ |
| 1202 |
|
a storage or distribution medium does not bring the other work under\n\ |
| 1203 |
|
the scope of this License.\n\ |
| 1204 |
|
\n\ |
| 1205 |
|
3. You may copy and distribute the Program (or a work based on it,\n\ |
| 1206 |
|
under Section 2) in object code or executable form under the terms of\n\ |
| 1207 |
|
Sections 1 and 2 above provided that you also do one of the following:\n\ |
| 1208 |
|
\n\ |
| 1209 |
|
a) Accompany it with the complete corresponding machine-readable\n\ |
| 1210 |
|
source code, which must be distributed under the terms of Sections\n\ |
| 1211 |
|
1 and 2 above on a medium customarily used for software interchange; or,\n\ |
| 1212 |
|
\n\ |
| 1213 |
|
b) Accompany it with a written offer, valid for at least three\n\ |
| 1214 |
|
years, to give any third party, for a charge no more than your\n\ |
| 1215 |
|
cost of physically performing source distribution, a complete\n\ |
| 1216 |
|
machine-readable copy of the corresponding source code, to be\n\ |
| 1217 |
|
distributed under the terms of Sections 1 and 2 above on a medium\n\ |
| 1218 |
|
customarily used for software interchange; or,\n\ |
| 1219 |
|
\n\ |
| 1220 |
|
c) Accompany it with the information you received as to the offer\n\ |
| 1221 |
|
to distribute corresponding source code. (This alternative is\n\ |
| 1222 |
|
allowed only for noncommercial distribution and only if you\n\ |
| 1223 |
|
received the program in object code or executable form with such\n\ |
| 1224 |
|
an offer, in accord with Subsection b above.)\n\ |
| 1225 |
|
\n\ |
| 1226 |
|
The source code for a work means the preferred form of the work for\n\ |
| 1227 |
|
making modifications to it. For an executable work, complete source\n\ |
| 1228 |
|
code means all the source code for all modules it contains, plus any\n\ |
| 1229 |
|
associated interface definition files, plus the scripts used to\n\ |
| 1230 |
|
control compilation and installation of the executable. However, as a\n\ |
| 1231 |
|
special exception, the source code distributed need not include\n\ |
| 1232 |
|
anything that is normally distributed (in either source or binary\n\ |
| 1233 |
|
form) with the major components (compiler, kernel, and so on) of the\n\ |
| 1234 |
|
operating system on which the executable runs, unless that component\n\ |
| 1235 |
|
itself accompanies the executable.\n\ |
| 1236 |
|
\n\ |
| 1237 |
|
If distribution of executable or object code is made by offering\n\ |
| 1238 |
|
access to copy from a designated place, then offering equivalent\n\ |
| 1239 |
|
access to copy the source code from the same place counts as\n\ |
| 1240 |
|
distribution of the source code, even though third parties are not\n\ |
| 1241 |
|
compelled to copy the source along with the object code.\n\ |
| 1242 |
|
\n\ |
| 1243 |
|
4. You may not copy, modify, sublicense, or distribute the Program\n\ |
| 1244 |
|
except as expressly provided under this License. Any attempt\n\ |
| 1245 |
|
otherwise to copy, modify, sublicense or distribute the Program is\n\ |
| 1246 |
|
void, and will automatically terminate your rights under this License.\n\ |
| 1247 |
|
However, parties who have received copies, or rights, from you under\n\ |
| 1248 |
|
this License will not have their licenses terminated so long as such\n\ |
| 1249 |
|
parties remain in full compliance.\n\ |
| 1250 |
|
\n\ |
| 1251 |
|
5. You are not required to accept this License, since you have not\n\ |
| 1252 |
|
signed it. However, nothing else grants you permission to modify or\n\ |
| 1253 |
|
distribute the Program or its derivative works. These actions are\n\ |
| 1254 |
|
prohibited by law if you do not accept this License. Therefore, by\n\ |
| 1255 |
|
modifying or distributing the Program (or any work based on the\n\ |
| 1256 |
|
Program), you indicate your acceptance of this License to do so, and\n\ |
| 1257 |
|
all its terms and conditions for copying, distributing or modifying\n\ |
| 1258 |
|
the Program or works based on it.\n\ |
| 1259 |
|
\n\ |
| 1260 |
|
6. Each time you redistribute the Program (or any work based on the\n\ |
| 1261 |
|
Program), the recipient automatically receives a license from the\n\ |
| 1262 |
|
original licensor to copy, distribute or modify the Program subject to\n\ |
| 1263 |
|
these terms and conditions. You may not impose any further\n\ |
| 1264 |
|
restrictions on the recipients' exercise of the rights granted herein.\n\ |
| 1265 |
|
You are not responsible for enforcing compliance by third parties to\n\ |
| 1266 |
|
this License.\n\ |
| 1267 |
|
\n\ |
| 1268 |
|
7. If, as a consequence of a court judgment or allegation of patent\n\ |
| 1269 |
|
infringement or for any other reason (not limited to patent issues),\n\ |
| 1270 |
|
conditions are imposed on you (whether by court order, agreement or\n\ |
| 1271 |
|
otherwise) that contradict the conditions of this License, they do not\n\ |
| 1272 |
|
excuse you from the conditions of this License. If you cannot\n\ |
| 1273 |
|
distribute so as to satisfy simultaneously your obligations under this\n\ |
| 1274 |
|
License and any other pertinent obligations, then as a consequence you\n\ |
| 1275 |
|
may not distribute the Program at all. For example, if a patent\n\ |
| 1276 |
|
license would not permit royalty-free redistribution of the Program by\n\ |
| 1277 |
|
all those who receive copies directly or indirectly through you, then\n\ |
| 1278 |
|
the only way you could satisfy both it and this License would be to\n\ |
| 1279 |
|
refrain entirely from distribution of the Program.\n\ |
| 1280 |
|
\n\ |
| 1281 |
|
If any portion of this section is held invalid or unenforceable under\n\ |
| 1282 |
|
any particular circumstance, the balance of the section is intended to\n\ |
| 1283 |
|
apply and the section as a whole is intended to apply in other\n\ |
| 1284 |
|
circumstances.\n\ |
| 1285 |
|
\n\ |
| 1286 |
|
It is not the purpose of this section to induce you to infringe any\n\ |
| 1287 |
|
patents or other property right claims or to contest validity of any\n\ |
| 1288 |
|
such claims; this section has the sole purpose of protecting the\n\ |
| 1289 |
|
integrity of the free software distribution system, which is\n\ |
| 1290 |
|
implemented by public license practices. Many people have made\n\ |
| 1291 |
|
generous contributions to the wide range of software distributed\n\ |
| 1292 |
|
through that system in reliance on consistent application of that\n\ |
| 1293 |
|
system; it is up to the author/donor to decide if he or she is willing\n\ |
| 1294 |
|
to distribute software through any other system and a licensee cannot\n\ |
| 1295 |
|
impose that choice.\n\ |
| 1296 |
|
\n\ |
| 1297 |
|
This section is intended to make thoroughly clear what is believed to\n\ |
| 1298 |
|
be a consequence of the rest of this License.\n\ |
| 1299 |
|
\n\ |
| 1300 |
|
8. If the distribution and/or use of the Program is restricted in\n\ |
| 1301 |
|
certain countries either by patents or by copyrighted interfaces, the\n\ |
| 1302 |
|
original copyright holder who places the Program under this License\n\ |
| 1303 |
|
may add an explicit geographical distribution limitation excluding\n\ |
| 1304 |
|
those countries, so that distribution is permitted only in or among\n\ |
| 1305 |
|
countries not thus excluded. In such case, this License incorporates\n\ |
| 1306 |
|
the limitation as if written in the body of this License.\n\ |
| 1307 |
|
\n\ |
| 1308 |
|
9. The Free Software Foundation may publish revised and/or new versions\n\ |
| 1309 |
|
of the General Public License from time to time. Such new versions will\n\ |
| 1310 |
|
be similar in spirit to the present version, but may differ in detail to\n\ |
| 1311 |
|
address new problems or concerns.\n\ |
| 1312 |
|
\n\ |
| 1313 |
|
Each version is given a distinguishing version number. If the Program\n\ |
| 1314 |
|
specifies a version number of this License which applies to it and \"any\n\ |
| 1315 |
|
later version\", you have the option of following the terms and conditions\n\ |
| 1316 |
|
either of that version or of any later version published by the Free\n\ |
| 1317 |
|
Software Foundation. If the Program does not specify a version number of\n\ |
| 1318 |
|
this License, you may choose any version ever published by the Free Software\n\ |
| 1319 |
|
Foundation.\n\ |
| 1320 |
|
\n\ |
| 1321 |
|
10. If you wish to incorporate parts of the Program into other free\n\ |
| 1322 |
|
programs whose distribution conditions are different, write to the author\n\ |
| 1323 |
|
to ask for permission. For software which is copyrighted by the Free\n\ |
| 1324 |
|
Software Foundation, write to the Free Software Foundation; we sometimes\n\ |
| 1325 |
|
make exceptions for this. Our decision will be guided by the two goals\n\ |
| 1326 |
|
of preserving the free status of all derivatives of our free software and\n\ |
| 1327 |
|
of promoting the sharing and reuse of software generally.\n"); |
| 1328 |
|
} |
| 1329 |
|
|
| 1330 |
|
extern void warranty(environment *env) |
| 1331 |
|
{ |
| 1332 |
|
printf(" NO WARRANTY\n\ |
| 1333 |
|
\n\ |
| 1334 |
|
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n\ |
| 1335 |
|
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN\n\ |
| 1336 |
|
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n\ |
| 1337 |
|
PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n\ |
| 1338 |
|
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n\ |
| 1339 |
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS\n\ |
| 1340 |
|
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE\n\ |
| 1341 |
|
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n\ |
| 1342 |
|
REPAIR OR CORRECTION.\n\ |
| 1343 |
|
\n\ |
| 1344 |
|
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n\ |
| 1345 |
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n\ |
| 1346 |
|
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n\ |
| 1347 |
|
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n\ |
| 1348 |
|
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n\ |
| 1349 |
|
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n\ |
| 1350 |
|
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n\ |
| 1351 |
|
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n\ |
| 1352 |
|
POSSIBILITY OF SUCH DAMAGES.\n"); |
| 1353 |
|
} |
| 1354 |
|
|
| 1355 |
|
/* General assoc function */ |
| 1356 |
|
void assocgen(environment *env, funcp eqfunc) |
| 1357 |
|
{ |
| 1358 |
|
value *key, *item; |
| 1359 |
|
|
| 1360 |
|
/* Needs two values on the stack, the top one must be an association |
| 1361 |
|
list */ |
| 1362 |
|
if(env->head->type==empty || CDR(env->head)->type==empty) { |
| 1363 |
|
printerr("Too Few Arguments"); |
| 1364 |
|
env->err= 1; |
| 1365 |
return; |
return; |
| 1366 |
} |
} |
| 1367 |
|
|
| 1368 |
temp= (*stack_head)->content.ptr; |
if(CAR(env->head)->type!=tcons) { |
| 1369 |
value= (*stack_head)->next; |
printerr("Bad Argument Type"); |
| 1370 |
temp->content= value->content; |
env->err= 2; |
| 1371 |
value->content.ptr=NULL; |
return; |
| 1372 |
temp->type= value->type; |
} |
| 1373 |
|
|
| 1374 |
toss(stack_head); toss(stack_head); |
key=CAR(CDR(env->head)); |
| 1375 |
} |
item=CAR(env->head); |
| 1376 |
|
|
| 1377 |
/* Quit stack. */ |
while(item->type == tcons){ |
| 1378 |
extern void quit() |
if(CAR(item)->type != tcons){ |
| 1379 |
{ |
printerr("Bad Argument Type"); |
| 1380 |
exit(EXIT_SUCCESS); |
env->err= 2; |
| 1381 |
} |
return; |
| 1382 |
|
} |
| 1383 |
|
push_val(env, key); |
| 1384 |
|
push_val(env, CAR(CAR(item))); |
| 1385 |
|
eqfunc(env); if(env->err) return; |
| 1386 |
|
|
| 1387 |
|
/* Check the result of 'eqfunc' */ |
| 1388 |
|
if(env->head->type==empty) { |
| 1389 |
|
printerr("Too Few Arguments"); |
| 1390 |
|
env->err= 1; |
| 1391 |
|
return; |
| 1392 |
|
} |
| 1393 |
|
if(CAR(env->head)->type!=integer) { |
| 1394 |
|
printerr("Bad Argument Type"); |
| 1395 |
|
env->err= 2; |
| 1396 |
|
return; |
| 1397 |
|
} |
| 1398 |
|
|
| 1399 |
int main() |
if(CAR(env->head)->content.i){ |
| 1400 |
{ |
toss(env); if(env->err) return; |
| 1401 |
stackitem* s= NULL; |
break; |
| 1402 |
hashtbl myhash; |
} |
| 1403 |
char in_string[100]; |
toss(env); if(env->err) return; |
| 1404 |
|
|
| 1405 |
init_hashtbl(myhash); |
if(item->type!=tcons) { |
| 1406 |
|
printerr("Bad Argument Type"); |
| 1407 |
|
env->err= 2; |
| 1408 |
|
return; |
| 1409 |
|
} |
| 1410 |
|
|
| 1411 |
printf("okidok\n "); |
item=CDR(item); |
| 1412 |
|
} |
| 1413 |
|
|
| 1414 |
while(fgets(in_string, 100, stdin) != NULL) { |
if(item->type == tcons){ /* A match was found */ |
| 1415 |
stack_read(&s, myhash, in_string); |
push_val(env, CAR(item)); |
| 1416 |
printf("okidok\n "); |
} else { |
| 1417 |
|
push_int(env, 0); |
| 1418 |
} |
} |
| 1419 |
|
swap(env); if(env->err) return; |
| 1420 |
|
toss(env); if(env->err) return; |
| 1421 |
|
swap(env); if(env->err) return; |
| 1422 |
|
toss(env); |
| 1423 |
|
} |
| 1424 |
|
|
| 1425 |
exit(EXIT_SUCCESS); |
/* Discard the top element of the stack. */ |
| 1426 |
|
extern void toss(environment *env) |
| 1427 |
|
{ |
| 1428 |
|
if(env->head->type==empty) { |
| 1429 |
|
printerr("Too Few Arguments"); |
| 1430 |
|
env->err= 1; |
| 1431 |
|
return; |
| 1432 |
|
} |
| 1433 |
|
|
| 1434 |
|
env->head= CDR(env->head); /* Remove the top stack item */ |
| 1435 |
} |
} |
| 1436 |
|
|
|
/* Local Variables: */ |
|
|
/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ |
|
|
/* End: */ |
|