Execute a command in a separate file
Is it possible to store a "command" in a separate file, and have it called whenever you input the name of that file?
Let's say: file help contains code: cout << "This is help file."; So is it possible to get the content of file without need of creating a functions in it, straight to action, like it is in Lua?
Well, I'm in the process of learning the C++ so embedding Lua won't make sense for me, yet that's a very good idea for my future projects, thank you.
What i'm trying to do is a simulation of command line os. Something like ComputerCraft. Basically you type in a command, the program then, looks for a actual file with same name as user's input and if found executes it's content. It is all done in runtime, check for file, execution, adding new etc.
Something like In console:
Program checks for the file named help and if found executes it Contents of help:
cout << "type help for help, programs for list of programs etc";
I want it that way to make it possible to create a new "command" file right from that simulated os and automatically make it usable with least effort possible for user.
I did it in lua before and I would like to know how and if it's possible in C++ I was working in Love2D, and it has a function love.filesystem.load(filename) basically what it does, it creates a function which has the content of the file as it's container, you assign it a name function = love.filesystem.load(filename) and then execute it as a normal function. What I'm seeking for is equivalent of it.
If you want to execute a script in a file, you will either need to use an existing scripting language and include an interpreter in your program, or invent your own scripting language and code how to interpret it.
If you want the files to contain C++ code to be executed at run-time, this is impossible or so you will be told. Actually it's not impossible; people who tell you that are technically wrong. But it is insanely difficult and definitely not a beginner's project.
You will either need to include a fully-fledged C++ compiler in your program, compile the code in the file at run-time and execute the result. Some virus scanners can get unhappy when a program tries to do that. (Sometimes my one complains about my creations in Visual Studio!)
Or you will need a run-time C++ interpreter. If such a thing exists, I would be very interested to know about it!
Practically speaking the best you could hope for is an interpreter that can work with a very limited subset of C++.