Add + and * to add_grammer_rule. Realized that rules should actually be passed to functions as params, with option for variadac, that will be up next
This commit is contained in:
63
method.kp
Normal file
63
method.kp
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
(def! our_obj (with-meta [0] (fn* () (set-nth! our_obj 0 (+ 1 (nth our_obj 0))))))
|
||||
|
||||
(def! get-value-helper (fn* (dict key idx) (if (>= idx (count dict)) nil (if (= key (nth dict idx)) (nth dict (+ idx 1)) (get-value-helper dict key (+ idx 2))))))
|
||||
(def! get-value (fn* (dict key) (get-value-helper dict key 0)))
|
||||
(def! method-call (fn* (object method & arguments) (let* (method_fn (get-value (meta object) method))
|
||||
(if (= method_fn nil)
|
||||
(println "no method " method)
|
||||
(apply method_fn object arguments)))))
|
||||
; method call syntax
|
||||
(add_grammer_rule 'form [ 'form "\\." 'atom 'optional_WS "\\(" 'optional_WS 'space_forms 'optional_WS "\\)" ] (fn* (xs) `(method-call ~(nth xs 0) '~(nth xs 2) ,(nth xs 6))))
|
||||
(def! actual_obj (with-meta [0] [
|
||||
'inc (fn* (o) (set-nth! o 0 (+ (nth o 0) 1)))
|
||||
'dec (fn* (o) (set-nth! o 0 (- (nth o 0) 1)))
|
||||
'set (fn* (o n) (set-nth! o 0 n))
|
||||
'get (fn* (o) (nth o 0))
|
||||
]))
|
||||
(def! to_be_saved (with-meta [1] [2]))
|
||||
(def! main (fn* () (let* ( a 7
|
||||
b [1]
|
||||
c (with-meta b "yolo") )
|
||||
(do
|
||||
(try*
|
||||
((fn* () (do
|
||||
(println b)
|
||||
(set-nth! b 0 2)
|
||||
(println b)
|
||||
(println c)
|
||||
(println (meta c))
|
||||
(println "world")
|
||||
(println to_be_saved)
|
||||
(println (meta to_be_saved))
|
||||
(println "Here in main testing our_obj")
|
||||
(println our_obj)
|
||||
((meta our_obj))
|
||||
(println our_obj)
|
||||
((meta our_obj))
|
||||
(println our_obj)
|
||||
(println "actual_obj" actual_obj)
|
||||
(method-call actual_obj 'inc)
|
||||
(println "actual_obj" actual_obj)
|
||||
(println "with get: " (method-call actual_obj 'get))
|
||||
(println "actual_obj" actual_obj)
|
||||
(method-call actual_obj 'dec)
|
||||
(method-call actual_obj 'dec)
|
||||
(println "actual_obj" actual_obj)
|
||||
(println "setting old style 654")
|
||||
(method-call actual_obj 'set 654)
|
||||
(println "actual_obj" actual_obj)
|
||||
(println "Ok, doing with new method call syntax")
|
||||
actual_obj.inc()
|
||||
(println "actual_obj" actual_obj)
|
||||
(println "setting new style 1337")
|
||||
actual_obj.set(1337)
|
||||
(println "actual_obj" actual_obj)
|
||||
(println "with get " actual_obj.get())
|
||||
a)))
|
||||
)))))
|
||||
(do
|
||||
(println "interp-main")
|
||||
(main)
|
||||
(println "done interp-main")
|
||||
nil)
|
||||
Reference in New Issue
Block a user