Seems pretty cool to me. I just wrote a node on programming with data structures which might give you food for thought.

Alternative ways to do it:

If you are thinking of developing a complex system, then at some point you might need to structure your language more, rather than just adding lots of commands with different patterns (sooner or later, you're likely to accidentally have 2 patterns that both match, especially if you load patterns from modules). How you do this is up to you. It might not be a bad idea to use the standard Unix form command [-option [argument...] ...] [command-argument...] and maybe even use one of the Getopt modules to parse the command line?

(Thinking some more) You have two choices:

syntax: centralised/delegated
semantics: centralised/delegated

Where centralised means "handled in the parser" and "delegated" means "handled in the module". I suggest centralised syntax, as much as possible, and delegated semantics. This combines predictability with flexibility. For example, given the input

foo -b baz boot.txt bop.txt

your central parser decides if it is a valid command, then passes it to the module (which has previously registered a "foo" command with the parser). The module decides what to do. All modules inherit from a Module class which provides an overridable method for deciding what to do. (E.g. "call the subroutine foo with the arguments parsed into a perl data structure as follows".)

What I am suggesting has some advantages. Your program could maintain state: e.g. "cd /foo" followed by "ls" prints something different from "ls" on its own. You would implement this by having the module which provides "cd" and "ls" use method calls on an object rather than just subroutine calls.

However, it all depends on how regular the commands are - which depends on what exactly you are trying to do. If everything is very regular, perhaps you can have centralised semantics too and just dispatch to particular subroutines, as now. OTOH, if you want a complex language, you might want to think of a parsing module like Parse::RecDescent

dave hj~


In reply to Re: Complex dispatch table by dash2
in thread Complex dispatch table by castaway

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.