Content deleted Content added
Stevebroshar (talk | contribs) →Examples of control tables: Normalize structure->add general section |
Stevebroshar (talk | contribs) →Examples: Collect non-language specific guys together |
||
Line 109:
|-
| {{mono|?}} || → ||Default
|}
The next example illustrates how a similar effect can be achieved in languages that do not support pointer definitions in data structures but do support indexed branching to a subroutine – contained within a ([[zero-based numbering|0-based]]) array of subroutine pointers. The table (CT2) is used to extract the index (from 2nd column) to the pointer array (CT2P). If pointer arrays are not supported, a SWITCH statement or equivalent can be used to alter the control flow to one of a sequence of program labels (e.g.: case0, case1, case2, case3, case4) which then either process the input directly, or else perform a call (with return) to the appropriate subroutine (default, Add, Subtract, Multiply or Divide,..) to deal with it.
:{| class="wikitable"
|+ CT2
! input 1!! {{mono|subr #}}
|-
| {{mono|A}} || {{mono|1}}
|-
| {{mono|S}} || {{mono|2}}
|-
| {{mono|M}} || {{mono|3}}
|-
| {{mono|D}} || {{mono|4}}
|-
| {{mono|?}} || {{mono|0}}
|}
As in above examples, it is possible to efficiently translate the potential [[ASCII]] input values (A,S,M,D or unknown) into a pointer array index without actually using a table lookup, but is shown here as a table for consistency with the first example.
:{| class="wikitable"
|+ CT2P<br/>{{nobold|pointer array}}
! !! [[pointer (computer programming)|pointer]] [[Array data structure|array]]
|-
| → || {{mono|default}}
|-
| → || {{mono|Add}}
|-
| → || {{mono|Subtract}}
|-
| → || {{mono|Multiply}}
|-
| → || {{mono|Divide}}
|-
| → || {{mono|?other}}
|}
Multi-dimensional control tables can be constructed that can be more complex than the above examples that might test for multiple conditions on multiple inputs or perform more than one 'action', based on some matching criteria. An 'action' can include a pointer to another subordinate control table. The simple example below has had an ''implicit'' 'OR' condition incorporated as an extra column (to handle lower case input, however in this instance, this could equally have been handled simply by having an extra entry for each of the lower case characters specifying the same subroutine identifier as the upper case characters). An extra column to count the actual run-time events for each input as they occur is also included.
:{| class="wikitable"
|+ CT3
! input 1!!alternate!! {{mono|subr #}}!! {{mono|count}}
|-
| {{mono|A}} ||{{mono|a}}|| {{mono|1}}|| {{mono|0}}
|-
| {{mono|S}} ||{{mono|s}}|| {{mono|2}}|| {{mono|0}}
|-
| {{mono|M}} ||{{mono|m}}|| {{mono|3}}|| {{mono|0}}
|-
| {{mono|D}} ||{{mono|d}}|| {{mono|4}}|| {{mono|0}}
|-
| {{mono|?}} ||{{mono|?}}|| {{mono|0}}|| {{mono|0}}
|}
The control table entries are then much more similar to conditional statements in [[procedural language]]s but, crucially, without the actual (language dependent) conditional statements (i.e. instructions) being present (the generic code is ''physically'' in the interpreter that processes the table entries, not in the table itself – which simply embodies the program logic via its structure and values).
In tables such as these, where a series of similar table entries defines the entire logic, a table entry number or pointer may effectively take the place of a [[program counter]] in more conventional programs and may be reset in an 'action', also specified in the table entry. The example below (CT4) shows how extending the earlier table, to include a 'next' entry (and/or including an 'alter flow' ([[branch (computer science)|jump]]) subroutine) can create a [[program loop|loop]] (This example is actually not the most efficient way to construct such a control table but, by demonstrating a gradual 'evolution' from the first examples above, shows how additional columns can be used to modify behaviour.) The fifth column demonstrates that more than one action can be initiated with a single table entry – in this case an action to be performed ''after'' the normal processing of each entry ('-' values mean 'no conditions' or 'no action').
[[Structured programming]] or [[structured programming|"Goto-less" code]], (incorporating the equivalent of '[[do while loop|DO WHILE]]' or '[[for loop]]' constructs), can also be accommodated with suitably designed and 'indented' control table structures.
{|
|- style="vertical-align: bottom;"
|
{| class="wikitable"
|+ CT4<br/>{{nobold|(a complete 'program' to read input1 and process, repeating until 'E' encountered)}}
! input 1!!alternate!! subr # !! count !! jump
|-
| {{sdash}} || {{sdash}} || {{mono|5}}|| {{mono|0}} || {{sdash}}
|-
| {{mono|E}} ||{{mono|e}}|| {{mono|7}}|| {{mono|0}} || {{sdash}}
|-
| {{mono|A}} ||{{mono|a}}|| {{mono|1}}|| {{mono|0}} || {{sdash}}
|-
| {{mono|S}} ||{{mono|s}}|| {{mono|2}}|| {{mono|0}} || {{sdash}}
|-
| {{mono|M}} ||{{mono|m}}|| {{mono|3}}|| {{mono|0}} || {{sdash}}
|-
| {{mono|D}} ||{{mono|d}}|| {{mono|4}}|| {{mono|0}} || {{sdash}}
|-
| {{mono|?}} ||{{mono|?}}|| {{mono|0}}|| {{mono|0}} || {{sdash}}
|-
| {{sdash}} || {{sdash}} || {{mono|6}}|| {{mono|0}} || {{mono|1}}
|}
|
{| class="wikitable"
|+ CT4P {{nobold|pointer array}}
! !! [[pointer (computer programming)|pointer]] [[array data structure|array]]
|-
| → || {{mono|Default}}
|-
| → || {{mono|Add}}
|-
| → || {{mono|Subtract}}
|-
| → || {{mono|Multiply}}
|-
| → || {{mono|Divide}}
|-
| → || {{mono|Read Input1}}
|-
| → || {{mono|Alter flow}}
|-
| → || {{mono|End}}
|}
|}
Line 242 ⟶ 344:
goto *handler_labels[i];
</syntaxhighlight>
===Table-driven rating===
|