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 |
masse |
1.13 |
struct symbol_struct; |
29 |
masse |
1.10 |
|
30 |
teddy |
1.1 |
/* A value of some type */ |
31 |
|
|
typedef struct { |
32 |
|
|
enum { |
33 |
teddy |
1.15 |
empty, /* The empty list */ |
34 |
teddy |
1.1 |
integer, |
35 |
masse |
1.5 |
tfloat, |
36 |
teddy |
1.1 |
string, |
37 |
|
|
func, /* Function pointer */ |
38 |
teddy |
1.11 |
symb, /* Symbol */ |
39 |
|
|
tcons /* A pair of two values */ |
40 |
masse |
1.5 |
} type:4; /* Type of stack element */ |
41 |
masse |
1.2 |
|
42 |
teddy |
1.9 |
union { |
43 |
|
|
struct { |
44 |
teddy |
1.11 |
unsigned int mark:1; /* Used internally in the GC */ |
45 |
|
|
unsigned int protect:1; /* Protect from GC */ |
46 |
teddy |
1.9 |
} flag; |
47 |
teddy |
1.11 |
unsigned int no_gc:2; /* Both flags as one integer */ |
48 |
|
|
} gc; /* Garbage collector stuff */ |
49 |
teddy |
1.1 |
|
50 |
|
|
union { |
51 |
|
|
void *ptr; /* Pointer to the content */ |
52 |
teddy |
1.11 |
struct cons_struct *c; /* ...or a pointer to a cons cell */ |
53 |
masse |
1.13 |
struct symbol_struct *sym; /* ...or a pointer to a symbol */ |
54 |
masse |
1.5 |
int i; /* ...or an integer */ |
55 |
teddy |
1.11 |
float f; /* ...or a floating point number */ |
56 |
teddy |
1.1 |
} content; /* Stores a pointer or an integer */ |
57 |
|
|
|
58 |
|
|
} value; |
59 |
|
|
|
60 |
masse |
1.10 |
/* An item (value) on a stack */ |
61 |
|
|
typedef struct stackitem_struct |
62 |
|
|
{ |
63 |
|
|
value *item; /* The value on the stack */ |
64 |
|
|
/* (This is never NULL) */ |
65 |
|
|
struct stackitem_struct *next; /* Next item */ |
66 |
|
|
} stackitem; |
67 |
|
|
|
68 |
teddy |
1.11 |
typedef struct cons_struct { /* A pair of two values */ |
69 |
masse |
1.10 |
value *car; |
70 |
|
|
value *cdr; |
71 |
teddy |
1.16 |
} pair; |
72 |
masse |
1.10 |
|
73 |
teddy |
1.1 |
/* A symbol with a name and possible value */ |
74 |
|
|
/* (These do not need reference counters, they are kept unique by |
75 |
|
|
hashing.) */ |
76 |
|
|
typedef struct symbol_struct { |
77 |
|
|
char *id; /* Symbol name */ |
78 |
|
|
value *val; /* The value (if any) bound to it */ |
79 |
|
|
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
80 |
|
|
} symbol; /* symbol is a kind of stack item. */ |
81 |
|
|
|
82 |
|
|
/* A type for a hash table for symbols */ |
83 |
|
|
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
84 |
|
|
|
85 |
|
|
/* An environment; gives access to the stack and a hash table of |
86 |
|
|
defined symbols */ |
87 |
|
|
typedef struct { |
88 |
masse |
1.12 |
value *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 |
teddy |
1.15 |
|
97 |
|
|
/* Garbage Collector stuff*/ |
98 |
|
|
stackitem *gc_ref; /* Stack of all allocated values */ |
99 |
|
|
int gc_limit; /* Run GC when this much is allocated */ |
100 |
|
|
int gc_count; /* Amount currently allocated */ |
101 |
teddy |
1.1 |
} environment; |
102 |
|
|
|
103 |
|
|
/* A type for pointers to external functions */ |
104 |
masse |
1.2 |
typedef void (*funcp)(environment*); /* funcp is a pointer to a void |
105 |
teddy |
1.1 |
function (environment *) */ |
106 |
|
|
|
107 |
masse |
1.2 |
void init_env(environment*); |
108 |
|
|
void printerr(const char*); |
109 |
|
|
extern void toss(environment*); |
110 |
|
|
symbol **hash(hashtbl, const char*); |
111 |
|
|
value* new_val(environment*); |
112 |
|
|
void gc_mark(value*); |
113 |
teddy |
1.17 |
void gc_maybe(environment*); |
114 |
masse |
1.2 |
extern void gc_init(environment*); |
115 |
teddy |
1.17 |
void protect(value*); |
116 |
|
|
void unprotect(value*); |
117 |
masse |
1.2 |
void push_val(environment*, value*); |
118 |
|
|
void push_int(environment*, int); |
119 |
masse |
1.5 |
void push_float(environment*, float); |
120 |
masse |
1.2 |
void push_cstring(environment*, const char*); |
121 |
|
|
char *mangle_str(const char*); |
122 |
|
|
extern void mangle(environment*); |
123 |
|
|
void push_sym(environment*, const char*); |
124 |
teddy |
1.1 |
extern void nl(); |
125 |
masse |
1.2 |
extern void type(environment*); |
126 |
teddy |
1.17 |
void print_val(value*, int, stackitem*); |
127 |
masse |
1.2 |
extern void print_(environment*); |
128 |
|
|
extern void print(environment*); |
129 |
|
|
extern void princ_(environment*); |
130 |
|
|
extern void princ(environment*); |
131 |
masse |
1.12 |
void print_st(value*, long); |
132 |
masse |
1.2 |
extern void printstack(environment*); |
133 |
|
|
extern void swap(environment*); |
134 |
|
|
extern void rot(environment*); |
135 |
|
|
extern void rcl(environment*); |
136 |
|
|
extern void eval(environment*); |
137 |
|
|
extern void rev(environment*); |
138 |
|
|
extern void pack(environment*); |
139 |
|
|
extern void expand(environment*); |
140 |
|
|
extern void eq(environment*); |
141 |
|
|
extern void not(environment*); |
142 |
|
|
extern void neq(environment*); |
143 |
|
|
extern void def(environment*); |
144 |
|
|
extern void quit(environment*); |
145 |
|
|
extern void clear(environment*); |
146 |
|
|
extern void words(environment*); |
147 |
|
|
void forget_sym(symbol**); |
148 |
|
|
extern void forget(environment*); |
149 |
|
|
extern void errn(environment*); |
150 |
|
|
extern void sx_2b(environment*); |
151 |
|
|
extern void sx_2d(environment*); |
152 |
|
|
extern void sx_3e(environment*); |
153 |
teddy |
1.17 |
extern void sx_3c(environment*); |
154 |
|
|
extern void sx_3c3d(environment*); |
155 |
|
|
extern void sx_3e3d(environment*); |
156 |
masse |
1.2 |
value *copy_val(environment*, value*); |
157 |
|
|
extern void sx_647570(environment*); |
158 |
|
|
extern void sx_6966(environment*); |
159 |
|
|
extern void ifelse(environment*); |
160 |
teddy |
1.17 |
extern void sx_656c7365(environment*); |
161 |
|
|
extern void then(environment*); |
162 |
masse |
1.2 |
extern void sx_7768696c65(environment*); |
163 |
|
|
extern void sx_666f72(environment*); |
164 |
teddy |
1.17 |
extern void foreach(environment*); |
165 |
masse |
1.2 |
extern void to(environment*); |
166 |
|
|
extern void readline(environment*); |
167 |
|
|
extern void sx_72656164(environment*); |
168 |
teddy |
1.17 |
extern void beep(environment*); |
169 |
|
|
extern void sx_77616974(environment*); |
170 |
masse |
1.5 |
extern void copying(environment*); |
171 |
|
|
extern void warranty(environment*); |
172 |
|
|
extern void sx_2a(environment*); |
173 |
|
|
extern void sx_2f(environment*); |
174 |
|
|
extern void mod(environment*); |
175 |
masse |
1.6 |
extern void sx_646976(environment*); |
176 |
teddy |
1.17 |
extern void setcar(environment*); |
177 |
|
|
extern void setcdr(environment*); |
178 |
|
|
extern void car(environment*); |
179 |
|
|
extern void cdr(environment*); |
180 |
|
|
extern void cons(environment*); |
181 |
teddy |
1.18 |
extern void assq(environment*); |
182 |
teddy |
1.19 |
void assocgen(environment*, funcp); |
183 |
masse |
1.20 |
extern void sx_646f(environment *); |