-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.c
118 lines (114 loc) · 2.9 KB
/
generic.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
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
#include "generic.h"
void print_value(struct Value *v)
{
switch (v->type)
{
case NIL:
print_nil();
return;
case PAIR:
print_pair((struct Pair*) v->value);
return;
case SYMBOL:
print_symbol((Symbol) v->value);
return;
case ENVIROMENT:
print_enviroment((struct Enviroment*) v->value);
return;
case BOOLEAN:
print_boolean((int*) v->value);
return;
case CLOSURE:
print_closure((struct Closure*) v->value);
return;
case INTRINSIC:
print_intrinsic();
return;
case INTEGER:
print_integer((int*) v->value);
return;
case CHARACTER:
print_character((struct Character*) v->value);
return;
case OPERATOR:
print_operator((struct Operator*) v->value);
break;
default:
fprintf(stderr, "print_value: unkown type\n");
}
}
struct Value *equal(struct Value *a, struct Value *b)
{
if (a == b)
return boolean(1);
if (!a || !b)
return boolean(0);
if (a->type != b->type)
return boolean(0);
switch (a->type)
{
case NIL:
return boolean(1);
case PAIR:
return equal_pair((struct Pair*) a->value, (struct Pair*) b->value);
case SYMBOL:
return equal_symbol((Symbol) a->value, (Symbol) b->value);
case ENVIROMENT:
return equal_enviroment((struct Enviroment*) a->value,
(struct Enviroment*) b->value);
case BOOLEAN:
return equal_boolean((int*) a->value, (int*) b->value);
case CLOSURE:
return equal_closure((struct Closure*) a->value,
(struct Closure*) b->value);
case INTRINSIC:
return equal_intrinsic((struct Intrinsic*) a->value,
(struct Intrinsic*) b->value);
case INTEGER:
return equal_integer((int*) a->value, (int*) b->value);
case CHARACTER:
return equal_character((struct Character*) a->value, (struct Character*) b->value);
case OPERATOR:
return equal_operator((struct Operator*) a->value,
(struct Operator*) b->value);
default:
fprintf(stderr, "equal: unkown type\n");
return 0;
}
}
void collect(struct Value *v)
{
if (!v)
return;
switch (v->type)
{
case PAIR:
free_pair((struct Pair*) v->value);
break;
case SYMBOL:
free_symbol((Symbol) v->value);
break;
case ENVIROMENT:
free_enviroment((struct Enviroment*) v->value);
break;
case CLOSURE:
free_closure((struct Closure*) v->value);
break;
case INTRINSIC:
free_intrinsic((struct Intrinsic*) v->value);
break;
case INTEGER:
free_integer((int*) v->value);
break;
case CHARACTER:
free_character((struct Character*) v->value);
break;
case OPERATOR:
free_operator((struct Operator*) v->value);
break;
case NIL:
case BOOLEAN:
return;
}
free_value(v);
}