switching to ports instead of streams

This commit is contained in:
Alex Shinn 2009-03-04 23:27:36 +09:00
parent eafc5f2136
commit 598e71c950
6 changed files with 464 additions and 342 deletions

View file

@ -5,4 +5,5 @@
#define USE_BOEHM 1
#define USE_HUFF_SYMS 1
#define USE_DEBUG 1
#define USE_STRING_STREAMS 1

View file

@ -31,7 +31,7 @@ void disasm (bytecode bc) {
case OP_GLOBAL_SET:
case OP_CALL:
case OP_PUSH:
write_sexp(stderr, ((sexp*)ip)[0]);
sexp_write(((sexp*)ip)[0], cur_error_port);
ip += sizeof(sexp);
break;
case OP_JUMP:
@ -75,7 +75,7 @@ void print_stack (sexp *stack, int top) {
for (i=0; i<top; i++) {
fprintf(stderr, " %02d: ", i);
fflush(stderr);
write_sexp(stderr, stack[i]);
sexp_write(stack[i], cur_error_port);
fprintf(stderr, "\n");
}
}

161
eval.c
View file

@ -6,6 +6,10 @@
/************************************************************************/
static int scheme_initialized_p = 0;
static sexp cur_input_port, cur_output_port, cur_error_port;
static struct core_form core_forms[] = {
{SEXP_CORE, "define", CORE_DEFINE},
{SEXP_CORE, "set!", CORE_SET},
@ -63,6 +67,22 @@ _OP(OPC_TYPE_PREDICATE, OP_EOFP, 1, 0, 0, 0, "eof-object?", 0),
/********************** environment utilities ***************************/
sexp sexp_set_car(sexp obj, sexp val) {
if (SEXP_PAIRP(obj))
return SEXP_CAR(obj) = val;
else {
sexp_debug("error: set-car! not a pair: ", obj);
return SEXP_ERROR;
}
}
sexp sexp_set_cdr(sexp obj, sexp val) {
if (SEXP_PAIRP(obj))
return SEXP_CDR(obj) = val;
else
return SEXP_ERROR;
}
sexp env_cell(env e, sexp key) {
sexp ls, res=NULL;
@ -81,7 +101,7 @@ sexp env_cell(env e, sexp key) {
int env_global_p (env e, sexp id) {
while (e->parent) {
if (assq(id, e->bindings) != SEXP_FALSE)
if (sexp_assq(id, e->bindings) != SEXP_FALSE)
return 0;
else
e = e->parent;
@ -94,7 +114,7 @@ void env_define(env e, sexp key, sexp value) {
if (cell) {
SEXP_CDR(cell) = value;
} else {
e->bindings = cons(cons(key, value), e->bindings);
e->bindings = sexp_cons(sexp_cons(key, value), e->bindings);
}
}
@ -105,7 +125,8 @@ env extend_env_closure (env e, sexp fv) {
e2->parent = e;
e2->bindings = SEXP_NULL;
for (i=0; SEXP_PAIRP(fv); fv = SEXP_CDR(fv), i++) {
e2->bindings = cons(cons(SEXP_CAR(fv), make_integer(i)), e2->bindings);
e2->bindings = sexp_cons(sexp_cons(SEXP_CAR(fv), sexp_make_integer(i)),
e2->bindings);
}
return e2;
}
@ -117,10 +138,10 @@ env make_standard_env() {
e->parent = NULL;
e->bindings = SEXP_NULL;
for (i=0; i<(sizeof(core_forms)/sizeof(struct core_form)); i++) {
env_define(e, intern(core_forms[i].name), (sexp)(&core_forms[i]));
env_define(e, sexp_intern(core_forms[i].name), (sexp)(&core_forms[i]));
}
for (i=0; i<(sizeof(opcodes)/sizeof(struct opcode)); i++) {
env_define(e, intern(opcodes[i].name), (sexp)(&opcodes[i]));
env_define(e, sexp_intern(opcodes[i].name), (sexp)(&opcodes[i]));
}
return e;
}
@ -168,7 +189,7 @@ void emit_word(bytecode *bc, unsigned int *i, sexp_uint_t val) {
#define emit_push(bc,i,obj) (emit(bc,i,OP_PUSH), emit_word(bc,i,(sexp_uint_t)obj))
sexp make_procedure(sexp bc, sexp vars) {
sexp sexp_make_procedure(sexp bc, sexp vars) {
sexp proc = SEXP_NEW();
if (! proc) return SEXP_ERROR;
proc->tag = SEXP_PROCEDURE;
@ -271,12 +292,13 @@ void analyze(sexp obj, bytecode *bc, unsigned int *i, env e,
analyze(SEXP_CADR(obj), bc, i, e, params, fv, sv, d);
}
} else {
for (o2 = reverse(SEXP_CDR(obj)); SEXP_PAIRP(o2); o2 = SEXP_CDR(o2)) {
for (o2 = sexp_reverse(SEXP_CDR(obj)); SEXP_PAIRP(o2);
o2 = SEXP_CDR(o2)) {
/* fprintf(stderr, ":: arg: %d\n", SEXP_CAR(o2)); */
analyze(SEXP_CAR(o2), bc, i, e, params, fv, sv, d);
}
emit(bc, i, ((opcode)o1)->op_name);
(*d) -= length(SEXP_CDDR(obj));
(*d) -= sexp_length(SEXP_CDDR(obj));
}
break;
default:
@ -311,14 +333,14 @@ void analyze_var_ref (sexp obj, bytecode *bc, unsigned int *i, env e,
sexp params, sexp fv, sexp sv, unsigned int *d) {
int tmp;
/* fprintf(stderr, "symbol lookup, param length: %d sv: ", length(params)); */
/* write_sexp(stderr, sv); */
/* sexp_write(sv, stderr); */
/* fprintf(stderr, "\n"); */
if ((tmp = list_index(params, obj)) >= 0) {
if ((tmp = sexp_list_index(params, obj)) >= 0) {
/* fprintf(stderr, "compiling local ref: %p => %d (d = %d)\n", obj, tmp, *d); */
emit(bc, i, OP_STACK_REF);
emit_word(bc, i, tmp + *d + 4);
(*d)++;
} else if ((tmp = list_index(fv, obj)) >= 0) {
} else if ((tmp = sexp_list_index(fv, obj)) >= 0) {
/* fprintf(stderr, "compiling closure ref: %p => %d\n", obj, tmp); */
emit(bc, i, OP_CLOSURE_REF);
emit_word(bc, i, tmp);
@ -329,7 +351,7 @@ void analyze_var_ref (sexp obj, bytecode *bc, unsigned int *i, env e,
emit_word(bc, i, (sexp_uint_t) obj);
(*d)++;
}
if (list_index(sv, obj) >= 0) {
if (sexp_list_index(sv, obj) >= 0) {
/* fprintf(stderr, "mutable variable, fetching CAR\n"); */
emit(bc, i, OP_CAR);
}
@ -338,10 +360,10 @@ void analyze_var_ref (sexp obj, bytecode *bc, unsigned int *i, env e,
void analyze_app (sexp obj, bytecode *bc, unsigned int *i,
env e, sexp params, sexp fv, sexp sv, unsigned int *d) {
sexp o1;
unsigned long len = length(SEXP_CDR(obj));
unsigned long len = sexp_length(SEXP_CDR(obj));
/* push the arguments onto the stack */
for (o1 = reverse(SEXP_CDR(obj)); SEXP_PAIRP(o1); o1 = SEXP_CDR(o1)) {
for (o1 = sexp_reverse(SEXP_CDR(obj)); SEXP_PAIRP(o1); o1 = SEXP_CDR(o1)) {
analyze(SEXP_CAR(o1), bc, i, e, params, fv, sv, d);
}
@ -350,18 +372,18 @@ void analyze_app (sexp obj, bytecode *bc, unsigned int *i,
/* make the call */
emit(bc, i, OP_CALL);
emit_word(bc, i, (sexp_uint_t) make_integer(len));
emit_word(bc, i, (sexp_uint_t) sexp_make_integer(len));
}
sexp free_vars (env e, sexp formals, sexp obj, sexp fv) {
sexp o1;
if (SEXP_SYMBOLP(obj)) {
if (env_global_p(e, obj)
|| (list_index(formals, obj) >= 0)
|| (list_index(fv, obj) >= 0))
|| (sexp_list_index(formals, obj) >= 0)
|| (sexp_list_index(fv, obj) >= 0))
return fv;
else
return cons(obj, fv);
return sexp_cons(obj, fv);
} else if (SEXP_PAIRP(obj)) {
if (SEXP_SYMBOLP(SEXP_CAR(obj))) {
if ((o1 = env_cell(e, SEXP_CAR(obj)))
@ -388,12 +410,12 @@ sexp set_vars (env e, sexp formals, sexp obj, sexp sv) {
if (SEXP_SYMBOLP(SEXP_CAR(obj))) {
if ((tmp = env_cell(e, SEXP_CAR(obj))) && SEXP_COREP(SEXP_CDR(tmp))) {
if (((core_form)SEXP_CDR(tmp))->code == CORE_LAMBDA) {
formals = lset_diff(formals, SEXP_CADR(obj));
formals = sexp_lset_diff(formals, SEXP_CADR(obj));
return set_vars(e, formals, SEXP_CADDR(obj), sv);
} else if (((core_form)SEXP_CDR(tmp))->code == CORE_SET
&& (list_index(formals, SEXP_CADR(obj)) >= 0)
&& ! (list_index(sv, SEXP_CADR(obj)) >= 0)) {
sv = cons(SEXP_CADR(obj), sv);
&& (sexp_list_index(formals, SEXP_CADR(obj)) >= 0)
&& ! (sexp_list_index(sv, SEXP_CADR(obj)) >= 0)) {
sv = sexp_cons(SEXP_CADR(obj), sv);
return set_vars(e, formals, SEXP_CADDR(obj), sv);
}
}
@ -412,17 +434,17 @@ void analyze_lambda (sexp name, sexp formals, sexp body,
sexp obj, ls, fv2 = free_vars(e, formals, body, SEXP_NULL);
env e2 = extend_env_closure(e, formals);
int k;
fprintf(stderr, "%d free-vars\n", length(fv2));
write_sexp(stderr, fv2);
fprintf(stderr, "%d free-vars\n", sexp_length(fv2));
sexp_write(fv2, cur_error_port);
fprintf(stderr, "\n");
obj = (sexp) compile(formals, body, e2, fv2, sv, 0);
emit_push(bc, i, SEXP_UNDEF);
emit_push(bc, i, make_integer(length(fv2)));
emit_push(bc, i, sexp_make_integer(sexp_length(fv2)));
emit(bc, i, OP_MAKE_VECTOR);
(*d)++;
for (ls=fv2, k=0; SEXP_PAIRP(ls); ls=SEXP_CDR(ls), k++) {
analyze_var_ref(SEXP_CAR(ls), bc, i, e, params, fv, SEXP_NULL, d);
emit_push(bc, i, make_integer(k));
emit_push(bc, i, sexp_make_integer(k));
emit(bc, i, OP_STACK_REF);
emit_word(bc, i, 3);
emit(bc, i, OP_VECTOR_SET);
@ -437,12 +459,12 @@ bytecode compile(sexp params, sexp obj, env e, sexp fv, sexp sv, int done_p) {
unsigned int i = 0, j, d = 0;
bytecode bc = (bytecode) SEXP_ALLOC(sizeof(struct bytecode)+INIT_BCODE_SIZE);
sexp sv2 = set_vars(e, params, obj, SEXP_NULL), ls;
/* fprintf(stderr, "set-vars: "); write_sexp(stderr, sv2); fprintf(stderr, "\n"); */
/* fprintf(stderr, "set-vars: "); sexp_write(sv2, stderr); fprintf(stderr, "\n"); */
bc->tag = SEXP_BYTECODE;
bc->len = INIT_BCODE_SIZE;
/* fprintf(stderr, "analyzing\n"); */
for (ls=params; SEXP_PAIRP(ls); ls=SEXP_CDR(ls)) {
if ((j = list_index(sv2, SEXP_CAR(ls)) >= 0)) {
if ((j = sexp_list_index(sv2, SEXP_CAR(ls)) >= 0)) {
/* fprintf(stderr, "consing mutable var\n"); */
emit_push(&bc, &i, SEXP_NULL);
emit(&bc, &i, OP_STACK_REF);
@ -453,9 +475,9 @@ bytecode compile(sexp params, sexp obj, env e, sexp fv, sexp sv, int done_p) {
emit(&bc, &i, OP_DROP);
}
}
sv = append(sv2, sv);
sv = sexp_append(sv2, sv);
for ( ; SEXP_PAIRP(obj); obj=SEXP_CDR(obj)) {
/* fprintf(stderr, "loop: "); write_sexp(stderr, obj); fprintf(stderr, "\n"); */
/* fprintf(stderr, "loop: "); sexp_write(obj, stderr); fprintf(stderr, "\n"); */
analyze(SEXP_CAR(obj), &bc, &i, e, params, fv, sv, &d);
if (SEXP_PAIRP(SEXP_CDR(obj))) emit(&bc, &i, OP_DROP);
}
@ -483,7 +505,7 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
case OP_GLOBAL_REF:
/* fprintf(stderr, "global ref: ip: %p => %p: ", ip, ((sexp*)ip)[0]); */
/* fflush(stderr); */
/* write_sexp(stderr, ((sexp*)ip)[0]); */
/* sexp_write(stderr, ((sexp*)ip)[0]); */
/* fprintf(stderr, "\n"); */
tmp = env_cell(e, ((sexp*)ip)[0]);
stack[top++]=SEXP_CDR(tmp);
@ -492,7 +514,7 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
case OP_GLOBAL_SET:
/* fprintf(stderr, "global set: %p: ", ((sexp*)ip)[0]); */
/* fflush(stderr); */
/* write_sexp(stderr, ((sexp*)ip)[0]); */
/* sexp_write(stderr, ((sexp*)ip)[0]); */
/* fprintf(stderr, "\n"); */
env_define(e, ((sexp*)ip)[0], stack[--top]);
ip += sizeof(sexp);
@ -501,7 +523,7 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
/* fprintf(stderr, "stack ref: ip=%p, %d - %d => ", */
/* ip, top, (sexp_uint_t) ((sexp*)ip)[0]); */
/* fflush(stderr); */
/* write_sexp(stderr, stack[top - (unsigned int) ((sexp*)ip)[0]]); */
/* sexp_write(stderr, stack[top - (unsigned int) ((sexp*)ip)[0]]); */
/* fprintf(stderr, "\n"); */
stack[top] = stack[top - (unsigned int) ((sexp*)ip)[0]];
ip += sizeof(sexp);
@ -515,35 +537,35 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
case OP_CLOSURE_REF:
/* fprintf(stderr, "closure-ref %d => ", ((sexp*)ip)[0]); */
/* fflush(stderr); */
/* write_sexp(stderr, vector_ref(cp,((sexp*)ip)[0])); */
/* sexp_write(stderr, vector_ref(cp,((sexp*)ip)[0])); */
/* fprintf(stderr, "\n"); */
stack[top++]=vector_ref(cp,((sexp*)ip)[0]);
stack[top++]=sexp_vector_ref(cp,((sexp*)ip)[0]);
ip += sizeof(sexp);
break;
case OP_VECTOR_REF:
stack[top-2]=vector_ref(stack[top-1], stack[top-2]);
stack[top-2]=sexp_vector_ref(stack[top-1], stack[top-2]);
top--;
break;
case OP_VECTOR_SET:
vector_set(stack[top-1], stack[top-2], stack[top-3]);
sexp_vector_set(stack[top-1], stack[top-2], stack[top-3]);
stack[top-3]=SEXP_UNDEF;
top-=2;
break;
case OP_STRING_REF:
stack[top-2]=string_ref(stack[top-1], stack[top-2]);
stack[top-2]=sexp_string_ref(stack[top-1], stack[top-2]);
top--;
break;
case OP_STRING_SET:
string_set(stack[top-1], stack[top-2], stack[top-3]);
sexp_string_set(stack[top-1], stack[top-2], stack[top-3]);
stack[top-3]=SEXP_UNDEF;
top-=2;
break;
case OP_MAKE_PROCEDURE:
stack[top-2]=make_procedure(stack[top-1], stack[top-2]);
stack[top-2]=sexp_make_procedure(stack[top-1], stack[top-2]);
top--;
break;
case OP_MAKE_VECTOR:
stack[top-2]=make_vector(unbox_integer(stack[top-1]), stack[top-2]);
stack[top-2]=sexp_make_vector(sexp_unbox_integer(stack[top-1]), stack[top-2]);
top--;
break;
case OP_PUSH:
@ -590,23 +612,23 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
stack[top-1]=(stack[top-1] == SEXP_EOF) ? SEXP_TRUE : SEXP_FALSE;
break;
case OP_CAR:
stack[top-1]=car(stack[top-1]);
stack[top-1]=sexp_car(stack[top-1]);
break;
case OP_CDR:
stack[top-1]=cdr(stack[top-1]);
stack[top-1]=sexp_cdr(stack[top-1]);
break;
case OP_SET_CAR:
set_car(stack[top-1], stack[top-2]);
sexp_set_car(stack[top-1], stack[top-2]);
stack[top-2]=SEXP_UNDEF;
top--;
break;
case OP_SET_CDR:
set_cdr(stack[top-1], stack[top-2]);
sexp_set_cdr(stack[top-1], stack[top-2]);
stack[top-2]=SEXP_UNDEF;
top--;
break;
case OP_CONS:
stack[top-2]=cons(stack[top-1], stack[top-2]);
stack[top-2]=sexp_cons(stack[top-1], stack[top-2]);
top--;
break;
case OP_ADD:
@ -657,16 +679,16 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
if (! SEXP_PROCEDUREP(tmp))
errx(2, "non-procedure application: %p", tmp);
stack[top-1] = (sexp) i;
stack[top] = make_integer(ip+4);
stack[top] = sexp_make_integer(ip+4);
stack[top+1] = cp;
top+=2;
bc = procedure_code(tmp);
bc = sexp_procedure_code(tmp);
/* print_bytecode(bc); */
/* disasm(bc); */
ip = bc->data;
cp = procedure_vars(tmp);
cp = sexp_procedure_vars(tmp);
fprintf(stderr, "... calling procedure at %p\ncp: ", ip);
write_sexp(stderr, cp);
/* sexp_write(cp, stderr); */
fprintf(stderr, "\n");
/* fprintf(stderr, "stack at %d\n", top); */
/* print_stack(stack, top); */
@ -688,14 +710,14 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
case OP_RET:
fprintf(stderr, "returning @ %d: ", top-1);
fflush(stderr);
write_sexp(stderr, stack[top-1]);
sexp_write(stack[top-1], cur_error_port);
fprintf(stderr, "...\n");
/* print_stack(stack, top); */
/* top-1 */
/* stack: args ... n ip result */
cp = stack[top-2];
ip = (unsigned char*) unbox_integer(stack[top-3]);
i = unbox_integer(stack[top-4]);
ip = (unsigned char*) sexp_unbox_integer(stack[top-3]);
i = sexp_unbox_integer(stack[top-4]);
stack[top-i-4] = stack[top-1];
top = top-i-3;
fprintf(stderr, "... done returning\n");
@ -703,7 +725,7 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
case OP_DONE:
fprintf(stderr, "finally returning @ %d: ", top-1);
fflush(stderr);
write_sexp(stderr, stack[top-1]);
sexp_write(stack[top-1], cur_error_port);
fprintf(stderr, "\n");
goto end_loop;
default:
@ -722,7 +744,7 @@ sexp vm(bytecode bc, env e, sexp* stack, unsigned int top) {
sexp eval_in_stack(sexp obj, env e, sexp* stack, unsigned int top) {
bytecode bc;
bc = compile(SEXP_NULL, cons(obj, SEXP_NULL), e, SEXP_NULL, SEXP_NULL, 1);
bc = compile(SEXP_NULL, sexp_cons(obj, SEXP_NULL), e, SEXP_NULL, SEXP_NULL, 1);
return vm(bc, e, stack, top);
}
@ -733,12 +755,22 @@ sexp eval(sexp obj, env e) {
return res;
}
void scheme_init() {
if (! scheme_initialized_p) {
scheme_initialized_p = 1;
sexp_init();
cur_input_port = sexp_make_input_port(stdin);
cur_output_port = sexp_make_output_port(stdout);
cur_error_port = sexp_make_output_port(stderr);
}
}
int main (int argc, char **argv) {
sexp obj, res, *stack;
sexp obj, res, in, out, *stack;
env e;
int i, quit=0;
sexp_init();
scheme_init();
e = make_standard_env();
stack = (sexp*) SEXP_ALLOC(sizeof(sexp) * INIT_STACK_SIZE);
@ -754,19 +786,18 @@ int main (int argc, char **argv) {
}
/* repl */
if (! quit) {
while (! quit) {
fprintf(stdout, "> ");
fflush(stdout);
while ((obj = read_sexp(stdin)) != SEXP_EOF) {
/* write_sexp(stdout, obj); */
obj = sexp_read(cur_input_port);
if (obj == SEXP_EOF) {
quit = 1;
} else {
res = eval_in_stack(obj, e, stack, 0);
if (res != SEXP_UNDEF) {
/* fprintf(stdout, "\n "); */
write_sexp(stdout, res);
fprintf(stdout, "\n");
sexp_write(res, cur_output_port);
sexp_write_char('\n', cur_output_port);
}
fprintf(stdout, "> ");
fflush(stdout);
}
}
return 0;

2
eval.h
View file

@ -12,6 +12,8 @@
#define INIT_BCODE_SIZE 128
#define INIT_STACK_SIZE 1024
#define sexp_debug(msg, obj) (sexp_write_string(msg,cur_error_port), sexp_write(obj, cur_error_port), sexp_write_char('\n',cur_error_port))
typedef struct bytecode {
char tag;
unsigned int len;

483
sexp.c
View file

@ -1,4 +1,4 @@
/* sexp.c -- sexp library implementation */
/* sexp.c -- standalone sexp library implementation */
/* Copyright (c) 2009 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */
@ -16,7 +16,7 @@ static struct huff_entry huff_table[] = {
};
#endif
static int initialized_p = 0;
static int sexp_initialized_p = 0;
static sexp the_dot_symbol;
static sexp the_quote_symbol;
@ -24,7 +24,7 @@ static sexp the_quasiquote_symbol;
static sexp the_unquote_symbol;
static sexp the_unquote_splicing_symbol;
static char separators[] = {
static char sexp_separators[] = {
/* 1 2 3 4 5 6 7 8 9 a b c d e f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, /* x0_ */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* x1_ */
@ -36,7 +36,7 @@ static char separators[] = {
static int is_separator(int c) {
/* return (!((c-9)&(~3))) | (~(c^4)); */
return 0<c && c<0x60 && separators[c];
return 0<c && c<0x60 && sexp_separators[c];
}
static sexp* symbol_table = NULL;
@ -47,26 +47,26 @@ static unsigned long symbol_table_primes[] = {
static int symbol_table_prime_index = 0;
static int symbol_table_count = 0;
void free_sexp (sexp obj) {
void sexp_free (sexp obj) {
int len, i;
sexp *elts;
if (SEXP_POINTERP(obj)) {
switch (obj->tag) {
case SEXP_PAIR:
free_sexp(car(obj));
free_sexp(cdr(obj));
sexp_free(SEXP_CAR(obj));
sexp_free(SEXP_CDR(obj));
break;
case SEXP_VECTOR:
len = vector_length(obj);
elts = vector_data(obj);
len = sexp_vector_length(obj);
elts = sexp_vector_data(obj);
for (i=0; i<len; i++) {
free_sexp(elts[i]);
sexp_free(elts[i]);
}
SEXP_FREE(elts);
break;
case SEXP_STRING:
case SEXP_SYMBOL:
SEXP_FREE(string_data(obj));
SEXP_FREE(sexp_string_data(obj));
break;
}
SEXP_FREE(obj);
@ -75,7 +75,7 @@ void free_sexp (sexp obj) {
/*************************** list utilities ***************************/
sexp cons(sexp head, sexp tail) {
sexp sexp_cons(sexp head, sexp tail) {
sexp pair = SEXP_NEW();
if (! pair) return SEXP_ERROR;
pair->tag = SEXP_PAIR;
@ -84,37 +84,21 @@ sexp cons(sexp head, sexp tail) {
return pair;
}
sexp car(sexp obj) {
sexp sexp_car(sexp obj) {
return (SEXP_PAIRP(obj)) ? SEXP_CAR(obj) : SEXP_ERROR;
}
sexp cdr(sexp obj) {
sexp sexp_cdr(sexp obj) {
return (SEXP_PAIRP(obj)) ? SEXP_CDR(obj) : SEXP_ERROR;
}
sexp set_car(sexp obj, sexp val) {
if (SEXP_PAIRP(obj))
return SEXP_CAR(obj) = val;
else {
sexp_debug("error: set-car! not a pair: ", obj);
return SEXP_ERROR;
}
}
sexp set_cdr(sexp obj, sexp val) {
if (SEXP_PAIRP(obj))
return SEXP_CDR(obj) = val;
else
return SEXP_ERROR;
}
int listp (sexp obj) {
int sexp_listp (sexp obj) {
while (SEXP_PAIRP(obj))
obj = SEXP_CDR(obj);
return (obj == SEXP_NULL);
}
int list_index (sexp ls, sexp elt) {
int sexp_list_index (sexp ls, sexp elt) {
int i=0;
while (SEXP_PAIRP(ls)) {
if (SEXP_CAR(ls) == elt)
@ -125,7 +109,7 @@ int list_index (sexp ls, sexp elt) {
return -1;
}
sexp memq (sexp x, sexp ls) {
sexp sexp_memq (sexp x, sexp ls) {
while (SEXP_PAIRP(ls))
if (x == SEXP_CAR(ls))
return ls;
@ -134,7 +118,7 @@ sexp memq (sexp x, sexp ls) {
return SEXP_FALSE;
}
sexp assq (sexp x, sexp ls) {
sexp sexp_assq (sexp x, sexp ls) {
while (SEXP_PAIRP(ls))
if (x == SEXP_CAAR(ls))
return ls;
@ -143,22 +127,22 @@ sexp assq (sexp x, sexp ls) {
return SEXP_FALSE;
}
sexp lset_diff(sexp a, sexp b) {
sexp sexp_lset_diff(sexp a, sexp b) {
sexp res = SEXP_NULL;
for ( ; SEXP_PAIRP(a); a=SEXP_CDR(a))
if (! list_index(b, SEXP_CAR(a)) >= 0)
res = cons(SEXP_CAR(a), res);
if (! sexp_list_index(b, SEXP_CAR(a)) >= 0)
res = sexp_cons(SEXP_CAR(a), res);
return res;
}
sexp reverse(sexp ls) {
sexp sexp_reverse(sexp ls) {
sexp res = SEXP_NULL;
for ( ; SEXP_PAIRP(ls); ls=SEXP_CDR(ls))
res = cons(SEXP_CAR(ls), res);
res = sexp_cons(SEXP_CAR(ls), res);
return res;
}
sexp nreverse(sexp ls) {
sexp sexp_nreverse(sexp ls) {
sexp a, b, tmp;
if (ls == SEXP_NULL) {
return ls;
@ -166,52 +150,52 @@ sexp nreverse(sexp ls) {
return SEXP_ERROR;
} else {
b=ls;
a=cdr(ls);
set_cdr(b, SEXP_NULL);
a=SEXP_CDR(ls);
SEXP_CDR(b) = SEXP_NULL;
for ( ; SEXP_PAIRP(a); b=a, a=tmp) {
tmp=cdr(a);
set_cdr(a, b);
tmp=SEXP_CDR(a);
SEXP_CDR(a) = b;
}
return b;
}
}
sexp append(sexp a, sexp b) {
for (a=reverse(a); SEXP_PAIRP(a); a=SEXP_CDR(a))
b = cons(SEXP_CAR(a), b);
sexp sexp_append(sexp a, sexp b) {
for (a=sexp_reverse(a); SEXP_PAIRP(a); a=SEXP_CDR(a))
b = sexp_cons(SEXP_CAR(a), b);
return b;
}
sexp list(int count, ...) {
sexp sexp_list(int count, ...) {
sexp res = SEXP_NULL;
int i;
va_list ap;
va_start(ap, count);
for (i=0; i<count; i++)
res = cons(va_arg(ap, sexp), res);
res = sexp_cons(va_arg(ap, sexp), res);
va_end(ap);
return nreverse(res);
return sexp_nreverse(res);
}
unsigned long length(sexp ls) {
unsigned long sexp_length(sexp ls) {
sexp x;
unsigned long res;
for (res=0, x=ls; SEXP_PAIRP(x); res++, x=cdr(x))
for (res=0, x=ls; SEXP_PAIRP(x); res++, x=SEXP_CDR(x))
;
return res;
}
/********************* strings, symbols, vectors **********************/
sexp make_flonum(double f) {
sexp sexp_make_flonum(double f) {
sexp x = SEXP_NEW();
if (! x) return SEXP_ERROR;
x->tag = SEXP_FLONUM;
flonum_value(x) = f;
sexp_flonum_value(x) = f;
return x;
}
sexp make_string(char *str) {
sexp sexp_make_string(char *str) {
sexp s = SEXP_NEW();
if (! s) return SEXP_ERROR;
unsigned long len = strlen(str);
@ -227,12 +211,12 @@ sexp make_string(char *str) {
#define FNV_PRIME 16777619
#define FNV_OFFSET_BASIS 2166136261uL
int string_hash(char *str, int acc) {
int sexp_string_hash(char *str, int acc) {
while (*str) {acc *= FNV_PRIME; acc ^= *str++;}
return acc;
}
sexp intern(char *str) {
sexp sexp_intern(char *str) {
struct huff_entry he;
sexp_uint_t len, res=FNV_OFFSET_BASIS, space=3, newbits, i, d, cell;
char c, *mystr, *p=str;
@ -253,15 +237,15 @@ sexp intern(char *str) {
#endif
normal_intern:
res = string_hash(p, res);
res = sexp_string_hash(p, res);
d = symbol_table_primes[symbol_table_prime_index];
cell = res % d;
for (i=0; i<d; i++) {
if (! symbol_table[cell]) {
break;
} else if (strncmp(str,
symbol_data(symbol_table[cell]),
symbol_length(symbol_table[cell])) == 0) {
sexp_symbol_data(symbol_table[cell]),
sexp_symbol_length(symbol_table[cell])) == 0) {
return symbol_table[cell];
}
cell = (cell * res + 1) % d;
@ -290,7 +274,7 @@ sexp intern(char *str) {
return symbol_table[cell];
}
sexp make_vector(unsigned long len, sexp dflt) {
sexp sexp_make_vector(unsigned long len, sexp dflt) {
int i;
sexp v = SEXP_NEW();
if (v == NULL) return SEXP_ERROR;
@ -305,21 +289,21 @@ sexp make_vector(unsigned long len, sexp dflt) {
return v;
}
sexp list_to_vector(sexp ls) {
sexp vec = make_vector(length(ls), SEXP_FALSE);
sexp sexp_list_to_vector(sexp ls) {
sexp vec = sexp_make_vector(sexp_length(ls), SEXP_FALSE);
if (vec == SEXP_ERROR) return vec;
sexp x;
sexp *elts = vector_data(vec);
sexp *elts = sexp_vector_data(vec);
int i;
for (i=0, x=ls; SEXP_PAIRP(x); i++, x=cdr(x))
elts[i] = car(x);
for (i=0, x=ls; SEXP_PAIRP(x); i++, x=SEXP_CDR(x))
elts[i] = SEXP_CAR(x);
return vec;
}
sexp vector(int count, ...) {
sexp vec = make_vector(count, SEXP_FALSE);
sexp sexp_vector(int count, ...) {
sexp vec = sexp_make_vector(count, SEXP_FALSE);
if (vec == SEXP_ERROR) return vec;
sexp *elts = vector_data(vec);
sexp *elts = sexp_vector_data(vec);
va_list ap;
int i;
@ -332,70 +316,145 @@ sexp vector(int count, ...) {
/************************ reading and writing *************************/
void write_sexp (FILE *out, sexp obj) {
#ifdef USE_STRING_STREAMS
int sstream_read(void *vec, char *dst, int n) {
int len = (int) sexp_vector_ref((sexp) vec, sexp_make_integer(1));
int pos = (int) sexp_vector_ref((sexp) vec, sexp_make_integer(2));
if (pos >= len) return 0;
if (n > (len - pos)) n = (len - pos);
memcpy(dst+pos, sexp_vector_ref((sexp) vec, sexp_make_integer(0)), n);
sexp_vector_set((sexp) vec, sexp_make_integer(2), (sexp)n);
return n;
}
int sstream_write(void *vec, const char *src, int n) {
return n;
}
off_t sstream_seek(void *vec, off_t offset, int whence) {
int pos;
if (whence == SEEK_SET) {
pos = offset;
} else if (whence == SEEK_CUR) {
pos = (int) sexp_vector_ref((sexp) vec, sexp_make_integer(2)) + offset;
} else { /* SEEK_END */
pos = (int) sexp_vector_ref((sexp) vec, sexp_make_integer(1)) + offset;
}
sexp_vector_set((sexp) vec, sexp_make_integer(2), (sexp)pos);
return pos;
}
int sstream_close(void *vec) {
sexp_free((sexp)vec);
}
sexp sexp_make_input_port(FILE* in) {
sexp p = SEXP_NEW();
if (p == NULL) return SEXP_ERROR;
p->tag = SEXP_IPORT;
p->data1 = in;
return p;
}
sexp sexp_make_output_port(FILE* out) {
sexp p = SEXP_NEW();
if (p == NULL) return SEXP_ERROR;
p->tag = SEXP_OPORT;
p->data1 = out;
return p;
}
sexp sexp_make_input_string_port(sexp str) {
FILE *in = fmemopen(sexp_string_data(str), sexp_string_length(str), "r");
return sexp_make_input_port(in);
}
sexp sexp_make_output_string_port() {
return SEXP_ERROR;
}
sexp sexp_get_output_string(sexp port) {
return SEXP_ERROR;
}
#endif
void sexp_write (sexp obj, sexp out) {
unsigned long len, i, c, res;
sexp x, *elts;
char *str;
if (! obj) {
fprintf(out, "#<null>");
sexp_write_string("#<null>", out);
} else if (SEXP_POINTERP(obj)) {
switch (obj->tag) {
case SEXP_PAIR:
fprintf(out, "(");
write_sexp(out, car(obj));
for (x=cdr(obj); SEXP_PAIRP(x); x=cdr(x)) {
fprintf(out, " ");
write_sexp(out, car(x));
sexp_write_char('(', out);
sexp_write(SEXP_CAR(obj), out);
for (x=SEXP_CDR(obj); SEXP_PAIRP(x); x=SEXP_CDR(x)) {
sexp_write_char(' ', out);
sexp_write(SEXP_CAR(x), out);
}
if (! SEXP_NULLP(x)) {
fprintf(out, " . ");
write_sexp(out, x);
sexp_write_string(" . ", out);
sexp_write(x, out);
}
fprintf(out, ")");
sexp_write_char(')', out);
break;
case SEXP_VECTOR:
len = vector_length(obj);
elts = vector_data(obj);
len = sexp_vector_length(obj);
elts = sexp_vector_data(obj);
if (len == 0) {
fprintf(out, "#()");
sexp_write_string("#()", out);
} else {
fprintf(out, "#(");
write_sexp(out, elts[0]);
sexp_write_string("#(", out);
sexp_write(out, elts[0]);
for (i=1; i<len; i++) {
fprintf(out, " ");
write_sexp(out, elts[i]);
sexp_write_char(' ', out);
sexp_write(out, elts[i]);
}
fprintf(out, ")");
sexp_write_char(')', out);
}
break;
case SEXP_FLONUM:
fprintf(out, "%g", flonum_value(obj));
break;
sexp_printf(out, "%g", sexp_flonum_value(obj)); break;
case SEXP_PROCEDURE:
fprintf(out, "#<procedure>");
break;
sexp_write_string("#<procedure>", out); break;
case SEXP_IPORT:
sexp_write_string("#<input-port>", out); break;
case SEXP_OPORT:
sexp_write_string("#<output-port>", out); break;
case SEXP_BYTECODE:
fprintf(out, "#<bytecode>");
break;
sexp_write_string("#<bytecode>", out); break;
case SEXP_ENV:
fprintf(out, "#<env>");
break;
sexp_write_string("#<env>", out); break;
case SEXP_STRING:
fprintf(out, "\"");
sexp_write_char('"', out);
i = sexp_string_length(obj);
str = sexp_string_data(obj);
/* FALLTHROUGH */
case SEXP_SYMBOL:
fprintf(out, "%s", string_data(obj));
if (obj->tag != SEXP_STRING) {
i = sexp_symbol_length(obj);
str = sexp_symbol_data(obj);
}
for ( ; i>=0; str++, i--) {
if (str[0] == '\\')
sexp_write_char('\\', out);
sexp_write_char(str[0], out);
}
if (obj->tag == SEXP_STRING)
fprintf(out, "\"");
sexp_write_char('"', out);
break;
}
} else if (SEXP_INTEGERP(obj)) {
fprintf(out, "%d", unbox_integer(obj));
sexp_printf(out, "%d", sexp_unbox_integer(obj));
} else if (SEXP_CHARP(obj)) {
if (33 <= unbox_character(obj) < 127) {
fprintf(out, "#\\%c", unbox_character(obj));
if (33 <= sexp_unbox_character(obj) < 127) {
sexp_printf(out, "#\\%c", sexp_unbox_character(obj));
} else {
fprintf(out, "#\\x%02d", unbox_character(obj));
sexp_printf(out, "#\\x%02d", sexp_unbox_character(obj));
}
} else if (SEXP_SYMBOLP(obj)) {
@ -404,51 +463,45 @@ void write_sexp (FILE *out, sexp obj) {
c = ((sexp_uint_t)obj)>>3;
while (c) {
#include "sexp-unhuff.c"
putc(res, out);
sexp_write_char(res, out);
}
}
} else
#endif
fprintf(out, "%s", symbol_data(obj));
} else {
switch ((sexp_uint_t) obj) {
case (sexp_uint_t) SEXP_NULL:
fprintf(out, "()");
break;
sexp_write_string("()", out); break;
case (sexp_uint_t) SEXP_TRUE:
fprintf(out, "#t");
break;
sexp_write_string("#t", out); break;
case (sexp_uint_t) SEXP_FALSE:
fprintf(out, "#f");
break;
sexp_write_string("#f", out); break;
case (sexp_uint_t) SEXP_EOF:
fprintf(out, "#<eof>");
break;
sexp_write_string("#<eof>", out); break;
case (sexp_uint_t) SEXP_UNDEF:
fprintf(out, "#<undef>");
break;
sexp_write_string("#<undef>", out); break;
default:
fprintf(out, "#<error>");
sexp_write_string("#<error>", out);
}
}
}
char* read_string(FILE *in) {
char* sexp_read_string(sexp in) {
char *buf, *tmp, *res;
char c;
int len;
int c, len, size=128;
buf = SEXP_ALLOC(128);
buf = SEXP_ALLOC(size); /* XXXX grow! */
tmp = buf;
for (c=fgetc(in); (c != EOF) && (c != '"'); c=fgetc(in)) {
if (c == '\\') {
c=fgetc(in);
for (c=sexp_read_char(in); c != '"'; c=sexp_read_char(in)) {
if (c == EOF) {
SEXP_FREE(buf);
return NULL;
} else if (c == '\\') {
c=sexp_read_char(in);
switch (c) {
case 'n':
c = '\n';
case 't':
c = '\t';
case 'n': c = '\n'; break;
case 't': c = '\t'; break;
}
*tmp++ = c;
} else {
@ -464,21 +517,20 @@ char* read_string(FILE *in) {
return res;
}
char* read_symbol(FILE *in, int init) {
char* sexp_read_symbol(sexp in, int init) {
char *buf, *tmp, *res;
char c;
int len;
int c, len, size=128;
buf = SEXP_ALLOC(128);
buf = SEXP_ALLOC(size);
tmp = buf;
if (init != EOF)
*tmp++ = init;
while (1) {
c=fgetc(in);
c=sexp_read_char(in);
if (c == EOF || is_separator(c)) {
ungetc(c, in);
sexp_push_char(c, in);
break;
}
*tmp++ = c;
@ -492,57 +544,56 @@ char* read_symbol(FILE *in, int init) {
return res;
}
sexp read_float_tail(FILE *in, long whole) {
sexp sexp_read_float_tail(sexp in, long whole) {
double res = 0.0, scale=0.1;
int c;
for (c=fgetc(in); isdigit(c); c=fgetc(in), scale*=0.1)
for (c=sexp_read_char(in); isdigit(c); c=sexp_read_char(in), scale*=0.1)
res += ((c<='9') ? (c - '0') : ((toupper(c) - 'A') + 10))*scale;
ungetc(c, in);
return make_flonum(whole + res);
sexp_push_char(c, in);
return sexp_make_flonum(whole + res);
}
sexp read_number(FILE *in, int base) {
sexp sexp_read_number(sexp in, int base) {
sexp tmp;
long res = 0, negativep = 0;
int c;
long res = 0, negativep = 0, c;
c = fgetc(in);
c = sexp_read_char(in);
if (c == '-') {
negativep = 1;
} else if (isdigit(c)) {
res = c - '0';
}
for (c=fgetc(in); isxdigit(c); c=fgetc(in))
for (c=sexp_read_char(in); isxdigit(c); c=sexp_read_char(in))
res = res * base + ((c<='9') ? (c - '0') : ((toupper(c) - 'A') + 10));
if (c=='.') {
if (base != 10) {
fprintf(stderr, "decimal found in non-base 10");
return SEXP_ERROR;
}
tmp = read_float_tail(in, res);
tmp = sexp_read_float_tail(in, res);
if (negativep && SEXP_FLONUMP(tmp))
flonum_value(tmp) = -1 * flonum_value(tmp);
sexp_flonum_value(tmp) = -1 * sexp_flonum_value(tmp);
return tmp;
} else {
ungetc(c, in);
sexp_push_char(c, in);
}
return make_integer(negativep ? -res : res);
return sexp_make_integer(negativep ? -res : res);
}
sexp read_sexp_raw (FILE *in) {
sexp sexp_read_raw (sexp in) {
sexp res, tmp, tmp2;
char *str;
int c1, c2;
scan_loop:
switch (c1 = fgetc(in)) {
switch (c1 = sexp_read_char(in)) {
case EOF:
res = SEXP_EOF;
break;
case ';':
while ((c1 = fgetc(in)) != EOF)
while ((c1 = sexp_read_char(in)) != EOF)
if (c1 == '\n')
break;
/* fallthrough */
@ -550,96 +601,98 @@ sexp read_sexp_raw (FILE *in) {
case '\t':
case '\n':
goto scan_loop;
break;
case '\'':
res = read_sexp(in);
res = list2(the_quote_symbol, res);
res = sexp_read(in);
res = sexp_list2(the_quote_symbol, res);
break;
case '`':
res = read_sexp(in);
res = list2(the_quasiquote_symbol, res);
res = sexp_read(in);
res = sexp_list2(the_quasiquote_symbol, res);
break;
case ',':
if ((c1 = fgetc(in)) == '@') {
res = read_sexp(in);
res = list2(the_unquote_splicing_symbol, res);
if ((c1 = sexp_read_char(in)) == '@') {
res = sexp_read(in);
res = sexp_list2(the_unquote_splicing_symbol, res);
} else {
ungetc(c1, in);
res = read_sexp(in);
res = list2(the_unquote_symbol, res);
sexp_push_char(c1, in);
res = sexp_read(in);
res = sexp_list2(the_unquote_symbol, res);
}
break;
case '"':
str = read_string(in);
res = make_string(str);
str = sexp_read_string(in);
res = sexp_make_string(str);
SEXP_FREE(str);
break;
case '(':
res = SEXP_NULL;
tmp = read_sexp_raw(in);
tmp = sexp_read_raw(in);
while ((tmp != SEXP_ERROR) && (tmp != SEXP_EOF) && (tmp != SEXP_CLOSE)) {
if (tmp == SEXP_RAWDOT) {
/* dotted list */
free_sexp(tmp);
tmp = read_sexp_raw(in);
if (read_sexp(in) != SEXP_CLOSE) {
if (res == SEXP_NULL) {
fprintf(stderr, "sexp: dot before any elements in list\n");
return SEXP_ERROR;
} else {
tmp = sexp_read_raw(in);
if (sexp_read(in) != SEXP_CLOSE) {
fprintf(stderr, "sexp: multiple tokens in dotted tail\n");
res = SEXP_ERROR;
sexp_free(res);
return SEXP_ERROR;
} else {
tmp2 = res;
res = nreverse(res);
set_cdr(tmp2, tmp);
res = sexp_nreverse(res);
SEXP_CDR(tmp2) = tmp;
return res;
}
}
} else {
res = cons(tmp, res);
tmp = read_sexp_raw(in);
res = sexp_cons(tmp, res);
tmp = sexp_read_raw(in);
}
}
if (tmp != SEXP_CLOSE) {
free_sexp(res);
sexp_free(res);
res = SEXP_ERROR;
}
res = nreverse(res);
res = sexp_nreverse(res);
break;
case '#':
switch (c1=fgetc(in)) {
switch (c1=sexp_read_char(in)) {
case 'b':
res = read_number(in, 2);
break;
res = sexp_read_number(in, 2); break;
case 'o':
res = read_number(in, 8);
break;
res = sexp_read_number(in, 8); break;
case 'd':
res = read_number(in, 10);
break;
res = sexp_read_number(in, 10); break;
case 'x':
res = read_number(in, 16);
break;
res = sexp_read_number(in, 16); break;
/* case 'e': */
/* case 'i': */
case 'f':
case 't':
c2 = fgetc(in);
c2 = sexp_read_char(in);
if (c2 == EOF || is_separator(c2)) {
res = (c1 == 't' ? SEXP_TRUE : SEXP_FALSE);
} else {
fprintf(stderr, "sexp: invalid syntax #%c%c\n", c1, c2);
res = SEXP_ERROR;
}
ungetc(c2, in);
sexp_push_char(c2, in);
break;
case ';':
sexp_read_raw(in);
goto scan_loop;
case '(':
ungetc(c1, in);
res = read_sexp(in);
if (! listp(res)) {
sexp_push_char(c1, in);
res = sexp_read(in);
if (! sexp_listp(res)) {
if (res != SEXP_ERROR) {
fprintf(stderr, "sexp: dotted list not allowed in vector syntax\n");
free_sexp(res);
sexp_free(res);
res = SEXP_ERROR;
}
} else {
res = list_to_vector(res);
res = sexp_list_to_vector(res);
}
break;
default:
@ -649,16 +702,16 @@ sexp read_sexp_raw (FILE *in) {
}
break;
case '.':
c1 = fgetc(in);
c1 = sexp_read_char(in);
if (c1 == EOF || is_separator(c1)) {
res = SEXP_RAWDOT;
} else if (isdigit(c1)) {
ungetc(c1,in );
res = read_float_tail(in, 0);
sexp_push_char(c1,in );
res = sexp_read_float_tail(in, 0);
} else {
ungetc(c1, in);
str = read_symbol(in, '.');
res = intern(str);
sexp_push_char(c1, in);
str = sexp_read_symbol(in, '.');
res = sexp_intern(str);
SEXP_FREE(str);
}
break;
@ -667,51 +720,51 @@ sexp read_sexp_raw (FILE *in) {
break;
case '+':
case '-':
c2 = fgetc(in);
c2 = sexp_read_char(in);
if (c2 == '.' || isdigit(c2)) {
ungetc(c2, in);
res = read_number(in, 10);
sexp_push_char(c2, in);
res = sexp_read_number(in, 10);
if (c1 == '-') res = sexp_mul(res, -1);
} else {
ungetc(c2, in);
str = read_symbol(in, c1);
res = intern(str);
sexp_push_char(c2, in);
str = sexp_read_symbol(in, c1);
res = sexp_intern(str);
SEXP_FREE(str);
}
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
ungetc(c1, in);
res = read_number(in, 10);
sexp_push_char(c1, in);
res = sexp_read_number(in, 10);
break;
default:
str = read_symbol(in, c1);
res = intern(str);
str = sexp_read_symbol(in, c1);
res = sexp_intern(str);
SEXP_FREE(str);
break;
}
return res;
}
sexp read_sexp (FILE *in) {
sexp res = read_sexp_raw(in);
sexp sexp_read (sexp in) {
sexp res = sexp_read_raw(in);
if ((res == SEXP_CLOSE) || (res == SEXP_RAWDOT))
res = SEXP_ERROR;
return res;
}
void sexp_init() {
if (! initialized_p) {
initialized_p = 1;
if (! sexp_initialized_p) {
sexp_initialized_p = 1;
#ifdef USE_BOEHM
GC_init();
#endif
symbol_table = SEXP_ALLOC(symbol_table_primes[0]*sizeof(sexp));
the_dot_symbol = intern(".");
the_quote_symbol = intern("quote");
the_quasiquote_symbol = intern("quasiquote");
the_unquote_symbol = intern("unquote");
the_unquote_splicing_symbol = intern("unquote-splicing");
the_dot_symbol = sexp_intern(".");
the_quote_symbol = sexp_intern("quote");
the_quasiquote_symbol = sexp_intern("quasiquote");
the_unquote_symbol = sexp_intern("unquote");
the_unquote_splicing_symbol = sexp_intern("unquote-splicing");
}
}

139
sexp.h
View file

@ -20,7 +20,9 @@
#define errx(code, msg, ...) (fprintf(stderr,msg"\n",__VA_ARGS__), exit(code))
#endif
#define sexp_debug(msg, obj) (fprintf(stderr,msg), fflush(stderr), write_sexp(stderr, obj), fprintf(stderr,"\n"))
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
#define SEXP_BSD
#endif
#ifdef USE_BOEHM
#include "gc/include/gc.h"
@ -70,6 +72,8 @@ enum sexp_types {
SEXP_VECTOR,
SEXP_FLONUM,
SEXP_BIGNUM,
SEXP_IPORT,
SEXP_OPORT,
/* the following are used only by the evaluator */
SEXP_PROCEDURE,
SEXP_ENV,
@ -109,6 +113,8 @@ typedef long sexp_sint_t;
#define SEXP_LSYMBOLP(x) (SEXP_POINTERP(x) && ((sexp)(x))->tag == SEXP_SYMBOL)
#define SEXP_VECTORP(x) (SEXP_POINTERP(x) && ((sexp)(x))->tag == SEXP_VECTOR)
#define SEXP_FLONUMP(x) (SEXP_POINTERP(x) && ((sexp)(x))->tag == SEXP_FLONUM)
#define SEXP_IPORTP(x) (SEXP_POINTERP(x) && ((sexp)(x))->tag == SEXP_IPORT)
#define SEXP_OPORTP(x) (SEXP_POINTERP(x) && ((sexp)(x))->tag == SEXP_OPORT)
#define SEXP_PROCEDUREP(x) (SEXP_POINTERP(x) && ((sexp)(x))->tag == SEXP_PROCEDURE)
#define SEXP_ENVP(x) (SEXP_POINTERP(x) && ((sexp)(x))->tag == SEXP_ENV)
#define SEXP_BYTECODEP(x) (SEXP_POINTERP(x) && ((sexp)(x))->tag ==SEXP_BYTECODE)
@ -120,34 +126,57 @@ typedef long sexp_sint_t;
#ifdef USE_HUFF_SYMS
#define SEXP_DOTP(x) (((sexp_uint_t)(x))==((0x5D00<<SEXP_IMMEDIATE_BITS)+SEXP_ISYMBOL_TAG))
#else
#define SEXP_DOTP(x) ((x)==the_dot_symbol)
#define SEXP_DOTP(x) ((x)==sexp_the_dot_symbol)
#endif
#define make_integer(n) ((sexp) (((long) n<<SEXP_FIXNUM_BITS) + SEXP_FIXNUM_TAG))
#define unbox_integer(n) ((long) n>>SEXP_FIXNUM_BITS)
#define make_character(n) ((sexp) (((long) n<<SEXP_EXTENDED_BITS) + SEXP_CHAR_TAG))
#define unbox_character(n) ((long) n>>SEXP_EXTENDED_BITS)
#define sexp_make_integer(n) ((sexp) (((long) n<<SEXP_FIXNUM_BITS) + SEXP_FIXNUM_TAG))
#define sexp_unbox_integer(n) ((long) n>>SEXP_FIXNUM_BITS)
#define sexp_make_character(n) ((sexp) (((long) n<<SEXP_EXTENDED_BITS) + SEXP_CHAR_TAG))
#define sexp_unbox_character(n) ((long) n>>SEXP_EXTENDED_BITS)
#define flonum_value(f) (((double*)(((sexp_uint_t)f)+sizeof(char)))[0])
#define sexp_flonum_value(f) (((double*)(((sexp_uint_t)f)+sizeof(char)))[0])
#define vector_length(x) ((sexp_uint_t) x->data1)
#define vector_data(x) ((sexp*) x->data2)
#define sexp_vector_length(x) ((sexp_uint_t) x->data1)
#define sexp_vector_data(x) ((sexp*) (((sexp)x)->data2))
#define vector_ref(x, i) (vector_data(x)[unbox_integer(i)])
#define vector_set(x, i, v) (vector_data(x)[unbox_integer(i)] = (v))
#define sexp_vector_ref(x, i) (sexp_vector_data(x)[sexp_unbox_integer(i)])
#define sexp_vector_set(x, i, v) (sexp_vector_data(x)[sexp_unbox_integer(i)] = (v))
#define procedure_code(x) ((bytecode) ((sexp)x)->data1)
#define procedure_vars(x) ((sexp) ((sexp)x)->data2)
#define sexp_procedure_code(x) ((bytecode) ((sexp)x)->data1)
#define sexp_procedure_vars(x) ((sexp) ((sexp)x)->data2)
#define string_length(x) ((sexp_uint_t) x->data1)
#define string_data(x) ((char*) x->data2)
#define sexp_string_length(x) ((sexp_uint_t) x->data1)
#define sexp_string_data(x) ((char*) x->data2)
#define string_ref(x, i) (make_character(string_data(x)[unbox_integer(i)]))
#define string_set(x, i, v) (string_data(x)[unbox_integer(i)] = unbox_character(v))
#define sexp_string_ref(x, i) (sexp_make_character(sexp_string_data(x)[sexp_unbox_integer(i)]))
#define sexp_string_set(x, i, v) (sexp_string_data(x)[sexp_unbox_integer(i)] = sexp_unbox_character(v))
#define symbol_pointer(x) ((sexp) (((sexp_uint_t)x)-SEXP_LSYMBOL_TAG))
#define symbol_length(x) ((sexp_uint_t) (symbol_pointer(x)->data1))
#define symbol_data(x) ((char*) (symbol_pointer(x)->data2))
#define sexp_port_stream(p) ((FILE*) ((sexp)p)->data1)
#ifdef USE_STRING_STREAMS
#ifdef SEXP_BSD
#define fmemopen(str, len, m) funopen(sexp_vector(3, (sexp)str, (sexp)len, (sexp)0), sstream_read, sstream_write, sstream_seek, sstream_close)
int sstream_read(void *vec, char *dst, int n);
int sstream_write(void *vec, const char *src, int n);
off_t sstream_seek(void *vec, off_t offset, int whence);
int sstream_close(void *vec);
#endif
#define sexp_read_char(p) (getc(sexp_port_stream(p)))
#define sexp_push_char(c, p) (ungetc(c, sexp_port_stream(p)))
#define sexp_write_char(c, p) (putc(c, sexp_port_stream(p)))
#define sexp_write_string(s, p) (fputs(s, sexp_port_stream(p)))
#define sexp_printf(p, s, ...) (fprintf(sexp_port_stream(p), s, __VA_ARGS__))
#else
sexp sexp_read_char(sexp port);
void sexp_push_char(sexp ch, sexp port);
void sexp_write_char(sexp ch, sexp port);
void sexp_write_string(sexp str, sexp port);
void sexp_printf(sexp port, sexp fmt, ...);
#endif
#define sexp_symbol_pointer(x) ((sexp) (((sexp_uint_t)x)-SEXP_LSYMBOL_TAG))
#define sexp_symbol_length(x) ((sexp_uint_t) (sexp_symbol_pointer(x)->data1))
#define sexp_symbol_data(x) ((char*) (sexp_symbol_pointer(x)->data2))
#define sexp_add(a, b) ((sexp)(((sexp_sint_t)a)+((sexp_sint_t)b)-SEXP_FIXNUM_TAG))
#define sexp_sub(a, b) ((sexp)(((sexp_sint_t)a)-((sexp_sint_t)b)+SEXP_FIXNUM_TAG))
@ -155,9 +184,10 @@ typedef long sexp_sint_t;
#define sexp_div(a, b) ((sexp)(((((sexp_sint_t)a)>>SEXP_FIXNUM_BITS)/(((sexp_sint_t)b)>>SEXP_FIXNUM_BITS))<<SEXP_FIXNUM_BITS)+SEXP_FIXNUM_TAG)
#define sexp_mod(a, b) ((sexp)(((((sexp_sint_t)a)>>SEXP_FIXNUM_BITS)%(((sexp_sint_t)b)>>SEXP_FIXNUM_BITS))<<SEXP_FIXNUM_BITS)+SEXP_FIXNUM_TAG)
#define list2(a, b) cons(a, cons(b, SEXP_NULL))
#define list3(a, b, c) cons(a, cons(b, cons(c, SEXP_NULL)))
#define list4(a, b, c, d) cons(a, cons(b, cons(c, cons(d, SEXP_NULL))))
#define sexp_list1(a) sexp_cons(a, SEXP_NULL)
#define sexp_list2(a, b) sexp_cons(a, sexp_cons(b, SEXP_NULL))
#define sexp_list3(a, b, c) sexp_cons(a, sexp_cons(b, sexp_cons(c, SEXP_NULL)))
#define sexp_list4(a, b, c, d) sexp_cons(a, sexp_cons(b, sexp_cons(c, sexp_cons(d, SEXP_NULL))))
#define SEXP_CAR(x) (((sexp)x)->data1)
#define SEXP_CDR(x) (((sexp)x)->data2)
@ -172,36 +202,41 @@ typedef long sexp_sint_t;
#define SEXP_CADDDR(x) (SEXP_CADR(SEXP_CDDR(x)))
#define SEXP_CDDDDR(x) (SEXP_CDDR(SEXP_CDDR(x)))
sexp cons(sexp head, sexp tail);
sexp car(sexp obj);
sexp cdr(sexp obj);
sexp set_car(sexp obj, sexp val);
sexp set_cdr(sexp obj, sexp val);
sexp sexp_cons(sexp head, sexp tail);
sexp sexp_car(sexp obj);
sexp sexp_cdr(sexp obj);
sexp sexp_set_car(sexp obj, sexp val);
sexp sexp_set_cdr(sexp obj, sexp val);
int listp(sexp obj);
int list_index(sexp ls, sexp elt);
sexp lset_diff(sexp a, sexp b);
sexp reverse(sexp ls);
sexp nreverse(sexp ls);
sexp append(sexp a, sexp b);
sexp list(int count, ...);
sexp memq(sexp x, sexp ls);
sexp assq (sexp x, sexp ls);
unsigned long length(sexp ls);
sexp make_string(char *str);
sexp make_flonum(double f);
int string_hash(char *str, int acc);
sexp intern(char *str);
sexp make_vector(unsigned long len, sexp dflt);
sexp list_to_vector(sexp ls);
sexp vector(int count, ...);
void write_sexp(FILE *out, sexp obj);
void free_sexp(sexp obj);
char* read_string(FILE *in);
char* read_symbol(FILE *in, int init);
sexp read_number(FILE *in, int base);
sexp read_sexp_raw(FILE *in);
sexp read_sexp(FILE *in);
int sexp_listp(sexp obj);
int sexp_list_index(sexp ls, sexp elt);
sexp sexp_lset_diff(sexp a, sexp b);
sexp sexp_reverse(sexp ls);
sexp sexp_nreverse(sexp ls);
sexp sexp_append(sexp a, sexp b);
sexp sexp_list(int count, ...);
sexp sexp_memq(sexp x, sexp ls);
sexp sexp_assq(sexp x, sexp ls);
unsigned long sexp_length(sexp ls);
sexp sexp_make_string(char *str);
sexp sexp_make_flonum(double f);
int sexp_string_hash(char *str, int acc);
sexp sexp_intern(char *str);
sexp sexp_make_vector(unsigned long len, sexp dflt);
sexp sexp_list_to_vector(sexp ls);
sexp sexp_vector(int count, ...);
void sexp_write(sexp obj, sexp out);
void sexp_free(sexp obj);
char* sexp_read_string(sexp in);
char* sexp_read_symbol(sexp in, int init);
sexp sexp_read_number(sexp in, int base);
sexp sexp_read_raw(sexp in);
sexp sexp_read(sexp in);
sexp sexp_make_input_port(FILE* in);
sexp sexp_make_output_port(FILE* out);
sexp sexp_make_input_string_port(sexp str);
sexp sexp_make_output_string_port();
sexp sexp_get_output_string(sexp port);
void sexp_init();
#endif /* ! SEXP_H */