Design question (specifically, script interpreter)

Discussion in 'C++' started by Ian Giblin, Nov 13, 2004.

  1. Ian Giblin

    Ian Giblin Guest

    I am an experienced C programmer, learning C++ by writinging a
    mathematical toolkit in the framework of a script interpreter. I am
    posting here to ask for advice (or references) on the object design
    and implimentation.

    Currently I have a portable "ScriptSession" class which contains the
    mechanics of looping with a user prompt, parsing a sentence and
    handling syntax errors, etc., and I wan this to be a class I can use
    for any script interpreter.

    One of the core functions is the execution of a command sentence. This
    necessarily involves some mathematical commands which should not be
    part of "ScriptSession" so I started by overloading a method called
    interpret, which is virtual in the base class:

    class ScriptSession
    {
    public:
    ...
    virtual bool ScriptSession::execute(void);
    }

    I have then written a method within the final application which
    overload this, but it needs to know a lot about the internals of the
    ScriptSession class so it has become rather ugly and feels like it is
    voilating encpasulation and so on. Also, I have put everything in
    here, even basic stuff like interpretation of the command "exit" which
    is universal, so that won't do.

    I bought "Design Patterns" (Gang of Four) but none of the patterns
    seem to really fit this nicely. Or maybe I'm just missing some of the
    important stuff. I apologise if I haven't explained the problem
    clearly enough; that's part of the problem of course.

    Any advice would be appreciated. Thanks,
    Ian Giblin.
     
    Ian Giblin, Nov 13, 2004
    #1
    1. Advertising

  2. Ian Giblin wrote:
    > I am an experienced C programmer, learning C++ by writinging a
    > mathematical toolkit in the framework of a script interpreter. I am
    > posting here to ask for advice (or references) on the object design
    > and implimentation.


    The first thing to recommend that comes to mind is comp.object
    newsgroup. OO Design has common principles that are not necessarily
    language-specific. Check it out.

    > Currently I have a portable "ScriptSession" class which contains the
    > mechanics of looping with a user prompt, parsing a sentence and
    > handling syntax errors, etc., and I wan this to be a class I can use
    > for any script interpreter.
    >
    > One of the core functions is the execution of a command sentence. This
    > necessarily involves some mathematical commands which should not be
    > part of "ScriptSession" so I started by overloading a method called
    > interpret, which is virtual in the base class:
    >
    > class ScriptSession
    > {
    > public:
    > ...
    > virtual bool ScriptSession::execute(void);


    I thought you said "a method called interpret"... And a small note on
    C++ syntax: you don't need a fully qualified name in a class definition.
    You usually write

    virtual bool execute();

    (oh, yes, 'void' is also superfluous).

    > }
    >
    > I have then written a method within the final application which
    > overload this,


    The correct term is "overrides".

    > but it needs to know a lot about the internals of the
    > ScriptSession class so it has become rather ugly and feels like it is
    > voilating encpasulation and so on.


    Yes, it rather sounds like a bad design.

    > Also, I have put everything in
    > here, even basic stuff like interpretation of the command "exit" which
    > is universal, so that won't do.


    Well, without seeing more of your 'ScriptSession' class (at least its
    interface, the implementation probably doesn't matter much) it is not
    clear what you mean that it needs to know and what exactly are those
    "internals". Can't you provide access to those internals through some
    protected functions?

    > I bought "Design Patterns" (Gang of Four) but none of the patterns
    > seem to really fit this nicely. Or maybe I'm just missing some of the
    > important stuff. I apologise if I haven't explained the problem
    > clearly enough; that's part of the problem of course.


    "Design Patterns" is by no means a "recipes for all occasions" type of
    book. You learn the most common ones so you can recognise patterns in
    your own code and describe them and later apply them.

    > Any advice would be appreciated. Thanks,
    > Ian Giblin.


    Well, take a look at comp.object and possibly ask them for an example
    of an interpreter framework that you could extend. Perhaps somebody
    here knows about it too (I don't), my colleagues whenever an interpreter
    is needed all wrote their own, which seems like an overkill, but that's
    up to them.

    Shake the Web (or google for "interpreter C++ framework"), see if
    something falls out. I bet other folks have written something in that
    area. While it is a good idea to write one for training, it isn't so
    good to get stuck on design when your actual goal is the language.

    Take a look at "Advanced C++. Programming Styles and idioms" by James
    Coplien. It's relatively old, but it's a good from design point of
    view. Helped me to figure some C++ stuff out. It assumes you know C++
    to some extend, of course.

    Good luck! If you decide to continue trying to figure out your stuff
    here, in comp.lang.c++, I strongly recommend making more specific
    questions, a generic topic like "I want to create an interpreter and I
    am stuck, help!" calls for a book rather than a newsgroup message.

    V
     
    Victor Bazarov, Nov 13, 2004
    #2
    1. Advertising

  3. Ian Giblin

    Phlip Guest

    Ian Giblin wrote:

    > I bought "Design Patterns" (Gang of Four) but none of the patterns
    > seem to really fit this nicely. Or maybe I'm just missing some of the
    > important stuff. I apologise if I haven't explained the problem
    > clearly enough; that's part of the problem of course.


    Dr. Phlip Rx's /Refactoring to Patterns/ by Josh Kerievsky.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Nov 13, 2004
    #3
  4. Ian Giblin

    Ian Giblin Guest

    Thanks for the detailed response.

    Victor Bazarov <> wrote in message news:<1drld.333765$wV.15439@attbi_s54>...
    >
    > I thought you said "a method called interpret"...


    Sorry. It annoys *me* when people make weird mistakes like that. So
    now, I'm annoyed with myself for not spotting it when I check the
    post.

    > And a small note on
    > C++ syntax: you don't need a fully qualified name in a class definition.
    > You usually write
    >
    > virtual bool execute();
    >
    > (oh, yes, 'void' is also superfluous).


    <gulp> I see... I'll probably keep the 'void' because it reminds me
    when I'm reading the code, but the class name prefix was a bit silly.

    I'll take your other comments to heart. I will see how far I get with
    this and ask on the object-oriented group if I cannot come up with a
    good framework.

    Regards, Ian.
     
    Ian Giblin, Nov 14, 2004
    #4
  5. Ian,

    Its a bit hard to understand your problem, you've not posted
    much code or information to work on.

    However, you might want to look at TCL. Although this is implemented
    in C ( I think), the basic model might be useful. TCL has a very regular
    syntax so its easy to write an interpreter which can recognize the "command"
    from the "options" and "data". Its then quite easy to add new commands
    by writing a C/C++ package which implements the necessary pieces required
    to register and execute the command. TCL provides the basic looping and
    waiting for commands, then parses, checks for errros, and executes each
    command or script you
    pass in. TCL has a fairly complex API for accessing the guts of the
    interpreter
    and for implementing commands.

    Python is also a model you could look at, as I remember, it was quite easy
    to write an extension
    (package of commands) to python in C/C++ ( or anything other language for
    that matter ) and
    load them into the python interpreter.

    Hope that helps, and good luck.
    dave


    "Ian Giblin" <> wrote in message
    news:...
    > I am an experienced C programmer, learning C++ by writinging a
    > mathematical toolkit in the framework of a script interpreter. I am
    > posting here to ask for advice (or references) on the object design
    > and implimentation.
    >
    > Currently I have a portable "ScriptSession" class which contains the
    > mechanics of looping with a user prompt, parsing a sentence and
    > handling syntax errors, etc., and I wan this to be a class I can use
    > for any script interpreter.
    >
    > One of the core functions is the execution of a command sentence. This
    > necessarily involves some mathematical commands which should not be
    > part of "ScriptSession" so I started by overloading a method called
    > interpret, which is virtual in the base class:
    >
    > class ScriptSession
    > {
    > public:
    > ...
    > virtual bool ScriptSession::execute(void);
    > }
    >
    > I have then written a method within the final application which
    > overload this, but it needs to know a lot about the internals of the
    > ScriptSession class so it has become rather ugly and feels like it is
    > voilating encpasulation and so on. Also, I have put everything in
    > here, even basic stuff like interpretation of the command "exit" which
    > is universal, so that won't do.
    >
    > I bought "Design Patterns" (Gang of Four) but none of the patterns
    > seem to really fit this nicely. Or maybe I'm just missing some of the
    > important stuff. I apologise if I haven't explained the problem
    > clearly enough; that's part of the problem of course.
    >
    > Any advice would be appreciated. Thanks,
    > Ian Giblin.
     
    Dave Townsend, Nov 15, 2004
    #5
    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. Chris Bedford
    Replies:
    1
    Views:
    7,016
    Chris Bedford
    Oct 6, 2003
  2. Pete Jereb
    Replies:
    0
    Views:
    329
    Pete Jereb
    Oct 7, 2003
  3. Steven T. Hatton

    Not specifically C++, but worth sharing

    Steven T. Hatton, Aug 25, 2005, in forum: C++
    Replies:
    2
    Views:
    324
    Gabriel
    Aug 25, 2005
  4. Alien
    Replies:
    6
    Views:
    496
  5. Replies:
    8
    Views:
    406
    James Kanze
    Jul 22, 2007
Loading...

Share This Page