aboutsummaryrefslogtreecommitdiff
path: root/atom.c
blob: cbcdbe84aa68dd7f1d998e7dbaf58e6c37051f87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "atom.h"
#include "list.h"

#include <stdlib.h>
#include <string.h>

struct atom true_atom = { ATOM_TRUE };
struct atom false_atom = { ATOM_FALSE };
struct atom nil_atom = { ATOM_NIL } ;

struct atom *atom_new(char type)
{
    struct atom *atom = calloc(1, sizeof(*atom));
    atom->type = type;
    return atom;
}

struct atom *atom_new_int(long l)
{
    struct atom *atom = atom_new(ATOM_INT);
    atom->l = l;
    return atom;
}

struct atom *atom_new_str(const char *str, int len)
{
    struct atom *atom = atom_new(ATOM_STR);
    atom->str.str = strndup(str, len);
    atom->str.len = len;
    return atom;
}

struct atom *atom_new_sym(const char *sym, int len)
{
    struct atom *atom = atom_new_str(sym, len);
    atom->type = ATOM_SYMBOL;
    return atom;
}

struct atom *atom_new_list(struct list *list)
{
    struct atom *atom = atom_new(ATOM_LIST);
    atom->list = list;
    return atom;
}

struct atom *atom_clone(struct atom *atom)
{
    switch (atom->type)
    {
    case ATOM_NIL:
    case ATOM_TRUE:
    case ATOM_FALSE:
        return atom;

    case ATOM_INT:
        return atom_new_int(atom->l);

    case ATOM_STR:
        return atom_new_str(atom->str.str, atom->str.len);

    case ATOM_SYMBOL:
        return atom_new_sym(atom->str.str, atom->str.len);

    case ATOM_LIST:
    {
        struct list *list = atom->list;
        struct list *clone = NULL, *last = NULL;

        while (list)
        {
            struct atom *a = LIST_GET_ATOM(list);
            struct atom *b = atom_clone(a);
            last = list_append(last, b);
            list = list->next;
            if (!clone)
                clone = last;
        }

        return atom_new_list(clone);
    }
    }

    return NULL;
}

#ifdef BUILD_TEST

#include "test_util.h"

TEST(atom_new)
{
    struct atom *atom = atom_new(ATOM_STR);
    ASSERT_TRUE(atom != NULL);
    ASSERT_EQ(ATOM_STR, atom->type);
}

TEST(atom_new_int)
{
    struct atom *atom = atom_new_int(42);
    ASSERT_TRUE(atom != NULL);
    ASSERT_EQ(42, atom->l);
}

TEST(atom_new_str)
{
    struct atom *atom = atom_new_str("foobar", 6);
    ASSERT_TRUE(atom != NULL);
    ASSERT_STREQ("foobar", atom->str.str);

    atom = atom_new_str("foobar", 3);
    ASSERT_TRUE(atom != NULL);
    ASSERT_STREQ("foo", atom->str.str);

    ASSERT_EQ(ATOM_STR, atom->type);
}

TEST(atom_new_sym)
{
    struct atom *atom = atom_new_sym("foobar", 6);
    ASSERT_TRUE(atom != NULL);
    ASSERT_STREQ("foobar", atom->str.str);

    atom = atom_new_sym("foobar", 3);
    ASSERT_TRUE(atom != NULL);
    ASSERT_STREQ("foo", atom->str.str);

    ASSERT_EQ(ATOM_SYMBOL, atom->type);
}

TEST(atom_new_list)
{
    struct list list;
    struct atom *atom = atom_new_list(&list);
    ASSERT_TRUE(atom != NULL);
    ASSERT_EQ(&list, atom->list);
    ASSERT_EQ(ATOM_LIST, atom->type);
}

#endif