2022-01-14 16:09:52 +00:00
|
|
|
## dictionary list (DIC)
|
|
|
|
{ 1 2 3 4 }
|
|
|
|
{ 1: 2, 3: 4}
|
|
|
|
{} -> empty dictionary
|
|
|
|
{ 1 } -> error, no value. dictionary requires even number of items.
|
|
|
|
|
|
|
|
## array list
|
|
|
|
[ 1 2 3 4]
|
|
|
|
[ 1, 2, 3, 4 ]
|
|
|
|
[] -> empty array
|
|
|
|
|
|
|
|
## byte array list
|
|
|
|
#[ 1 2 3 4 ]
|
|
|
|
#[ 1, 2, 3, 4 ]
|
|
|
|
each item must be in the byte range.
|
|
|
|
if a given value is not a number in the allowed range, an exception error is raised.
|
|
|
|
(try
|
|
|
|
(set a 20)
|
|
|
|
#[ 1 2 3 (+ a 300)] ; this throws an catchable exception.
|
|
|
|
catch(e)
|
|
|
|
(printf "EXCEPTION - %O\n" e)
|
|
|
|
)
|
|
|
|
|
|
|
|
## non-executable list (QLIST)
|
|
|
|
#(1 2 3 4)
|
|
|
|
#(1 2 3 4 . 5)
|
|
|
|
#() -> same as null
|
|
|
|
comma not allowed to seperate items.
|
|
|
|
|
|
|
|
## varaible declaration list (VLIST)
|
|
|
|
| a b c |
|
|
|
|
|
|
|
|
|
|
|
|
## class declaration with methods.
|
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
(defclass X
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
| x y | ; instance variables
|
|
|
|
::: | bob jim | ; class variables
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
; instance variables and class variables must not collide with those of parent classes.
|
|
|
|
; they must not collide with method names of parent classes
|
|
|
|
|
|
|
|
(set bob "Bob") ; can access class variables. disallowed to access instance variables
|
|
|
|
|
|
|
|
(defun setX (a)
|
|
|
|
(set x a)
|
|
|
|
)
|
|
|
|
|
|
|
|
; instance method. a method name must not collide with instance variable names and class variable names.
|
|
|
|
; the name can be the same as method names of parent classes.
|
|
|
|
(defun K (a b)
|
|
|
|
(:self Y a)
|
|
|
|
(return (+ a b x y))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defun Y (a)
|
|
|
|
(printf "Y=>%d [%s]\n" a bob)
|
|
|
|
)
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
(defun ::: KK (a b)
|
|
|
|
(printf "K=>%s\n" bob) ; a class method can access class variables but not instance variables
|
|
|
|
(return (+ a b))
|
|
|
|
)
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
(set jim (lambda (a b) (+ a b))) ; an anonymous function created
|
2022-01-14 16:09:52 +00:00
|
|
|
)
|
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
(set r (object-new X))
|
|
|
|
(:r Y 10)
|
|
|
|
(printf ">>%d\n" (:X KK 77 99))
|
2022-01-14 16:09:52 +00:00
|
|
|
|
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
## method invocation
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
send the message aaa to the receiver
|
|
|
|
(:self aaa)
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
send the message aaa to the receiver but let it resolve the method in the superclass side.
|
|
|
|
(:super aaa)
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
send the message dump to the object pointed to by x with arguments 1, 2, 3.
|
|
|
|
(:x dump 1 2 3)
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
## method types
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
- class method
|
|
|
|
- class instantiation method
|
|
|
|
- instance method
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
## dynamic dispatching by method name
|
|
|
|
(defclass X
|
|
|
|
(defun t1 (x) (printf "t1 = %d\n" (+ x x x)))
|
|
|
|
(defun t2 (x) (printf "t2 = %d\n" (* x x x)))
|
|
|
|
)
|
2022-01-14 16:09:52 +00:00
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
(defun get-name-1() "t1")
|
|
|
|
(defun get-name-2() "t2")
|
|
|
|
|
|
|
|
(set x (object-new X))
|
|
|
|
|
|
|
|
(:x (get-name-1) 100) ; must be same as (:x t1 100)
|
|
|
|
(:x (get-name-2) 100) ; must be same as (:x t2 100)
|
|
|
|
|
|
|
|
## Something to look into..
|
2022-01-14 16:09:52 +00:00
|
|
|
|
|
|
|
normal function call
|
|
|
|
(f arg1 arg2 arg3)
|
|
|
|
(rcv f arg1 arg2)
|
|
|
|
|
2022-02-27 19:35:47 +00:00
|
|
|
## dynamic method invocation???
|
2022-01-14 16:09:52 +00:00
|
|
|
(:X (f) arg1 arg2)
|
|
|
|
as long as f returns a symbol, it can also invoke a method??
|
|
|
|
|
|
|
|
|
|
|
|
(defun getX() X) ; ->it must return an object
|
|
|
|
((getX)->show "hello")
|
|
|
|
|
|
|
|
X.Y
|
|
|
|
push X
|
|
|
|
push_symbol Y
|
|
|
|
lookup
|
|
|
|
|
|
|
|
(X.Y)
|
|
|
|
push X
|
|
|
|
push_symbol Y
|
|
|
|
lookup
|
|
|
|
call 0
|
|
|
|
|
|
|
|
X.Y.Z
|
|
|
|
push X
|
|
|
|
push_symbol Y
|
|
|
|
lookup
|
|
|
|
push_symbol Z
|
|
|
|
lookup
|
|
|
|
|
|
|
|
--- if this is within a method, it must become push_instvar
|
|
|
|
self.x
|
|
|
|
push self
|
|
|
|
push symbol x
|
|
|
|
lookup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun f(a, b)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
fun f(a, b) -> (c, d)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class X
|
|
|
|
{
|
|
|
|
var x, y, z
|
|
|
|
var! printer;
|
|
|
|
|
|
|
|
printer := Printer.new();
|
|
|
|
|
|
|
|
fun! new(a, b)
|
|
|
|
{
|
|
|
|
return super.new().init(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
fun init(a, b)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
fun show(a, b)
|
|
|
|
{
|
|
|
|
Printer.dump(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
x := X.new(10, 20);
|
|
|
|
x.show (40, 50);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------
|