-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.c
63 lines (54 loc) · 1.22 KB
/
atom.c
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
#include "atom.h"
#include "exception.h"
#include <malloc.h>
Atom* nil;
Atom* tAtom;
Atom* fAtom;
Atom* car(Atom* x) {
if (x->type == atomPair) {
return x->data.child[0];
}
else {
exception("car", "Not a pair");
return nil;
}
}
Atom* cdr(Atom* x) {
if (x->type == atomPair) {
return x->data.child[1];
}
else {
exception("cdr", "Not a pair");
return nil;
}
}
Atom* cons(Atom* x, Atom* y) {
Atom* result = (Atom*) malloc(sizeof(Atom));
result->type = atomPair;
result->data.child[0] = x;
result->data.child[1] = y;
return result;
}
Atom* createReservedSymbol(char* symbol) {
Atom* result = (Atom*) malloc(sizeof(Atom));
result->type = atomSymbol;
result->data.symbol = createSymbolFromStr(symbol);
return result;
}
Atom* createNumber(Number number) {
Atom* result = (Atom*) malloc(sizeof(Atom));
result->type = atomNumber;
result->data.number = number;
return result;
}
Atom* createSymbol(Symbol symbol) {
Atom* result = (Atom*) malloc(sizeof(Atom));
result->type = atomSymbol;
result->data.symbol = symbol;
return result;
}
void installAtomPackage() {
nil = createReservedSymbol("nil");
tAtom = createReservedSymbol("#t");
fAtom = createReservedSymbol("#f");
}