Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Polymorphism: Implementing the Expressions Language #12

Open
lparolari opened this issue Apr 23, 2021 · 0 comments
Open

Dynamic Polymorphism: Implementing the Expressions Language #12

lparolari opened this issue Apr 23, 2021 · 0 comments
Labels
triage To be categorized

Comments

@lparolari
Copy link
Owner

An attempt... (needs review, no time for make it better)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

template <typename T>
class Expr
{
public:
    virtual int parse(std::string in) = 0;

    virtual T eval() = 0;
};

template <typename T>
class BinOp : public Expr<T>
{
public:
    Expr<T> *_left;
    Expr<T> *_right;

    BinOp(Expr<T> *left, Expr<T> *right)
    {
        _left = left;
        _right = right;
    }
};

template <typename T>
class Sum : public BinOp<T>
{
public:
    Sum(Expr<T> *left, Expr<T> *right) : BinOp<T>(left, right) {}

    virtual T eval()
    {
        return this->_left->eval() + this->_right->eval();
    }
};

template <typename T>
class Val : public Expr<T>
{
private:
    T _val;

public:
    Val() {}
    Val(T val)
    {
        _val = val;
    }

    virtual int parse(std::string in, int start)
    {
        int i = start;
        while (std::isdigit(in[i]))
        {
            _val = _val * 10 + in[i] - '0';
            i++;
        }
        return i;
    }

    virtual T eval()
    {
        return _val;
    }
};

template <typename T>
Expr<T> *make_val(T val)
{
    return new Val<T>(val);
}

template <typename T>
Expr<T> *make_sum(Expr<T> *op1, Expr<T> *op2) { return new Sum<T>(op1, op2); }

// std::vector<std::string> tokenize(std::string in)
// {
//     std::vector<std::string> tokens;

//     std::string token = "";

//     for (int i = 0; i < in.size(); i++)
//     {
//         char c = in[i];

//         if (c == ' ')
//         {
//             if (token != "")
//             {
//                 tokens.push_back(token);
//                 token = "";
//             }
//             continue;
//         };

//         if (c >= '0' && c < '9')
//         {
//             token += c;
//         }

//         if (c == '+' || c == '*')
//         {
//             tokens.push_back({c});
//         }
//     }

//     return tokens;
// }

// void parse(std::vector<std::string> tokens)
// {
//     std::reverse(tokens.begin(), tokens.end());

//     int op1 = -1;
//     int op2 = -1;

//     while (tokens.size() > 1)
//     {
//         std::string t = tokens.back();
//         std::cout << t << "A" << std::endl;
//         if (t == "+")
//         {
//             std::cout << op1 + op2 << std::endl;
//             tokens.push_back(std::to_string(op1 + op2));
//         }
//         else
//         {
//             if (op1 == -1)
//             {
//                 op1 == std::stoi(t);
//             }
//             else
//             {
//                 op2 == std::stoi(t);
//             }
//         }

//         tokens.pop_back();
//     }
//     // for (int i = 0; i < tokens.size(); i++) {
//     //     std::string token = tokens[i];

//     //     if (token == '+') {

//     //     }
//     // }
// }

int main()
{
    Val<int> *v = new Val<int>();

    v->parse(" 50");

    // std::vector<std::string> ts = tokenize("1 2 +");
    // parse(ts);

    // Expr<int> *myExpr = make_sum(make_val(1), make_val(2));

    std::cout << v->eval() << std::endl;
}
@lparolari lparolari added the triage To be categorized label Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage To be categorized
Projects
None yet
Development

No branches or pull requests

1 participant