Πέμπτη 5 Φεβρουαρίου 2026

Ternary Logic

https://rosettacode.org/wiki/Ternary_logic#M2000_Interpreter 

Task
  • Define a new type that emulates ternary logic by storing data trits.
  • Given all the binary logic operators of the original programming language, reimplement these operators for the new Ternary logic type trit.
  • Generate a sampling of results using trit variables.


module Ternary_logic {
class trit {
private:
variant val
function copy() {
m=this
m.trit
=m
}
public:
enum ternary {
True="True"
Maybe="Maybe"
False="False"
}
function true() {
=.copy(.True)
}
function maybe() {
=.copy(.Maybe)
}
function false() {
=.copy(.False)
}
operator "==" (k as trit) {
push .val=k.val
}
operator "^^" (k as trit) {
select enum .val
case .True
.val<=k.val
case .False
.val<=.False
case else
if k.val=.False then .val<=.False else .val<=.Maybe
end select
}
operator "^|" (k as trit) {
select enum .val
case .True
.val<=k.val
case .False
.val<=.True
case else
if k.val=.True then .val<=.True else .val<=.Maybe
end select
}
operator "||" (k as trit) {
select enum .val
case .False
.val<=k.val
case .True
.val<=.True
case else
if k.val=.True then .val<=.True else .val<=.Maybe
end select
}
operator "~~" (k as trit) {
select enum .val
case .True
.val<=k.val
case .False
if k.val=.True then .val<=.False else.if k.val=.False then .val<=.True else .val<=k.val
case else
.val<=.Maybe
end select
}
operator unary {
select enum .val
case .True
.val<=.False
case .False
.val<=.True
end select
}
group value {
value {
link parent val to val
=val
}
}
module trit {
if empty or not isnum then
read s as .ternary=.Maybe
.val<=s
else.if isnum then
read what
if what then
.val<=.True
else
.val<=.False
end if
end if
}
}
function enum2array(t) {
m=each(t)
while m {data eval(m)}
=array([])
}
string out, nl={
}
q=trit()
m=trit()
k=enum2array(q.ternary)
out ="not a" + nl
a=each(k)
while a
q=trit(array(a))
z=-q
out +="    ternary_not "+(q.value) + " = " + (z.value) + nl
end while
out +="a and b" + nl
a=each(k)
while a
b=each(k)
while b
q=trit(array(a))
m=trit(array(b))
z=q ^^ m
out += "    " + (q.value) + " ternary_and " + (m.value) + " = " + (z.value) + nl
end while
end while
out +="a or b" + nl
a=each(k)
while a
b=each(k)
while b
q=trit(array(a))
m=trit(array(b))
z=q || m
out += "    " + (q.value) + " ternary_or " + (m.value) + " = " + (z.value) + nl
end while
end while
out +="if a then b" + nl
a=each(k)
while a
b=each(k)
while b
q=trit(array(a))
m=trit(array(b))
z=q ^| m
out += "    if " + (q.value) + " then " + (m.value) + " = " + (z.value) + nl
end while
end while
out +="a is equivalent to b" + nl
a=each(k)
while a
b=each(k)
while b
q=trit(array(a))
m=trit(array(b))
z=q ~~ m
out += "    "+(q.value) + " is equivalent to " + (m.value) + " = " + (z.value) + nl
end while
end while
report out
clipboard out
}
Ternary_logic

Output:

not a

    ternary_not True = False

    ternary_not Maybe = Maybe

    ternary_not False = True

a and b

    True ternary_and True = True

    True ternary_and Maybe = Maybe

    True ternary_and False = False

    Maybe ternary_and True = Maybe

    Maybe ternary_and Maybe = Maybe

    Maybe ternary_and False = False

    False ternary_and True = False

    False ternary_and Maybe = False

    False ternary_and False = False

a or b

    True ternary_or True = True

    True ternary_or Maybe = True

    True ternary_or False = True

    Maybe ternary_or True = True

    Maybe ternary_or Maybe = Maybe

    Maybe ternary_or False = Maybe

    False ternary_or True = True

    False ternary_or Maybe = Maybe

    False ternary_or False = False

if a then b

    if True then True = True

    if True then Maybe = Maybe

    if True then False = False

    if Maybe then True = True

    if Maybe then Maybe = Maybe

    if Maybe then False = Maybe

    if False then True = True

    if False then Maybe = True

    if False then False = True

a is equivalent to b

    True is equivalent to True = True

    True is equivalent to Maybe = Maybe

    True is equivalent to False = False

    Maybe is equivalent to True = Maybe

    Maybe is equivalent to Maybe = Maybe

    Maybe is equivalent to False = Maybe

    False is equivalent to True = False

    False is equivalent to Maybe = Maybe

    False is equivalent to False = True



Τετάρτη 21 Ιανουαρίου 2026

Array Length

https://rosettacode.org/wiki/Array_length#M2000_Interprete

Update using the three different objects for array: tuple, mArray, RefArray. Old versions of M2000 had only mArray, always as Variant (and tuple was mArray), then a RefArray used with squared brackets but using various item size from Byte, then mArray also drive a RefArray (using dimensions over RefArray - which the second object is hidden) so we may have A(10,2) as Byte, then tuple are an object always flat and for variants only. The B()=A when A point to tuple copies to B() as mArray. An mArray is heavier object, because holds array for one to ten dimensions of values for low and high bound, p;us an array for each dimension for offsets on a flat array (1d)  for item values (or pointer to RefArray, if we use specific type). A pointer to array may point to tuple or mArray. A pointer to RefArray can't change to anything else (can be value for a variant type variable or array item). A RefArray may have one or two dimensions, but we can construct any number by using a variant type RefArray using each item a refArray and so on. So we can make jagged arrays, using RefArrays. We can do the same using mArray of type variant or tuple. So A=((,), (1,), (1,2), (1,2,3)) is a jagged array of type tuple. The (,) is an empty tuple (length zero). For mArray arrays the A()=B() is a copy of B() to A(). and A()=A when A is pointer to mArray or tuple is also a copy, but A=B() is a pointer to B() copied to A.

' A is a pointer to array
A=("Apple", "Orange")
Print  Len(A)=2 ' True
Print Type$(A)="tuple"
Dim A(10,2) as Byte=32, B(), C(10,2)
Print Len(A())=20, Len(B())=0, Len(C())=20
C(1,1)="String"
C(2,0)=100, 3
Dim C(6,2)
Print Len(C())=12
Print C(2,0)=100, C(2,1)=3, C(1,1)="String"
B()=A ' this is a copy
A=(,)
Print B(1)="Orange", Len(B())=2, Len(A)=0
Print Type$(B())="mArray", Type$(A)="tuple"
A=B() ' now A point to B()
Print Type$(A)="mArray"
Print A(5,1)=32, Type$(A(5,1))="Byte"
A=A() ' point to A() we address through A as a flat 1d array.
Print Type$(A,0)="Byte", A#val(0)=32, A#sum()=32*20, A()#sum()=32*20
Print Dimension(A())=2 ' Number of dimensions
Print Dimension(A(), 1)=10, Dimension(A(), 2)=2 ' length for each dimension
Print Dimension(A(), 1, 0)=0, Dimension(A(), 1, 1)=9 ' lower and upper bound on 1st Dimension


' RefArray type of Arrays (always 0 lower bound)
Variant D[100]
Print Len(D)=101, Type$(D)="RefArray"
Byte F[9][1]
Print Len(F)=10
F[3][0]=111
F[3][4]=105
D[3]=F[3][] ' copy a row from F[3] to D[3]
F[3][0]++

Print D[3][0]=111, D[3][4]=105, F[3][0]=112