Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am 99% certain this will require code compiling at runtime, but it is necessary.

I am thinking of making a very simple app that allows advanced users to write in a script-like language in order to customize certain things (basically a build-your-own-app app). This includes OpenGL shaders, custom launch animations, and so on. The thing is that I am purposefully making the language very robust, and, while it is working perfectly, it is very slow. I was thinking about simply translating the script into C code (prototype is already working smoothly) and turning that C code into a code block that can be saved and loaded whenever I need it.

I can do this with the Cocoa/Cocoa-Touch 'NSDictionary' but the overhead in Objective-C really bothers me. I am afraid that the complex code created with Objective-C will be too costly to run like a norma app. You may say this is not going to cause problems, but the constant re-interpreting and numerous function calls that I am already doing will force me to save every byte I have at my disposal. That left me with one option, as far as I can see. I need to compile the code and integrate it into the executable. My question is this: can I compile code stored in a char array and store that in a code block immediately without a relaunch? If so, how?

Note: This app is being made to run on Mac OS 10.6 and higher. It is currently compatible with all versions of Linux and Windows 7 as well, and I would like to keep it that way for as long as possible so it can be released on those operating systems. I do not want to force potential users to get a different operating system if they want to use this app. I can and will sort out all of the other complications, but this issue is confusing me too much.

share|improve this question
add comment

1 Answer 1

You are looking for a scripting language that can be embedded into your application. That is, a language which is designed to be interpreted at runtime (in many cases quite efficiently) and can be used to allow user extension of your native code written in C/C++/ObjC.

There are many to choose from. Lua and Python are two quite popular ones, particularly in games. There are also embeddable libraries for JavaScript, not to mention Ruby, TCL, LISP, C#, and countless others.

If for some reason you are dead set on using a language like C, C++, or Objective-C, your options are much more limited. First, just realize that it's a bad idea anyhow. Only "real programmers" are comfortable with languages like C/C++/ObjC. Really, only "real programmers" are generally comfortable with any language, even "made for beginner" scripting languages (programming is programming, and programming requires a level of knowledge, expertise, and so on to do even slightly competently), but it's more so for systems level languages. If you want to interpret a subset of C or C++ at runtime, there is CINT/Clint, which are interpreters built off of Clang.

If you want an offline compilation process, you can of course just save your code snippet out to a file and invoke an actual compiler. On OS X, you can invoke GCC or Clang as a sub-process to compile code into a shared library, which you can then dynamically load using dlopen()/dlsym()/dlclose(). Again, though, I do not recommend this approach at all, unless you have very particular needs.

Embedding Lua, Python, or so on will let you keep compatibility with OS X and Windows, not to mention Linux, iOS, Android, and consoles. Lua is used by a number of commercial games as their scripting/logic engine, and Python has seen use in commercial games too. C# is popularized thanks to XNA, and is used as the extension/scripting language of Unity 3D.

You can also build your own from scratch, but clearly the dictionary approach you mentioned is going to be non-optimal for all but the simplest approaches. How to write your own scripting language is a very, very involved topic; I could answer dozens of specific questions on that topic and still barely scratch the surface.

My recommendation there, if you are so inclined as to learn how to design a language interpret, is to start researching the existing Open Source scripting languages (which is practically all of them). There is a lot design documentation and developer discussion history (via mailing list archives and the like) for almost every major scripting language in use today, not to mention all of the code is available online. Lua is by far the smallest and simplest of the popular extension libraries, although I would not consider its code to be the best for learning from.

I do not, sadly, know of any resources on getting started with writing a custom script language. There are some classic texts on general compiler design around, which you can easily find via Google.

share|improve this answer
1  
This question over on Programmers has answers with some good advice on writing your own custom scripting language. –  Eric Dec 29 '12 at 9:33
    
@Eric: Wow, I've never seen one of the books in the answers, and its cover text makes it sound pretty thorough. I'll have to get a copy after I fiscally recover from the winter Steam/GOG sales. Thank you. :) –  Sean Middleditch Dec 29 '12 at 9:55
1  
I also recommend using an existing scripting language - first, someone has gone through all the trouble for you, and second, if it's a popular scripting language, there will already be people who know it. I also wouldn't be so worried about performance; see pygame or love2d for examples. –  Jari Komppa Dec 29 '12 at 11:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.