Fixes in lambda/function definitions (body args). Cleanup of examples and decumentation. Added convenience methods for JS interop, lists and objects.

This commit is contained in:
Kuba Antosik
2021-04-04 18:35:26 +10:00
parent 8d23e5264e
commit 0cebbe52cb
24 changed files with 655 additions and 120 deletions

View File

@@ -35,9 +35,9 @@
;
(defn redraw ()
(
(clear)
(rec 300)))
(rec 300))
;
(on "animate" redraw)

View File

@@ -3,6 +3,9 @@
;
(clear)
(def frame
(get-frame))
;
(def gradient-line
(line frame:c 0 frame:c frame:h))

View File

@@ -2,6 +2,9 @@
(clear)
(def frame
(get-frame))
; times
@@ -26,13 +29,13 @@
; position on a circle from angle
(defn circle-pos
(cx cy r a) {:x
(cx cy r a) (object "x"
(add cx
(mul r
(cos a))) :y
(cos a))) "y"
(add cy
(mul r
(sin a)))})
(sin a)))))
; draw
@@ -42,10 +45,10 @@
(
(stroke
(line cx cy
(:x
(circle-pos cx cy r a))
(:y
(circle-pos cx cy r a))) "white" 2)))
(get
(circle-pos cx cy r a) "x")
(get
(circle-pos cx cy r a) "y")) "white" 2)))
;
(defn draw-star

View File

@@ -2,7 +2,8 @@
(def theme
(get-theme))
(def frame
(get-frame))
; ex: theme:f_high

View File

@@ -1,5 +1,11 @@
(clear)
(def theme
(get-theme))
(def frame
(get-frame))
(defn branch
(v)
(if

View File

@@ -2,6 +2,9 @@
(clear)
(def theme
(get-theme))
(transform:move 150 150)
(fill

View File

@@ -0,0 +1,168 @@
;A simple MIDI visualiser
;showing activity per note
;and channel.
(clear)
(def theme
(get-theme))
(resize 1200 600)
(def maxcirclesize 50)
(def mincirclesize 20)
(def circlexdist 40)
(def circleydist 30)
(def notes
(object 0 "A" 1 "A#" 2 "B" 3 "C" 4 "C#" 5 "D" 6 "D#" 7 "E" 8 "F" 9 "F#" 10 "G" 11 "G#"))
(def sharpnotes
(object 1 "A#" 4 "C#" 6 "D#" 9 "F#" 11 "G#"))
(def frame
(get-frame))
(def jswindow
(js))
(defn isblackkey
(num)
(def distfromA0
(sub num 21))
(def semitonenum
(mod distfromA0 12))
(not
(eq
(get sharpnotes semitonenum) undefined)))
(defn circlebynotenum
(num)
(circle
(mul
(sub num 47) circlexdist)
(if
(isblackkey num) circleydist
(mul 2.5 circleydist)) mincirclesize))
(def reactivecircles
(reduce
(range 48 76)
(λ
(acc num index)
(set acc num
(circlebynotenum num)))
(object)))
(def channelcircles
(map
(range 0 15)
(λ
(num)
(circle
(add
(mul num circlexdist 1.25) circlexdist) 200 mincirclesize))))
(defn js-exec
(obj fname listargs)
(def boundfunction
(js-bind
(get obj fname) obj))
(def result
(apply boundfunction
(if
(eq listargs undefined) () listargs))) result)
(defn midimsghandler
(midiMessage)
(def eventType
(get
(:data midiMessage) "0"))
;zero based
(def channelNum
(logand eventType 15))
;ignore clock in debug to keep things cleaner
(if
(not
(eq eventType 248))
(debug "incoming MIDI:" "CH" channelNum
(:data midiMessage)))
(def noteNum
(get
(:data midiMessage) "1"))
(def noteVelocity
(get
(:data midiMessage) "2"))
(set
(get channelcircles channelNum) "r"
(add mincirclesize
(mul
(sub maxcirclesize mincirclesize)
(div noteVelocity 100))))
(set
(or
(get reactivecircles noteNum)()) "r"
(add mincirclesize
(mul
(sub maxcirclesize mincirclesize)
(div noteVelocity 100)))))
(defn midiokhandler
(midiAccess)
(def midiInputs
(js-exec
(:inputs midiAccess) "values"))
(eachof midiInputs
(λ
(input id)
(debug "Setting listener on"
(:name input)
(:manufacturer input))
(set input "onmidimessage" midimsghandler))))
(defn midierrhandler
(err)
(debug "midierrhandler" err))
(js-exec
(js-exec
(:navigator jswindow) "requestMIDIAccess") "then"
(list midiokhandler midierrhandler))
(defn drawcircle
(arglist)
(def notenum
(first arglist))
(def i
(last arglist))
(if
(gt
(:r i) mincirclesize)
(set i "r"
(sub
(:r i) 0.6)))
(fill i
(if
(isblackkey notenum)
(theme:f_med)
(theme:f_low))))
(defn redraw ()
(clear)
(each
(entries reactivecircles) drawcircle)
(each channelcircles
(λ
(s i)
(fill s theme:b_high)
(fill
(text
(sub s:cx mincirclesize)
(add s:cy mincirclesize 18) 18
(concat "CH" i)) theme:b_inv 2))))
;
(on "animate" redraw)

View File

@@ -0,0 +1,3 @@
# Not implemented (yet)
This set of examples shows the limitations of Ronin's LISP.

View File

@@ -0,0 +1,44 @@
;To inspect the results of these tests,
;open your browser's debugging tools
;
(usually F12)
and navigate to the
;JavaScript console.
(clear)
(def mydog
(object "color" "gold" "coat" "long" :speed "quick" :health "good"))
(debug mydog)
(test "My dog's color is"
(get mydog "color") "gold")
(test "My dog's coat is"
(get mydog "coat") "long")
(test "My dog's speed is"
(get mydog "speed") "quick")
;the last one passes only because :health
;in the set instruction above
;resolves to undefined - and
;technically you can have one object value
;for key=undefined
(test "My dog's health is"
(get mydog :health) "good")
;You can, however, use obj:param syntax to
;get properties of objects.
;A shorthand for
(get obj "param")
.
;
(test "Get color" mydog:color "gold")
;Also,
(:coat mydog)
;is another shorthand.
(test "Get coat shorthand function"
(:coat mydog) "long")

View File

@@ -2,17 +2,31 @@
(clear)
(defn glitch
(rec)
(if (gt rec 1)
(
(translate
(rect (random 400) (random 400) (random 10) (random 10))
(pos (random 400) (random 400)))
(glitch (sub rec 1)))))
(import
"../static/crystal.jpg"
(import $path
(rect 0 0 400 400))
(defn translate
(source-rect dest-pos)
(
(paste
(copy source-rect)
(rect dest-pos:x dest-pos:y source-rect:w source-rect:h))))
(defn glitch
(rec)
(if
(gt rec 1)
(
(translate
(rect
(random 1 400)
(random 1 400)
(random 1 10)
(random 1 10))
(pos
(random 1 400)
(random 1 400)))
(glitch
(sub rec 1)))))
(glitch 500)

View File

@@ -1,6 +1,12 @@
; Normalize photo colors
(open $path 0.5)
;(pick (rect 0 0 100 100)) will return the average color
;of 100x100px rectangle.
;without extra arguments,
;pick grabs the whole canvas by default,
;and calculates the average color.
(def average-color
(pick))
(pixels normalize average-color)

View File

@@ -1,4 +1,4 @@
; saturate image
(open "../static/crystal.jpg")
(pixels
(frame) saturation 12)
(open $path)
(pixels saturation 12
(get-frame))

View File

@@ -2,7 +2,7 @@
; drag an image on the window
(open $path)
;
(pixels
(rect 100 100 400 400) saturation 0)
(pixels
(rect 300 300 400 400) contrast 0.5)
(pixels saturation 0
(rect 100 100 400 400))
(pixels contrast 0.5
(rect 300 300 400 400))

View File

@@ -6,7 +6,7 @@
(if
(gt rec 0)
(
(import "../static/crystal.jpg"
(import $path
(rect
(random 200)
(random 200)

View File

@@ -18,63 +18,10 @@
(pick
(guide
(rect $xy unit unit))))
(def color-2
(pick
(guide
(rect $xy unit unit))))
(def color-3
(pick
(guide
(rect $xy unit unit))))
(def color-4
(pick
(guide
(rect $xy unit unit))))
(def color-5
(pick
(guide
(rect $xy unit unit))))
(def color-6
(pick
(guide
(rect $xy unit unit))))
(def color-7
(pick
(guide
(rect $xy unit unit))))
(def color-8
(pick
(guide
(rect $xy unit unit))))
(echo color-1)
; display
(fill
(circle
(mul 20 2) pos-row-1 18) color-1)
(fill
(circle
(mul 20 4) pos-row-1 18) color-2)
(fill
(circle
(mul 20 6) pos-row-1 18) color-3)
(fill
(circle
(mul 20 8) pos-row-1 18) color-4)
(fill
(circle
(mul 20 3) pos-row-2 18) color-5)
(fill
(circle
(mul 20 5) pos-row-2 18) color-6)
(fill
(circle
(mul 20 7) pos-row-2 18) color-7)
(fill
(circle
(mul 20 9) pos-row-2 18) color-8)
;
(def res
(add color-1:hex ":" color-2:hex ":" color-3:hex ":" color-4:hex ":" color-5:hex ":" color-6:hex ":" color-7:hex ":" color-8:hex))
(echo
(add res ":" res))

View File

@@ -1,4 +1,4 @@
(resize 600 200)
(resize 600 800)
(clear)
@@ -14,6 +14,9 @@
(guide
(line 0 100 600 100)) colors))
;collect colors to prepared list,
;in particular points from the gradient
;marked by the guides
(each picked-colors
(λ
(color id)
@@ -25,5 +28,33 @@
(mul id
(div 600 9)) 100)))))))
;show picked colors as swatches
(each picked-colors
(λ
(color id)
(
;swatch circle
(fill
(circle
20 (add (mul id
(div 600 9)) 300) 18) color)
"black")))
;show picked colors as text
(each picked-colors
(λ
(color id)
(
(fill
(text
12 (add (mul id
(div 600 9)) 300 5) 24
(concat id ": "
(get
(get picked-colors
(concat "" id)) "hex"))) "black"))))
;get the first color in different formats
(echo
(get picked-colors:1 "hex"))
(get picked-colors:0 "hex")
(get picked-colors:0 "rgba"))

View File

@@ -7,5 +7,8 @@
(add
(mul index 40) 50) 40 name) "red"))
;this will display the list of loaded files.
;drag a few pictures into Ronin, then eval
;this script again.
(each
(files) print-file)

View File

@@ -3,6 +3,8 @@
;
(clear)
(def frame (get-frame))
;
(def gradient-line
(line frame:c 0 frame:c frame:h))

134
examples/tests/lists.lisp Normal file
View File

@@ -0,0 +1,134 @@
(test "Native last call - list of numbers"
(last
(1 2 3)) 3)
(test "Native last call - list of numbers, 1 element"
(last
(1)) 1)
(test "Native last call - list of numbers, no elements"
(last ()) undefined)
(test "Native last call - list of strings, 1 element"
(last
("abc")) "abc")
(test "Native last call - list of strings, many elements"
(last
("ala" "bla" "cla")) "cla")
;functions defined as defn/lambda args bodyinstrs...
(defn lastfn
(listarg) (debug "some other instruction")
(last listarg))
(test "Proxied last call - list of numbers"
(lastfn
(1 2 3)) 3)
(test "Proxied last call - list of numbers, 1 element"
(lastfn
(1)) 1)
(test "Proxied last call - list of numbers, no elements"
(lastfn ()) undefined)
(test "Proxied last call - list of strings, 1 element"
(lastfn
("abc")) "abc")
(test "Proxied last call - list of strings, many elements"
(lastfn
("ala" "bla" "cla")) "cla")
(test "Lambda last call - list of numbers"
((λ
(listarg) (debug "some other instruction")
(last listarg))
(1 2 3)) 3)
(test "Lambda last call - list of numbers, 1 element"
((λ
(listarg) (debug "some other instruction")
(last listarg))
(1)) 1)
(test "Lambda last call - list of numbers, no elements"
((λ
(listarg) (debug "some other instruction")
(last listarg)) ()) undefined)
(test "Lambda last call - list of strings, 1 element"
((λ
(listarg) (debug "some other instruction")
(last listarg))
("abc")) "abc")
(test "Lambda last call - list of strings, many elements"
((λ
(listarg) (debug "some other instruction")
(last listarg))
("ala" "bla" "cla")) "cla")
;functions defined as defn/lambda args bodyinstr
(defn lastfn2
(listarg)
(last listarg))
(test "Proxied last call - list of numbers"
(lastfn2
(1 2 3)) 3)
(test "Proxied last call - list of numbers, 1 element"
(lastfn2
(1)) 1)
(test "Proxied last call - list of numbers, no elements"
(lastfn2 ()) undefined)
(test "Proxied last call - list of strings, 1 element"
(lastfn2
("abc")) "abc")
(test "Proxied last call - list of strings, many elements"
(lastfn2
("ala" "bla" "cla")) "cla")
(test "Lambda last call - list of numbers"
((λ
(listarg)
(last listarg))
(1 2 3)) 3)
(test "Lambda last call - list of numbers, 1 element"
((λ
(listarg)
(last listarg))
(1)) 1)
(test "Lambda last call - list of numbers, no elements"
((λ
(listarg)
(last listarg)) ()) undefined)
(test "Lambda last call - list of strings, 1 element"
((λ
(listarg)
(last listarg))
("abc")) "abc")
(test "Lambda last call - list of strings, many elements"
((λ
(listarg)
(last listarg))
("ala" "bla" "cla")) "cla")