15 |
/* mtrace, muntrace */ |
/* mtrace, muntrace */ |
16 |
#include <mcheck.h> |
#include <mcheck.h> |
17 |
|
|
18 |
#define HASHTBLSIZE 2048 |
#include "stack.h" |
|
|
|
|
/* First, define some types. */ |
|
|
|
|
|
/* A value of some type */ |
|
|
typedef struct { |
|
|
enum { |
|
|
integer, |
|
|
string, |
|
|
func, /* Function pointer */ |
|
|
symb, |
|
|
list |
|
|
} type; /* Type of stack element */ |
|
|
|
|
|
union { |
|
|
void *ptr; /* Pointer to the content */ |
|
|
int val; /* ...or an integer */ |
|
|
} content; /* Stores a pointer or an integer */ |
|
|
|
|
|
int gc_garb; |
|
|
|
|
|
} value; |
|
|
|
|
|
/* A symbol with a name and possible value */ |
|
|
/* (These do not need reference counters, they are kept unique by |
|
|
hashing.) */ |
|
|
typedef struct symbol_struct { |
|
|
char *id; /* Symbol name */ |
|
|
value *val; /* The value (if any) bound to it */ |
|
|
struct symbol_struct *next; /* In case of hashing conflicts, a */ |
|
|
} symbol; /* symbol is a kind of stack item. */ |
|
|
|
|
|
/* A type for a hash table for symbols */ |
|
|
typedef symbol *hashtbl[HASHTBLSIZE]; /* Hash table declaration */ |
|
|
|
|
|
/* An item (value) on a stack */ |
|
|
typedef struct stackitem_struct |
|
|
{ |
|
|
value *item; /* The value on the stack */ |
|
|
/* (This is never NULL) */ |
|
|
struct stackitem_struct *next; /* Next item */ |
|
|
} stackitem; |
|
|
|
|
|
/* An environment; gives access to the stack and a hash table of |
|
|
defined symbols */ |
|
|
typedef struct { |
|
|
stackitem *gc_ref; |
|
|
int gc_limit, gc_count; |
|
|
|
|
|
stackitem *head; /* Head of the stack */ |
|
|
hashtbl symbols; /* Hash table of all variable bindings */ |
|
|
int err; /* Error flag */ |
|
|
char *in_string; /* Input pending to be read */ |
|
|
char *free_string; /* Free this string when all input is |
|
|
read from in_string */ |
|
|
FILE *inputstream; /* stdin or a file, most likely */ |
|
|
int interactive; /* print prompts, stack, etc */ |
|
|
} environment; |
|
|
|
|
|
/* A type for pointers to external functions */ |
|
|
typedef void (*funcp)(environment *); /* funcp is a pointer to a void |
|
|
function (environment *) */ |
|
19 |
|
|
20 |
/* Initialize a newly created environment */ |
/* Initialize a newly created environment */ |
21 |
void init_env(environment *env) |
void init_env(environment *env) |
83 |
} |
} |
84 |
} |
} |
85 |
|
|
|
extern void gc_init(environment*); |
|
|
|
|
86 |
value* new_val(environment *env) { |
value* new_val(environment *env) { |
87 |
value *nval= malloc(sizeof(value)); |
value *nval= malloc(sizeof(value)); |
88 |
stackitem *nitem= malloc(sizeof(stackitem)); |
stackitem *nitem= malloc(sizeof(stackitem)); |
745 |
toss(env); toss(env); |
toss(env); toss(env); |
746 |
} |
} |
747 |
|
|
|
extern void clear(environment *); |
|
|
void forget_sym(symbol **); |
|
|
|
|
748 |
/* Quit stack. */ |
/* Quit stack. */ |
749 |
extern void quit(environment *env) |
extern void quit(environment *env) |
750 |
{ |
{ |
832 |
push_int(env, env->err); |
push_int(env, env->err); |
833 |
} |
} |
834 |
|
|
|
extern void sx_72656164(environment*); |
|
|
|
|
835 |
int main(int argc, char **argv) |
int main(int argc, char **argv) |
836 |
{ |
{ |
837 |
environment myenv; |
environment myenv; |