Δευτέρα 30 Απριλίου 2018

Revision 8 Version 9.3

Some bugs removed. There is a new operator IS for comparing pointers.

An example using SuperClass and Pointers. We want to make some groups with a common SuperClass and each one we wish to get a specific Id from a counter in SuperClass.



SuperClass Super {
      Unique:
            counter
      Public:
      \\ Properties are groups with values
      \\ we set only Value part (no Set part)
      \\ So these are read only
      Property stuff$ {value}
      Property id {value}
      \\ this is just a read/write public member
      aValue=0
      Function Final Many {
            For SuperClass {
                  =.counter
            }                  
      }
}
\\ we create a Lambda function with closur the SuperClass
\\ we make a tmp group based on SuperClass
ClassSuper=Lambda Super (aName$="No name", content=-1) -> {
      tmp=Super
      \\ attach two modules
      Group tmp {
      \\ using Class: we get no copy of this
      Class:
            Module addone {
                  For SuperClass {
                        .counter++
                  }
            }
            Module Init {
                  \\ .[stuff]$ and .[id] are the private variables for properties
                  Read .[stuff]$
                  For SuperClass {
                        This.[id]<=.counter
                   }
            }
      }
      \\ execute
      tmp.addone
      tmp.init aName$
      tmp.aValue=content
    \\  =tmp
    \\ we can return a pointer to a copy of tmp
    \\ because tmp is a named group, if we return a pointer using ->tmp then
    \\ we get a reference to a not exist group (tmp deleted at the exit of function)
    \\ a copy of tmp is not a named group. All members are inside object
    \\ using =tmp we just push the object to evaluator
    \\ and A, B and A(3) get a unique object, the first too as named groups, the third as is
    \\ but using ->(tmp) we make A, B and A(3) pointers, so A and B are not named groups
    \\ from a named group we can get reference from a member
    \\ to do the same with a pointer to group we have to open it with a For { }

    \\so here we return a copy of tmp which have a hidden pointer to SuperClass
      ->(tmp)
}
A=ClassSuper("George", 100)
B=ClassSuper("Babis", 150)
Dim A(10)
A(3)=ClassSuper()
\\ using For we make temporary staticaly bound objects
\\ we use one dot for first, two dots for second, three dots for third
For A, B, A(3) {
      Print .stuff$, ..stuff$, ...stuff$ ' "George" "Babis" "No Name"
      Print .id, ..id, ...id ' 1  2  3
}
\\ or we can use => to access members
Print A=>aValue, B=>aValue, A(3)=>aValue ' 100  150  -1
Print A=>many() ' 3
Module TestRef (&x) {
      x+=10
}
For A(3) {
      TestRef &.aValue ' see dot after &
      Print .aValue
}




Another example with superclass and pointers



module beta {
      function sup {
            superclass alfa {
            \\ x has the superclass and each group from this superclass
                  x=10
                  Function Super.X {
                        for superclass {
                              =.X
                        }
                  }
                  module zeta {
                        for superclass {
                              .x++
                              Print .x
                        }
                        .delta
                  }
                  module delta {
                        for superclass {
                              .x++
                              Print .x
                        }
                  }
            }
            =alfa
      }
      \\ we make a pointer m
      m->sup()
      \\ we call modules zeta in group in m
      m=>zeta
      m=>zeta
      Print m=>x=10
      \\ now we push to stack the pointer
      Push m
}
\\ so now we call beta module
beta
\\ and now we get the pointer in p
Read p
\\ we can call zeta again 2 times
p=>zeta
p=>zeta
Print p=>x =10
\\ we make a pointer from a copy of  group which point the p pointer
\\ p1->p makes a copy of pointer only
p1->(p)
p1=>zeta
p1=>zeta
Print p1=>x=10
p=>x+=20
p1=>x+=30
Print p=>x=30, p1=>x=40
Print p is p1 = false
Print Valid(@p as p1) = true ' they have same superclass
p2->p1
Print p1 is p2 = true
p1->0 ' we give an empty group to point
Print p1 is p2 = false
p1->p2
Print p1 is p2 = true
Print Valid(@p as p2) = true ' they have same superclass
Print p2=>Super.X()=22 ' all groups from same superclass can read values of superclass





Some small examples added here:


One line function with recursion

Def fib(x)=If(x>1->fib(x-1)+fib(x-2), x)
For i= 0 to 10
      Print fib(i)
Next i


If we wish to use reference to function, and recursion we have to change the call to same name, using Lambda() or Lambda$() if used for strings.


Def fib(x)=If(x>1->lambda(x-1)+lambda(x-2), x)
For i= 0 to 10
      Print fib(i)
Next i

Push &fib()
Read &AnyName()
Print AnyName(8)=21

 
Advanced Fibonacci Function with precomputed values, using Lambda Function and a pointer to array closure
Function FiboFactory {
      F=lambda A=(1,1) (x) ->{
            Link A to A()
            if x<=0 then =A(0) : Exit
            if x<=Len(A) then =A(x-1) : Exit
            For i=Len(A) to x-1 {
                  A=Cons(A,(A(i-1)+A(i-2),))
            }
            =A(Len(A)-1)
      }
      \\ Number readed from stack
      For i=1 to Number {
           Call Void F(i) ' use void to drop value
      }
      =F
}
Fib=FiboFactory(200)
For i=190 to 210 {
      Print i, Fib(i)
}




Using Eval() and anonymous function

N=10
Print Eval("12+78*N") , 12+78*N
Print Function("{Read X: =X**2}", 2)=4


Using Every milliseconds {} to animate console form

Show
Window 72, 12000;
Cursor 0, Height/2
Report 2, "HELLO WORLD"
l=3600
k=60000-l
Motion.w 60000
Wait 500
Every 100 {
      k*=4/5
      Motion.w k, 3600
      If k<l then exit
}
Motion.w 3600,3600
Wait 2000
Window 12, 0 ' 0 used for monitor 1

Three Statements to open a Form
Declare Form1 Form
Method Form1, "Show", 1
Declare Form1 Nothing




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

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

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