| 1 |
teddy |
1.4 |
/* |
| 2 |
|
|
stack - an interactive interpreter for a stack-based language |
| 3 |
|
|
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn |
| 4 |
|
|
|
| 5 |
|
|
This program is free software; you can redistribute it and/or modify |
| 6 |
|
|
it under the terms of the GNU General Public License as published by |
| 7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 8 |
|
|
(at your option) any later version. |
| 9 |
|
|
|
| 10 |
|
|
This program is distributed in the hope that it will be useful, |
| 11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
|
|
GNU General Public License for more details. |
| 14 |
|
|
|
| 15 |
|
|
You should have received a copy of the GNU General Public License |
| 16 |
|
|
along with this program; if not, write to the Free Software |
| 17 |
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
|
|
|
| 19 |
|
|
Authors: Mats Alritzson <masse@fukt.bth.se> |
| 20 |
|
|
Teddy Hogeborn <teddy@fukt.bth.se> |
| 21 |
|
|
*/ |
| 22 |
teddy |
1.1 |
|
| 23 |
|
|
#define HASHTBLSIZE 2048 |
| 24 |
|
|
|
| 25 |
|
|
/* First, define some types. */ |
| 26 |
|
|
|
| 27 |
masse |
1.10 |
struct cons_struct; |
| 28 |
|
|
|
| 29 |
teddy |
1.1 |
/* A value of some type */ |
| 30 |
|
|
typedef struct { |
| 31 |
|
|
enum { |
| 32 |
|
|
integer, |
| 33 |
masse |
1.5 |
tfloat, |
| 34 |
teddy |
1.1 |
string, |
| 35 |
|
|
func, /* Function pointer */ |
| 36 |
teddy |
1.11 |
symb, /* Symbol */ |
| 37 |
|
|
tcons /* A pair of two values */ |
| 38 |
masse |
1.5 |
} type:4; /* Type of stack element */ |
| 39 |
masse |
1.2 |
|
| 40 |
teddy |
1.9 |
union { |
| 41 |
|
|
struct { |
| 42 |
teddy |
1.11 |
unsigned int mark:1; /* Used internally in the GC */ |
| 43 |
|
|
unsigned int protect:1; /* Protect from GC */ |
| 44 |
teddy |
1.9 |
} flag; |
| 45 |
teddy |
1.11 |
unsigned int no_gc:2; /* Both flags as one integer */ |
| 46 |
|
|
} gc; /* Garbage collector stuff */ |
| 47 |
teddy |
1.1 |
|
| 48 |
|
|
union { |
| 49 |
|
|
void *ptr; /* Pointer to the content */ |
| 50 |
teddy |
1.11 |
struct cons_struct *c; /* ...or a pointer to a cons cell */ |
| 51 |
masse |
1.5 |
int i; /* ...or an integer */ |
| 52 |
teddy |
1.11 |
float f; /* ...or a floating point number */ |
| 53 |
teddy |
1.1 |
} content; /* Stores a pointer or an integer */ |
| 54 |
|
|
|
| 55 |
|
|
} value; |
| 56 |
|
|
|
| 57 |
masse |
1.10 |
/* An item (value) on a stack */ |
| 58 |
|
|
typedef struct stackitem_struct |
| 59 |
|
|
{ |
| 60 |
|
|
value *item; /* The value on the stack */ |
| 61 |
|
|
/* (This is never NULL) */ |
| 62 |
|
|
struct stackitem_struct *next; /* Next item */ |
| 63 |
|
|
} stackitem; |
| 64 |
|
|
|
| 65 |
teddy |
1.11 |
typedef struct cons_struct { /* A pair of two values */ |
| 66 |
masse |
1.10 |
value *car; |
| 67 |
|
|
value *cdr; |
| 68 |
|
|
} cons; |
| 69 |
|
|
|
| 70 |
teddy |
1.1 |
/* A symbol with a name and possible value */ |
| 71 |
|
|
/* (These do not need reference counters, they are kept unique by |
| 72 |
|
|
hashing.) */ |
| 73 |
|
|
typedef struct symbol_struct { |
| 74 |
|
|
char *id; /* Symbol name */ |
| 75 |
|
|
value *val; /* The value (if any) bound to it */ |
| 76 |
|
|
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
| 77 |
|
|
} symbol; /* symbol is a kind of stack item. */ |
| 78 |
|
|
|
| 79 |
|
|
/* A type for a hash table for symbols */ |
| 80 |
|
|
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
| 81 |
|
|
|
| 82 |
|
|
/* An environment; gives access to the stack and a hash table of |
| 83 |
|
|
defined symbols */ |
| 84 |
|
|
typedef struct { |
| 85 |
|
|
stackitem *gc_ref; |
| 86 |
|
|
int gc_limit, gc_count; |
| 87 |
|
|
|
| 88 |
teddy |
1.11 |
cons *head; /* Head of the stack */ |
| 89 |
teddy |
1.1 |
hashtbl symbols; /* Hash table of all variable bindings */ |
| 90 |
|
|
int err; /* Error flag */ |
| 91 |
|
|
char *in_string; /* Input pending to be read */ |
| 92 |
|
|
char *free_string; /* Free this string when all input is |
| 93 |
teddy |
1.4 |
read from in_string */ |
| 94 |
teddy |
1.1 |
FILE *inputstream; /* stdin or a file, most likely */ |
| 95 |
|
|
int interactive; /* print prompts, stack, etc */ |
| 96 |
|
|
} environment; |
| 97 |
|
|
|
| 98 |
|
|
/* A type for pointers to external functions */ |
| 99 |
masse |
1.2 |
typedef void (*funcp)(environment*); /* funcp is a pointer to a void |
| 100 |
teddy |
1.1 |
function (environment *) */ |
| 101 |
|
|
|
| 102 |
masse |
1.2 |
void init_env(environment*); |
| 103 |
|
|
void printerr(const char*); |
| 104 |
|
|
extern void toss(environment*); |
| 105 |
|
|
symbol **hash(hashtbl, const char*); |
| 106 |
|
|
value* new_val(environment*); |
| 107 |
|
|
void gc_mark(value*); |
| 108 |
teddy |
1.7 |
void gc_maybe(environment *env); |
| 109 |
masse |
1.2 |
extern void gc_init(environment*); |
| 110 |
|
|
void push_val(environment*, value*); |
| 111 |
|
|
void push_int(environment*, int); |
| 112 |
masse |
1.5 |
void push_float(environment*, float); |
| 113 |
masse |
1.2 |
void push_cstring(environment*, const char*); |
| 114 |
|
|
char *mangle_str(const char*); |
| 115 |
|
|
extern void mangle(environment*); |
| 116 |
|
|
void push_sym(environment*, const char*); |
| 117 |
teddy |
1.1 |
extern void nl(); |
| 118 |
masse |
1.2 |
extern void type(environment*); |
| 119 |
masse |
1.10 |
void print_h(cons*, int); |
| 120 |
masse |
1.2 |
extern void print_(environment*); |
| 121 |
|
|
extern void print(environment*); |
| 122 |
|
|
extern void princ_(environment*); |
| 123 |
|
|
extern void princ(environment*); |
| 124 |
masse |
1.10 |
void print_st(cons*, long); |
| 125 |
masse |
1.2 |
extern void printstack(environment*); |
| 126 |
|
|
extern void swap(environment*); |
| 127 |
|
|
extern void rot(environment*); |
| 128 |
|
|
extern void rcl(environment*); |
| 129 |
|
|
extern void eval(environment*); |
| 130 |
|
|
extern void rev(environment*); |
| 131 |
|
|
extern void pack(environment*); |
| 132 |
|
|
extern void expand(environment*); |
| 133 |
|
|
extern void eq(environment*); |
| 134 |
|
|
extern void not(environment*); |
| 135 |
|
|
extern void neq(environment*); |
| 136 |
|
|
extern void def(environment*); |
| 137 |
|
|
extern void quit(environment*); |
| 138 |
|
|
extern void clear(environment*); |
| 139 |
|
|
extern void words(environment*); |
| 140 |
|
|
void forget_sym(symbol**); |
| 141 |
|
|
extern void forget(environment*); |
| 142 |
|
|
extern void errn(environment*); |
| 143 |
|
|
extern void sx_2b(environment*); |
| 144 |
|
|
extern void sx_2d(environment*); |
| 145 |
|
|
extern void sx_3e(environment*); |
| 146 |
|
|
value *copy_val(environment*, value*); |
| 147 |
|
|
extern void sx_647570(environment*); |
| 148 |
|
|
extern void sx_6966(environment*); |
| 149 |
|
|
extern void ifelse(environment*); |
| 150 |
|
|
extern void sx_7768696c65(environment*); |
| 151 |
|
|
extern void sx_666f72(environment*); |
| 152 |
|
|
extern void to(environment*); |
| 153 |
|
|
extern void readline(environment*); |
| 154 |
|
|
extern void sx_72656164(environment*); |
| 155 |
|
|
extern void foreach(environment*); |
| 156 |
masse |
1.8 |
void protect(value*); |
| 157 |
|
|
void unprotect(value*); |
| 158 |
masse |
1.5 |
extern void copying(environment*); |
| 159 |
|
|
extern void warranty(environment*); |
| 160 |
|
|
extern void sx_2a(environment*); |
| 161 |
|
|
extern void sx_2f(environment*); |
| 162 |
|
|
extern void mod(environment*); |
| 163 |
|
|
extern void sx_3c(environment*); |
| 164 |
|
|
extern void sx_3c3d(environment*); |
| 165 |
|
|
extern void sx_3e3d(environment*); |
| 166 |
masse |
1.6 |
extern void sx_646976(environment*); |