Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ This file is loaded at startup, so one can recall previous commands

Known bugs:
* recall of multiline commands does not work as expected.
* this doesn't have tail call optimization, and each call uses a good deal of
the C stack. A recursion deeper than a few thousand calls stops with the error
"Stack overflow: the recursion is too deep".
* a recursion which is not a tail call (such as `(+ 1 (f (- n 1)))`) uses the C stack:
deeper than about 25000 calls it stops with the error "Stack overflow: the
recursion is too deep".

Original README (completed)
===============
Expand Down Expand Up @@ -283,9 +283,21 @@ is returned (`()` if there is none).
`()`. This is the only loop supported by MiniLisp.

If you are familiar with Scheme, you might be wondering if you could write a
loop by tail recursion in MiniLisp. The answer is no. Tail calls consume stack
space in MiniLisp, so a loop written as recursion will fail with the "Stack
overflow" error after a few thousand iterations.
loop by tail recursion in MiniLisp. The answer is yes: a call in tail position
does not consume any stack space. This is the case of the last expression of the
body of a function, of `progn`, of the branches of `if`, and of whatever a macro
expands to such an expression. So a loop can be written as a recursion with
an accumulator, and run for as long as needed:

(defun sum (n acc)
(if (= n 0)
acc
(sum (- n 1) (+ n acc)))) ; tail call
(sum 1000000 0) ; -> 500000500000

A recursion which is not a tail call, such as `(+ 1 (f (- n 1)))`, still needs
a stack frame for each level and fails with the "Stack overflow" error after
about 25000 calls.

### Imperative programming

Expand Down
5 changes: 4 additions & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,14 @@ bool gc_collect(void *root, size_t new_size) {
while (scan1 < scan2) {
switch (scan1->type) {
case TINT:
case TSYMBOL:
case TPRIMITIVE:
case TSTRING:
// Any of the above types does not contain a pointer to a GC-managed object.
break;
case TSYMBOL:
if (scan1->bind)
scan1->bind = forward(scan1->bind);
break;
case TCELL:
scan1->car = forward(scan1->car);
scan1->cdr = forward(scan1->cdr);
Expand Down
9 changes: 9 additions & 0 deletions src/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ static inline void** add_root_frame(void **prev_frame, int size, void *frame[])
Obj **var3 = (Obj **)(root_frame + 3); \
Obj **var4 = (Obj **)(root_frame + 4);

#define DEFINE5(prev_frame, var1, var2, var3, var4, var5) \
void *root_frame[7]; \
prev_frame = add_root_frame(prev_frame, 5, root_frame); \
Obj **var1 = (Obj **)(root_frame + 1); \
Obj **var2 = (Obj **)(root_frame + 2); \
Obj **var3 = (Obj **)(root_frame + 3); \
Obj **var4 = (Obj **)(root_frame + 4); \
Obj **var5 = (Obj **)(root_frame + 5);


void gc_init(void);
Obj *alloc(void *root, int type, size_t size);
Expand Down
189 changes: 136 additions & 53 deletions src/minilisp.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ static Obj *cons(void *root, Obj **car, Obj **cdr) {
}

static Obj *make_symbol(void *root, const char *name) {
Obj *sym = alloc(root, TSYMBOL, strlen(name) + 1);
strcpy(sym->name, name);
Obj *sym = alloc(root, TSYMBOL, sizeof(Obj *) + strlen(name) + 1);
sym->bind = NULL;
strcpy(sym->sym_name, name);
sym->line_num = filepos.line_num;
return sym;
}
Expand Down Expand Up @@ -241,7 +242,7 @@ static Obj *read_list(void *root) {
// but return the existing one.
static Obj *intern(void *root, const char *name) {
for (Obj *p = Symbols; p != Nil; p = p->cdr)
if (strcmp(name, p->car->name) == 0)
if (strcmp(name, p->car->sym_name) == 0)
return p->car;
DEFINE1(root, sym);
*sym = make_symbol(root, name);
Expand Down Expand Up @@ -360,7 +361,7 @@ static void print(Obj *obj) {

case TINT : printf("%lld", obj->value);
break;
case TSYMBOL: fputs(obj->name, stdout);
case TSYMBOL: fputs(obj->sym_name, stdout);
break;
case TPRIMITIVE: fputs("<primitive>", stdout);
break;
Expand Down Expand Up @@ -403,8 +404,24 @@ static int length(Obj *list) {

static Obj *eval(void *root, Obj **env, Obj **obj);

// The global environment is the frame at the top of the chain of frames, the only one which has no
// parent. Its variables are not in the frame itself, they are stored in the symbols.
static inline bool is_global(Obj *env) {
return env->up == Nil;
}

static void add_variable(void *root, Obj **env, Obj **sym, Obj **val) {
DEFINE2(root, vars, tmp);
if (is_global(*env)) {
// Redefining a variable just changes its value: the symbol is the only way to reach it.
if ((*sym)->bind) {
(*sym)->bind->cdr = *val;
} else {
*tmp = cons(root, sym, val);
(*sym)->bind = *tmp;
}
return;
}
*vars = (*env)->vars;
*tmp = acons(root, sym, val, vars);
(*env)->vars = *tmp;
Expand Down Expand Up @@ -459,40 +476,37 @@ static bool is_list(Obj *obj) {
return obj == Nil || obj->type == TCELL;
}

static Obj *apply_func(void *root, Obj **env, Obj **fn, Obj **args) {
DEFINE3(root, params, newenv, body);
// Returns the new environment frame in which the body of the function fn is evaluated: the
// parameters are bound to the (already evaluated) arguments. The args slot is modified.
static Obj *bind_args(void *root, Obj **fn, Obj **args) {
DEFINE2(root, params, newenv);
*params = (*fn)->params;
*newenv = (*fn)->env;
*newenv = push_env(root, newenv, params, args);
*body = (*fn)->body;
return progn(root, newenv, body);
return push_env(root, newenv, params, args);
}

// Apply fn with args.
static Obj *apply(void *root, Obj **env, Obj **fn, Obj **args) {
if (!is_list(*args))
error("argument must be a list", (*args)->line_num);
if ((*fn)->type == TPRIMITIVE)
return (*fn)->fn(root, env, args);
if ((*fn)->type == TFUNCTION) {
DEFINE1(root, eargs);
*eargs = eval_list(root, env, args);
return apply_func(root, env, fn, eargs);
}
error("not supported", (*args)->line_num);
return Nil; //fix warning
// Calls the function or macro fn with the (already evaluated, for a function) arguments, and
// returns the value of its body. This is not a tail call: used for the macros expansion.
static Obj *apply_func(void *root, Obj **env, Obj **fn, Obj **args) {
DEFINE2(root, newenv, body);
*newenv = bind_args(root, fn, args);
*body = (*fn)->body;
return progn(root, newenv, body);
}

// Searches for a variable by symbol. Returns null if not found.
static Obj *find(Obj **env, Obj *sym) {
for (Obj *p = *env; p != Nil; p = p->up) { // search all environments
// Search the local environments first...
Obj *p = *env;
for (; !is_global(p); p = p->up) {
for (Obj *cell = p->vars; cell != Nil; cell = cell->cdr) {
Obj *bind = cell->car;
if (sym == bind->car)
return bind;
}
}
return NULL;
// ...then the global one, which is stored in the symbol.
return sym->bind;
}

// Expands the given macro application form.
Expand All @@ -508,54 +522,123 @@ static Obj *macroexpand(void *root, Obj **env, Obj **obj) {
return apply_func(root, env, macro, args);
}

// Evaluates the S expression.
static Obj *eval(void *root, Obj **env, Obj **obj) {
check_stack((*obj)->line_num);
switch ((*obj)->type) {
// The special forms which are evaluated by eval itself, in order to handle the tail calls.
static Obj *prim_if(void *root, Obj **env, Obj **list);
static Obj *prim_progn(void *root, Obj **env, Obj **list);

// Evaluates the objects which are not function application forms. Returns NULL for a form.
static inline Obj *eval_simple(Obj **env, Obj *obj) {
switch (obj->type) {
case TINT:
case TPRIMITIVE:
case TFUNCTION:
case TTRUE:
case TSTRING:
case TNIL:
// Self-evaluating objects
return *obj;
return obj;
case TSYMBOL: {
// Variable
Obj *bind = find(env, *obj);
if (!bind) {
error("Undefined symbol: %s", (*obj)->line_num, (*obj)->name);
}
Obj *bind = find(env, obj);
if (!bind)
error("Undefined symbol: %s", obj->line_num, obj->sym_name);
return bind->cdr;
}
case TCELL: {
case TCELL:
return NULL;
default:
error("Bug: eval: Unknown tag type: %d", obj->line_num, obj->type);
}
}

// Evaluates a function application form, or anything else that comes in tail position.
//
// The tail calls do not consume stack: the expression in tail position of an "if", of a "progn",
// of the body of a function or of the expansion of a macro is not evaluated by a recursive call.
// Instead, it becomes the current expression and the loop goes on. For that, the current
// expression and environment live in slots owned by this function. The slots of the caller must
// not be modified, because the caller keeps using them.
static Obj *eval_form(void *root, Obj **env, Obj **obj) {
DEFINE5(root, cur, cenv, fn, args, tmp);
*cur = *obj;
*cenv = *env;
for (;;) {
Obj *r = eval_simple(cenv, *cur);
if (r)
return r;

// Function application form
DEFINE3(root, fn, expanded, args);
*args = (*obj)->cdr;
if ((*obj)->car->type == TSYMBOL) {
*args = (*cur)->cdr;
if ((*cur)->car->type == TSYMBOL) {
// Look the head symbol up only once: it is either a macro to expand or the function
// to call.
Obj *bind = find(env, (*obj)->car);
Obj *bind = find(cenv, (*cur)->car);
if (!bind)
error("Undefined symbol: %s", (*obj)->car->line_num, (*obj)->car->name);
error("Undefined symbol: %s", (*cur)->car->line_num, (*cur)->car->sym_name);
*fn = bind->cdr;
if ((*fn)->type == TMACRO) {
*expanded = apply_func(root, env, fn, args);
return eval(root, env, expanded);
}
} else {
*fn = (*obj)->car;
*fn = eval(root, env, fn);
*fn = (*cur)->car;
*fn = eval(root, cenv, fn);
}

if ((*fn)->type == TMACRO) {
// Expand, then evaluate the expansion in place of the form
*cur = apply_func(root, cenv, fn, args);
continue;
}
if ((*fn)->type != TPRIMITIVE && (*fn)->type != TFUNCTION)
error("The head of a list must be a function",
(*fn)->line_num ? (*fn)->line_num : (*obj)->line_num);
return apply(root, env, fn, args);
}
default:
error("Bug: eval: Unknown tag type: %d", (*obj)->line_num, (*obj)->type);
(*fn)->line_num ? (*fn)->line_num : (*cur)->line_num);
if (!is_list(*args))
error("argument must be a list", (*args)->line_num);

if ((*fn)->type == TFUNCTION) {
// Evaluate the arguments, then the body in a new environment
*tmp = eval_list(root, cenv, args);
*cenv = bind_args(root, fn, tmp);
*args = (*fn)->body;
goto sequence;
}

if ((*fn)->fn == prim_if) {
// (if cond then else ...)
if (length(*args) < 2)
error("Malformed if", (*cur)->line_num);
*tmp = (*args)->car;
*tmp = eval(root, cenv, tmp);
if (*tmp != Nil) {
*cur = (*args)->cdr->car;
continue;
}
*args = (*args)->cdr->cdr;
goto sequence;
}
if ((*fn)->fn == prim_progn)
goto sequence;

// The other primitives are not evaluating anything in tail position
return (*fn)->fn(root, cenv, args);

sequence:
// Evaluates the expressions of the list *args. The last one is in tail position.
if (*args == Nil)
return Nil;
while ((*args)->type == TCELL && (*args)->cdr != Nil) {
*tmp = (*args)->car;
eval(root, cenv, tmp);
*args = (*args)->cdr;
}
if ((*args)->type != TCELL)
error("Improper list of expressions", (*args)->line_num);
*cur = (*args)->car;
}
return Nil; // fix warning
}

// Evaluates the S expression.
static Obj *eval(void *root, Obj **env, Obj **obj) {
check_stack((*obj)->line_num);
Obj *r = eval_simple(env, *obj);
return r ? r : eval_form(root, env, obj);
}

//======================================================================
Expand Down Expand Up @@ -615,7 +698,7 @@ static Obj *prim_setq(void *root, Obj **env, Obj **list) {
DEFINE2(root, bind, value);
*bind = find(env, (*list)->car);
if (!*bind)
error("Unbound variable %s", (*list)->line_num, (*list)->car->name);
error("Unbound variable %s", (*list)->line_num, (*list)->car->sym_name);
*value = (*list)->cdr->car;
*value = eval(root, env, value);
(*bind)->cdr = *value;
Expand Down Expand Up @@ -1000,7 +1083,7 @@ static Obj *prim_symbol_to_string(void *root, Obj **env, Obj **list) {

// The name is copied out of the GC heap before allocating
char name[SYMBOL_MAX_LEN + 1];
strncpy(name, args->car->name, sizeof(name) - 1);
strncpy(name, args->car->sym_name, sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
return make_string(root, name);
}
Expand Down
Loading
Loading