History variables: Difference between revisions

m
Line 1,117:
</pre>
=={{header|M2000 Interpreter}}==
We can make objects type of Group to act as History Variables. Each group has a pointer to a stack object (a linked list).
First we see how we work with Stack (each module/function see a "current stack", we can make pointers to new stacks, but we can't get pointer of current stack). There are some statement no demonstrate here, such as Shift and ShiftBack (move top to a place, and move back to top), and Over (make new items by copying items).
 
<lang M2000 Interpreter>
Flush ' empty curtrent stack
\\ a is a pointer to a new stack object
a=stack:=1,2@,3%
Print Len(A)=3
For i=1 to Len(a) {
Print StackType$(a, i)="Number"
}
b=stack
Print len(b)=0
Push 1, 2 \\ to current stack
Stack b {
\\ make b the current stack
Data 1, 2, 3
} ' Data add to bottom
\\ now current stack get the old object
Stack b {Push 1, 2, 3 } ' Push add to top, so 3 is at top
Stack b {
While not empty {
Read k ' Read used to pop value to a variable
\\ number pop value in an expression
Print k, number, ' 3 2 1 1 2 3
}
Print
}
z=[] ' [] pop all values from current stack to a new stack, and return a pointer
Print z ' 2 1
z=Stack(a, z,a, z) ' merge stack objects, to a new one, preserve a and z
Print Len(z)
\\ empty a using a new object
a=stack
b=stack:=1,2,3
\\ z has two stack objects
z=stack:= a, b
Stack b { data 1000}
b=stack ' b point to a new stack
\\ we get pointer back
b=stackitem(z, 2)
Print stackitem(b, 4)=1000
</lang>
 
So now lets see the code:
 
<lang M2000 Interpreter>
Module CheckHistoryVariables {
Anonymous user