Home
Parsing
Eval

eval6b

It's high time I addressed the problem of multi-line programs. No change to the grammar though. We just need to modify 'GetNextToken()' to fetch more input when it gets to the end of the current line, and do an extra little bit of initialisation in 'reinit()'.

eval6.cpp

GetNextToken()
    ...
    new:        if (StmtIndex >= StmtLen) {         // If we have run out of input, ...
    new:            if (more != NULL && more()) {   // ... try to get some more.
    new:                StmtLen = strlen (statement);
    new:                StmtIndex = 0;              // If we did get more, start at the beginning of the line again.
    new:            }
    new:        }
reinit()
    ...
    old: void proglet_t::reinit (void) {
    new: void proglet_t::reinit (bool more_func (void)) {
    ...
    new:     more = more_func;
    ...
    new:     more = NULL;
eval6b.h
    new:     bool (*more) (void);    // Pointer to a function which will fetch more input.

To make use of this we also need to tweak the way we call things, to give the parser a function to call to fetch more input.
main6b.cpp

    new: FILE        *f;
    new: static char line[1000];
    ...
    new: static bool more (void) {
    new:     return GetLine (f, line, sizeof (line));
    new: }
    ...
    new:         prog.reinit(more);
    ...
    old:         if (GetLine (f, line, sizeof (line))) {
    new:         if (more()) {

If our input doesn't come from a file, we can simply replace this version of more() with one that fills the line buffer from the relevant source. If this line is all there is, we can pass a NULL pointer to reinit().


Eval6b source.
Eval6b header.
Sample application source.
Eval6b data.
Eval6b executable.

Eval
Parsing
Home
Valid HTML 4.01 Any comments or queries, write to me.
Site last updated 4 July, 2004