Skip to content

Commit

Permalink
removed duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
kimtg committed Mar 4, 2016
1 parent 2950b0c commit 2e2f62c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 115 deletions.
174 changes: 60 additions & 114 deletions arc.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void stack_restore_add(int saved_size, atom a) {
}

void consider_gc() {
if (alloc_count > 4 * alloc_count_old)
gc();
if (alloc_count > 4 * alloc_count_old)
gc();
}

atom cons(atom car_val, atom cdr_val)
Expand Down Expand Up @@ -2438,130 +2438,76 @@ error eval_expr(atom expr, atom env, atom *result)

p = &cdr(*p);
}
/* tail call optimization of err = apply(op, args, result); */
{

if (op.type == T_CLOSURE) {
/* tail call optimization of err = apply(op, args, result); */
atom fn = op;
atom env2, arg_names, body;

if (fn.type == T_BUILTIN) {
err = (*fn.value.builtin)(args, result);
stack_restore_add(ss, *result);
return err;
}
else if (fn.type == T_CLOSURE) {
env2 = env_create(car(fn));
arg_names = car(cdr(fn));
body = cdr(cdr(fn));

/* Bind the arguments */
while (!no(arg_names)) {
if (arg_names.type == T_SYM) {
env_assign(env2, arg_names.value.symbol, args);
args = nil;
break;
}
atom arg_name = car(arg_names);
if (arg_name.type == T_SYM) {
if (no(args)) {/* missing argument */
stack_restore(ss);
return ERROR_ARGS;
}
env_assign(env2, arg_name.value.symbol, car(args));
args = cdr(args);
}
else { /* (o ARG [DEFAULT]) */
atom val;
if (no(args)) { /* missing argument */
if (no(cdr(cdr(arg_name))))
val = nil;
else {
error err = eval_expr(car(cdr(cdr(arg_name))), env2, &val);
if (err) {
stack_restore(ss);
return err;
}
}
} else {
val = car(args);
args = cdr(args);
}
env_assign(env2, car(cdr(arg_name)).value.symbol, val);
}
arg_names = cdr(arg_names);
}
if (!no(args)) {
stack_restore(ss);
return ERROR_ARGS;
env2 = env_create(car(fn));
arg_names = car(cdr(fn));
body = cdr(cdr(fn));

/* Bind the arguments */
while (!no(arg_names)) {
if (arg_names.type == T_SYM) {
env_assign(env2, arg_names.value.symbol, args);
args = nil;
break;
}

/* Evaluate the body */
while (!no(body)) {
if (no(cdr(body))) {
/* tail call */
expr = car(body);
env = env2;
goto start_eval;
}
error err = eval_expr(car(body), env2, result);
if (err) {
atom arg_name = car(arg_names);
if (arg_name.type == T_SYM) {
if (no(args)) {/* missing argument */
stack_restore(ss);
return err;
return ERROR_ARGS;
}
body = cdr(body);
env_assign(env2, arg_name.value.symbol, car(args));
args = cdr(args);
}
stack_restore_add(ss, *result);
return ERROR_OK;
}
else if (fn.type == T_CONTINUATION) {
if (len(args) != 1) { stack_restore(ss); return ERROR_ARGS; }
thrown = car(args);
longjmp(*fn.value.jb, 1);
}
else if (fn.type == T_STRING) { /* implicit indexing for string */
if (len(args) != 1) { stack_restore(ss); return ERROR_ARGS; }
long index = (long)(car(args)).value.number;
*result = make_char(fn.value.str->value[index]);
stack_restore_add(ss, *result);
return ERROR_OK;
}
else if (fn.type == T_CONS && listp(fn)) { /* implicit indexing for list */
if (len(args) != 1) { stack_restore(ss); return ERROR_ARGS; }
long index = (long)(car(args)).value.number;
atom a = fn;
long i;
for (i = 0; i < index; i++) {
a = cdr(a);
if (no(a)) {
*result = nil;
stack_restore(ss);
return ERROR_OK;
else { /* (o ARG [DEFAULT]) */
atom val;
if (no(args)) { /* missing argument */
if (no(cdr(cdr(arg_name))))
val = nil;
else {
error err = eval_expr(car(cdr(cdr(arg_name))), env2, &val);
if (err) {
stack_restore(ss);
return err;
}
}
} else {
val = car(args);
args = cdr(args);
}
env_assign(env2, car(cdr(arg_name)).value.symbol, val);
}
*result = car(a);
stack_restore_add(ss, *result);
return ERROR_OK;
arg_names = cdr(arg_names);
}
else if (fn.type == T_TABLE) { /* implicit indexing for table */
long len1 = len(args);
if (len1 != 1 && len1 != 2) { stack_restore(ss); return ERROR_ARGS; }
atom *pkey = &car(args);
struct table_entry *pair = table_get(fn.value.table, *pkey);
if (pair) {
*result = pair->v;
if (!no(args)) {
stack_restore(ss);
return ERROR_ARGS;
}

/* Evaluate the body */
while (!no(body)) {
if (no(cdr(body))) {
/* tail call */
expr = car(body);
env = env2;
goto start_eval;
}
else {
if (len1 == 2) /* default value is specified */
*result = car(cdr(args));
else
*result = nil;
error err = eval_expr(car(body), env2, result);
if (err) {
stack_restore(ss);
return err;
}
stack_restore_add(ss, *result);
return ERROR_OK;
}
else {
stack_restore(ss);
return ERROR_TYPE;
body = cdr(body);
}
stack_restore_add(ss, *result);
return ERROR_OK;
}
else {
err = apply(op, args, result);
}
stack_restore_add(ss, *result);
return err;
Expand Down
2 changes: 1 addition & 1 deletion arc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef _INC_ARC
#define _INC_ARC

#define VERSION "0.10"
#define VERSION "0.10.1"

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
Expand Down

0 comments on commit 2e2f62c

Please sign in to comment.