Writing an app that previews the result of a small part of code

Discussion in 'Mac Programming' started by Soulstorm, Aug 30, 2012.

  1. Soulstorm macrumors 68000


    Feb 1, 2005
    This may seem strange, so I will try to explain it as best I can:

    I want to write an application that will accept some code as an input and will produce a visual output. The input will be in Objective C and the output will be the output that this code describes.

    The output may be text or graphics based, it doesn't matter. What matters is that I don't know how can I make this input be handled as Objective C code and be executed by the system as such.

    Can anyone point me in the right direction?
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    I assume you mean the input is Objective-C source code. That is, a program or code fragment to be evaluated.

    If you do mean source code, you have two options: compile it, or interpret it.

    Interpreting it requires an Objective-C interpreter, and I don't know that such a thing exists. So set that aside as a possible solution.

    Compiling it requires an Objective-C compiler, which clearly do exist.

    The next thing is to answer the question, "How do you make it compilable?". The short answer is "Put it in a file". Now you can compile it and run it. Hand-wave here about exactly how to do those latter two.

    So now you have to figure out how to put it in a file. You also have to figure out how to recognize different possible inputs, varying from a code fragment, to a complete function, to a possible class or classes. That by itself isn't an easy thing to do.

    Next, you have to figure out how to identify what kind of output the source code produces: text or graphical. Also not an easy thing to do, especially since graphical output needs a graphical drawing context to draw into. There's no such thing as an isolated graphical output, at least not in the same way that text output can be isolated.

    TL;DR - It's a complex and difficult problem to solve.
    Please explain exactly what you're trying to accomplish.
  3. Soulstorm thread starter macrumors 68000


    Feb 1, 2005
    Thanks for your answer:

    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.
  4. KnightWRX macrumors Pentium


    Jan 28, 2009
    Quebec, Canada
    The answer depends on what type of code. If you're talking about Quartz, it's a standard C API, as such, you'll have to learn about function pointers, how to parse a string by components to get your users function and seperate it from the different parameters.

    Basically, you'll have to build a big hash containing the string representations of the Quartz functions as keys and the function pointer to use as a value. You'll look up the function name entered by the user to get its pointer and convert any parameters from strings to proper int/float/string/pointer values and pass them along to your function pointer as you execute it.

    For Objective-C objects and methods, it's a bit easier. NSSelectorFromString() and NSObject's respondsToSelector and performSelector should help you build something, but getting object instances to manipulate will be left up to you (so if say the user passes a line containing a NSString pointer declaration with initilialisation, you'll have to create a new instance yourself with which to run his subsequent code).

    It's all doable, albeit, a big pain for very little value.
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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:
    #import <imports-go-here>
    void main()
      setup code goes here;
      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.
  6. jared_kipe macrumors 68030


    Dec 8, 2003
    This is true.
    If you only expect a handful of functions and no control structures, the process would be pretty trivial.

Share This Page