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