0

I am trying to solve some exercises but have been stuck for a very long time now and can't seem to find any solutions online nor on the exercise page, so I hope someone can help me understand and solve this.

I was given the following function that I were to create a class to that accepts the syntax that is used in the function.

container_t example_1() {
    auto value1 = 5.0;
    auto value2 = 30.0;

    auto v = container_t{};
    auto env = v.environment();

    auto A = v("A", 1);
    auto B = v("B", 0);
    auto C = v("C", 0);

    v(A + B >>= C, value1);
    v(C >>= B + A, value2);

    return v;

The idea is that v("A", 1); should save the alias A to a symbol table with its corresponding value. That I seem to have figured out.

The hard part for me is how I can also allow the expression: A + B >>= C whilst saving it as an class object of some sort, or perhaps just to a different symbol table. Please note that the >>= operator should be overloaded and is to be considered as an arrow -> indicating that if you take A + B you get C.

I've created the following class and operator overload but am missing the expression part. Any help would be appreciated!

class container_t {
    environment env;

public:
    environment* environment() {
        return &env;
    }

    container_t() { };

    template <typename T, typename Data = std::size_t>
    container_t& operator()(T molecule, Data value) {
        if (is_string_v<T>) {
            env.insertMoleculeToSymbolTable(molecule, value);
            return *this;
        } else if (std::is_arithmetic<T>::value) {
            // I never get this far ... 
            std::cout << "Is reaction" << std::endl;
        }
    }
};
  • You seem to be asking how to write an interpreter for arithmetic expressions. You should read a book on compiler construction – n. m. could be an AI May 11 '21 at 05:25
  • see [link](https://stackoverflow.com/questions/1515399/can-you-make-custom-operators-in-c) – debanshu das May 11 '21 at 05:33
  • @debanshudas The exercises explicitly said not to use anything a part from the STL. So this unfortunately does not help me much .. – dennis401 May 11 '21 at 05:42
  • First overload `operator+()` that would return an object of class `T`. For class `T` overload `operator>>=()` that would return an object of type `S`. I don't understand the problem you're having. An example (a kind of): https://godbolt.org/z/7bMb5s6E6 – Evg May 11 '21 at 06:04

0 Answers0