microScheme
cons.hpp
Go to the documentation of this file.
1 
9 #ifndef CONS_HPP
10 #define CONS_HPP
11 
12 #include <iostream>
13 #include "Cell.hpp"
14 
15 using namespace std;
16 
20 extern Cell* const nil;
21 
26 inline Cell* make_int(const int i)
27 {
28  return new IntCell(i);
29 }
30 
35 inline Cell* make_double(const double d)
36 {
37  return new DoubleCell(d);
38 }
39 
44 inline Cell* make_symbol(const char* const s)
45 {
46  return new SymbolCell(s);
47 }
48 
54 inline Cell* cons(Cell* const my_car, Cell* const my_cdr)
55 {
56  return new ConsCell(my_car, my_cdr);
57 }
58 
64 inline Cell* lambda(Cell* const my_formals, Cell* const my_body)
65 {
66  return new ProcedureCell(my_formals, my_body);
67 }
68 
73 inline bool nullp(Cell* const c)
74 {
75  return (c == nil);
76 }
77 
82 inline bool listp(Cell* const c)
83 {
84  return nullp(c) || c->is_cons();
85 }
86 
91 inline bool procedurep(Cell* const c)
92 {
93  return !nullp(c) && c->is_procedure();
94 }
95 
100 inline bool intp(Cell* const c)
101 {
102  return !nullp(c) && c->is_int();
103 }
104 
109 inline bool doublep(Cell* const c)
110 {
111  return !nullp(c) && c->is_double();
112 }
113 
118 inline bool symbolp(Cell* const c)
119 {
120  return !nullp(c) && c->is_symbol();
121 }
122 
127 inline int get_int(Cell* const c)
128 {
129  return c->get_int();
130 }
131 
136 inline double get_double(Cell* const c)
137 {
138  return c->get_double();
139 }
140 
146 inline string get_symbol(Cell* const c)
147 {
148  return c->get_symbol();
149 }
150 
155 inline Cell* car(Cell* const c)
156 {
157  return c->get_car();
158 }
159 
164 inline Cell* cdr(Cell* const c)
165 {
166  return c->get_cdr();
167 }
168 
174 inline Cell* get_formals(Cell* const c)
175 {
176  return c->get_formals();
177 }
178 
184 inline Cell* get_body(Cell* const c)
185 {
186  return c->get_body();
187 }
188 
194 inline ostream& operator<<(ostream& os, const Cell& c)
195 {
196  c.print(os);
197  return os;
198 }
199 
200 #endif // CONS_HPP
bool listp(Cell *const c)
Check if c points to a list (i.e., nil or a cons cell).
Definition: cons.hpp:82
Cell * make_int(const int i)
Make an int cell.
Definition: cons.hpp:26
Cell * make_double(const double d)
Make a double cell.
Definition: cons.hpp:35
double get_double(Cell *const c)
Accessor (error if c is not a double cell).
Definition: cons.hpp:136
Cell * make_symbol(const char *const s)
Make a symbol cell.
Definition: cons.hpp:44
int get_int(Cell *const c)
Accessor (error if c is not an int cell).
Definition: cons.hpp:127
string get_symbol(Cell *const c)
Retrieve the symbol name as a string (error if c is not a symbol cell).
Definition: cons.hpp:146
bool intp(Cell *const c)
Check if c points to an int cell.
Definition: cons.hpp:100
Cell * cdr(Cell *const c)
Accessor (error if c is not a string cell).
Definition: cons.hpp:164
ostream & operator<<(ostream &os, const Cell &c)
Print the subtree rooted at c, in s-expression notation.
Definition: cons.hpp:194
bool nullp(Cell *const c)
Check if c points to an empty list, i.e., is a null pointer.
Definition: cons.hpp:73
Cell *const nil
The null pointer value.
Cell * get_formals(Cell *const c)
Accessor (error if c is not a procedure cell).
Definition: cons.hpp:174
bool symbolp(Cell *const c)
Check if c points to a symbol cell.
Definition: cons.hpp:118
Cell * get_body(Cell *const c)
Accessor (error if c is not a procedure cell).
Definition: cons.hpp:184
Cell * lambda(Cell *const my_formals, Cell *const my_body)
Make a procedure cell.
Definition: cons.hpp:64
Cell * cons(Cell *const my_car, Cell *const my_cdr)
Make a conspair cell.
Definition: cons.hpp:54
bool procedurep(Cell *const c)
Check if c is a procedure cell.
Definition: cons.hpp:91
Cell * car(Cell *const c)
Accessor (error if c is not a cons cell).
Definition: cons.hpp:155
bool doublep(Cell *const c)
Check if c points to a double cell.
Definition: cons.hpp:109