From 60aa8b9ff8cdb91d1b9e179663ba2eb7fe32602e Mon Sep 17 00:00:00 2001 From: Shautvast Date: Tue, 22 Oct 2024 10:29:05 +0200 Subject: [PATCH] moved the method --- .gitignore | 2 ++ src/parser.hpp | 58 ++++++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index a47c673..a381d51 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ target/ .idea .cache + +.DS_Store diff --git a/src/parser.hpp b/src/parser.hpp index a64be8a..0d9272a 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -7,16 +7,18 @@ enum class ExprType { Binary, Grouping, Unary, Literal, None }; +/// Base class for expressions class Expression { public: - virtual ExprType type() { return ExprType::None; }; - virtual std::string to_string() { return "Expression()"; }; - virtual ~Expression() {} + virtual ExprType get_type() = 0; // abstract, getter for tyoe + virtual std::string to_string() = 0; // abstract, string rep for debugging + virtual ~Expression() = 0; // destructor }; +/// An expression with two operands class Binary : public Expression { public: - ExprType type() override { return ExprType::Binary; } + ExprType get_type() override { return ExprType::Binary; } std::string to_string() override { return "(" + token_name(op->tokentype) + " " + left->to_string() + " " + right->to_string() + ")"; @@ -33,18 +35,20 @@ public: } }; +/// An expression between parentheses class Grouping : public Expression { public: - ExprType type() override { return ExprType::Grouping; } + ExprType get_type() override { return ExprType::Grouping; } std::string to_string() override { return "(" + expr->to_string() + ")"; } Expression *expr; Grouping(Expression *_expr) : expr(_expr){}; ~Grouping() override { delete expr; } }; +/// An expression with one operand (operator is `-` or `!`) class Unary : public Expression { public: - ExprType type() override { return ExprType::Unary; } + ExprType get_type() override { return ExprType::Unary; } std::string to_string() override { return token_name(op->tokentype) + right->to_string(); } @@ -58,31 +62,16 @@ public: } }; +/// empty class that is the type of the Nil value class Void {}; +/// encapsulates a value: numeric, string etc class Literal : public Expression { public: - ExprType type() override { return ExprType::Literal; } - std::string to_string() override { - std::string text; - switch (valuetype) { - case String: - text = "\"" + value.str + "\""; - break; - case Numeric: - text = std::to_string(value.numeric); - break; - case Boolean: - text = value.boolean ? "True" : "False"; - break; - case Nil: - text = "NULL"; - break; - } - return text; - } enum ValueType { String, Numeric, Boolean, Nil } valuetype; + ExprType get_type() override { return ExprType::Literal; } + union Value { double_t numeric; bool boolean; @@ -100,6 +89,25 @@ public: Literal(double_t _numeric) : valuetype(ValueType::Numeric), value(_numeric) {} Literal(std::string _str) : valuetype(ValueType::String), value(_str) {} Literal(bool _boolean) : valuetype(ValueType::Boolean), value(_boolean) {} + + std::string to_string() override { + std::string text; + switch (valuetype) { + case String: + text = "\"" + value.str + "\""; + break; + case Numeric: + text = std::to_string(value.numeric); + break; + case Boolean: + text = value.boolean ? "True" : "False"; + break; + case Nil: + text = "NULL"; + break; + } + return text; + } }; class Parser {