-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLispNode.cpp
106 lines (88 loc) · 2.13 KB
/
LispNode.cpp
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
#include "LispNode.h"
#include "extra.h"
LispNode::LispNode(LispType type): type{type}, head{nullptr} {
}
LispNode::~LispNode() {
if(type == LispType::AtomPure || type == LispType::AtomBoolean || type == LispType::AtomString) {
free(string);
}
if(type == LispType::List) {
head = nullptr;
}
}
bool LispNode::operator==(const LispNode &other) const {
switch(type) {
case AtomPure:
case AtomBoolean:
case AtomString:
return (strcmp(string, other.string) == 0);
case AtomCharacter:
case AtomNumericIntegral:
return (number_i == other.number_i);
case AtomNumericReal:
return (number_r == other.number_r);
case List:
return (this == &other);
default:
return false;
}
}
bool LispNode::is_atom() {
return (type != LispType::List);
}
bool LispNode::is_list() {
return (type == LispType::List);
}
bool LispNode::is_pure() {
return (!is_boolean() && !is_numeric() && !is_string() && !is_character());
}
bool LispNode::is_boolean() {
return (type == LispType::AtomBoolean);
}
bool LispNode::is_string() {
return (type == LispType::AtomString);
}
bool LispNode::is_character() {
return (type == LispType::AtomCharacter);
}
bool LispNode::is_numeric() {
return (type == LispType::AtomNumericIntegral || type == LispType::AtomNumericReal);
}
bool LispNode::is_numeric_integral() {
return (type == LispType::AtomNumericIntegral);
}
bool LispNode::is_numeric_real() {
return (type == LispType::AtomNumericReal);
}
void LispNode::print() {
switch(type) {
case AtomPure:
case AtomBoolean:
fputs(string, stdout);
break;
case AtomString:
fputs("\"", stdout);
fputs(string, stdout);
fputs("\"", stdout);
break;
case AtomCharacter:
fputs("#\\", stdout);
fputc(static_cast<int>(number_i), stdout);
break;
case AtomNumericIntegral:
print_integral(number_i);
break;
case AtomNumericReal:
print_real(number_r);
break;
case List:
fputs("(", stdout);
for(Box *current = get_head_pointer(); current != nullptr; current = current->get_next_pointer()) {
current->item->print();
if(current->next != nullptr) {
fputs(" ", stdout);
}
}
fputs(")", stdout);
}
}