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

Annotation of /stack/stack.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.28 - (hide annotations)
Mon Aug 18 14:39:16 2003 UTC (20 years, 8 months ago) by masse
Branch: MAIN
CVS Tags: HEAD
Changes since 1.27: +3 -1 lines
File MIME type: text/plain
stack.c (printerr): Modified to accept error type 5.
(check_args): Modified to accept "empty" as argument.
symbols.c: New symbols (sx_72616e646f6d), (seed), (ticks), (push) and (pop).

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

root@recompile.se
ViewVC Help
Powered by ViewVC 1.1.26