Plot Script
expression.hpp
Go to the documentation of this file.
1 
4 #ifndef EXPRESSION_HPP
5 #define EXPRESSION_HPP
6 
7 #include <string>
8 #include <vector>
9 
10 #include "token.hpp"
11 #include "atom.hpp"
12 
13 // forward declare Environment
14 class Environment;
15 
22 class Expression {
23 public:
24 
25  typedef std::vector<Expression>::const_iterator ConstIteratorType;
26 
28  Expression();
29 
33  Expression(const Atom & a);
34 
36  Expression(const Expression & a);
37 
39  Expression & operator=(const Expression & a);
40 
42  Atom & head();
43 
45  const Atom & head() const;
46 
48  void append(const Atom & a);
49 
51  Expression * tail();
52 
54  ConstIteratorType tailConstBegin() const noexcept;
55 
57  ConstIteratorType tailConstEnd() const noexcept;
58 
60  bool isHeadNumber() const noexcept;
61 
63  bool isHeadSymbol() const noexcept;
64 
67 
69  bool operator==(const Expression & exp) const noexcept;
70 
71 private:
72 
73  // the head of the expression
74  Atom m_head;
75 
76  // the tail list is expressed as a vector for access efficiency
77  // and cache coherence, at the cost of wasted memory.
78  std::vector<Expression> m_tail;
79 
80  // convenience typedef
81  typedef std::vector<Expression>::iterator IteratorType;
82 
83  // internal helper methods
84  Expression handle_lookup(const Atom & head, const Environment & env);
85  Expression handle_define(Environment & env);
86  Expression handle_begin(Environment & env);
87 };
88 
90 std::ostream & operator<<(std::ostream & out, const Expression & exp);
91 
93 bool operator!=(const Expression & left, const Expression & right) noexcept;
94 
95 #endif
Expression eval(Environment &env)
Evaluate expression using a post-order traversal (recursive)
Definition: expression.cpp:170
bool isHeadSymbol() const noexcept
convienience member to determine if head atom is a symbol
Definition: expression.cpp:52
Expression()
Default construct and Expression, whose type in NoneType.
Definition: expression.cpp:9
An expression is a tree of Atoms.
Definition: expression.hpp:22
bool isHeadNumber() const noexcept
convienience member to determine if head atom is a number
Definition: expression.cpp:48
A class representing the interpreter environment.
Definition: environment.hpp:40
Atom & head()
return a reference to the head Atom
Definition: expression.cpp:40
void append(const Atom &a)
append Atom to tail of the expression
Definition: expression.cpp:57
A variant type that may be a Number or Symbol or the default type None.
Definition: atom.hpp:14
std::vector< Expression >::const_iterator ConstIteratorType
Definition: expression.hpp:25
ConstIteratorType tailConstEnd() const noexcept
return a const-iterator to the tail end
Definition: expression.cpp:76
Expression & operator=(const Expression &a)
deep-copy assign an expression (recursive)
Definition: expression.cpp:25
ConstIteratorType tailConstBegin() const noexcept
return a const-iterator to the beginning of tail
Definition: expression.cpp:72
Expression * tail()
return a pointer to the last expression in the tail, or nullptr
Definition: expression.cpp:62