-1

Say i have a string var x = 2 + 3 ; i can convert it into expression in javasript using eval(); is there any way to convert string into executable c++ expression same as eval ( using built-in function or customize code ) ? what i mean to do is try to find out result of a one variable linear equation . I found this code in c++ forum

#include <iostream>

struct VAR{
    float i;
};

struct LINE{//k*x+a
    float a, k;
    VAR* x;

    LINE(){}
    LINE(int a) : a(a), k(0), x(0) {}
    LINE(VAR& v) : a(0), k(1), x(&v) {}
};

LINE operator + (LINE A, LINE B){//assumes that A.x == 0 or B.x == 0 or A.x == B.x
    LINE R;
    R.a = A.a + B.a;
    R.k = A.k + B.k;
    if(A.x) R.x = A.x;
    else R.x = B.x;
    return R;
}

LINE operator - (LINE A, LINE B){//same as +
    LINE R;
    R.a = A.a - B.a;
    R.k = A.k - B.k;
    if(A.x) R.x = A.x;
    else R.x = B.x;
    return R;
}

LINE operator * (LINE A, LINE B){//assumes that A.x == 0 or B.x == 0
    LINE R;
    R.a = A.a * B.a;
    R.k = A.k * B.a + B.k * A.a;
    if(A.x) R.x = A.x;
    else R.x = B.x;
    return R;
}

LINE operator / (LINE A, LINE B){//assumes that B.x == 0
    LINE R;
    R.a = A.a / B.a;
    R.k = A.k / B.a;
    R.x = A.x;
    return R;
}

void operator == (LINE A, LINE B){
    LINE C = A - B;
    C.x->i = -C.a/C.k;
}

int main(){
    VAR x;
    5 == (2 + (x-7)*10)/2;

    std::cout << "x = " << x.i;
    std::cin.get();

    return 0;
}

its work fine. Now what i want to do is execute this "5 == (2 + (x-7)*10)/2;" statement as eval in c++ .

Edit 1: Thank you all , problem is solved :)

Shakil
  • 4,520
  • 3
  • 26
  • 36
  • Saving it to `hoge.cpp` with proper function definiton header & footer? – MikeCAT May 03 '16 at 06:15
  • 2
    Possible duplicate of [Is there C/C++ equivalent of eval("function(arg1, arg2)")?](http://stackoverflow.com/questions/11078267/is-there-c-c-equivalent-of-evalfunctionarg1-arg2) – cf- May 03 '16 at 06:15
  • The "do what I mean" function hasn't been invented yet. – Art May 03 '16 at 06:15
  • 1
    So you want to **parse** the expression and **compile** it into something that you can **interpret**? – Some programmer dude May 03 '16 at 06:16
  • @Art: but `eval` is available in many languages. – Matteo Italia May 03 '16 at 06:17
  • @MatteoItalia Yes, in *dynamic* or *interpreted* languages. Not in statically compiled languages. – Some programmer dude May 03 '16 at 06:18
  • Voting to reopen, it's quite clear what he is asking. The fact that in C++ is not trivial to achieve is irrelevant. – Matteo Italia May 03 '16 at 06:19
  • @JoachimPileborg: think again. In C# it's entirely possible. – Matteo Italia May 03 '16 at 06:19
  • @MatteoItalia I agree that this isn't an unclear question, but I do think it should be closed as a dup (unless there's some major difference between this and the question I VTC'd as dup?). Not sure if reopen-reclose is worth it. – cf- May 03 '16 at 06:21
  • @MatteoItalia Just because C# (and its sibling languages) compiles into single executable files doesn't mean it's not a dynamic and basically interpreted language (just like Java). You might want to read a little more about the [Common Language Infrastructure](https://en.wikipedia.org/wiki/Common_Language_Infrastructure). – Some programmer dude May 03 '16 at 06:27
  • 1
    @JoachimPileborg: interpretation (whatever you mean by that) has literally nothing to do with being able to generate code at runtime, I don't see why you insist on that. Compiling to MSIL instead of native code is just an accident (and actually, once the JIT has done its work it goes down to native code, there's no opcode dispatch loop). You could build your C++ `eval` bundling - say - `libclang` in your C++ runtime, making it generate machine code in memory adhering to the platform calling convention, and returning a function pointer to its the entrypoint. – Matteo Italia May 03 '16 at 06:42
  • @computerfreaker: it's mostly a "political" reopen. I see way too often this attitude in the C++ community - if some task is "difficult" for some inherent weakness (in whatever sense) of the language the question is quickly downvoted or closed as "unclear what you are asking". If this was asked in - say - the Python tag everybody would be replying (or appropriately dupe-closing) with `eval`. – Matteo Italia May 03 '16 at 06:49
  • @JoachimPileborg: anyhow, my original comment was not addressed at you, neither was discussing the difficulties of implementing `eval` in C++. I was just saying that Art's comment was plain stupid - it's not a "do what I mean" function that was asked here, but an `eval`-like feature, which is neither unreasonable to ask for nor impossible to implement in general (as testified by the fact that many higher-level languages provide it). That's it. – Matteo Italia May 03 '16 at 06:51
  • @MatteoItalia `eval` generally evaluates an expression in the same language. `var x = 2 + 3;` doesn't look like a C++ expression to me. That was my point. – Art May 03 '16 at 07:17

1 Answers1

0

Alas, as Joachim pointed out, C++ is a statically compiled language, not an interpreted or dynamically compiled language like Java, Python, ... are. So, there is no such a function eval() in standard C++.

However, you may be interested in Embedded Ch, an embeddable C++ interpreter that provides such eval() function and more. It is not free software, though.

shrike
  • 4,449
  • 2
  • 22
  • 38