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

Contents of /stack/stack.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (show annotations)
Mon Aug 11 14:31:48 2003 UTC (20 years, 8 months ago) by masse
Branch: MAIN
Changes since 1.25: +19 -11 lines
File MIME type: text/plain
(check_args) New function to ease the checking of parameters.

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26