The package directly provides only a single command, and all the functionality is made available through
its methods.
::grammar::me::cpu::coredisasmasm
This method returns a list containing a disassembly of the match instructions in asm. The format
of asm is specified in the section MATCHPROGRAMREPRESENTATION.
Each element of the result contains instruction label, instruction name, and the instruction
arguments, in this order. The label can be the empty string. Jump destinations are shown as
labels, strings and tokens unencoded. Token names are prefixed with their numeric id, if, and only
if a tokmap is defined. The two components are separated by a colon.
::grammar::me::cpu::coreasmasm
This method returns code in the format as specified in section MATCHPROGRAMREPRESENTATION
generated from ME assembly code asm, which is in the format as returned by the method disasm.
::grammar::me::cpu::corenewasm
This method creates state value for a ME virtual machine in its initial state and returns it as
its result.
The argument matchcode contains a Tcl representation of the match instructions the machine has to
execute while parsing the input stream. Its format is specified in the section MATCHPROGRAMREPRESENTATION.
The tokmap argument taken by the implementation provided by the package grammar::me::tcl is here
hidden inside of the match instructions and therefore not needed.
::grammar::me::cpu::corelcstatelocation
This method takes the state value of a ME virtual machine and uses it to convert a location in the
input stream (as offset) into a line number and column index. The result of the method is a
2-element list containing the two pieces in the order mentioned in the previous sentence.
Note that the method cannot convert locations which the machine has not yet read from the input
stream. In other words, if the machine has read 7 characters so far it is possible to convert the
offsets 0 to 6, but nothing beyond that. This also shows that it is not possible to convert
offsets which refer to locations before the beginning of the stream.
This utility allows higher levels to convert the location offsets found in the error status and
the AST into more human readable data.
::grammar::me::cpu::coretokstate ?from ?to??
This method takes the state value of a ME virtual machine and returns a Tcl list containing the
part of the input stream between the locations from and to (both inclusive). If to is not
specified it will default to the value of from. If from is not specified either the whole input
stream is returned.
This method places the same restrictions on its location arguments as the method lc.
::grammar::me::cpu::corepcstate
This method takes the state value of a ME virtual machine and returns the current value of the
stored program counter.
::grammar::me::cpu::coreiseofstate
This method takes the state value of a ME virtual machine and returns the current value of the
stored eof flag.
::grammar::me::cpu::coreatstate
This method takes the state value of a ME virtual machine and returns the current location in the
input stream.
::grammar::me::cpu::coreccstate
This method takes the state value of a ME virtual machine and returns the current token.
::grammar::me::cpu::coresvstate
This method takes the state value of a ME virtual machine and returns the current semantic value
stored in it. This is an abstract syntax tree as specified in the document grammar::me_ast,
section ASTVALUES.
::grammar::me::cpu::coreokstate
This method takes the state value of a ME virtual machine and returns the match status stored in
it.
::grammar::me::cpu::coreerrorstate
This method takes the state value of a ME virtual machine and returns the current error status
stored in it.
::grammar::me::cpu::corelstkstate
This method takes the state value of a ME virtual machine and returns the location stack.
::grammar::me::cpu::coreastkstate
This method takes the state value of a ME virtual machine and returns the AST stack.
::grammar::me::cpu::coremstkstate
This method takes the state value of a ME virtual machine and returns the AST marker stack.
::grammar::me::cpu::coreestkstate
This method takes the state value of a ME virtual machine and returns the error stack.
::grammar::me::cpu::corerstkstate
This method takes the state value of a ME virtual machine and returns the subroutine return stack.
::grammar::me::cpu::corencstate
This method takes the state value of a ME virtual machine and returns the nonterminal match cache
as a dictionary.
::grammar::me::cpu::coreaststate
This method takes the state value of a ME virtual machine and returns the abstract syntax tree
currently at the top of the AST stack stored in it. This is an abstract syntax tree as specified
in the document grammar::me_ast, section ASTVALUES.
::grammar::me::cpu::corehaltedstate
This method takes the state value of a ME virtual machine and returns the current halt status
stored in it, i.e. if the machine has stopped or not.
::grammar::me::cpu::corecodestate
This method takes the state value of a ME virtual machine and returns the code stored in it, i.e.
the instructions executed by the machine.
::grammar::me::cpu::coreeofstatevar
This method takes the state value of a ME virtual machine as stored in the variable named by
statevar and modifies it so that the eof flag inside is set. This signals to the machine that
whatever token are in the input queue are the last to be processed. There will be no more.
::grammar::me::cpu::coreputstatevartoklexlinecol
This method takes the state value of a ME virtual machine as stored in the variable named by
statevar and modifies it so that the token tok is added to the end of the input queue, with
associated lexeme data lex and line/column information.
The operation will fail with an error if the eof flag of the machine has been set through the
method eof.
::grammar::me::cpu::corerunstatevar ?n?
This method takes the state value of a ME virtual machine as stored in the variable named by
statevar, executes a number of instructions and stores the state resulting from their
modifications back into the variable.
The execution loop will run until either
• n instructions have been executed, or
• a halt instruction was executed, or
• the input queue is empty and the code is asking for more tokens to process.
If no limit n was set only the last two conditions are checked for.
MATCHPROGRAMREPRESENTATION
A match program is represented by nested Tcl list. The first element, asm, is a list of integer numbers,
the instructions to execute, and their arguments. The second element, pool, is a list of strings,
referenced by the instructions, for error messages, token names, etc. The third element, tokmap, provides
ordering information for the tokens, mapping their names to their numerical rank. This element can be
empty, forcing lexicographic comparison when matching ranges.
All ME instructions are encoded as integer numbers, with the mapping given below. A number of the
instructions, those which handle error messages, have been given an additional argument to supply that
message explicitly instead of having it constructed from token names, etc. This allows the machine state
to store only the message ids instead of the full strings.
Jump destination arguments are absolute indices into the asm element, refering to the instruction to jump
to. Any string arguments are absolute indices into the pool element. Tokens, characters, messages, and
token (actually character) classes to match are coded as references into the pool as well.
[1] "ict_advancemessage"
[2] "ict_match_tokentokmessage"
[3] "ict_match_tokrangetokbegintokendmessage"
[4] "ict_match_tokclasscodemessage"
[5] "inc_restorebranchlabelnt"
[6] "inc_savent"
[7] "icf_ntcallbranchlabel"
[8] "icf_ntreturn"
[9] "iok_ok"
[10] "iok_fail"
[11] "iok_negate"
[12] "icf_jalwaysbranchlabel"
[13] "icf_jokbranchlabel"
[14] "icf_jfailbranchlabel"
[15] "icf_halt"
[16] "icl_push"
[17] "icl_rewind"
[18] "icl_pop"
[19] "ier_push"
[20] "ier_clear"
[21] "ier_nonterminalmessage"
[22] "ier_merge"
[23] "isv_clear"
[24] "isv_terminal"
[25] "isv_nonterminal_leafnt"
[26] "isv_nonterminal_rangent"
[27] "isv_nonterminal_reducent"
[28] "ias_push"
[29] "ias_mark"
[30] "ias_mrewind"
[31] "ias_mpop"