BASIC advice wanted.

Discussion in 'C Programming' started by Malcolm, Jan 3, 2004.

  1. Malcolm

    Malcolm Guest

    After some days' hard work I am now the proud possessor of an ANSI C BASIC
    interpreter.

    The question is, how is it most useful?

    At the moment I have a function

    int basic(const char *script, FILE *in, FILE *out, FILE *err);

    It returns 0 on success or -1 on fail.
    and I'm calling it with stdin, stdout and stderr (err is for reporting
    errors in the script, not for user errors).

    This is fine for test purposes, but realistically it is not going to be
    useful for anything beyond teaching newbies how to program in BASIC.

    The main motive is to use the BASIC interpreter as a component of editors.
    The idea is that a game designer can write a little BASIC program, maybe to
    control the diffusion pattern of smoke particles, or maybe for some
    AI-related stuff. Since the script is interpreted there is no reason to
    recompile.

    However setting up temporary files to pass data in and out of the BASIC
    seems clumsy. There is also no way in C to specify a "user" FILE *,
    something that pops up a Window to get input, for example.

    I'm also not happy with the BASIC INPUT statement, it's fine for stdin, but
    not so good for reading values from a typical formatted text file. However I
    don't want to stray too far from core BASIC, or else the user will have to
    learn a new programming language to use the program.
     
    Malcolm, Jan 3, 2004
    #1
    1. Advertising

  2. "Malcolm" <> wrote in message
    news:bt7btn$niv$...
    > After some days' hard work I am now the proud possessor of an ANSI C BASIC
    > interpreter.


    I doubt it. How do you deal with things like PEEK, POKE, PLOT and all the
    other graphics or sound commands in ANSI C?

    > The question is, how is it most useful?


    No, the question is, what is your C question? ;-)

    Peter
     
    Peter Pichler, Jan 3, 2004
    #2
    1. Advertising

  3. Peter Pichler <> scribbled the following:
    > "Malcolm" <> wrote in message
    > news:bt7btn$niv$...
    >> After some days' hard work I am now the proud possessor of an ANSI C BASIC
    >> interpreter.


    > I doubt it. How do you deal with things like PEEK, POKE, PLOT and all the
    > other graphics or sound commands in ANSI C?


    Perhaps his interpreter interprets a BASIC version which doesn't have
    them? ANSI BASIC is incredibly small. It's even smaller than C=64 BASIC
    V2.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The question of copying music from the Internet is like a two-barreled sword."
    - Finnish rap artist Ezkimo
     
    Joona I Palaste, Jan 3, 2004
    #3
  4. "Joona I Palaste" <> wrote:
    >
    > ANSI BASIC is incredibly small.


    ANSI BASIC? One learns something new every day!
     
    Peter Pichler, Jan 3, 2004
    #4
  5. Peter Pichler <> scribbled the following:
    > "Joona I Palaste" <> wrote:
    >> ANSI BASIC is incredibly small.


    > ANSI BASIC? One learns something new every day!


    Yes. I didn't know about it either until Dan Pop told me. Apparently
    it's even missing the IF... THEN structure. The only thing ANSI BASIC
    can do in an IF statement is a GOTO. Several other BASIC dialects allow
    calling other statements too.
    (Though I would be surprised if this was valid BASIC:)

    10 FOR I=1 TO 10
    20 IF I<9 THEN NEXT I

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "As a boy, I often dreamed of being a baseball, but now we must go forward, not
    backward, upward, not forward, and always whirling, whirling towards freedom!"
    - Kang
     
    Joona I Palaste, Jan 3, 2004
    #5
  6. Joona I Palaste <> scribbled the following:
    > Peter Pichler <> scribbled the following:
    >> "Joona I Palaste" <> wrote:
    >>> ANSI BASIC is incredibly small.


    >> ANSI BASIC? One learns something new every day!


    > Yes. I didn't know about it either until Dan Pop told me. Apparently
    > it's even missing the IF... THEN structure. The only thing ANSI BASIC
    > can do in an IF statement is a GOTO. Several other BASIC dialects allow
    > calling other statements too.
    > (Though I would be surprised if this was valid BASIC:)


    > 10 FOR I=1 TO 10
    > 20 IF I<9 THEN NEXT I


    Well, C=64 BASIC V2 seems to accept it... it's treating "NEXT I" as
    "increment I and go back to the FOR command". So therefore the above
    is equivalent to:

    10 FOR I=1 TO 10
    20 IF NOT(I<9) THEN END
    30 NEXT I

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
     
    Joona I Palaste, Jan 3, 2004
    #6
  7. Malcolm

    Malcolm Guest

    "Peter Pichler" <> wrote in message
    > > After some days' hard work I am now the proud possessor of an
    > > ANSI C BASIC interpreter.

    >
    > I doubt it. How do you deal with things like PEEK, POKE, PLOT
    > and all the other graphics or sound commands in ANSI C?
    >

    It's a cut down BASIC without any hardware-specific commands.
    >
    > > The question is, how is it most useful?

    >
    > No, the question is, what is your C question? ;-)
    >

    That's the C question. Given that I've got a BASIC interpreter, what is the
    most useful interface to the rest of the C program that calls it?
     
    Malcolm, Jan 4, 2004
    #7
  8. Malcolm <> scribbled the following:
    > "Peter Pichler" <> wrote in message
    >> No, the question is, what is your C question? ;-)
    >>

    > That's the C question. Given that I've got a BASIC interpreter, what is the
    > most useful interface to the rest of the C program that calls it?


    Your BASIC interpreter is written in ANSI C, but is your program around
    it also written in ANSI C? If not, then you can use a non-standard "pipe
    stream" and pass it as an argument to your interpreting function. That
    way you can send data between your outer program and your interpreter
    efficiently.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "That's no raisin - it's an ALIEN!"
    - Tourist in MTV's Oddities
     
    Joona I Palaste, Jan 4, 2004
    #8
  9. On Sun, 04 Jan 2004 10:43:19 +0000, Joona I Palaste wrote:

    > Malcolm <> scribbled the following:


    >> Given that I've got a BASIC interpreter, what is the
    >> most useful interface to the rest of the C program that calls it?

    >
    > Your BASIC interpreter is written in ANSI C, but is your program around
    > it also written in ANSI C? If not, then you can use a non-standard "pipe
    > stream" and pass it as an argument to your interpreting function. That
    > way you can send data between your outer program and your interpreter
    > efficiently.


    How about callback functions? Something like:

    typedef int (*input_handler)(char**);
    typedef int (*output_handler)(const char*);
    typedef int (*error_handler)(int,int,char*); /* errno, line, text */

    int basic(const char *script,
    input_handler ih,
    output_handler oh,
    error_handler eh);

    Giving the calling program the opportunity to define functions to deal
    with input, output or errors. These programs may then write to file or
    process the error or do whatever they feel like.

    (And you can provide handler functions that writes to files if that's
    what the user wants them to do.)


    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
     
    Nils Petter Vaskinn, Jan 4, 2004
    #9
  10. Nils Petter Vaskinn <> scribbled the following:
    > On Sun, 04 Jan 2004 10:43:19 +0000, Joona I Palaste wrote:
    >> Malcolm <> scribbled the following:
    >>> Given that I've got a BASIC interpreter, what is the
    >>> most useful interface to the rest of the C program that calls it?

    >>
    >> Your BASIC interpreter is written in ANSI C, but is your program around
    >> it also written in ANSI C? If not, then you can use a non-standard "pipe
    >> stream" and pass it as an argument to your interpreting function. That
    >> way you can send data between your outer program and your interpreter
    >> efficiently.


    > How about callback functions? Something like:


    > typedef int (*input_handler)(char**);
    > typedef int (*output_handler)(const char*);
    > typedef int (*error_handler)(int,int,char*); /* errno, line, text */


    > int basic(const char *script,
    > input_handler ih,
    > output_handler oh,
    > error_handler eh);


    > Giving the calling program the opportunity to define functions to deal
    > with input, output or errors. These programs may then write to file or
    > process the error or do whatever they feel like.


    > (And you can provide handler functions that writes to files if that's
    > what the user wants them to do.)


    I think this design is good. If this were Java, I'd design the BASIC
    interpreter so that I/O is handled by calling interfaces, one called
    Input, the other called Output. Implementors of the outside system
    would have to implement these interfaces and pass the implementations
    as parameters to the interpreter object.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It's not survival of the fattest, it's survival of the fittest."
    - Ludvig von Drake
     
    Joona I Palaste, Jan 4, 2004
    #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. Junpei
    Replies:
    1
    Views:
    497
    Jim Gibson
    May 27, 2004
  2. PCC
    Replies:
    0
    Views:
    351
  3. Harvey
    Replies:
    0
    Views:
    790
    Harvey
    Jul 16, 2004
  4. Harvey
    Replies:
    1
    Views:
    896
    Daniel
    Jul 16, 2004
  5. Frank Millman
    Replies:
    10
    Views:
    636
    Frank Millman
    Jul 14, 2003
Loading...

Share This Page