-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcons.hpp
executable file
·190 lines (168 loc) · 4.06 KB
/
cons.hpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* \file cons.hpp
*
* Encapsulates an abstract interface layer for a cons list ADT,
* without using member functions. Makes no assumptions about what
* kind of concrete type Cell will be defined to be.
*/
#ifndef CONS_HPP
#define CONS_HPP
#include "Cell.hpp"
#include <string>
#include <iostream>
/**
* \brief The null pointer value.
*/
extern Cell* const nil;
/**
* \brief Make an int cell.
* \param i The initial int value to be stored in the new cell.
*/
inline Cell* make_int(const int i)
{
return (Cell*) new IntCell(i);
}
/**
* \brief Make a double cell.
* \param d The initial double value to be stored in the new cell.
*/
inline Cell* make_double(const double d)
{
return (Cell*) new DoubleCell(d);
}
/**
* \brief Make a symbol cell.
* \param s The initial symbol name to be stored in the new cell.
*/
inline Cell* make_symbol(const char* const s)
{
return (Cell*) new SymbolCell(s);
}
/**
* \brief Make a conspair cell.
* \param my_car The initial car pointer to be stored in the new cell.
* \param my_cdr The initial cdr pointer to be stored in the new cell.
*/
inline Cell* cons(Cell* const my_car, Cell* const my_cdr)
{
return (Cell*) new ConsCell(my_car, my_cdr);
}
/**
* \brief Make a procedure cell.
* \param my_args Arguments
* \param my_body Body or in other words, the implementation
*/
inline Cell* lambda(Cell* const my_args, Cell* const my_body)
{
return (Cell*) new ProcedureCell(my_args, my_body);
}
/**
* \brief Check if c points to an empty list, i.e., is a null pointer.
* \return True iff c points to an empty list, i.e., is a null pointer.
*/
inline bool nullp(Cell* const c)
{
return (c == nil);
}
/**
* \brief Check if c points to a list (i.e., nil or a cons cell).
* \return True iff c points to a list (i.e., nil or a cons cell).
*/
inline bool listp(Cell* const c)
{
return nullp(c) || c->is_cons();
}
/**
* \brief Check if c points to an int cell.
* \return True iff c points to an int cell.
*/
inline bool intp(Cell* const c)
{
return !nullp(c) && c->is_int();
}
/**
* \brief Check if c points to a double cell.
* \return True iff c points to a double cell.
*/
inline bool doublep(Cell* const c)
{
return !nullp(c) && c->is_double();
}
/**
* \brief Check if c points to a symbol cell.
* \return True iff c points to a symbol cell.
*/
inline bool symbolp(Cell* const c)
{
return !nullp(c) && c->is_symbol();
}
/**
* \brief Accessor (error if c is not an int cell).
* \return The value in the int cell pointed to by c.
*/
inline int get_int(Cell* const c)
{
return c->get_int();
}
/**
* \brief Accessor (error if c is not a double cell).
* \return The value in the double cell pointed to by c.
*/
inline double get_double(Cell* const c)
{
return c->get_double();
}
/**
* \brief Retrieve the symbol name as a string (error if c is not a
* symbol cell).
* \return The symbol name in the symbol cell pointed to by c.
*/
inline std::string get_symbol(Cell* const c)
{
return c->get_symbol();
}
/**
* \brief Accessor (error if c is not a cons cell).
* \return The car pointer in the cons cell pointed to by c.
*/
inline Cell* car(Cell* const c)
{
return c->get_car();
}
/**
* \brief Accessor (error if c is not a string cell).
* \return The cdr pointer in the cons cell pointed to by c.
*/
inline Cell* cdr(Cell* const c)
{
return c->get_cdr();
}
/**
* \brief Accessor (error if c is not a procedure cell).
* \return Pointer to the cons list of formal parameters for the function
* pointed to by c.
*/
inline Cell* get_formals(Cell* const c)
{
return c->get_formals();
}
/**
* \brief Accessor (error if c is not a procedure cell).
* \return Pointer to the cons list containing the expression defining the
* body for the function pointed to by c.
*/
inline Cell* get_body(Cell* const c)
{
return c->get_body();
}
/**
* \brief Print the subtree rooted at c, in s-expression notation.
* \param os The output stream to print to.
* \param c The root cell of the subtree to be printed.
*/
inline std::ostream& operator<<(std::ostream& os, const Cell& c)
{
c.print(os);
return os;
}
#endif // CONS_HPP