LogiXpr
Loading...
Searching...
No Matches
expression.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <cctype>
9#include <iostream>
10#include <memory>
11#include <set>
12#include <stack>
13#include <string>
14#include <unordered_map>
15
21#define AND "&"
22#define OR "|"
23#define NOT "!"
24#define XOR "^"
25#define IMPLIES "->"
26#define IFF "<=>"
27#define OPEN "("
28#define CLOSE ")"
29#define TRUE "T"
30#define FALSE "F"
36const static std::unordered_map<std::string, int> precedence = {
37 {NOT, 5}, {AND, 4}, {OR, 3}, {XOR, 2}, {IMPLIES, 1}, {IFF, 0}};
38
43public:
49 Expression(std::string value);
50
56 std::string getValue();
57
63 bool hasLeft();
64
70 bool hasRight();
71
77 bool isBinary();
78
84 bool isVar();
85
91 std::shared_ptr<Expression> getParent();
92
98 std::shared_ptr<Expression> getLeft();
99
105 std::shared_ptr<Expression> getRight();
106
113 void setLeft(std::shared_ptr<Expression> left, std::shared_ptr<Expression> parent);
114
120 void setRight(std::shared_ptr<Expression> right, std::shared_ptr<Expression> parent);
121
127 void setParent(std::shared_ptr<Expression> parent);
128
135 std::set<std::string> getVariables();
136
142 std::shared_ptr<Expression> clone();
143
150 std::shared_ptr<Expression> cloneTree();
151
160 bool compare(std::shared_ptr<Expression> other);
161
170 bool compareTree(std::shared_ptr<Expression> other);
171
177 std::string toString();
178
184 std::string toStringTree();
185
191 std::string toStringMinimal();
192
193private:
197 std::string value;
198
202 std::shared_ptr<Expression> parent;
203
207 std::shared_ptr<Expression> left;
208
212 std::shared_ptr<Expression> right;
213};
Abstract syntax tree for logic expressions.
Definition expression.h:42
std::shared_ptr< Expression > right
Pointer to right subexpression.
Definition expression.h:212
bool compareTree(std::shared_ptr< Expression > other)
Compare the entire expression tree to another expression tree.
std::string getValue()
Get the value of the current expression.
std::shared_ptr< Expression > getLeft()
Get the left subexpression of the current expression.
bool compare(std::shared_ptr< Expression > other)
Compare the current expression to another expression.
std::set< std::string > getVariables()
Get all the variables in the current expression and subexpressions.
void setRight(std::shared_ptr< Expression > right, std::shared_ptr< Expression > parent)
Sets the right child expression of this expression.
std::shared_ptr< Expression > getRight()
Get the right subexpression of the current expression.
std::string toString()
Convert the current expression to a string.
std::string toStringTree()
Convert the whole expression tree to a string.
std::shared_ptr< Expression > left
Pointer to left subexpression.
Definition expression.h:207
std::shared_ptr< Expression > getParent()
Get the parent expression of the current expression.
bool hasRight()
Check if the current expression has a right subexpression.
bool isBinary()
Check if the current expression is a binary expression.
std::shared_ptr< Expression > cloneTree()
Create a clone of the whole expression tree and returns the pointer to the node where it is called.
bool isVar()
Check if the current expression is a variable.
void setParent(std::shared_ptr< Expression > parent)
Sets the parent node expression of this expression.
std::shared_ptr< Expression > clone()
Create a clone of the current expression and subexpressions.
std::string value
The current expression.
Definition expression.h:197
std::string toStringMinimal()
Convert the current expression to a string with minimal parentheses.
bool hasLeft()
Check if the current expression has a left subexpression.
void setLeft(std::shared_ptr< Expression > left, std::shared_ptr< Expression > parent)
Sets the left child expression of this expression.
std::shared_ptr< Expression > parent
Pointer to parent expression.
Definition expression.h:202
#define IMPLIES
Definition expression.h:25
#define IFF
Definition expression.h:26
#define OR
Definition expression.h:22
#define XOR
Definition expression.h:24
#define AND
Definition expression.h:21
#define NOT
Definition expression.h:23