The >>
and <<
operators are used to write a value to a variable
and read a value from a variable respectively. Both operators must be followed
by a variable name, although it can be of any type. The >>
operator pops
a value off the stack and assigns it to the designated variable - the variable
will be created if neccessary. If so, then it will be created with the type of
the value popped off the stack. If the variable already exists then its current
value will be overwritten with the new one. However, if the type of the value
popped off the stack differs from the variable type it will be cast to the
same type as the variable. The <<
operator takes the value of the
designated variable and pushes it on the stack. The type of the stack argument
will be the type of the variable. There is an error if the variable has not
been defined. There are some examples below.
"hello" >>S <<S 123 >>S <<S + .
The first example creates a string variable S
and assigns the string
literal "hello"
to it. The second pushes the contents of S
back
on the stack and then assigns the numeric literal 123
to the string
variable S
thus converting it into a string. This is then pushed onto
the stack and the two strings are concatenated and the result printed, which
will be the string literal "hello123"
.
It is not actually necessary to use the <<
operator as any variable name
will be looked up first as an equate call and if it is not an equate call then
as a variable reference. The <<
operator can be used where there is a
name clash for example.
Go to the first, previous, next, last section, table of contents.