/[cvs]/stack/stack.h
ViewVC logotype

Annotation of /stack/stack.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (hide annotations)
Fri Aug 8 14:20:49 2003 UTC (20 years, 9 months ago) by masse
Branch: MAIN
Changes since 1.24: +2 -0 lines
File MIME type: text/plain
stack.c: Trying to clean up. Moving big text mass to separate file.
Eliminating the use of function toss.

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 masse 1.22 #define CAR(X) ((X)->content.c->car)
26     #define CDR(X) ((X)->content.c->cdr)
27    
28     /* printf, sscanf, fgets, fprintf, fopen, perror */
29     #include <stdio.h>
30     /* exit, EXIT_SUCCESS, malloc, free */
31     #include <stdlib.h>
32     /* NULL */
33     #include <stddef.h>
34     /* dlopen, dlsym, dlerror */
35     #include <dlfcn.h>
36     /* strcmp, strcpy, strlen, strcat, strdup */
37     #include <string.h>
38     /* getopt, STDIN_FILENO, STDOUT_FILENO, usleep */
39     #include <unistd.h>
40     /* EX_NOINPUT, EX_USAGE */
41     #include <sysexits.h>
42     /* assert */
43     #include <assert.h>
44 masse 1.25 /* waitpid */
45     #include <sys/wait.h>
46 masse 1.22
47     #ifdef __linux__
48     /* mtrace, muntrace */
49     #include <mcheck.h>
50     /* ioctl */
51     #include <sys/ioctl.h>
52     /* KDMKTONE */
53     #include <linux/kd.h>
54     #endif /* __linux__ */
55    
56    
57    
58 teddy 1.1 /* First, define some types. */
59    
60 masse 1.10 struct cons_struct;
61 masse 1.13 struct symbol_struct;
62 masse 1.23 struct environment_struct;
63    
64     /* A type for pointers to external functions */
65     typedef void (*funcp)(struct environment_struct*);
66     /* funcp is a pointer to a void function (environment *) */
67 masse 1.10
68 teddy 1.1 /* A value of some type */
69     typedef struct {
70     enum {
71 teddy 1.15 empty, /* The empty list */
72 teddy 1.1 integer,
73 masse 1.5 tfloat,
74 teddy 1.1 string,
75     func, /* Function pointer */
76 teddy 1.11 symb, /* Symbol */
77 teddy 1.21 tcons, /* A pair of two values */
78     port /* An I/O port */
79 masse 1.5 } type:4; /* Type of stack element */
80 masse 1.2
81 teddy 1.9 union {
82     struct {
83 teddy 1.11 unsigned int mark:1; /* Used internally in the GC */
84     unsigned int protect:1; /* Protect from GC */
85 teddy 1.9 } flag;
86 teddy 1.11 unsigned int no_gc:2; /* Both flags as one integer */
87     } gc; /* Garbage collector stuff */
88 teddy 1.1
89     union {
90     void *ptr; /* Pointer to the content */
91 teddy 1.11 struct cons_struct *c; /* ...or a pointer to a cons cell */
92 masse 1.13 struct symbol_struct *sym; /* ...or a pointer to a symbol */
93 teddy 1.21 FILE *p; /* ...or an I/O stream */
94 masse 1.5 int i; /* ...or an integer */
95 teddy 1.11 float f; /* ...or a floating point number */
96 masse 1.24 funcp func; /* ...or a function pointer */
97 masse 1.23 char *string; /* ...or a string */
98 teddy 1.1 } content; /* Stores a pointer or an integer */
99    
100     } value;
101    
102 masse 1.10 /* An item (value) on a stack */
103     typedef struct stackitem_struct
104     {
105     value *item; /* The value on the stack */
106     /* (This is never NULL) */
107     struct stackitem_struct *next; /* Next item */
108     } stackitem;
109    
110 teddy 1.11 typedef struct cons_struct { /* A pair of two values */
111 masse 1.10 value *car;
112     value *cdr;
113 teddy 1.16 } pair;
114 masse 1.10
115 teddy 1.1 /* A symbol with a name and possible value */
116     /* (These do not need reference counters, they are kept unique by
117     hashing.) */
118     typedef struct symbol_struct {
119     char *id; /* Symbol name */
120     value *val; /* The value (if any) bound to it */
121     struct symbol_struct *next; /* In case of hashing conflicts, a */
122     } symbol; /* symbol is a kind of stack item. */
123    
124     /* A type for a hash table for symbols */
125     typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */
126    
127     /* An environment; gives access to the stack and a hash table of
128     defined symbols */
129     typedef struct {
130 masse 1.12 value *head; /* Head of the stack */
131 teddy 1.1 hashtbl symbols; /* Hash table of all variable bindings */
132     int err; /* Error flag */
133     char *in_string; /* Input pending to be read */
134     char *free_string; /* Free this string when all input is
135 teddy 1.4 read from in_string */
136 teddy 1.1 FILE *inputstream; /* stdin or a file, most likely */
137     int interactive; /* print prompts, stack, etc */
138 teddy 1.15
139     /* Garbage Collector stuff*/
140     stackitem *gc_ref; /* Stack of all allocated values */
141     int gc_limit; /* Run GC when this much is allocated */
142     int gc_count; /* Amount currently allocated */
143 teddy 1.1 } environment;
144    
145    
146 masse 1.2 void init_env(environment*);
147     void printerr(const char*);
148     extern void toss(environment*);
149     symbol **hash(hashtbl, const char*);
150     value* new_val(environment*);
151     void gc_mark(value*);
152 teddy 1.17 void gc_maybe(environment*);
153 masse 1.2 extern void gc_init(environment*);
154 teddy 1.17 void protect(value*);
155     void unprotect(value*);
156 masse 1.2 void push_val(environment*, value*);
157     void push_int(environment*, int);
158 masse 1.5 void push_float(environment*, float);
159 masse 1.2 void push_cstring(environment*, const char*);
160     char *mangle_str(const char*);
161     extern void mangle(environment*);
162     void push_sym(environment*, const char*);
163 teddy 1.21 extern void nl(environment*);
164     extern void nlport(environment*);
165 masse 1.2 extern void type(environment*);
166 teddy 1.21 void print_val(environment *, value*, int, stackitem*, FILE*);
167 masse 1.2 extern void print_(environment*);
168     extern void print(environment*);
169     extern void princ_(environment*);
170     extern void princ(environment*);
171 teddy 1.21 extern void printport_(environment*);
172     extern void printport(environment*);
173     extern void princport_(environment*);
174     extern void princport(environment*);
175     void print_st(environment*, value*, long);
176 masse 1.2 extern void printstack(environment*);
177     extern void swap(environment*);
178     extern void rot(environment*);
179     extern void rcl(environment*);
180     extern void eval(environment*);
181     extern void rev(environment*);
182     extern void pack(environment*);
183     extern void expand(environment*);
184     extern void eq(environment*);
185     extern void not(environment*);
186     extern void neq(environment*);
187     extern void def(environment*);
188     extern void quit(environment*);
189     extern void clear(environment*);
190     extern void words(environment*);
191     void forget_sym(symbol**);
192     extern void forget(environment*);
193     extern void errn(environment*);
194     extern void sx_2b(environment*);
195     extern void sx_2d(environment*);
196     extern void sx_3e(environment*);
197 teddy 1.17 extern void sx_3c(environment*);
198     extern void sx_3c3d(environment*);
199     extern void sx_3e3d(environment*);
200 masse 1.2 value *copy_val(environment*, value*);
201     extern void sx_647570(environment*);
202     extern void sx_6966(environment*);
203     extern void ifelse(environment*);
204 teddy 1.17 extern void sx_656c7365(environment*);
205     extern void then(environment*);
206 masse 1.2 extern void sx_7768696c65(environment*);
207     extern void sx_666f72(environment*);
208 teddy 1.17 extern void foreach(environment*);
209 masse 1.2 extern void to(environment*);
210     extern void readline(environment*);
211 teddy 1.21 extern void readlineport(environment*);
212     void readlinestream(environment*, FILE*);
213 masse 1.2 extern void sx_72656164(environment*);
214 teddy 1.21 extern void readport(environment*);
215     void readstream(environment*, FILE*);
216 teddy 1.17 extern void beep(environment*);
217     extern void sx_77616974(environment*);
218 masse 1.5 extern void copying(environment*);
219     extern void warranty(environment*);
220     extern void sx_2a(environment*);
221     extern void sx_2f(environment*);
222     extern void mod(environment*);
223 masse 1.6 extern void sx_646976(environment*);
224 teddy 1.17 extern void setcar(environment*);
225     extern void setcdr(environment*);
226     extern void car(environment*);
227     extern void cdr(environment*);
228     extern void cons(environment*);
229 teddy 1.18 extern void assq(environment*);
230 teddy 1.19 void assocgen(environment*, funcp);
231 masse 1.20 extern void sx_646f(environment *);
232 teddy 1.21 extern void sx_6f70656e(environment*);
233     extern void sx_636c6f7365(environment*);

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26