get rid of unions
This commit is contained in:
parent
6fd95bd486
commit
a5d77019f3
2 changed files with 33 additions and 41 deletions
|
|
@ -8,7 +8,6 @@ using namespace std;
|
||||||
Expression::~Expression() = default;
|
Expression::~Expression() = default;
|
||||||
|
|
||||||
// class Binary
|
// class Binary
|
||||||
ExprType Binary::get_type() { return ExprType::Binary; }
|
|
||||||
|
|
||||||
string Binary::as_string() {
|
string Binary::as_string() {
|
||||||
return "(" + token_name(op->tokentype) + " " + left->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;
|
Binary::~Binary() = default;
|
||||||
|
|
||||||
// class Grouping
|
// class Grouping
|
||||||
ExprType Grouping::get_type() { return ExprType::Grouping; }
|
|
||||||
|
|
||||||
string Grouping::as_string() { return "(" + expr->as_string() + ")"; }
|
string Grouping::as_string() { return "(" + expr->as_string() + ")"; }
|
||||||
|
|
||||||
|
|
@ -30,7 +28,6 @@ Grouping::Grouping(Expression *_expr) : expr(_expr){};
|
||||||
Grouping::~Grouping() = default;
|
Grouping::~Grouping() = default;
|
||||||
|
|
||||||
// class Unary
|
// class Unary
|
||||||
ExprType Unary::get_type() { return ExprType::Unary; }
|
|
||||||
|
|
||||||
string Unary::as_string() {
|
string Unary::as_string() {
|
||||||
return token_name(op->tokentype) + right->as_string();
|
return token_name(op->tokentype) + right->as_string();
|
||||||
|
|
@ -44,21 +41,19 @@ Unary::~Unary() = default;
|
||||||
// class Literal
|
// class Literal
|
||||||
string Literal::as_string() {
|
string Literal::as_string() {
|
||||||
string text;
|
string text;
|
||||||
switch (valuetype) {
|
if (holds_alternative<string>(value)) {
|
||||||
case String:
|
return "\"" + get<string>(value) + "\"";
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
return text;
|
if (holds_alternative<double_t>(value)) {
|
||||||
|
return to_string(get<double_t>(value));
|
||||||
|
}
|
||||||
|
if (holds_alternative<bool>(value)) {
|
||||||
|
return get<bool>(value) ? "True" : "False";
|
||||||
|
}
|
||||||
|
if (holds_alternative<NilType>(value)) {
|
||||||
|
return "NULL";
|
||||||
|
}
|
||||||
|
return "unexpected";
|
||||||
}
|
}
|
||||||
|
|
||||||
// class Parser
|
// class Parser
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#include "error.hpp"
|
#include "error.hpp"
|
||||||
#include "tokens.hpp"
|
#include "tokens.hpp"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
enum class ExprType { Binary, Grouping, Unary, Literal, None };
|
enum class ExprType { Binary, Grouping, Unary, Literal, None };
|
||||||
|
|
@ -12,7 +13,6 @@ using namespace std;
|
||||||
/// Base class for expressions
|
/// Base class for expressions
|
||||||
class Expression {
|
class Expression {
|
||||||
public:
|
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();
|
virtual ~Expression();
|
||||||
};
|
};
|
||||||
|
|
@ -24,7 +24,6 @@ class Binary : public Expression {
|
||||||
unique_ptr<Expression> right;
|
unique_ptr<Expression> right;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ExprType get_type() override;
|
|
||||||
string as_string() override;
|
string as_string() override;
|
||||||
Binary(Expression *_left, Token *_operator, Expression *_right);
|
Binary(Expression *_left, Token *_operator, Expression *_right);
|
||||||
~Binary();
|
~Binary();
|
||||||
|
|
@ -35,7 +34,6 @@ class Grouping : public Expression {
|
||||||
unique_ptr<Expression> expr;
|
unique_ptr<Expression> expr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ExprType get_type() override;
|
|
||||||
string as_string() override;
|
string as_string() override;
|
||||||
Grouping(Expression *_expr);
|
Grouping(Expression *_expr);
|
||||||
~Grouping();
|
~Grouping();
|
||||||
|
|
@ -47,7 +45,6 @@ class Unary : public Expression {
|
||||||
unique_ptr<Expression> right;
|
unique_ptr<Expression> right;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ExprType get_type() override;
|
|
||||||
string as_string() override;
|
string as_string() override;
|
||||||
Unary(Token *_operator, Expression *_right);
|
Unary(Token *_operator, Expression *_right);
|
||||||
~Unary();
|
~Unary();
|
||||||
|
|
@ -55,31 +52,31 @@ public:
|
||||||
|
|
||||||
/// empty class that is the type of the Nil value
|
/// empty class that is the type of the Nil value
|
||||||
class NilType {};
|
class NilType {};
|
||||||
|
typedef std::variant<double_t, bool, string, NilType> Value;
|
||||||
|
|
||||||
/// encapsulates a value: numeric, string etc
|
/// encapsulates a value: numeric, string etc
|
||||||
class Literal : public Expression {
|
class Literal : public Expression {
|
||||||
public:
|
public:
|
||||||
enum ValueType { String, Numeric, Boolean, Nil } valuetype;
|
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 {
|
// Value(double_t _numeric) : numeric(_numeric) {}
|
||||||
double_t numeric;
|
// Value(bool _boolean) : boolean(_boolean) {}
|
||||||
bool boolean;
|
// Value(string _str) : str(_str) {}
|
||||||
string str;
|
// Value(NilType v) : dummy(v) {}
|
||||||
NilType dummy;
|
// ~Value() {}
|
||||||
|
// } value;
|
||||||
|
|
||||||
Value(double_t _numeric) : numeric(_numeric) {}
|
Literal(NilType v) : value(v){};
|
||||||
Value(bool _boolean) : boolean(_boolean) {}
|
Literal(double_t _numeric) : value(_numeric){};
|
||||||
Value(string _str) : str(_str) {}
|
Literal(string _str) : value(_str){};
|
||||||
Value(NilType v) : dummy(v) {}
|
Literal(bool _boolean) : value(_boolean){};
|
||||||
~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){};
|
|
||||||
|
|
||||||
string as_string() override;
|
string as_string() override;
|
||||||
};
|
};
|
||||||
|
|
@ -122,8 +119,8 @@ class Parser {
|
||||||
Result<Expression *> term();
|
Result<Expression *> term();
|
||||||
/// tries to parse the tokens as an equality (`a == b` / `a!= b`)
|
/// tries to parse the tokens as an equality (`a == b` / `a!= b`)
|
||||||
Result<Expression *> equality();
|
Result<Expression *> equality();
|
||||||
/// tries to parse the tokens as a comparison (`a > b` / `a >= b` / `a < b` /
|
/// tries to parse the tokens as a comparison (`a > b` / `a >= b` / `a < b`
|
||||||
/// `a <= b` )
|
/// / `a <= b` )
|
||||||
Result<Expression *> comparison();
|
Result<Expression *> comparison();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue