MIXAL
expression.h
Go to the documentation of this file.
1 #ifndef INCLUDE_EXPRESSION_H_
2 #define INCLUDE_EXPRESSION_H_
3 
4 #include <string>
5 #include <vector>
6 #include <unordered_map>
7 #include <unordered_set>
8 #include "errors.h"
9 
15 namespace mixal {
16 
18 enum class AtomicType {
19  INTEGER,
20  SYMBOL,
21  ASTERISK,
22 };
23 
25 struct Atomic {
27  bool negative;
28  int32_t integer;
29  std::string symbol;
32  explicit Atomic(AtomicType _type = AtomicType::INTEGER, bool _negative = false);
34  Atomic(AtomicType _type, int32_t _value, bool _negative = false);
36  Atomic(AtomicType _type, const std::string& _value, bool _negative = false);
38  Atomic(const Atomic& atomic);
39 
41  bool operator==(const Atomic& atomic);
43  bool operator!=(const Atomic& atomic);
45  friend std::ostream& operator<<(std::ostream& out, const Atomic& atomic);
46 
51  static bool isLocalSymbol(const std::string& symbol);
57  bool isLocalSymbol() const;
68  void replaceSymbol(const std::string& symbol);
69 };
70 
75 struct AtomicValue {
76  bool negative;
77  int32_t value;
78 
80  AtomicValue();
82  explicit AtomicValue(int32_t _value);
84  AtomicValue(const AtomicValue& atomicValue);
86  AtomicValue& operator=(const AtomicValue& atomicValue);
87 };
88 
90 enum class Operation {
91  ADD,
92  SUBTRACT,
93  MULTIPLY,
94  FLOOR_DIV,
95  // FLOAT_DIV, /**< `//` */
96  FIELD,
97 };
98 
100 std::ostream& operator<<(std::ostream& out, Operation operation);
101 
103 class Expression {
104  public:
106  Expression();
111  explicit Expression(const std::string& expression, const std::string& lineSymbol = "");
112 
114  static Expression getConstExpression(const AtomicValue& value);
119  static Expression getConstExpression(const std::string& symbol);
124  static Expression getConstOffsetExpression(const std::string& symbol, int32_t offset);
133  static bool isValidFirst(char ch);
141  static bool isValidChar(char ch);
142 
144  inline bool evaluated() const { return _evaluated; }
146  inline const AtomicValue& result() const { return _result; }
147 
149  inline bool literalConstant() const { return _literalConstant; }
150 
152  inline const std::unordered_set<std::string> depends() const { return _depends; }
154  inline const std::vector<Atomic> atomics() const { return _atomics; }
156  inline const std::vector<Operation> operations() const { return _operations; }
157 
166  void parse(const std::string& expression, const std::string& lineSymbol);
173  bool evaluate(const std::unordered_map<std::string, AtomicValue>& constants);
174 
176  friend std::ostream& operator<<(std::ostream& out, const Expression& expression);
177 
182  void replaceSymbol(const std::unordered_map<std::string, std::string>& mapping);
183 
185  void reset();
186 
188  bool operator==(const Expression& expression);
190  bool operator!=(const Expression& expression);
191 
192  private:
193  bool _evaluated;
194  AtomicValue _result;
196  bool _literalConstant;
198  std::unordered_set<std::string> _depends;
199  std::vector<Atomic> _atomics;
200  std::vector<Operation> _operations;
201 };
202 
203 }; // namespace mixal
204 
205 
206 #endif // INCLUDE_EXPRESSION_H_
mixal::Expression::operator==
bool operator==(const Expression &expression)
Definition: expression.cpp:314
mixal::Expression::replaceSymbol
void replaceSymbol(const std::unordered_map< std::string, std::string > &mapping)
Definition: expression.cpp:293
mixal::AtomicValue::operator=
AtomicValue & operator=(const AtomicValue &atomicValue)
Definition: atomic.cpp:68
mixal::Expression::isValidFirst
static bool isValidFirst(char ch)
Definition: expression.cpp:46
mixal::Expression::operator<<
friend std::ostream & operator<<(std::ostream &out, const Expression &expression)
Definition: expression.cpp:346
mixal::Atomic::operator!=
bool operator!=(const Atomic &atomic)
Definition: atomic.cpp:31
mixal::Atomic::negative
bool negative
Definition: expression.h:27
mixal::Expression::getConstOffsetExpression
static Expression getConstOffsetExpression(const std::string &symbol, int32_t offset)
Definition: expression.cpp:38
mixal::Expression::isValidChar
static bool isValidChar(char ch)
Definition: expression.cpp:50
mixal::Operation
Operation
Definition: expression.h:90
mixal::Atomic::Atomic
Atomic(AtomicType _type=AtomicType::INTEGER, bool _negative=false)
Definition: atomic.cpp:6
errors.h
The definition of some errors.
mixal::Atomic::integer
int32_t integer
Definition: expression.h:28
mixal::Expression::parse
void parse(const std::string &expression, const std::string &lineSymbol)
Definition: expression.cpp:89
mixal::Expression::evaluated
bool evaluated() const
Definition: expression.h:144
mixal::Expression::evaluate
bool evaluate(const std::unordered_map< std::string, AtomicValue > &constants)
Definition: expression.cpp:233
mixal::Expression::atomics
const std::vector< Atomic > atomics() const
Definition: expression.h:154
mixal::Expression::reset
void reset()
Definition: expression.cpp:306
mixal::Expression::getConstExpression
static Expression getConstExpression(const AtomicValue &value)
Definition: expression.cpp:23
mixal::Atomic::type
AtomicType type
Definition: expression.h:26
mixal::Expression::depends
const std::unordered_set< std::string > depends() const
Definition: expression.h:152
mixal::Atomic::replaceSymbol
void replaceSymbol(const std::string &symbol)
Definition: atomic.cpp:54
mixal::Atomic::symbol
std::string symbol
Definition: expression.h:29
mixal::Expression::literalConstant
bool literalConstant() const
Definition: expression.h:149
mixal::AtomicType
AtomicType
Definition: expression.h:18
mixal::Expression::operator!=
bool operator!=(const Expression &expression)
Definition: expression.cpp:331
mixal::AtomicValue
Definition: expression.h:75
mixal::Atomic::isLocalSymbol
bool isLocalSymbol() const
Definition: atomic.cpp:50
mixal::Expression::result
const AtomicValue & result() const
Definition: expression.h:146
mixal::Expression::Expression
Expression()
Definition: expression.cpp:13
mixal::Expression
Definition: expression.h:103
mixal::Expression::operations
const std::vector< Operation > operations() const
Definition: expression.h:156
mixal::Atomic
Definition: expression.h:25
mixal::Atomic::operator==
bool operator==(const Atomic &atomic)
Definition: atomic.cpp:24
mixal::Atomic::operator<<
friend std::ostream & operator<<(std::ostream &out, const Atomic &atomic)
Definition: atomic.cpp:35
mixal::AtomicValue::AtomicValue
AtomicValue()
Definition: atomic.cpp:58