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 |
/* First, define some types. */ |
26 |
|
27 |
/* A value of some type */ |
28 |
typedef struct { |
29 |
enum { |
30 |
integer, |
31 |
string, |
32 |
func, /* Function pointer */ |
33 |
symb, |
34 |
list |
35 |
} type:3; /* Type of stack element */ |
36 |
|
37 |
int gc_garb:1; |
38 |
|
39 |
union { |
40 |
void *ptr; /* Pointer to the content */ |
41 |
int val; /* ...or an integer */ |
42 |
} content; /* Stores a pointer or an integer */ |
43 |
|
44 |
} value; |
45 |
|
46 |
/* A symbol with a name and possible value */ |
47 |
/* (These do not need reference counters, they are kept unique by |
48 |
hashing.) */ |
49 |
typedef struct symbol_struct { |
50 |
char *id; /* Symbol name */ |
51 |
value *val; /* The value (if any) bound to it */ |
52 |
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
53 |
} symbol; /* symbol is a kind of stack item. */ |
54 |
|
55 |
/* A type for a hash table for symbols */ |
56 |
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
57 |
|
58 |
/* An item (value) on a stack */ |
59 |
typedef struct stackitem_struct |
60 |
{ |
61 |
value *item; /* The value on the stack */ |
62 |
/* (This is never NULL) */ |
63 |
struct stackitem_struct *next; /* Next item */ |
64 |
} stackitem; |
65 |
|
66 |
/* An environment; gives access to the stack and a hash table of |
67 |
defined symbols */ |
68 |
typedef struct { |
69 |
stackitem *gc_ref; |
70 |
stackitem *gc_protect; |
71 |
int gc_limit, gc_count; |
72 |
|
73 |
stackitem *head; /* Head of the stack */ |
74 |
hashtbl symbols; /* Hash table of all variable bindings */ |
75 |
int err; /* Error flag */ |
76 |
char *in_string; /* Input pending to be read */ |
77 |
char *free_string; /* Free this string when all input is |
78 |
read from in_string */ |
79 |
FILE *inputstream; /* stdin or a file, most likely */ |
80 |
int interactive; /* print prompts, stack, etc */ |
81 |
} environment; |
82 |
|
83 |
/* A type for pointers to external functions */ |
84 |
typedef void (*funcp)(environment*); /* funcp is a pointer to a void |
85 |
function (environment *) */ |
86 |
|
87 |
void init_env(environment*); |
88 |
void printerr(const char*); |
89 |
extern void toss(environment*); |
90 |
symbol **hash(hashtbl, const char*); |
91 |
value* new_val(environment*); |
92 |
void gc_mark(value*); |
93 |
extern void gc_init(environment*); |
94 |
void push_val(environment*, value*); |
95 |
void push_int(environment*, int); |
96 |
void push_cstring(environment*, const char*); |
97 |
char *mangle_str(const char*); |
98 |
extern void mangle(environment*); |
99 |
void push_sym(environment*, const char*); |
100 |
extern void nl(); |
101 |
extern void type(environment*); |
102 |
void print_h(stackitem*, int); |
103 |
extern void print_(environment*); |
104 |
extern void print(environment*); |
105 |
extern void princ_(environment*); |
106 |
extern void princ(environment*); |
107 |
void print_st(stackitem*, long); |
108 |
extern void printstack(environment*); |
109 |
extern void swap(environment*); |
110 |
extern void rot(environment*); |
111 |
extern void rcl(environment*); |
112 |
extern void eval(environment*); |
113 |
extern void rev(environment*); |
114 |
extern void pack(environment*); |
115 |
extern void expand(environment*); |
116 |
extern void eq(environment*); |
117 |
extern void not(environment*); |
118 |
extern void neq(environment*); |
119 |
extern void def(environment*); |
120 |
extern void quit(environment*); |
121 |
extern void clear(environment*); |
122 |
extern void words(environment*); |
123 |
void forget_sym(symbol**); |
124 |
extern void forget(environment*); |
125 |
extern void errn(environment*); |
126 |
extern void sx_2b(environment*); |
127 |
extern void sx_2d(environment*); |
128 |
extern void sx_3e(environment*); |
129 |
value *copy_val(environment*, value*); |
130 |
extern void sx_647570(environment*); |
131 |
extern void sx_6966(environment*); |
132 |
extern void ifelse(environment*); |
133 |
extern void sx_7768696c65(environment*); |
134 |
extern void sx_666f72(environment*); |
135 |
extern void to(environment*); |
136 |
extern void readline(environment*); |
137 |
extern void sx_72656164(environment*); |
138 |
extern void foreach(environment*); |
139 |
void protect(environment*, value*); |
140 |
void unprotect(environment*); |
141 |
extern void copying(environment *env); |
142 |
extern void warranty(environment *env); |