in reply to Complex dispatch table
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~
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Complex dispatch table
by castaway (Parson) on Feb 20, 2003 at 10:51 UTC | |
by dash2 (Hermit) on Feb 20, 2003 at 11:29 UTC | |
by castaway (Parson) on Feb 20, 2003 at 13:05 UTC | |
by Thelonius (Priest) on Feb 20, 2003 at 12:42 UTC |