lake/dlist.c
Sami Samhuri 54831a34fa added eval and apply, lambdas, other goodies
- add booleans, dotted lists, functions
- proper lexical scope
- special forms: define, set!, and, or, quote, lambda
- added some basic list manipulations
- print functions and dotted lists
- removed nil
- more robust in general
2011-04-20 01:38:53 -07:00

50 lines
1 KiB
C

/**
* dlist.c
* Lake Scheme
*
* Copyright 2011 Sami Samhuri
* MIT License
*
*/
#include "dlist.h"
#include "lake.h"
static LakeDottedList *dlist_alloc(void)
{
LakeDottedList *dlist = g_malloc(sizeof(LakeDottedList));
VAL(dlist)->type = TYPE_DLIST;
VAL(dlist)->size = sizeof(LakeDottedList);
return dlist;
}
LakeDottedList *dlist_make(LakeList *head, LakeVal *tail)
{
LakeDottedList *dlist = dlist_alloc();
dlist->head = head;
dlist->tail = tail;
return dlist;
}
char *dlist_repr(LakeDottedList *dlist)
{
GString *s = g_string_new("(");
int i;
char *s2;
if (dlist->head) {
for (i = 0; i < LIST_N(dlist->head); ++i) {
s2 = repr(LIST_VAL(dlist->head, i));
g_string_append(s, s2);
g_free(s2);
if (i != LIST_N(dlist->head) - 1) g_string_append(s, " ");
}
}
g_string_append(s, " . ");
s2 = repr(dlist->tail);
g_string_append(s, s2);
g_free(s2);
g_string_append(s, ")");
gchar *repr = s->str;
g_string_free(s, FALSE); /* don't free char data */
return repr;
}