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