Κυριακή 29 Σεπτεμβρίου 2024

Version 12, Revision 36

A small bug fixed (one line of code), so now a copy of an event object copy the enable state (which we can alter using the keywords Hold and Release)

The example bellow didn't print number a, because the hold state copied to c.

event a {
read a
function {
print a
}
}
event a hold
c=a
call event c, 4


This is an example of use of an event object as a visitor. The pointer()  (used without parameter) return the null object (is a group with type Null, which we can't add anything, and we can merge to). Using the Null object we always have pointer to point something. This example has no use of Hold or Release on event, but we can insert Event PrintAnum Hold after the Event PrintAnum New PrintThis() to see what happen.

Report {
      Tree traversal
             1
            / \
           /   \
          /    \
         2      3
        / \    /
       4   5   6
      /      / \
     7      8   9

}
Pen 15 {Print "OOP - Event object as visitor"}
Module OOP {
    Class Node {
    Public:
        x, LeftNode=pointer(), RightNode=pointer()
        Module preorder (visitor){
            T->This
            printtree(T)
            sub printtree(T as pointer)
                If T is type Null then Exit sub
                call event visitor, T=>x
                printtree(T=>LeftNode)
                printtree(T=>RightNode)
            end sub
        }
        Module inorder (visitor){
            T->This
            printtree(T)
            sub printtree(T as pointer)
                If T is type Null then Exit sub
                printtree(T=>LeftNode)
                call event visitor, T=>x
                printtree(T=>RightNode)
            end sub
        }
        Module postorder (visitor) {
            T->This
            printtree(T)
            sub printtree(T as pointer)
                If T is type Null then Exit sub
                printtree(T=>LeftNode)
                printtree(T=>RightNode)
                call event visitor, T=>x
            end sub
        }
        Module level_order (visitor){
            T->This
            Stack New {
                printtree(T)
                if empty then exit
                Read T
                Loop
            }
            sub printtree(T as pointer)
                If T is type Null else
                  call event visitor, T=>x
                  Data T=>LeftNode, T=>RightNode
                end if
            end sub
        }
        remove {
          Print "Node removed ";.x
        }
    Class:
        \\ after class:  anything exist one time,
        \\ not included in final object
        Module Node {
            Read .x
            \\ read ? for optional values
            Read ? .LeftNode, .RightNode
        }
    }
    \\ NodeTree return a pointer to a new Node
    Function NodeTree {
        \\ ![] pass currrent stack to Node()
        ->Node(![])
    }

    Tree=NodeTree(1, NodeTree(2,NodeTree(4, NodeTree(7)), NodeTree(5)), NodeTree(3, NodeTree(6, NodeTree(8), NodeTree(9))))
    Event PrintAnum {
        read x
    }
    Function PrintThis(x) {
          Print x;" ";
    }
    Event PrintAnum New PrintThis()
    printnum=lambda PrintAnum (title$) -> {
        Print
        Print title$;
        =PrintAnum
    }
    Tree=>preorder printnum("preorder:  ")
    Tree=>inorder printnum("inorder:   ")
    Tree=>postorder printnum("postorder:   ")
    Tree=>level_order printnum("level-order: ")
    Print
    Print "Removing nodes like a preorder traversal"
}
OOP


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.