-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve links between individual constructs in internal representation (issue #73) #96
Changes from all commits
e2dd274
e9de3f4
89cd265
aa6fdfb
46b7a68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @file src/types/expression_type.h | ||
* @brief Declaration of class ExpressionType. | ||
* @copyright (c) 2017 Avast Software, licensed under the MIT license | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace yaramod { | ||
|
||
///< Type of the expression. | ||
enum class ExpressionType | ||
{ | ||
Undefined, | ||
Bool, | ||
Int, | ||
String, | ||
Regexp, | ||
Object, | ||
Float | ||
}; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,11 @@ | |
#include <variant> | ||
|
||
#include "yaramod/yaramod_error.h" | ||
#include "yaramod/types/symbol.h" | ||
|
||
namespace yaramod { | ||
|
||
class Symbol; | ||
class String; | ||
|
||
/** | ||
* Class representing literal. Literal can be either | ||
|
@@ -32,6 +33,8 @@ class Symbol; | |
class Literal | ||
{ | ||
public: | ||
|
||
using ReferenceType = const Literal*; | ||
/// @name Costructors | ||
/// @{ | ||
Literal() { assert(is<std::string>()); }; | ||
|
@@ -42,8 +45,9 @@ class Literal | |
explicit Literal(std::int64_t value, const std::optional<std::string>& integral_formatted_value = std::nullopt); | ||
explicit Literal(std::uint64_t value, const std::optional<std::string>& integral_formatted_value = std::nullopt); | ||
explicit Literal(double value, const std::optional<std::string>& integral_formatted_value = std::nullopt); | ||
explicit Literal(const std::shared_ptr<Symbol>& value, const std::string& name); | ||
explicit Literal(std::shared_ptr<Symbol>&& value, const std::string& name); | ||
explicit Literal(const std::shared_ptr<Symbol>& value); | ||
explicit Literal(std::shared_ptr<Symbol>&& value); | ||
explicit Literal(ReferenceType value); | ||
|
||
Literal(Literal&& literal) = default; | ||
Literal(const Literal& literal) = default; | ||
|
@@ -58,16 +62,18 @@ class Literal | |
bool isInt() const { return is<std::int64_t>() || is<std::uint64_t>(); } | ||
bool isFloat() const { return is<double>(); } | ||
bool isSymbol() const { return is<std::shared_ptr<Symbol>>(); } | ||
bool isLiteralReference() const { return is<ReferenceType>(); } | ||
/// @} | ||
|
||
/// @name Getter methods | ||
/// @{ | ||
const std::string& getString() const { return std::get<std::string>(_value); } | ||
bool getBool() const { return std::get<bool>(_value); } | ||
const std::string& getString() const { assert(isString()); return std::get<std::string>(_value); } | ||
bool getBool() const { assert(isBool()); return std::get<bool>(_value); } | ||
std::int64_t getInt() const { return is<std::int64_t>() ? std::get<std::int64_t>(_value) : std::get<std::uint64_t>(_value); } | ||
std::uint64_t getUInt() const { return is<std::uint64_t>() ? std::get<std::uint64_t>(_value) : std::get<std::int64_t>(_value); } | ||
double getFloat() const { return std::get<double>(_value); } | ||
const std::shared_ptr<Symbol>& getSymbol() const { return std::get<std::shared_ptr<Symbol>>(_value); } | ||
double getFloat() const { assert(isFloat()); return std::get<double>(_value); } | ||
const std::shared_ptr<Symbol>& getSymbol() const { assert(isSymbol()); return std::get<std::shared_ptr<Symbol>>(_value); } | ||
ReferenceType getLiteralReference() const { assert(isLiteralReference()); return std::get<ReferenceType>(_value); } | ||
std::string getFormattedValue() const; | ||
/// @} | ||
|
||
|
@@ -79,8 +85,9 @@ class Literal | |
void setValue(std::int64_t i, const std::optional<std::string>& integral_formatted_value = std::nullopt); | ||
void setValue(std::uint64_t i, const std::optional<std::string>& integral_formatted_value = std::nullopt); | ||
void setValue(double f, const std::optional<std::string>& integral_formatted_value = std::nullopt); | ||
void setValue(const std::shared_ptr<Symbol>& s, const std::string& symbol_name); | ||
void setValue(std::shared_ptr<Symbol>&& s, std::string&& symbol_name); | ||
void setValue(const std::shared_ptr<Symbol>& s); | ||
void setValue(std::shared_ptr<Symbol>&& s); | ||
void setValue(ReferenceType l); | ||
/// @} | ||
|
||
/// @name String representation | ||
|
@@ -96,6 +103,12 @@ class Literal | |
os << literal._formatted_value.value(); | ||
else if (literal.isBool()) | ||
os << (literal.getBool() ? "true" : "false"); | ||
else if (literal.isSymbol()) | ||
os << literal.getSymbol()->getName(); | ||
else if (literal.isLiteralReference()) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: there are no curly braces in other parts of this condition so I wouldn't expect them here either. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will fix it in my next PR, thank you for merging this. |
||
os << *(literal.getLiteralReference()); | ||
} | ||
else | ||
std::visit( | ||
[&os](auto&& v) | ||
|
@@ -115,7 +128,7 @@ class Literal | |
/// For an integral literal x there are two options: | ||
/// i. x it is unformatted: _formatted_value is empty AND _value contains x | ||
/// ii. x it is formatted: _formatted_value contains x's string representation AND _value contains pure x | ||
std::variant<std::string, bool, std::int64_t, std::uint64_t, double, std::shared_ptr<Symbol>> _value; ///< Value used for all literals: | ||
std::variant<std::string, bool, std::int64_t, std::uint64_t, double, std::shared_ptr<Symbol>, ReferenceType> _value; ///< Value used for all literals: | ||
std::optional<std::string> _formatted_value; ///< Value used for integral literals with particular formatting | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the motivation for taking out this type out of
Expression
itself?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Literal
class now needs to accessSymbol::getName
method. That means it needs to include the symbol.h file. In thesymbol.h
file there has been include ofexpression.h
so we would end up with cyclic dependency (see picture). Luckily, theSymbol
class only needs access toExpressionType
which has been extracted fromExpression::Type
to avoid this cyclic dependency.