I am trying to create some kind of a tester. The user will give in some lines of code in Objective C, and he will see a graphical result, i.e. he gives some Quartz lines of code and he sees on the right of the screen what these lines draw. No functions, no classes, no fancy stuff, just sequential lines of code.
Please post examples of the lines of code. Post a simple example as well as a complex example.
KnightWRX's post basically boils down to "write an interpreter". Personally, I'm not sure a simple mapping of names to function-ptrs would work well enough. For example, if there are variables, as there likely will be for anything involving paths, contexts, or any other data types, then that takes more than just function-ptrs.
If you've never written an interpreter before, it will be an eye-opening learning experience. You'll question your sanity in doing it at least a half-dozen times. But you'll learn a lot even if it takes way longer than expected, or even if you give up in exasperation.
Another strategy is to take the input, wrap it in a standardized function template, then compile that into an executable and run it. But without seeing examples of what kind of code is expected, there's no way to know if that would work.
The gist of it is simple. You have your input source code as text, call it X. You then write a file that looks like this:
Code:
#import <imports-go-here>
void main()
{
setup code goes here;
X
teardown code goes here;
}
Then you run the compiler on that file and either get compiler error messages or a runnable program. If you get a runnable program, you run it and get the output.
The setup and teardown probably won't be easy to figure out. Nor will the "get the output" part, because the compiled program is running as a separate process, yet the output you want is the drawing that was done in that process. So you have to figure out which of Quartz's drawing contexts will produce drawn output that can be transferred between processes. Maybe the PDF-file output context will work. That's just a guess.
Another approach instead of a separate process with its own main() is to make the template be a loadable library or bundle. Then you compile it, load it, and it runs in the original hosting process. It'd be something like a compiled-on-the-fly plugin. You'd have to look up how to do all that, because compiled-on-the-fly plugins aren't a typical scenario, so you shouldn't expect to find a lot of examples.
This approach, as either a separate process or plugins, might not be easier than writing an interpreter.
I'm using the word "template" in the sense of a source file with some parts predefined. I don't mean any class-template, C++ template, or anything else.