--- stack/stack.h 2002/02/17 04:03:57 1.2 +++ stack/stack.h 2003/08/13 11:58:00 1.27 @@ -1,27 +1,123 @@ +/* + stack - an interactive interpreter for a stack-based language + Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Authors: Mats Alritzson + Teddy Hogeborn +*/ #define HASHTBLSIZE 2048 +#define CAR(X) ((X)->content.c->car) +#define CDR(X) ((X)->content.c->cdr) + +/* printf, sscanf, fgets, fprintf, fopen, perror */ +#include +/* exit, EXIT_SUCCESS, malloc, free */ +#include +/* NULL */ +#include +/* dlopen, dlsym, dlerror */ +#include +/* strcmp, strcpy, strlen, strcat, strdup */ +#include +/* getopt, STDIN_FILENO, STDOUT_FILENO, usleep */ +#include +/* EX_NOINPUT, EX_USAGE */ +#include +/* assert */ +#include +/* waitpid */ +#include +/* va_list, va_start, va_arg, va_end */ +#include + +#ifdef __linux__ +/* mtrace, muntrace */ +#include +/* ioctl */ +#include +/* KDMKTONE */ +#include +#endif /* __linux__ */ + + + /* First, define some types. */ +struct cons_struct; +struct symbol_struct; +struct environment_struct; + +/* A type for pointers to external functions */ +typedef void (*funcp)(struct environment_struct*); +/* funcp is a pointer to a void function (environment *) */ + +enum type_enum { + unknown, + empty, /* The empty list */ + integer, + tfloat, + string, + func, /* Function pointer */ + symb, /* Symbol */ + tcons, /* A pair of two values */ + port /* An I/O port */ +}; /* Type of stack element */ + + /* A value of some type */ typedef struct { - enum { - integer, - string, - func, /* Function pointer */ - symb, - list - } type:3; /* Type of stack element */ + enum type_enum type:4; - int gc_garb:1; + union { + struct { + unsigned int mark:1; /* Used internally in the GC */ + unsigned int protect:1; /* Protect from GC */ + } flag; + unsigned int no_gc:2; /* Both flags as one integer */ + } gc; /* Garbage collector stuff */ union { void *ptr; /* Pointer to the content */ - int val; /* ...or an integer */ + struct cons_struct *c; /* ...or a pointer to a cons cell */ + struct symbol_struct *sym; /* ...or a pointer to a symbol */ + FILE *p; /* ...or an I/O stream */ + int i; /* ...or an integer */ + float f; /* ...or a floating point number */ + funcp func; /* ...or a function pointer */ + char *string; /* ...or a string */ } content; /* Stores a pointer or an integer */ } value; +/* An item (value) on a stack */ +typedef struct stackitem_struct +{ + value *item; /* The value on the stack */ + /* (This is never NULL) */ + struct stackitem_struct *next; /* Next item */ +} stackitem; + +typedef struct cons_struct { /* A pair of two values */ + value *car; + value *cdr; +} pair; + /* A symbol with a name and possible value */ /* (These do not need reference counters, they are kept unique by hashing.) */ @@ -34,55 +130,56 @@ /* A type for a hash table for symbols */ typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ -/* An item (value) on a stack */ -typedef struct stackitem_struct -{ - value *item; /* The value on the stack */ - /* (This is never NULL) */ - struct stackitem_struct *next; /* Next item */ -} stackitem; - /* An environment; gives access to the stack and a hash table of defined symbols */ typedef struct { - stackitem *gc_ref; - int gc_limit, gc_count; - - stackitem *head; /* Head of the stack */ + value *head; /* Head of the stack */ hashtbl symbols; /* Hash table of all variable bindings */ int err; /* Error flag */ + char *errsymb; char *in_string; /* Input pending to be read */ char *free_string; /* Free this string when all input is - read from in_string */ + read from in_string */ FILE *inputstream; /* stdin or a file, most likely */ int interactive; /* print prompts, stack, etc */ + + /* Garbage Collector stuff*/ + stackitem *gc_ref; /* Stack of all allocated values */ + int gc_limit; /* Run GC when this much is allocated */ + int gc_count; /* Amount currently allocated */ } environment; -/* A type for pointers to external functions */ -typedef void (*funcp)(environment*); /* funcp is a pointer to a void - function (environment *) */ void init_env(environment*); -void printerr(const char*); +void printerr(environment*); extern void toss(environment*); symbol **hash(hashtbl, const char*); value* new_val(environment*); void gc_mark(value*); +void gc_maybe(environment*); extern void gc_init(environment*); +void protect(value*); +void unprotect(value*); void push_val(environment*, value*); void push_int(environment*, int); +void push_float(environment*, float); void push_cstring(environment*, const char*); char *mangle_str(const char*); extern void mangle(environment*); void push_sym(environment*, const char*); -extern void nl(); +extern void nl(environment*); +extern void nlport(environment*); extern void type(environment*); -void print_h(stackitem*, int); +void print_val(environment *, value*, int, stackitem*, FILE*); extern void print_(environment*); extern void print(environment*); extern void princ_(environment*); extern void princ(environment*); -void print_st(stackitem*, long); +extern void printport_(environment*); +extern void printport(environment*); +extern void princport_(environment*); +extern void princport(environment*); +void print_st(environment*, value*, long); extern void printstack(environment*); extern void swap(environment*); extern void rot(environment*); @@ -104,13 +201,41 @@ extern void sx_2b(environment*); extern void sx_2d(environment*); extern void sx_3e(environment*); +extern void sx_3c(environment*); +extern void sx_3c3d(environment*); +extern void sx_3e3d(environment*); value *copy_val(environment*, value*); extern void sx_647570(environment*); extern void sx_6966(environment*); extern void ifelse(environment*); +extern void sx_656c7365(environment*); +extern void then(environment*); extern void sx_7768696c65(environment*); extern void sx_666f72(environment*); +extern void foreach(environment*); extern void to(environment*); extern void readline(environment*); +extern void readlineport(environment*); +void readlinestream(environment*, FILE*); extern void sx_72656164(environment*); -extern void foreach(environment*); +extern void readport(environment*); +void readstream(environment*, FILE*); +extern void beep(environment*); +extern void sx_77616974(environment*); +extern void copying(environment*); +extern void warranty(environment*); +extern void sx_2a(environment*); +extern void sx_2f(environment*); +extern void mod(environment*); +extern void sx_646976(environment*); +extern void setcar(environment*); +extern void setcdr(environment*); +extern void car(environment*); +extern void cdr(environment*); +extern void cons(environment*); +extern void assq(environment*); +void assocgen(environment*, funcp); +extern void sx_646f(environment *); +extern void sx_6f70656e(environment*); +extern void sx_636c6f7365(environment*); +int check_args(environment*, ...);