diff --git a/src/parser.cpp b/src/parser.cpp index 38df427..419b8e4 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -8,7 +8,6 @@ using namespace std; Expression::~Expression() = default; // class Binary -ExprType Binary::get_type() { return ExprType::Binary; } string Binary::as_string() { return "(" + token_name(op->tokentype) + " " + left->as_string() + " " + @@ -21,7 +20,6 @@ Binary::Binary(Expression *_left, Token *_operator, Expression *_right) Binary::~Binary() = default; // class Grouping -ExprType Grouping::get_type() { return ExprType::Grouping; } string Grouping::as_string() { return "(" + expr->as_string() + ")"; } @@ -30,7 +28,6 @@ Grouping::Grouping(Expression *_expr) : expr(_expr){}; Grouping::~Grouping() = default; // class Unary -ExprType Unary::get_type() { return ExprType::Unary; } string Unary::as_string() { return token_name(op->tokentype) + right->as_string(); @@ -44,21 +41,19 @@ Unary::~Unary() = default; // class Literal string Literal::as_string() { string text; - switch (valuetype) { - case String: - text = "\"" + value.str + "\""; - break; - case Numeric: - text = to_string(value.numeric); - break; - case Boolean: - text = value.boolean ? "True" : "False"; - break; - case Nil: - text = "NULL"; - break; + if (holds_alternative(value)) { + return "\"" + get(value) + "\""; } - return text; + if (holds_alternative(value)) { + return to_string(get(value)); + } + if (holds_alternative(value)) { + return get(value) ? "True" : "False"; + } + if (holds_alternative(value)) { + return "NULL"; + } + return "unexpected"; } // class Parser diff --git a/src/parser.hpp b/src/parser.hpp index 362fc68..fe22fa2 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -3,6 +3,7 @@ #include "error.hpp" #include "tokens.hpp" #include +#include #include enum class ExprType { Binary, Grouping, Unary, Literal, None }; @@ -12,8 +13,7 @@ using namespace std; /// Base class for expressions class Expression { public: - virtual ExprType get_type() = 0; // get the expression type - virtual string as_string() = 0; // get string rep for debugging + virtual string as_string() = 0; // get string rep for debugging virtual ~Expression(); }; @@ -24,7 +24,6 @@ class Binary : public Expression { unique_ptr right; public: - ExprType get_type() override; string as_string() override; Binary(Expression *_left, Token *_operator, Expression *_right); ~Binary(); @@ -35,7 +34,6 @@ class Grouping : public Expression { unique_ptr expr; public: - ExprType get_type() override; string as_string() override; Grouping(Expression *_expr); ~Grouping(); @@ -47,7 +45,6 @@ class Unary : public Expression { unique_ptr right; public: - ExprType get_type() override; string as_string() override; Unary(Token *_operator, Expression *_right); ~Unary(); @@ -55,31 +52,31 @@ public: /// empty class that is the type of the Nil value class NilType {}; +typedef std::variant Value; /// encapsulates a value: numeric, string etc class Literal : public Expression { public: enum ValueType { String, Numeric, Boolean, Nil } valuetype; - ExprType get_type() override { return ExprType::Literal; } + Value value; + // union Value { + // double_t numeric; + // bool boolean; + // string str; + // NilType dummy; - union Value { - double_t numeric; - bool boolean; - string str; - NilType dummy; + // Value(double_t _numeric) : numeric(_numeric) {} + // Value(bool _boolean) : boolean(_boolean) {} + // Value(string _str) : str(_str) {} + // Value(NilType v) : dummy(v) {} + // ~Value() {} + // } value; - Value(double_t _numeric) : numeric(_numeric) {} - Value(bool _boolean) : boolean(_boolean) {} - Value(string _str) : str(_str) {} - Value(NilType v) : dummy(v) {} - ~Value() {} - } value; - - Literal(NilType v) : valuetype(ValueType::Nil), value(v){}; - Literal(double_t _numeric) : valuetype(ValueType::Numeric), value(_numeric){}; - Literal(string _str) : valuetype(ValueType::String), value(_str){}; - Literal(bool _boolean) : valuetype(ValueType::Boolean), value(_boolean){}; + Literal(NilType v) : value(v){}; + Literal(double_t _numeric) : value(_numeric){}; + Literal(string _str) : value(_str){}; + Literal(bool _boolean) : value(_boolean){}; string as_string() override; }; @@ -122,8 +119,8 @@ class Parser { Result term(); /// tries to parse the tokens as an equality (`a == b` / `a!= b`) Result equality(); - /// tries to parse the tokens as a comparison (`a > b` / `a >= b` / `a < b` / - /// `a <= b` ) + /// tries to parse the tokens as a comparison (`a > b` / `a >= b` / `a < b` + /// / `a <= b` ) Result comparison(); public: