RosettaCodeData/Task/History-variables/M2000-Interpreter/history-variables-2.m2000
2023-07-01 13:44:08 -04:00

179 lines
4.6 KiB
Text

Module CheckHistoryVariables {
Class History {
Private:
myvalue=stack
Public:
Group Count {
Value {
link parent myvalue to m
=Len(m)
}
}
Function CopyMe {
m=This
.myvalue<=stack(.myvalue)
=Group(m)
}
Group CopyValues {
Value {
link parent myvalue to m
=Stack(m)
}
}
Function Values (x as long) {
if x>=0 and x<=len(.myvalue) then =StackItem(.myvalue, x)
Else Error "Fault index"
}
Module Back {
if len(.myvalue)<2 then exit
Stack .myvalue {Drop}
}
Operator "++" {
Stack .myvalue {
Push stackitem()+1
}
}
Operator "--" {
Stack .myvalue {
Push stackitem()-1
}
}
Operator "+=" (x) {
Stack .myvalue {
Push stackitem()+x
} }
Operator "-=" (x) {
Stack .myvalue {
Push stackitem()-x
}
}
Operator "/=" (x){
if x==0 then error "division by zero"
Stack .myvalue {
Push stackitem()/x
}
}
Operator "*=" (x){
Stack .myvalue {
Push stackitem()*x
}
}
Value {
=StackItem(.myvalue)
}
Set {
Read x : Stack .myvalue { Push x}
}
Class:
Module History {
If Match("N") then Read x : Stack .myvalue { Push x}
}
}
N=History()
N=1
N=2
N=3
Print N, N.Values(2), N.Values(3), N.Count
\\ Get a copy of history
m=N.CopyValues
Print len(m)=3
\\ just show all
Print m ' 3 2 1
\\ or iterate from last to first
k=each(m, -1, 1)
While k {
Print Stackitem(k) \\ 1 NL 2 NL 3 (NL = new line)
}
N1=N.CopyMe()
N=5
N1=4
N=6
Print N
Print N1.Count=4, N.Count=5
Print N1, N
Print N.CopyValues ' 6 5 3 2 1
Print N1.CopyValues ' 4 3 2 1
Print N1<N
N=N+1
Print N.Count=6
Print N.CopyValues ' 6 5 3 2 1
Print "Go Back"
While N.Count>1 {
N.Back
Print N
}
N+=10
N*=2
Print N.CopyValues ' 22 11 1
}
CheckHistoryVariables
\\ for strings
Module CheckStringHistoryVariables {
Class History$ {
Private:
myvalue=stack
Public:
Group Count {
Value {
link parent myvalue to m
=Len(m)
}
}
Function CopyMe {
m=This
.myvalue<=stack(.myvalue)
=Group(m)
}
Group CopyValues {
Value {
link parent myvalue to m
=Stack(m)
}
}
Function Values$ (x as long) {
if x>=0 and x<=len(.myvalue) then =StackItem$(.myvalue, x)
Else Error "Fault index"
}
Module Back {
if len(.myvalue)<2 then exit
Stack .myvalue {Drop}
}
Operator "+=" (x$) {
Stack .myvalue {
Push stackitem$()+x$
} }
Value {
=StackItem$(.myvalue)
}
Set {
Read x$ : Stack .myvalue { Push x$}
}
Class:
Module History {
If Match("S") then Read x$ : Stack .myvalue { Push x$}
}
}
N$=History$("First")
N$="Second"
N$="Third"
Print N.Count=3
M=N.CopyValues
K=Each(M, -1, 1)
While K {
Print StackItem$(K)
}
N$+=" and this"
Print N.Count=4
Print N.CopyValues
N.Back
Print N.Count=3
Print N.CopyValues
Print N.Values$(2)="Second"
Input "New value:", N$
Print N$
Print N.Count=4
Print N.CopyValues
}
CheckStringHistoryVariables