From f39d5d687d8476f2585a253ef42c1bc1fa5a8fe9 Mon Sep 17 00:00:00 2001
From: Misha Brukman
Definition Of Operation Of Built In Words | |||||||
LOGICAL OPERATIONS |
Definition Of Operation Of Built In Words | |||||||
LOGICAL OPERATIONS | |||||||
Word | -Name | -Operation | -Description | +Word | +Name | +Operation | +Description |
< | -LT | -w1 w2 -- b | -Two values (w1 and w2) are popped off the stack and + | ||||
< | +LT | +w1 w2 -- b | +Two values (w1 and w2) are popped off the stack and compared. If w1 is less than w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back on the stack. | ||||
> | -GT | -w1 w2 -- b | -Two values (w1 and w2) are popped off the stack and + | ||||
> | +GT | +w1 w2 -- b | +Two values (w1 and w2) are popped off the stack and compared. If w1 is greater than w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back on the stack. | ||||
>= | -GE | -w1 w2 -- b | -Two values (w1 and w2) are popped off the stack and + | ||||
>= | +GE | +w1 w2 -- b | +Two values (w1 and w2) are popped off the stack and compared. If w1 is greater than or equal to w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back on the stack. | ||||
<= | -LE | -w1 w2 -- b | -Two values (w1 and w2) are popped off the stack and + | ||||
<= | +LE | +w1 w2 -- b | +Two values (w1 and w2) are popped off the stack and compared. If w1 is less than or equal to w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back on the stack. | ||||
= | -EQ | -w1 w2 -- b | -Two values (w1 and w2) are popped off the stack and + | ||||
= | +EQ | +w1 w2 -- b | +Two values (w1 and w2) are popped off the stack and compared. If w1 is equal to w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back | ||||
<> | -NE | -w1 w2 -- b | -Two values (w1 and w2) are popped off the stack and + | ||||
<> | +NE | +w1 w2 -- b | +Two values (w1 and w2) are popped off the stack and compared. If w1 is equal to w2, TRUE is pushed back on the stack, otherwise FALSE is pushed back | ||||
FALSE | -FALSE | --- b | -The boolean value FALSE (0) is pushed on to the stack. | +||||
FALSE | +FALSE | +-- b | +The boolean value FALSE (0) is pushed on to the stack. | ||||
TRUE | -TRUE | --- b | -The boolean value TRUE (-1) is pushed on to the stack. | +||||
TRUE | +TRUE | +-- b | +The boolean value TRUE (-1) is pushed on to the stack. | ||||
BITWISE OPERATORS | |||||||
Word | -Name | -Operation | -Description | +Word | +Name | +Operation | +Description |
<< | -SHL | -w1 w2 -- w1<<w2 | -Two values (w1 and w2) are popped off the stack. The w2 + | ||||
<< | +SHL | +w1 w2 -- w1<<w2 | +Two values (w1 and w2) are popped off the stack. The w2 operand is shifted left by the number of bits given by the w1 operand. The result is pushed back to the stack. | ||||
>> | -SHR | -w1 w2 -- w1>>w2 | -Two values (w1 and w2) are popped off the stack. The w2 + | ||||
>> | +SHR | +w1 w2 -- w1>>w2 | +Two values (w1 and w2) are popped off the stack. The w2 operand is shifted right by the number of bits given by the w1 operand. The result is pushed back to the stack. | ||||
OR | -OR | -w1 w2 -- w2|w1 | -Two values (w1 and w2) are popped off the stack. The values + | ||||
OR | +OR | +w1 w2 -- w2|w1 | +Two values (w1 and w2) are popped off the stack. The values are bitwise OR'd together and pushed back on the stack. This is not a logical OR. The sequence 1 2 OR yields 3 not 1. | ||||
AND | -AND | -w1 w2 -- w2&w1 | -Two values (w1 and w2) are popped off the stack. The values + | ||||
AND | +AND | +w1 w2 -- w2&w1 | +Two values (w1 and w2) are popped off the stack. The values are bitwise AND'd together and pushed back on the stack. This is not a logical AND. The sequence 1 2 AND yields 0 not 1. | ||||
XOR | -XOR | -w1 w2 -- w2^w1 | -Two values (w1 and w2) are popped off the stack. The values + | ||||
XOR | +XOR | +w1 w2 -- w2^w1 | +Two values (w1 and w2) are popped off the stack. The values are bitwise exclusive OR'd together and pushed back on the stack. For example, The sequence 1 3 XOR yields 2. | ||||
ARITHMETIC OPERATORS | |||||||
Word | -Name | -Operation | -Description | +Word | +Name | +Operation | +Description |
ABS | -ABS | -w -- |w| | -One value s popped off the stack; its absolute value is computed + | ||||
ABS | +ABS | +w -- |w| | +One value s popped off the stack; its absolute value is computed and then pushed on to the stack. If w1 is -1 then w2 is 1. If w1 is 1 then w2 is also 1. | ||||
NEG | -NEG | -w -- -w | -One value is popped off the stack which is negated and then + | ||||
NEG | +NEG | +w -- -w | +One value is popped off the stack which is negated and then pushed back on to the stack. If w1 is -1 then w2 is 1. If w1 is 1 then w2 is -1. | ||||
+ | -ADD | -w1 w2 -- w2+w1 | -Two values are popped off the stack. Their sum is pushed back + | ||||
+ | +ADD | +w1 w2 -- w2+w1 | +Two values are popped off the stack. Their sum is pushed back on to the stack | ||||
- | -SUB | -w1 w2 -- w2-w1 | -Two values are popped off the stack. Their difference is pushed back + | ||||
- | +SUB | +w1 w2 -- w2-w1 | +Two values are popped off the stack. Their difference is pushed back on to the stack | ||||
* | -MUL | -w1 w2 -- w2*w1 | -Two values are popped off the stack. Their product is pushed back + | ||||
* | +MUL | +w1 w2 -- w2*w1 | +Two values are popped off the stack. Their product is pushed back on to the stack | ||||
/ | -DIV | -w1 w2 -- w2/w1 | -Two values are popped off the stack. Their quotient is pushed back + | ||||
/ | +DIV | +w1 w2 -- w2/w1 | +Two values are popped off the stack. Their quotient is pushed back on to the stack | ||||
MOD | -MOD | -w1 w2 -- w2%w1 | -Two values are popped off the stack. Their remainder after division + | ||||
MOD | +MOD | +w1 w2 -- w2%w1 | +Two values are popped off the stack. Their remainder after division of w1 by w2 is pushed back on to the stack | ||||
*/ | -STAR_SLAH | -w1 w2 w3 -- (w3*w2)/w1 | -Three values are popped off the stack. The product of w1 and w2 is + | ||||
*/ | +STAR_SLAH | +w1 w2 w3 -- (w3*w2)/w1 | +Three values are popped off the stack. The product of w1 and w2 is divided by w3. The result is pushed back on to the stack. | ||||
++ | -INCR | -w -- w+1 | -One value is popped off the stack. It is incremented by one and then + | ||||
++ | +INCR | +w -- w+1 | +One value is popped off the stack. It is incremented by one and then pushed back on to the stack. | ||||
-- | -DECR | -w -- w-1 | -One value is popped off the stack. It is decremented by one and then + | ||||
-- | +DECR | +w -- w-1 | +One value is popped off the stack. It is decremented by one and then pushed back on to the stack. | ||||
MIN | -MIN | -w1 w2 -- (w2<w1?w2:w1) | -Two values are popped off the stack. The larger one is pushed back + | ||||
MIN | +MIN | +w1 w2 -- (w2<w1?w2:w1) | +Two values are popped off the stack. The larger one is pushed back on to the stack. | ||||
MAX | -MAX | -w1 w2 -- (w2>w1?w2:w1) | -Two values are popped off the stack. The larger value is pushed back + | ||||
MAX | +MAX | +w1 w2 -- (w2>w1?w2:w1) | +Two values are popped off the stack. The larger value is pushed back on to the stack. | ||||
STACK MANIPULATION OPERATORS | |||||||
Word | -Name | -Operation | -Description | +Word | +Name | +Operation | +Description |
DROP | -DROP | -w -- | -One value is popped off the stack. | +||||
DROP | +DROP | +w -- | +One value is popped off the stack. | ||||
DROP2 | -DROP2 | -w1 w2 -- | -Two values are popped off the stack. | +||||
DROP2 | +DROP2 | +w1 w2 -- | +Two values are popped off the stack. | ||||
NIP | -NIP | -w1 w2 -- w2 | -The second value on the stack is removed from the stack. That is, + | ||||
NIP | +NIP | +w1 w2 -- w2 | +The second value on the stack is removed from the stack. That is, a value is popped off the stack and retained. Then a second value is popped and the retained value is pushed. | ||||
NIP2 | -NIP2 | -w1 w2 w3 w4 -- w3 w4 | -The third and fourth values on the stack are removed from it. That is, + | ||||
NIP2 | +NIP2 | +w1 w2 w3 w4 -- w3 w4 | +The third and fourth values on the stack are removed from it. That is, two values are popped and retained. Then two more values are popped and the two retained values are pushed back on. | ||||
DUP | -DUP | -w1 -- w1 w1 | -One value is popped off the stack. That value is then pushed on to + | ||||
DUP | +DUP | +w1 -- w1 w1 | +One value is popped off the stack. That value is then pushed on to the stack twice to duplicate the top stack vaue. | ||||
DUP2 | -DUP2 | -w1 w2 -- w1 w2 w1 w2 | -The top two values on the stack are duplicated. That is, two vaues + | ||||
DUP2 | +DUP2 | +w1 w2 -- w1 w2 w1 w2 | +The top two values on the stack are duplicated. That is, two vaues are popped off the stack. They are alternately pushed back on the stack twice each. | ||||
SWAP | -SWAP | -w1 w2 -- w2 w1 | -The top two stack items are reversed in their order. That is, two + | ||||
SWAP | +SWAP | +w1 w2 -- w2 w1 | +The top two stack items are reversed in their order. That is, two values are popped off the stack and pushed back on to the stack in the opposite order they were popped. | ||||
SWAP2 | -SWAP2 | -w1 w2 w3 w4 -- w3 w4 w2 w1 | -The top four stack items are swapped in pairs. That is, two values + | ||||
SWAP2 | +SWAP2 | +w1 w2 w3 w4 -- w3 w4 w2 w1 | +The top four stack items are swapped in pairs. That is, two values are popped and retained. Then, two more values are popped and retained. The values are pushed back on to the stack in the reverse order but in pairs. | ||||
OVER | -OVER | -w1 w2-- w1 w2 w1 | -Two values are popped from the stack. They are pushed back + | ||||
OVER | +OVER | +w1 w2-- w1 w2 w1 | +Two values are popped from the stack. They are pushed back on to the stack in the order w1 w2 w1. This seems to cause the top stack element to be duplicated "over" the next value. | ||||
OVER2 | -OVER2 | -w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 | -The third and fourth values on the stack are replicated on to the + | ||||
OVER2 | +OVER2 | +w1 w2 w3 w4 -- w1 w2 w3 w4 w1 w2 | +The third and fourth values on the stack are replicated on to the top of the stack | ||||
ROT | -ROT | -w1 w2 w3 -- w2 w3 w1 | -The top three values are rotated. That is, three value are popped + | ||||
ROT | +ROT | +w1 w2 w3 -- w2 w3 w1 | +The top three values are rotated. That is, three value are popped off the stack. They are pushed back on to the stack in the order w1 w3 w2. | ||||
ROT2 | -ROT2 | -w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 | -Like ROT but the rotation is done using three pairs instead of + | ||||
ROT2 | +ROT2 | +w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 | +Like ROT but the rotation is done using three pairs instead of three singles. | ||||
RROT | -RROT | -w1 w2 w3 -- w2 w3 w1 | -Reverse rotation. Like ROT, but it rotates the other way around. + | ||||
RROT | +RROT | +w1 w2 w3 -- w2 w3 w1 | +Reverse rotation. Like ROT, but it rotates the other way around. Essentially, the third element on the stack is moved to the top of the stack. | ||||
RROT2 | -RROT2 | -w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 | -Double reverse rotation. Like RROT but the rotation is done using + | ||||
RROT2 | +RROT2 | +w1 w2 w3 w4 w5 w6 -- w3 w4 w5 w6 w1 w2 | +Double reverse rotation. Like RROT but the rotation is done using three pairs instead of three singles. The fifth and sixth stack elements are moved to the first and second positions | ||||
TUCK | -TUCK | -w1 w2 -- w2 w1 w2 | -Similar to OVER except that the second operand is being + | ||||
TUCK | +TUCK | +w1 w2 -- w2 w1 w2 | +Similar to OVER except that the second operand is being replicated. Essentially, the first operand is being "tucked" in between two instances of the second operand. Logically, two values are popped off the stack. They are placed back on the stack in the order w2 w1 w2. | ||||
TUCK2 | -TUCK2 | -w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 | -Like TUCK but a pair of elements is tucked over two pairs. + | ||||
TUCK2 | +TUCK2 | +w1 w2 w3 w4 -- w3 w4 w1 w2 w3 w4 | +Like TUCK but a pair of elements is tucked over two pairs. That is, the top two elements of the stack are duplicated and inserted into the stack at the fifth and positions. | ||||
PICK | -PICK | -x0 ... Xn n -- x0 ... Xn x0 | -The top of the stack is used as an index into the remainder of + | ||||
PICK | +PICK | +x0 ... Xn n -- x0 ... Xn x0 | +The top of the stack is used as an index into the remainder of the stack. The element at the nth position replaces the index (top of stack). This is useful for cycling through a set of values. Note that indexing is zero based. So, if n=0 then you get the second item on the stack. If n=1 you get the third, etc. Note also that the index is replaced by the n'th value. | ||||
SELECT | -SELECT | -m n X0..Xm Xm+1 .. Xn -- Xm | -This is like PICK but the list is removed and you need to specify + | ||||
SELECT | +SELECT | +m n X0..Xm Xm+1 .. Xn -- Xm | +This is like PICK but the list is removed and you need to specify both the index and the size of the list. Careful with this one, the wrong value for n can blow away a huge amount of the stack. | ||||
ROLL | -ROLL | -x0 x1 .. xn n -- x1 .. xn x0 | -Not Implemented. This one has been left as an exercise to + | ||||
ROLL | +ROLL | +x0 x1 .. xn n -- x1 .. xn x0 | +Not Implemented. This one has been left as an exercise to the student. See Exercise. ROLL requires a value, "n", to be on the top of the stack. This value specifies how far into the stack to "roll". The n'th value is moved (not @@ -844,23 +849,23 @@ using the following construction: | ||||
MEMORY OPERATORS | |||||||
Word | -Name | -Operation | -Description | +Word | +Name | +Operation | +Description |
MALLOC | -MALLOC | -w1 -- p | -One value is popped off the stack. The value is used as the size + | ||||
MALLOC | +MALLOC | +w1 -- p | +One value is popped off the stack. The value is used as the size of a memory block to allocate. The size is in bytes, not words. The memory allocation is completed and the address of the memory block is pushed on to the stack. | ||||
FREE | -FREE | -p -- | -One pointer value is popped off the stack. The value should be + | ||||
FREE | +FREE | +p -- | +One pointer value is popped off the stack. The value should be the address of a memory block created by the MALLOC operation. The associated memory block is freed. Nothing is pushed back on the stack. Many bugs can be created by attempting to FREE something @@ -872,20 +877,20 @@ using the following construction: the stack (for the FREE at the end) and that every use of the pointer is preceded by a DUP to retain the copy for FREE. | ||||
GET | -GET | -w1 p -- w2 p | -An integer index and a pointer to a memory block are popped of + | ||||
GET | +GET | +w1 p -- w2 p | +An integer index and a pointer to a memory block are popped of the block. The index is used to index one byte from the memory block. That byte value is retained, the pointer is pushed again and the retained value is pushed. Note that the pointer value s essentially retained in its position so this doesn't count as a "use ptr" in the FREE idiom. | ||||
PUT | -PUT | -w1 w2 p -- p | -An integer value is popped of the stack. This is the value to + | ||||
PUT | +PUT | +w1 w2 p -- p | +An integer value is popped of the stack. This is the value to be put into a memory block. Another integer value is popped of the stack. This is the indexed byte in the memory block. A pointer to the memory block is popped off the stack. The @@ -897,31 +902,31 @@ using the following construction: | ||||
CONTROL FLOW OPERATORS | |||||||
Word | -Name | -Operation | -Description | +Word | +Name | +Operation | +Description |
RETURN | -RETURN | --- | -The currently executing definition returns immediately to its caller. + | ||||
RETURN | +RETURN | +-- | +The currently executing definition returns immediately to its caller.
Note that there is an implicit RETURN at the end of each
definition, logically located at the semi-colon. The sequence
RETURN ; is valid but redundant. |
||||
EXIT | -EXIT | -w1 -- | -A return value for the program is popped off the stack. The program is + | ||||
EXIT | +EXIT | +w1 -- | +A return value for the program is popped off the stack. The program is
then immediately terminated. This is normally an abnormal exit from the
program. For a normal exit (when MAIN finishes), the exit
code will always be zero in accordance with UNIX conventions. |
||||
RECURSE | -RECURSE | --- | -The currently executed definition is called again. This operation is + | ||||
RECURSE | +RECURSE | +-- | +The currently executed definition is called again. This operation is
needed since the definition of a word doesn't exist until the semi colon
is reacher. Attempting something like: : recurser recurser ; will yield and error saying that @@ -929,24 +934,24 @@ using the following construction: to: : recurser RECURSE ; |
||||
IF (words...) ENDIF | -IF (words...) ENDIF | -b -- | -A boolean value is popped of the stack. If it is non-zero then the "words..." + | ||||
IF (words...) ENDIF | +IF (words...) ENDIF | +b -- | +A boolean value is popped of the stack. If it is non-zero then the "words..." are executed. Otherwise, execution continues immediately following the ENDIF. | ||||
IF (words...) ELSE (words...) ENDIF | -IF (words...) ELSE (words...) ENDIF | -b -- | -A boolean value is popped of the stack. If it is non-zero then the "words..." + | ||||
IF (words...) ELSE (words...) ENDIF | +IF (words...) ELSE (words...) ENDIF | +b -- | +A boolean value is popped of the stack. If it is non-zero then the "words..." between IF and ELSE are executed. Otherwise the words between ELSE and ENDIF are executed. In either case, after the (words....) have executed, execution continues immediately following the ENDIF. | ||||
WHILE (words...) END | -WHILE (words...) END | -b -- b | -The boolean value on the top of the stack is examined. If it is non-zero then the + | ||||
WHILE (words...) END | +WHILE (words...) END | +b -- b | +The boolean value on the top of the stack is examined. If it is non-zero then the "words..." between WHILE and END are executed. Execution then begins again at the WHILE where another boolean is popped off the stack. To prevent this operation from eating up the entire stack, you should push on to the stack (just before the END) a boolean value that indicates @@ -964,63 +969,65 @@ using the following construction: | ||||
INPUT & OUTPUT OPERATORS | |||||||
Word | -Name | -Operation | -Description | +Word | +Name | +Operation | +Description |
SPACE | -SPACE | --- | -A space character is put out. There is no stack effect. | +||||
SPACE | +SPACE | +-- | +A space character is put out. There is no stack effect. | ||||
TAB | -TAB | --- | -A tab character is put out. There is no stack effect. | +||||
TAB | +TAB | +-- | +A tab character is put out. There is no stack effect. | ||||
CR | -CR | --- | -A carriage return character is put out. There is no stack effect. | +||||
CR | +CR | +-- | +A carriage return character is put out. There is no stack effect. | ||||
>s | -OUT_STR | --- | -A string pointer is popped from the stack. It is put out. | +||||
>s | +OUT_STR | +-- | +A string pointer is popped from the stack. It is put out. | ||||
>d | -OUT_STR | --- | -A value is popped from the stack. It is put out as a decimal integer. | +||||
>d | +OUT_STR | +-- | +A value is popped from the stack. It is put out as a decimal + integer. | ||||
>c | -OUT_CHR | --- | -A value is popped from the stack. It is put out as an ASCII character. | +||||
>c | +OUT_CHR | +-- | +A value is popped from the stack. It is put out as an ASCII + character. | ||||
<s | -IN_STR | --- s | -A string is read from the input via the scanf(3) format string " %as". The - resulting string is pushed on to the stack. | +||||
<s | +IN_STR | +-- s | +A string is read from the input via the scanf(3) format string " %as". + The resulting string is pushed on to the stack. | ||||
<d | -IN_STR | --- w | -An integer is read from the input via the scanf(3) format string " %d". The - resulting value is pushed on to the stack | +||||
<d | +IN_STR | +-- w | +An integer is read from the input via the scanf(3) format string " %d". + The resulting value is pushed on to the stack | ||||
<c | -IN_CHR | --- w | -A single character is read from the input via the scanf(3) format string - " %c". The value is converted to an integer and pushed on to the stack. | +||||
<c | +IN_CHR | +-- w | +A single character is read from the input via the scanf(3) format string + " %c". The value is converted to an integer and pushed on to the stack. | ||||
DUMP | -DUMP | --- | -The stack contents are dumped to standard output. This is useful for + | ||||
DUMP | +DUMP | +-- | +The stack contents are dumped to standard output. This is useful for debugging your definitions. Put DUMP at the beginning and end of a definition to see instantly the net effect of the definition. |