Original Parser

version 1 by Ron Newcomb

  • Home page
  • Beginning
  • Previous
  • Next



  • Section - Snippets

    To say (snipped - a snippet) (this is PrintSnippet):
        if the snipped is invalid or the ending position of the snipped is greater than the word count:
            unless the snipped is the empty snippet, issue the cannot say invalid snippets run-time problem for the snipped as "Attempt to say a snippet value which is currently invalid.";
            stop;
        say the player's input buffer from the left edge of the snipped to the right edge of the snipped.

    [ PrintSnippet snip from to i w1 w2;
        w1 = snip/100; w2 = w1 + (snip%100) - 1;
        if ((w2<w1) || (w1<1) || (w2>WordCount())) {
            if ((w1 == 1) && (w2 == 0)) rfalse;
            return RunTimeProblem(RTP_SAYINVALIDSNIPPET, w1, w2);
        }
        from = WordAddress(w1);
        to = WordAddress(w2) + WordLength(w2) - 1;
        for (i=from: i<=to: i++) print (char) i->0;
    ]

    To cut (snipped - a snippet): replace the snipped with "".

    To replace (old words - a snippet) with (new text - some text) (this is SpliceSnippet):
        if the old words are invalid:
            unless the old words are the empty snippet, issue the cannot splice invalid snippets run-time problem for the old words as "Attempt to splice a snippet value which is currently invalid.";
            stop;
        save the pending paragraph break;
        save the paragraphing status;
        copy the new text into the secondary input buffer;
        let the new words' length be the number of elements in the secondary input buffer;
        replace the old words with the new words' length in letters from the 1st [position];
        restore the paragraphing status;
        restore the pending paragraph break.

    [ SpliceSnippet snip t i w1 w2 nextw at endsnippet newlen;
        w1 = snip/100; w2 = w1 + (snip%100) - 1;
        if ((w2<w1) || (w1<1)) {
            if ((w1 == 1) && (w2 == 0)) return;
            return RunTimeProblem(RTP_SPLICEINVALIDSNIPPET, w1, w2);
        }
        @push say__p; @push say__pc;
        nextw = w2 + 1;
        at = WordAddress(w1) - buffer;
        if (nextw <= WordCount()) endsnippet = 100*nextw + (WordCount() - nextw + 1);
        buffer2-->0 = INPUT_BUFFER_LEN;
        newlen = VM_PrintToBuffer(buffer2, INPUT_BUFFER_LEN, SpliceSnippet__TextPrinter, t, endsnippet);
        for (i=0: (i<newlen) && (at+i<120): i++) buffer->(at+i) = buffer2->(WORDSIZE+i);
        #Ifdef TARGET_ZCODE; buffer->1 = at+i; #ifnot; buffer-->0 = at+i; #endif;
        for (:at+i<120:i++) buffer->(at+i) = ' ';
        VM_Tokenise(buffer, parse);
        players_command = 100 + WordCount();
        @pull say__pc; @pull say__p;
    ]

    [ SpliceSnippet__TextPrinter t endsnippet;
        PrintText(t);
        if (endsnippet) { print " "; PrintSnippet(endsnippet); }
    ]

    [ Very like a relation, but checking it must set "the matched text" as a side-effect. ]
    [ Remember the case where "the matched text" is itself passed into this function. ]
    To decide if (snipped - a snippet) includes (T - a topic) (this is SnippetIncludes):
        if the snipped is invalid:
            unless the snipped is the empty snippet, issue the cannot include invalid snippets run-time problem for the snipped as "Attempt to match a snippet value which is currently invalid.";
            now the matched text is the empty snippet;
            no;
        unless T is currently a rule: [ might happen if there's a blank row in a table with a topic column. ]
            [issue the bad topic run-time problem as "Attempt to see if a snippet of text matches something which is not a topic.";]
            now the matched text is the empty snippet;
            no;
        repeat with spot running from the position part of the snipped to the ending position of the snipped:
            let the match attempt be T applied to the spot;
            if the match attempt is not parse fails:
                now the matched text is the insertion point at the spot lengthened by the parser's current word position - the spot;
                yes;
        now the matched text is the empty snippet;
        no.

    To decide if (S - a snippet) does not include (T - a topic): if S includes T, no; otherwise yes.


    [ SnippetIncludes test snippet w1 w2 wlen i j;
        w1 = snippet/100; w2 = w1 + (snippet%100) - 1;
        if ((w2<w1) || (w1<1)) {
            if ((w1 == 1) && (w2 == 0)) rfalse;
            return RunTimeProblem(RTP_INCLUDEINVALIDSNIPPET, w1, w2);
        }
        if (metaclass(test) == Routine) {
            wlen = snippet%100;
            for (i=w1, j=wlen: j>0: i++, j-- ) {
                if (((test)(i, 0)) ~= GPR_FAIL) return i*100+wn-i;
            }
        }
        rfalse;
    ]


    To decide if (snipped - a snippet) matches (T - a topic) (this is SnippetMatches):
        now the parser's current word position is 1;
        if T is currently blank, no;
        if T is currently a rule:
            let the match attempt be T applied to the snipped;
            decide on whether or not the match attempt is not parse fails;
        issue the bad topic run-time problem as "Attempt to see if a snippet of text matches something which is not a topic.";
        no.

    To decide if (S - a snippet) does not match (T - a topic): if S matches T, no; otherwise yes.

    [ SnippetMatches snippet topic_gpr rv;
        wn=1;
        if (topic_gpr == 0) rfalse;
        if (metaclass(topic_gpr) == Routine) {
            rv = (topic_gpr)(snippet/100, snippet%100);
            if (rv ~= GPR_FAIL) rtrue;
            rfalse;
        }
        RunTimeProblem(RTP_BADTOPIC);
        rfalse;
    ]