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 
63 inline bool nullp(Cell* const c)
64 {
65  return (c == nil);
66 }
67 
72 inline bool listp(Cell* const c)
73 {
74  return nullp(c) || c->is_cons();
75 }
76 
81 inline bool intp(Cell* const c)
82 {
83  return !nullp(c) && c->is_int();
84 }
85 
90 inline bool doublep(Cell* const c)
91 {
92  return !nullp(c) && c->is_double();
93 }
94 
99 inline bool symbolp(Cell* const c)
100 {
101  return !nullp(c) && c->is_symbol();
102 }
103 
108 inline int get_int(Cell* const c)
109 {
110  return c->get_int();
111 }
112 
117 inline double get_double(Cell* const c)
118 {
119  return c->get_double();
120 }
121 
127 inline string get_symbol(Cell* const c)
128 {
129  return c->get_symbol();
130 }
131 
136 inline Cell* car(Cell* const c)
137 {
138  return c->get_car();
139 }
140 
145 inline Cell* cdr(Cell* const c)
146 {
147  return c->get_cdr();
148 }
149 
155 inline ostream& operator<<(ostream& os, const Cell& c)
156 {
157  c.print(os);
158  return os;
159 }
160 
161 #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:72
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:117
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:108
string get_symbol(Cell *const c)
Retrieve the symbol name as a string (error if c is not a symbol cell).
Definition: cons.hpp:127
bool intp(Cell *const c)
Check if c points to an int cell.
Definition: cons.hpp:81
Cell * cdr(Cell *const c)
Accessor (error if c is not a string cell).
Definition: cons.hpp:145
ostream & operator<<(ostream &os, const Cell &c)
Print the subtree rooted at c, in s-expression notation.
Definition: cons.hpp:155
bool nullp(Cell *const c)
Check if c points to an empty list, i.e., is a null pointer.
Definition: cons.hpp:63
Cell *const nil
The null pointer value.
bool symbolp(Cell *const c)
Check if c points to a symbol cell.
Definition: cons.hpp:99
Cell * cons(Cell *const my_car, Cell *const my_cdr)
Make a conspair cell.
Definition: cons.hpp:54
Cell * car(Cell *const c)
Accessor (error if c is not a cons cell).
Definition: cons.hpp:136
bool doublep(Cell *const c)
Check if c points to a double cell.
Definition: cons.hpp:90