Geeth W Geeth W - 5 months ago 30
Node.js Question

Why cant I use c++ syntax on Node.JS terminal?

I always wondered why I can't use C++ syntax on the Node.js terminal.

For example, V8 is a JavaScript engine that implements ECMAScript. Node.js itself is a C++ program that tells the V8 engine to use it's function. So these function are written C++ code hence this application should be able to take in C++ native syntax. Meaning if I were to type C++ code in the Node.js terminal, then the Node.js terminal should be able to pass those syntax to the C++ modules in the Node.js core and output successfully. But instead it gives a :

SyntaxError: Unexpected identifier
at Object.exports.createScript (vm.js:24:10)
at REPLServer.defaultEval (repl.js:236:25)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:441:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:188:7)
at REPLServer.Interface._onLine (readline.js:224:10)
at REPLServer.Interface._line (readline.js:566:8)
at REPLServer.Interface._ttyWrite (readline.js:843:14)


I am just looking for a clear explanation how the two languages are abstracted although Node.js is written on top of C++.

Answer

Just because a program is written in C++, doesn't mean it can interpret C++ code. Would you expect this program to understand C++ code when you run it?

#include <iostream>

int main()
{
    std::string line;
    while (std::getline(std::cin, line)) {
        std::cout << line << '\n';
    }
}