RosettaCodeData/Task/Polymorphism/M2000-Interpreter/polymorphism-2.m2000

77 lines
1.8 KiB
Plaintext

Class PointA {
Property x=0~
Property Y=0~
Operator "=" (n1) {
n=group(n1)
if n.x=.x Then if n.y=.y then push true : exit
push false
}
Module Print {
Print "Point" , .x, .y
}
Class:
Module PointA {
\\ ? means optionally
Read ? .[x], .[y]
}
}
Class Circle {
Property R=300~ ' type single
Operator "=" (n1) {
n=group(n1)
n2=This ' get a copy of this to check n against n2
if valid(@n as n2) else push false :exit
if n.x=.x Then if n.y=.y then if n.r=.r then push true : exit
push false
}
Module Print {
Print "Circle", .x, .y, .r
}
Class:
Module Circle {
if match("nn") then {
M=PointA(Number, Number)
} Else.if match("G") then {
M=PointA()
Read M
} Else M=PointA()
M=This
\\ If match("N") then Read M.r \\ check if a number is in top of stack
\\ Read ? M.r \\ optionally
Read M.r \\ for this example, r has value, so this used if stack is empty.
This=M
}
}
A=PointA(10,3)
C=Circle(20,10,5)
D=Circle(A, 100)
B=A
K=PointA()
Z=Circle(A)
P=PointA(600,700)
\\ N is a pointer to array
N=(A, B, C, D, K, P, Z)
M=each(N)
While M {
For This {
\\ a copy in MM
MM=Array(M)
MM.Print
Print A=MM, D=MM ' using MM=A interpreter use "=" from MM
}
}
\\ pA is a pointer to D (a named group)
pA->D
Print pA=D, pA=Z
pA=>Print
\\ pA is a pointer to a copy of D (a float group)
pA->(D)
Print pA=D, pA=Z
pA=>Print
\\ rA is a reference to D (& is optional in Link statement)
Link &D to &rA
rA.Print