Can C ++ also be interpreted instead of compiling?

I know that interpreting C ++ code may not have practical value, and this question is intended only for entertainment and learning purposes.

Is it possible to interpret a C ++ code operator with an operator instead of compiling it? Also explain the reason for the answer.

If this is not possible, is there a subset of the language that can be interpreted?

+4
source share
2 answers

It depends on what you mean by the expression "statement by statement". In most cases, C ++ is strictly the upper language: if you wanted to use something, you had to declare or define it earlier. Therefore, there is no problem.

However, there are exceptions to the top-down approach. For example, the body of a class member function sees declarations of members of class data that lexically follow it in the source code. You can call the inline function, which was declared but not yet defined in the translation block (the definition must appear before the end of TU).

They may or may not violate your concept of โ€œexpression by expression,โ€ depending on what the concept is.

EDIT based on your comment:

If the interpreter has no idea about the current statement, it cannot hope to interpret C ++ code. Counterexamples using the problem points above:

 #include <iostream> struct C { void foo() { std::cout << i << '\n'; } int i; }; int main() { C c; ci = 0; c.foo(); } 

or

 #include <iostream> inline void foo(); int main() { foo(); } inline void foo() { std::cout << "x\n"; } 

It should not even include built-in functions:

 extern int i; int main() { return i; } int i = 0; 
+4
source

There is no clear boundary between compilation and interpretation. Most languages โ€‹โ€‹that are usually considered interpreted are actually compiled for some kind of virtual machine. The same can be done for C ++.

+3
source

Source: https://habr.com/ru/post/1500906/


All Articles