separated implementation from headers for the Expression classes
This commit is contained in:
parent
60aa8b9ff8
commit
9477e68e18
2 changed files with 89 additions and 51 deletions
|
|
@ -1,5 +1,68 @@
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
|
|
||||||
|
Expression::~Expression() {}
|
||||||
|
|
||||||
|
// class Binary
|
||||||
|
ExprType Binary::get_type() { return ExprType::Binary; }
|
||||||
|
|
||||||
|
std::string Binary::to_string() {
|
||||||
|
return "(" + token_name(op->tokentype) + " " + left->to_string() + " " +
|
||||||
|
right->to_string() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
Binary::Binary(Expression *_left, Token *_operator, Expression *_right)
|
||||||
|
: left(_left), op(_operator), right(_right){};
|
||||||
|
|
||||||
|
Binary::~Binary() {
|
||||||
|
delete left;
|
||||||
|
delete right;
|
||||||
|
delete op;
|
||||||
|
}
|
||||||
|
|
||||||
|
// class Grouping
|
||||||
|
ExprType Grouping::get_type() { return ExprType::Grouping; }
|
||||||
|
|
||||||
|
std::string Grouping::to_string() { return "(" + expr->to_string() + ")"; }
|
||||||
|
|
||||||
|
Grouping::Grouping(Expression *_expr) : expr(_expr){};
|
||||||
|
|
||||||
|
Grouping::~Grouping() { delete expr; }
|
||||||
|
|
||||||
|
// class Unary
|
||||||
|
ExprType Unary::get_type() { return ExprType::Unary; }
|
||||||
|
|
||||||
|
std::string Unary::to_string() {
|
||||||
|
return token_name(op->tokentype) + right->to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Unary::Unary(Token *_operator, Expression *_right)
|
||||||
|
: op(_operator), right(_right){};
|
||||||
|
|
||||||
|
Unary::~Unary() {
|
||||||
|
delete right;
|
||||||
|
delete op;
|
||||||
|
}
|
||||||
|
|
||||||
|
// class Literal
|
||||||
|
std::string Literal::to_string() {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
Expression *Parser::parse(std::vector<Token> tokenlist) {
|
Expression *Parser::parse(std::vector<Token> tokenlist) {
|
||||||
tokens = tokenlist;
|
tokens = tokenlist;
|
||||||
current_token = 0;
|
current_token = 0;
|
||||||
|
|
|
||||||
|
|
@ -12,54 +12,46 @@ class Expression {
|
||||||
public:
|
public:
|
||||||
virtual ExprType get_type() = 0; // abstract, getter for tyoe
|
virtual ExprType get_type() = 0; // abstract, getter for tyoe
|
||||||
virtual std::string to_string() = 0; // abstract, string rep for debugging
|
virtual std::string to_string() = 0; // abstract, string rep for debugging
|
||||||
virtual ~Expression() = 0; // destructor
|
virtual ~Expression(); // destructor
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An expression with two operands
|
/// An expression with two operands
|
||||||
class Binary : public Expression {
|
class Binary : public Expression {
|
||||||
public:
|
private:
|
||||||
ExprType get_type() override { return ExprType::Binary; }
|
|
||||||
std::string to_string() override {
|
|
||||||
return "(" + token_name(op->tokentype) + " " + left->to_string() + " " +
|
|
||||||
right->to_string() + ")";
|
|
||||||
}
|
|
||||||
Expression *left;
|
Expression *left;
|
||||||
Token *op;
|
Token *op;
|
||||||
Expression *right;
|
Expression *right;
|
||||||
Binary(Expression *_left, Token *_operator, Expression *_right)
|
|
||||||
: left(_left), op(_operator), right(_right){};
|
public:
|
||||||
~Binary() override {
|
ExprType get_type() override;
|
||||||
delete left;
|
std::string to_string() override;
|
||||||
delete right;
|
Binary(Expression *_left, Token *_operator, Expression *_right);
|
||||||
delete op;
|
~Binary();
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An expression between parentheses
|
/// An expression between parentheses
|
||||||
class Grouping : public Expression {
|
class Grouping : public Expression {
|
||||||
public:
|
private:
|
||||||
ExprType get_type() override { return ExprType::Grouping; }
|
|
||||||
std::string to_string() override { return "(" + expr->to_string() + ")"; }
|
|
||||||
Expression *expr;
|
Expression *expr;
|
||||||
Grouping(Expression *_expr) : expr(_expr){};
|
|
||||||
~Grouping() override { delete expr; }
|
public:
|
||||||
|
ExprType get_type() override;
|
||||||
|
std::string to_string() override;
|
||||||
|
Grouping(Expression *_expr);
|
||||||
|
~Grouping();
|
||||||
};
|
};
|
||||||
|
|
||||||
/// An expression with one operand (operator is `-` or `!`)
|
/// An expression with one operand (operator is `-` or `!`)
|
||||||
class Unary : public Expression {
|
class Unary : public Expression {
|
||||||
public:
|
private:
|
||||||
ExprType get_type() override { return ExprType::Unary; }
|
|
||||||
std::string to_string() override {
|
|
||||||
return token_name(op->tokentype) + right->to_string();
|
|
||||||
}
|
|
||||||
Token *op;
|
Token *op;
|
||||||
Expression *right;
|
Expression *right;
|
||||||
|
|
||||||
Unary(Token *_operator, Expression *_right) : op(_operator), right(_right){};
|
public:
|
||||||
~Unary() override {
|
ExprType get_type() override;
|
||||||
delete right;
|
std::string to_string() override;
|
||||||
delete op;
|
Unary(Token *_operator, Expression *_right);
|
||||||
}
|
~Unary();
|
||||||
};
|
};
|
||||||
|
|
||||||
/// empty class that is the type of the Nil value
|
/// empty class that is the type of the Nil value
|
||||||
|
|
@ -85,29 +77,12 @@ public:
|
||||||
~Value() {}
|
~Value() {}
|
||||||
} value;
|
} value;
|
||||||
|
|
||||||
Literal(Void v) : valuetype(ValueType::Nil), value(v) {}
|
Literal(Void v) : valuetype(ValueType::Nil), value(v){};
|
||||||
Literal(double_t _numeric) : valuetype(ValueType::Numeric), value(_numeric) {}
|
Literal(double_t _numeric) : valuetype(ValueType::Numeric), value(_numeric){};
|
||||||
Literal(std::string _str) : valuetype(ValueType::String), value(_str) {}
|
Literal(std::string _str) : valuetype(ValueType::String), value(_str){};
|
||||||
Literal(bool _boolean) : valuetype(ValueType::Boolean), value(_boolean) {}
|
Literal(bool _boolean) : valuetype(ValueType::Boolean), value(_boolean){};
|
||||||
|
|
||||||
std::string to_string() override {
|
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 {
|
class Parser {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue