--- stack/stack.h 2002/02/16 00:51:32 1.1 +++ stack/stack.h 2002/03/20 17:19:46 1.18 @@ -1,27 +1,75 @@ +/* + 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 /* First, define some types. */ +struct cons_struct; +struct symbol_struct; + /* A value of some type */ typedef struct { enum { + empty, /* The empty list */ integer, + tfloat, string, func, /* Function pointer */ - symb, - list - } type; /* Type of stack element */ + symb, /* Symbol */ + tcons /* A pair of two values */ + } type:4; /* Type of stack element */ + + 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 */ + int i; /* ...or an integer */ + float f; /* ...or a floating point number */ } content; /* Stores a pointer or an integer */ - int gc_garb; - } 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,82 +82,100 @@ /* 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 *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 +typedef void (*funcp)(environment*); /* funcp is a pointer to a void function (environment *) */ -void init_env(environment *env); -void printerr(const char* in_string); -extern void toss(environment *env); -symbol **hash(hashtbl in_hashtbl, const char *in_string); -value* new_val(environment *env); -void gc_mark(value *val); -extern void gc_init(environment *env); -void push_val(environment *env, value *val); -void push_int(environment *env, int in_val); -void push_cstring(environment *env, const char *in_string); -char *mangle_str(const char *old_string); -extern void mangle(environment *env); -void push_sym(environment *env, const char *in_string); +void init_env(environment*); +void printerr(const char*); +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 type(environment *env); -void print_h(stackitem *stack_head, int noquote); -extern void print_(environment *env); -extern void print(environment *env); -extern void princ_(environment *env); -extern void princ(environment *env); -void print_st(stackitem *stack_head, long counter); -extern void printstack(environment *env); -extern void swap(environment *env); -extern void rot(environment *env); -extern void rcl(environment *env); -extern void eval(environment *env); -extern void rev(environment *env); -extern void pack(environment *env); -extern void expand(environment *env); -extern void eq(environment *env); -extern void not(environment *env); -extern void neq(environment *env); -extern void def(environment *env); -extern void quit(environment *env); -extern void clear(environment *env); -extern void words(environment *env); -void forget_sym(symbol **hash_entry); -extern void forget(environment *env); -extern void errn(environment *env); -extern void sx_2b(environment *env); -extern void sx_2d(environment *env); -extern void sx_3e(environment *env); -value *copy_val(environment *env, value *old_value); -extern void sx_647570(environment *env); -extern void sx_6966(environment *env); -extern void ifelse(environment *env); -extern void sx_7768696c65(environment *env); -extern void sx_666f72(environment *env); -extern void to(environment *env); -extern void readline(environment *env); -extern void sx_72656164(environment *env); +extern void type(environment*); +void print_val(value*, int, stackitem*); +extern void print_(environment*); +extern void print(environment*); +extern void princ_(environment*); +extern void princ(environment*); +void print_st(value*, long); +extern void printstack(environment*); +extern void swap(environment*); +extern void rot(environment*); +extern void rcl(environment*); +extern void eval(environment*); +extern void rev(environment*); +extern void pack(environment*); +extern void expand(environment*); +extern void eq(environment*); +extern void not(environment*); +extern void neq(environment*); +extern void def(environment*); +extern void quit(environment*); +extern void clear(environment*); +extern void words(environment*); +void forget_sym(symbol**); +extern void forget(environment*); +extern void errn(environment*); +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 sx_72656164(environment*); +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*);