-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.hpp
158 lines (115 loc) · 4.9 KB
/
expression.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
/*! \file expression.hpp
Defines the Expression type and assiciated functions.
*/
#ifndef EXPRESSION_HPP
#define EXPRESSION_HPP
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include<algorithm>
#include <iomanip>
#include "token.hpp"
#include "atom.hpp"
#include "message_queue.h"
const int BOXSCALE = 20;
const double POINTSIZE = 0.5;
const int LINETHICKNESS = 0;
const int OUTTERLINE = 3;
const int SAMPLING = 50;
typedef message_queue<std::string> MessageQueueStr;
// forward declare Environment
class Environment;
/*! \class Expression
\brief An expression is a tree of Atoms.
An expression is an atom called the head followed by a (possibly empty)
list of expressions called the tail.
*/
class Expression {
public:
typedef std::vector<Expression>::const_iterator ConstIteratorType;
/// Default construct and Expression, whose type in NoneType
Expression();
/*! Construct an Expression with given Atom as head an empty tail
\param atom the atom to make the head
*/
Expression(const Atom & a);
/// deep-copy construct an expression (recursive)
Expression(const Expression & a);
/// deep-copy assign an expression (recursive)
Expression & operator=(const Expression & a);
/// return a reference to the head Atom
Atom & head();
/// return a const-reference to the head Atom
const Atom & head() const;
/// append Atom to tail of the expression
void append(const Atom & a);
/// push back an expression to tail of expression vector
void pushback(const Expression & a);
/// return a pointer to the last expression in the tail, or nullptr
Expression * tail();
/// return a point to the first expression in the tail
Expression * first_of_tail();
/// set property into the expression
void add_property(const std::string & keyword,const Expression & exp);
// return size of tail
int tailSize() const noexcept;
int propertySize() const noexcept;
//get expression property inside property map
Expression getProperty(const std::string & keyword) const noexcept;
// replace the variable inside lambda with the input variable
//Expression replace_LambdaVariables(const Expression & argument, const Expression & procedure);
/// return a const-iterator to the beginning of tail
ConstIteratorType tailConstBegin() const noexcept;
/// return a const-iterator to the tail end
ConstIteratorType tailConstEnd() const noexcept;
/// convienience member to determine if head atom is a number
bool isHeadNumber() const noexcept;
/// convienience member to determine if head atom is a string constant
bool isHeadStringConstant() const noexcept;
/// convienience member to determine if head atom is a symbol
bool isHeadSymbol() const noexcept;
/// convienience member to determine if head atom is a complextype
bool isHeadComplex() const noexcept;
/// check to see if tail is empty
bool isTailEmpty() const noexcept;
/// Evaluate expression using a post-order traversal (recursive)
Expression eval(Environment & env);
/// equality comparison for two expressions (recursive)
bool operator==(const Expression & exp) const noexcept;
private:
// the head of the expression
Atom m_head;
// the tail list is expressed as a vector for access efficiency
// and cache coherence, at the cost of wasted memory.
std::vector<Expression> m_tail;
// convenience typedef
typedef std::vector<Expression>::iterator IteratorType;
std::map <std::string, Expression> m_property;
// internal helper methods
Expression handle_lookup(const Atom & head, const Environment & env);
Expression handle_define(Environment & env);
Expression handle_begin(Environment & env);
Expression handle_lambda(Environment & env);
Expression handle_apply(Environment & env);
Expression handle_map(Environment & env);
Expression handle_setprop(Environment & env);
Expression handle_getprop(Environment & env);
Expression handle_continuousplot(Environment & env);
};
///
/// Render expression to output stream
std::ostream & operator<<(std::ostream & out, const Expression & exp);
/// inequality comparison for two expressions (recursive)
bool operator!=(const Expression & left, const Expression & right) noexcept;
#endif
/*********************** helper function *************************/
/******************************************************************/
Expression makePoint(double x, double y, double point_size);
Expression makeLine(Expression point1, Expression point2);
void get_borderLine(std::unordered_map<std::string, double> data, Expression & result);
std::unordered_map<std::string, double> checkAndScalePoints(Expression points);
double getTextScale(Expression options);
double getTextScale(Expression options);
void checkAndMakeOptionList(Expression option, Expression & result, double text_scale, std::unordered_map<std::string, double> data);
void addAlAuOlOu(std::unordered_map<std::string, double> data, Expression & result, double text_scale);