RosettaCodeData/Task/Generic-swap/M2000-Interpreter/generic-swap-2.m2000
2023-07-01 13:44:08 -04:00

95 lines
2.1 KiB
Text

a=1000
b=50
Swap a,b
Print a, b
A$="Hello"
B$="There"
Swap A$, B$
Print A$, B$
Dim A(4)
A(0):=1,2,3,4
Swap A(3), A(2)
Print A(3), A(2)
\\ Groups are Values
Group alfa {
x=10, y=20
}
Group Beta {
x=40, y=50
}
\\ with List we show the public variables
\\ so among other variables there are:
\\ alfa[Group], alfa.x=10, alfa.y=20, beta[group], beta.x=40, beta.y=50
\\ So Alfa.x and Beta.x are simple variables, we can use swap
Swap Alfa.x, Beta.x
Print Alfa.x, Beta.x
Swap Alfa.x, Beta.x
List
\\ We have to use a third variable to hold value
For This {
\\ Local always make a new variable, and shadow any same local variable
Local M=alfa
alfa=beta
beta=m
}
\\ Now M erased (defined in For This block)
Print Alfa.x=40, Alfa.y=50
Print Beta.x=10, Beta.y=20
\\ Using -> we make pointers to Alfa, and Beta
\\ These pointers are valid until Alfa and Beta erased, or get Empty Group (->0)
pA->Alfa
pB->Beta
Print pA=>x=40, pA=>y=50
Print pB=>x=10, pB=>y=20
Swap pA,pB
Print pA=>x=10, pA=>y=20 ' pA point to beta
Print pB=>x=40, pB=>y=50 'pB point to alfa
Print type$(pA)="Group",Valid(pA=>X)=True
pA->0
pB->0
Print type$(pA)="Group",Valid(pA=>X)=False
\\ These pointers are valid until get Empty Group (->0), they point to a copy of Alfa and Beta
\\ both are in heap as "closed groups"
pA->(Alfa)
pB->(Beta)
Print pA=>x, pA=>y
\\ swap need variables or arrays
\\ pA=>x are closed to object so we have to open the object, and use the open one, where all public variables of group can be used
For pA, pB {
Swap .x, .y
}
Print pA=>x, pA=>y
For pA, pB {
Swap .x, .y
}
Print pA=>x, pA=>y
Print pB=>x, pB=>y
Swap pA,pB
Print pA=>x, pA=>y
Print pB=>x, pB=>y
L1=lambda x=1->{=x : x++}
L2=lambda x=100->{=x : x--}
Print L1()=1, L2()=100
Swap L1, L2
Print L1()=99, L2()=2
Swap L1, L2
Print L1()=3, L2()=98
\\ swap change pointers to containers (here pointers to arrays)
A=(1,2,3,4,5)
B=(6,7,8,9,10)
Swap A, B
Print A
Print B
\\ Arrays with () in names are values
Dim A(10)=1, B(10)=2
For This {
Dim C()
C()=A()
A()=B()
B()=C()
}
Print A()
Print B()