finish operation vector building
This commit is contained in:
+94
-1
@@ -24,7 +24,9 @@ public:
|
||||
enum TokenType {
|
||||
VAR,
|
||||
CONST,
|
||||
OPERATOR
|
||||
OPERATOR,
|
||||
CV, // compound value (result of an operation)
|
||||
NAT, // not a token
|
||||
} t;
|
||||
|
||||
enum Operator {
|
||||
@@ -41,6 +43,10 @@ public:
|
||||
std::shared_ptr<std::string> s;
|
||||
double d;
|
||||
Operator o;
|
||||
struct {
|
||||
int w; // weight
|
||||
int i; // index in operation vector
|
||||
} cv;
|
||||
|
||||
explicit TokenValue(std::string& s)
|
||||
: s(std::make_shared<std::string>(s))
|
||||
@@ -89,6 +95,27 @@ public:
|
||||
t = OPERATOR;
|
||||
}
|
||||
|
||||
Token(const Expression::Token& other)
|
||||
{
|
||||
t = other.t;
|
||||
|
||||
switch (t)
|
||||
{
|
||||
case VAR:
|
||||
v.s = other.v.s;
|
||||
break;
|
||||
case CONST:
|
||||
v.d = other.v.d;
|
||||
break;
|
||||
case OPERATOR:
|
||||
v.o = other.v.o;
|
||||
break;
|
||||
default:
|
||||
std::cout << "Warning: token type is invalid\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Token(Expression::Token&& other)
|
||||
{
|
||||
t = other.t;
|
||||
@@ -128,6 +155,24 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<std::string> to_math_str(void) const
|
||||
{
|
||||
switch (t)
|
||||
{
|
||||
case VAR:
|
||||
return v.s;
|
||||
case CONST:
|
||||
{
|
||||
char buffer[128] = {0};
|
||||
return std::make_shared<std::string>((std::ostringstream() << v.d).str());
|
||||
}
|
||||
case OPERATOR:
|
||||
return std::make_shared<std::string>(op_math_str());
|
||||
default:
|
||||
return std::make_shared<std::string>("Not a valid token!");
|
||||
}
|
||||
}
|
||||
|
||||
const char *type_str(void) const
|
||||
{
|
||||
switch (t)
|
||||
@@ -170,6 +215,54 @@ public:
|
||||
return "Invalid operator";
|
||||
}
|
||||
}
|
||||
|
||||
const char *op_math_str(void) const
|
||||
{
|
||||
if (t != OPERATOR)
|
||||
{
|
||||
return "Token is not an operator!";
|
||||
}
|
||||
|
||||
switch (v.o)
|
||||
{
|
||||
case OPA:
|
||||
return "(";
|
||||
case CPA:
|
||||
return ")";
|
||||
case ADD:
|
||||
return "+";
|
||||
case SUB:
|
||||
return "-";
|
||||
case MUL:
|
||||
return "*";
|
||||
case DIV:
|
||||
return "/";
|
||||
case POW:
|
||||
return "^";
|
||||
default:
|
||||
return "Invalid operator";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct Operation
|
||||
{
|
||||
Token o; // operator
|
||||
|
||||
enum Type
|
||||
{
|
||||
NORMAL,
|
||||
DERIVED,
|
||||
} tl, tr; // type left, type right
|
||||
|
||||
int il, ir; // index left, index right
|
||||
int w; // operation weight
|
||||
|
||||
Operation(enum Expression::Token::Operator o, enum Type tl, int il, enum Type tr, int ir, int w)
|
||||
: o(o), tl(tl), tr(tr), il(il), ir(ir), w(w)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
class TreeNode {
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
|
||||
namespace Parser {
|
||||
|
||||
std::shared_ptr<std::vector<Expression::Token>> tokenize_input(const char * const input_string);
|
||||
std::vector<Expression::Token> tokenize_input(const char * const input_string);
|
||||
int assert_same(const std::vector<Expression::Token>& a, const std::vector<Expression::Token>& b);
|
||||
void print_token_vector(const std::vector<Expression::Token>& tv);
|
||||
|
||||
|
||||
+5
-1
@@ -4,9 +4,13 @@
|
||||
#include "config.h"
|
||||
|
||||
#if DEBUG == 1
|
||||
#define pr_debug(...) std::cout << "[" << __func__ << ":" << __LINE__ << "] debug: " << __VA_ARGS__ << "\n"
|
||||
#define pr_debug(...) std::cout << "[" << __FILE__ << ":" << __LINE__ << " " << __func__ << "] debug: " << __VA_ARGS__ << "\n"
|
||||
#define pri_debug(...) std::cout << __VA_ARGS__
|
||||
#define prs_debug(...) std::cout << "[" << __FILE__ << ":" << __LINE__ << " " << __func__ << "] debug: " << __VA_ARGS__
|
||||
#else
|
||||
#define pr_debug(...) do {} while (0)
|
||||
#define pri_debug(...) do {} while (0)
|
||||
#define prs_debug(...) do {} while (0)
|
||||
#endif
|
||||
|
||||
#define pr_warn(...) std::cout << "[" << __FILE__ << ":" << __LINE__ << " " << __func__ << "] warning: " << __VA_ARGS__ << "\n"
|
||||
|
||||
+1
-2
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "expression.hpp"
|
||||
|
||||
namespace Syntax
|
||||
@@ -13,6 +12,6 @@ struct SyntaxIssue
|
||||
};
|
||||
|
||||
std::vector<SyntaxIssue> check_syntax(std::vector<Expression::Token>& tv);
|
||||
std::shared_ptr<Expression::TreeNode> build_op_tree(std::vector<Expression::Token>& tv);
|
||||
std::vector<Expression::Operation> build_op_vector(const std::vector<Expression::Token>& tv);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user