views:

58

answers:

3

I'm creating a game in XNA and was thinking of creating my own scripting language (extremely simple mind you). I know there's better ways to go about this (and that I'm reinventing the wheel), but I want the learning experience more than to be productive and fast.

When confronted with code at run time, from what I understand, the usual approach is to parse into a machine code or byte code or something else that is actually executable and then execute that, right? But, for instance, when Chrome first came out they said their JavaScript engine was fast because it compiles the JavaScript into machine code. This implies other engines weren't compiling into machine code.

I'd prefer not compiling to a lower language, so are there any known modern techniques for parsing and executing code without compiling to low level? Perhaps something like parsing the code into some sort of tree, branching through the tree, and comparing each symbol and calling some function that handles that symbol? (Wild guessing and stabbing in the dark)

+2  A: 

I personally wouldn't roll your own parser ( turning the input into tokens ) or lexer ( checking the input tokens for your language grammar ). Take a look at ANTLR for parsing/lexing - it's a great framework and has full source code if you want to dig into the guts of it.

For executing code that you've parsed, I'd look at running a simple virtual machine or even better look at llvm which is an open-source(ish) attempt to standardise the virtual machine byte code format and provide nice features like JITing ( turning your script compiled byte code into assembly ).

I wouldn't discourage you from the more advanced options that you machine such as native machine code execution but bear in mind that this is a very specialist area and gets real complex, real fast!

Earlz pointed out that my reply might seem to imply 'don't bother doing this yourself. Re-reading my post it does sound a bit that way. The reason I mentioned ANTLR and LLVM is they both have heaps of source code and tutorials so I feel this is a good reference source. Take it as a base and play

zebrabox
Umm, "learning experience" was mentioned. Also, I wrote an interpreter in C# and it was actually fairly easy... (as in, I had it up and turing complete within like 2-3 weeks of programming)
Earlz
@Earlz For sure but both of those tools I mentioned have source code and are good general resources if you're interested in the field. Don't want to discourage 'roll your own' but always nice to have a base to refer to
zebrabox
Thanks for the links. Seeing source code definitely should give a feel for how parsing and lexing are done
Bob
@zebrabox, ok I removed my -1. I thought you were discouraging rolling your own.. Using them as a source code reference is a whole other thing :)
Earlz
A: 

You can try this framework for building languages (it works well with XNA): http://www.meta-alternative.net/mbase.html

There are some tutorials: http://www.meta-alternative.net/calc.pdf

http://www.meta-alternative.net/pfront.pdf

SK-logic
A: 

Python is great as a scripting language. I would recommend you make a C# binding for its C API and use that. Embedding Python is easy. Your application can define functions, types/classes and variables inside modules which the Python interpreter can access. The application can also call functions in Python scripts and get a result back. These two features combined gives you a two-way communication scheme.

Basically, you get the Python syntax and semantics for free. What you would need to implement is the API your application exposes to Python. An example could be access to game logic functions and render functions. Python scripts would then define functions which calls these, and the host application would invoke the Python functions (with parameters) to get work done.

EDIT: Seems like IronPython can save you even more work. It's a C# implementation of CPython, and has its own embedding API: http://www.ironpython.net/

Mads Elvheim