ℙ𝕖𝕡 🙴 ℕ𝕠𝕞

home | documentation | examples | translators | download | blog | all blog posts

The monkey knows which tree it is climbing up. Colombian saying

the ℙ𝕖𝕡 accumulator register

The accumulator : a ℙ𝕖𝕡 register for simple counting.

The machine contains single integer accumulator register that can be incremented (with the command a+ ), decremented (with the command a- ) and set to also set to zero . This register is useful for counting occurrences of miscellaneous elements such as keywords brackets, sentences, nouns or any other syntactical elements that may occur during parsing and translating.

The pep virtual machine accumulator register can be displayed with the count command and it can be set to zero with the zero commands.

using the accumulator to count words


    while [:space:]; clear;
    whilenot [:space:]; add "\n"; print; clear; a+;
    (eof) { add "* found "; count; add " words.\n"; print; quit; }
  

translate the above script into the go language, compile and run

    # save the script above in 'wordcount.pss'
    pep -f tr/translate.go.pss wordcount.pss > wordcount.go
    go build wordcount.go
    echo "how many words?" | ./wordcount
    # go seems to find an extra word compared to the
    # pep interpreter, which may be a translation bug
  

For counting characters and lines that have been read from the input-stream the ℙ𝕖𝕡 machine contains automatic character-count and line-count registers that are updated every time a character is read or a newline character is read. These automatic count registers can be displayed with the chars and lines commands and can be set to zero with the nochars and nolines commands.

An example of the use of the accumulator register is given during the parsing of “quotesets” in old versions of the “compile.pss” script. The accumulator in this case, keeps track of the target for true-jumps.

count how many "x" characters occur in the input stream
 r; 'x' {a+;} d; <eof> { add ' # of Xs == '; count; print; }

the accumulator and state

I quite often use the accumulator as a way to keep track of “state ” or more specifically, whether a nom script is currently parsing within a particular grammar structure or outside of the structure. This was not the original purpose of the accumulator register but it seems to work more or less well.

Examples of this use are the scripts /eg/text.tohtml.pss or /eg/nom.syntax.reference.pss