File manipulation

Discussion in 'Ruby' started by Tim Mcd, Jul 31, 2008.

  1. Tim Mcd

    Tim Mcd Guest

    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.)
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Mcd, Jul 31, 2008
    #1
    1. Advertising

  2. On 31.07.2008 20:22, 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.)


    Depends how many phrases you got there. If there are just a few of them
    and they are fixed, you can do something like this:

    phrases = {
    "foo" => "bar",
    "baz" => "boo!",
    }

    dat = File.read input_file

    dat.gsub! Regexp.union(phrases.keys) do |m|
    phrases[m] or raise "Phrase not found: #{m.inspect}"
    end

    File.open out, "w" do |io|
    io.write dat
    end

    Kind regards

    robert
     
    Robert Klemme, Jul 31, 2008
    #2
    1. Advertising

  3. Hi Robert,

    It seems to me that that's a hell of a difficult thing to do.

    Take your Sword.new() call. There are a lot of different ways I
    might code that (especially in Ruby!). A simple search and replace
    will probably not be very reliable.

    s = Sword.new()
    OR
    swords = []; swords << sword.new()
    OR
    Event.new(Sword.new.strike:)martin))

    ...or whatever. And that's just one statement -- it says nothing of
    the multitude of different ways that a given problem can be solved
    using Ruby code.

    Is there any particular reason why you need to translate Ruby to LPC?
    Why not just use Ruby as the in-game language?


    (Confession time: I'm writing a MUSH myself, although mine is really
    just a learning exercise, and, I suspect, a great deal less ambitious
    than your project. For various reasons I'd dismissed the idea of
    users writing code within the game, one of them being that I couldn't
    see of any easy way to protect the game from malicious code. I'd be
    interested on your thoughts on this, either inside or outside the
    list.)

    Shadowfirebird.


    On Thu, Jul 31, 2008 at 7:49 PM, Robert Klemme
    <> wrote:
    > On 31.07.2008 20:22, 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.)

    >
    > Depends how many phrases you got there. If there are just a few of them and
    > they are fixed, you can do something like this:
    >
    > phrases = {
    > "foo" => "bar",
    > "baz" => "boo!",
    > }
    >
    > dat = File.read input_file
    >
    > dat.gsub! Regexp.union(phrases.keys) do |m|
    > phrases[m] or raise "Phrase not found: #{m.inspect}"
    > end
    >
    > File.open out, "w" do |io|
    > io.write dat
    > end
    >
    > Kind regards
    >
    > robert
    >
    >




    --
    Me, I imagine places that I have never seen / The colored lights in
    fountains, blue and green / And I imagine places that I will never go
    / Behind these clouds that hang here dark and low
    But it's there when I'm holding you / There when I'm sleeping too /
    There when there's nothing left of me / Hanging out behind the
    burned-out factories / Out of reach but leading me / Into the
    beautiful sea
     
    Shadowfirebird, Jul 31, 2008
    #3
  4. Sorry: I should have said, "Hi Tim".

    On Thu, Jul 31, 2008 at 9:48 PM, Shadowfirebird
    <> wrote:
    > Hi Robert,
    >
    > It seems to me that that's a hell of a difficult thing to do.
    >
    > Take your Sword.new() call. There are a lot of different ways I
    > might code that (especially in Ruby!). A simple search and replace
    > will probably not be very reliable.
    >
    > s = Sword.new()
    > OR
    > swords = []; swords << sword.new()
    > OR
    > Event.new(Sword.new.strike:)martin))
    >
    > ...or whatever. And that's just one statement -- it says nothing of
    > the multitude of different ways that a given problem can be solved
    > using Ruby code.
    >
    > Is there any particular reason why you need to translate Ruby to LPC?
    > Why not just use Ruby as the in-game language?
    >
    >
    > (Confession time: I'm writing a MUSH myself, although mine is really
    > just a learning exercise, and, I suspect, a great deal less ambitious
    > than your project. For various reasons I'd dismissed the idea of
    > users writing code within the game, one of them being that I couldn't
    > see of any easy way to protect the game from malicious code. I'd be
    > interested on your thoughts on this, either inside or outside the
    > list.)
    >
    > Shadowfirebird.
    >
    >
    > On Thu, Jul 31, 2008 at 7:49 PM, Robert Klemme
    > <> wrote:
    >> On 31.07.2008 20:22, 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.)

    >>
    >> Depends how many phrases you got there. If there are just a few of them and
    >> they are fixed, you can do something like this:
    >>
    >> phrases = {
    >> "foo" => "bar",
    >> "baz" => "boo!",
    >> }
    >>
    >> dat = File.read input_file
    >>
    >> dat.gsub! Regexp.union(phrases.keys) do |m|
    >> phrases[m] or raise "Phrase not found: #{m.inspect}"
    >> end
    >>
    >> File.open out, "w" do |io|
    >> io.write dat
    >> end
    >>
    >> Kind regards
    >>
    >> robert
    >>
    >>

    >
    >
    >
    > --
    > Me, I imagine places that I have never seen / The colored lights in
    > fountains, blue and green / And I imagine places that I will never go
    > / Behind these clouds that hang here dark and low
    > But it's there when I'm holding you / There when I'm sleeping too /
    > There when there's nothing left of me / Hanging out behind the
    > burned-out factories / Out of reach but leading me / Into the
    > beautiful sea
    >
    >




    --
    Me, I imagine places that I have never seen / The colored lights in
    fountains, blue and green / And I imagine places that I will never go
    / Behind these clouds that hang here dark and low
    But it's there when I'm holding you / There when I'm sleeping too /
    There when there's nothing left of me / Hanging out behind the
    burned-out factories / Out of reach but leading me / Into the
    beautiful sea
     
    Shadowfirebird, Jul 31, 2008
    #4
  5. Tim Mcd

    Tim Mcd Guest

    Hmm... could I possibly see your MUSH code? I have pretty much stuck to
    Interactive Fiction with ruby (afraid of networking!) I was wanting to
    port Ruby to LPC just for the fun of it. Being able to create rooms on
    my MUD with Ruby would be fun! ^_^ Oh, and Shadowfirebird, my email is:


    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Mcd, Jul 31, 2008
    #5
  6. On Thu, Jul 31, 2008 at 1:48 PM, Shadowfirebird
    <> wrote:
    > Event.new(Sword.new.strike:)martin))


    hey!!!!

    martin
     
    Martin DeMello, Aug 1, 2008
    #6
  7. Tim Mcd

    Guest

    Event.new(shadowfirebird.cast:)heal, :martin))

    On 8/1/08, Martin DeMello <> wrote:
    > On Thu, Jul 31, 2008 at 1:48 PM, Shadowfirebird
    > <> wrote:
    >> Event.new(Sword.new.strike:)martin))

    >
    > hey!!!!
    >
    > martin
    >
    >



    --
    Me, I imagine places that I have never seen / The colored lights in
    fountains, blue and green / And I imagine places that I will never go
    / Behind these clouds that hang here dark and low
    But it's there when I'm holding you / There when I'm sleeping too /
    There when there's nothing left of me / Hanging out behind the
    burned-out factories / Out of reach but leading me / Into the
    beautiful sea
     
    , Aug 1, 2008
    #7
  8. Tim Mcd

    Guest

    Well, my current working code is only a proof-of-concept thing and
    extremely icky; one big file, with a bunch of stuff hardcoded. But
    you can walk around, pick up and drop stuff, dig rooms, and create
    items. For better or worse, I'll forward it to you.

    My own inspiration came from Jon Lambert's TeensyMud:
    http://sourcery.dyndns.org/wiki.cgi?TeensyMud

    And, even better, this post, again by Jon, that does a MUD in 15 lines
    of Ruby(!): http://redhanded.hobix.com/bits/mudIn15LinesOfRuby.html

    If you're listening, Jon, thanks.

    Shadowfirebird.

    On 7/31/08, Tim Mcd <> wrote:
    > Hmm... could I possibly see your MUSH code? I have pretty much stuck to
    > Interactive Fiction with ruby (afraid of networking!) I was wanting to
    > port Ruby to LPC just for the fun of it. Being able to create rooms on
    > my MUD with Ruby would be fun! ^_^ Oh, and Shadowfirebird, my email is:
    >
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Me, I imagine places that I have never seen / The colored lights in
    fountains, blue and green / And I imagine places that I will never go
    / Behind these clouds that hang here dark and low
    But it's there when I'm holding you / There when I'm sleeping too /
    There when there's nothing left of me / Hanging out behind the
    burned-out factories / Out of reach but leading me / Into the
    beautiful sea
     
    , Aug 1, 2008
    #8
  9. Tim Mcd

    Ian Hobson Guest

    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
     
    Ian Hobson, Aug 1, 2008
    #9
  10. Tim Mcd

    Phlip Guest

    >> Event.new(Sword.new.strike:)martin))
    >
    > hey!!!!
    >
    > martin


    No worries. It's just a symbol.
     
    Phlip, Aug 1, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB

    Resource File Manipulation

    JezB, Feb 25, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    323
  2. Roedy Green

    Zip file manipulation

    Roedy Green, Aug 17, 2003, in forum: Java
    Replies:
    2
    Views:
    865
    Roedy Green
    Aug 17, 2003
  3. John Harrison

    Re: file overwrite data manipulation

    John Harrison, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    5,963
    John Harrison
    Jun 27, 2003
  4. Chris Theis
    Replies:
    0
    Views:
    812
    Chris Theis
    Jun 26, 2003
  5. Darren

    File Manipulation

    Darren, Oct 25, 2003, in forum: C++
    Replies:
    1
    Views:
    339
    Jon Bell
    Oct 25, 2003
Loading...

Share This Page