Tim Mcd wrote:
So, I am currently working on a MUD (Online multiplayer text based game,
like an MMO). The MUD's scripting language is LPC. I am trying to create
a ruby script, that reads another ruby file, and changes all instances
of 'Sword-of-might = Sword.new(...)' to something like 'static void
create() {...}'. Nothing too fancy, just me being able to go through a
file finding different bits of ruby that I can then replace with the
equivalent LPC code.
An example of how it might work is: Ruby has a table of ruby phrases and
what to turn them into if encountered. Ruby then reads the given file,
and if it finds any of the given phrases, turns them into the
corresponding LPC that I have defined.
So, the problem is, I have no idea how to go about doing something
like this in Ruby. Any thoughts/help or places to look? (tutorials,
etc.)
Hi Tim,
I suspect your phrase replacement scheme will not cover all your needs. You need a more powerfull technique.
May I suggest you structure it like a compiler. Use a tree structure for the first intermediate form, generated (with dictionary of names or symbol table) from the first pass. The second pass would walk this tree, calling first level semantic routines for each node. These would, in their turn call routines (methods in an object) that knows about LPC. The interface between the first and second level back end is important, but need never actually go to disk.
Not knowing the structure of the files, I can't say how much of the structure of Ruby you will have to include. Until that is established, you can't decide if you are going to go top-down or bottom-up - see any book on compilers. I find top-down easeir to undertand, generates more meaningsul error messages, and has better error recovery than bottom-up, but that may be my brain.
By replacing one set of LPC routines with another set, you get an interpreter of the file. This is the same as replacing the LPC object with an interpreting object. Note, if you call the semantic routines earlier, instead of creating the tree form, then compiling may be quicker, but interpretation becomes impossible. You can't recompile each time through a loop, becasue you get problems with the state being changed as well as slow performance.
You might also look at what the LPC compiler actually produces and generate the same. This will give you a way to write your scripts in (a subset of) Ruby.
Regards
Ian