From 09ccccf4496798f54cb49110c3a06256cc7fea00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Thu, 11 May 2023 14:47:48 -0700 Subject: Remove some nested Whens --- virtual-programs/archive/highlight.folk | 12 ++++----- virtual-programs/archive/image.folk | 16 +++++------- virtual-programs/archive/points-up-test.folk | 6 ++--- virtual-programs/archive/points-up.folk | 30 ++++++++++------------ virtual-programs/camera-slice.folk | 11 +++----- virtual-programs/images.folk | 16 +++++------- virtual-programs/intersect.folk | 38 +++++++++++++--------------- virtual-programs/label.folk | 14 +++++----- virtual-programs/outline.folk | 4 +-- virtual-programs/process.folk | 6 ++--- virtual-programs/regions.folk | 6 ++--- 11 files changed, 66 insertions(+), 93 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/archive/highlight.folk b/virtual-programs/archive/highlight.folk index d4206622..79cdb4db 100644 --- a/virtual-programs/archive/highlight.folk +++ b/virtual-programs/archive/highlight.folk @@ -1,10 +1,8 @@ Wish $this has filename "highlight.folk" -When /someone/ wishes /thing/ is highlighted /color/ { - When $thing has region /region/ { - # it's not really correct to just stick a side-effect in the - # When handler like this. but we did it in Realtalk, and it - # was ok, so whatever for now - Display::fillRect device $x $y [expr {$x+$width}] [expr {$y+$height}] $color - } +When /someone/ wishes /thing/ is highlighted /color/ & /thing/ has region /region/ { + # it's not really correct to just stick a side-effect in the + # When handler like this. but we did it in Realtalk, and it + # was ok, so whatever for now + Display::fillRect device $x $y [expr {$x+$width}] [expr {$y+$height}] $color } diff --git a/virtual-programs/archive/image.folk b/virtual-programs/archive/image.folk index 4d2ca6fd..b3364d14 100644 --- a/virtual-programs/archive/image.folk +++ b/virtual-programs/archive/image.folk @@ -19,14 +19,12 @@ namespace eval ::image { namespace ensemble create } -When the camera frame is /f/ { - When $this has region /r/ { - lassign [projectorToCamera [lindex $r 0 0]] x0 y0 - lassign [projectorToCamera [lindex $r 0 2]] x1 y1 - set thisimage [image subimage $f \ - [expr {min($x0,$x1)}] [expr {min($y0,$y1)}] \ - [expr {int(abs($x1-$x0))}] [expr {int(abs($y1-$y0))}]] - Wish display runs [list Display::image {*}[lindex $r 0 0] $thisimage] - } +When the camera frame is /f/ & $this has region /r/ { + lassign [projectorToCamera [lindex $r 0 0]] x0 y0 + lassign [projectorToCamera [lindex $r 0 2]] x1 y1 + set thisimage [image subimage $f \ + [expr {min($x0,$x1)}] [expr {min($y0,$y1)}] \ + [expr {int(abs($x1-$x0))}] [expr {int(abs($y1-$y0))}]] + Wish display runs [list Display::image {*}[lindex $r 0 0] $thisimage] } Wish $this is outlined red diff --git a/virtual-programs/archive/points-up-test.folk b/virtual-programs/archive/points-up-test.folk index b0b90ae9..c4fcff4d 100644 --- a/virtual-programs/archive/points-up-test.folk +++ b/virtual-programs/archive/points-up-test.folk @@ -1,8 +1,6 @@ Wish $this is highlighted blue Wish $this points up -When $this points up at /target/ { - When $target has program code /code/ { - Wish $target is labelled "being pointed at" - } +When $this points up at /target/ & /target/ has program code /code/ { + Wish $target is labelled "being pointed at" } diff --git a/virtual-programs/archive/points-up.folk b/virtual-programs/archive/points-up.folk index 24a3a4c9..e5c12126 100644 --- a/virtual-programs/archive/points-up.folk +++ b/virtual-programs/archive/points-up.folk @@ -1,20 +1,18 @@ -When /someone/ wishes /rect/ points up { - When $rect is a rectangle with x /x/ y /y/ width /width/ height /height/ { - set wx [expr {$x+$width/2}] - set wy [expr {$y-40}] - set ww 5 - set wh 40 - Claim $rect-whisker is a rectangle with x $wx y $wy width $ww height $wh - Wish $rect-whisker is highlighted green +When /someone/ wishes /rect/ points up & /rect/ is a rectangle with x /x/ y /y/ width /width/ height /height/ { + set wx [expr {$x+$width/2}] + set wy [expr {$y-40}] + set ww 5 + set wh 40 + Claim $rect-whisker is a rectangle with x $wx y $wy width $ww height $wh + Wish $rect-whisker is highlighted green - When /target/ is a rectangle with x /tx/ y /ty/ width /tw/ height /th/ { - if {$target != $rect && \ - [rectanglesOverlap \ - [list $wx $wy] [list [expr {$wx+$ww}] [expr {$wy+$wh}]] \ - [list $tx $ty] [list [expr {$tx+$tw}] [expr {$ty+$th}]] \ - false]} { - Claim $rect points up at $target - } + When /target/ is a rectangle with x /tx/ y /ty/ width /tw/ height /th/ { + if {$target != $rect && \ + [rectanglesOverlap \ + [list $wx $wy] [list [expr {$wx+$ww}] [expr {$wy+$wh}]] \ + [list $tx $ty] [list [expr {$tx+$tw}] [expr {$ty+$th}]] \ + false]} { + Claim $rect points up at $target } } } diff --git a/virtual-programs/camera-slice.folk b/virtual-programs/camera-slice.folk index c4e49e2c..5d36f2f1 100644 --- a/virtual-programs/camera-slice.folk +++ b/virtual-programs/camera-slice.folk @@ -4,11 +4,8 @@ return Wish $this has camera image Wish $this is outlined thick palegoldenrod -When $this has camera image /im/ { - When $this has region /r/ { - set coords [lmap n [lindex $r 0 0] {expr $n * 1}] - Wish display runs [list Display::image {*}$coords $im] - Wish $this is labelled "$coords $im" - # filter $im - } +When $this has camera image /im/ & $this has region /r/ { + set coords [lmap n [lindex $r 0 0] {expr $n * 1}] + Wish display runs [list Display::image {*}$coords $im] + Wish $this is labelled "$coords $im" } diff --git a/virtual-programs/images.folk b/virtual-programs/images.folk index 84c74faa..42adeb10 100644 --- a/virtual-programs/images.folk +++ b/virtual-programs/images.folk @@ -22,14 +22,10 @@ namespace eval ::image { namespace ensemble create } -When /someone/ wishes /p/ has camera image { - When the camera frame is /f/ { - When $p has region /r/ { - lassign [regionToBbox $r] minX minY maxX maxY - set subimage [image subimage $f \ - $minX $minY \ - [expr {int($maxX - $minX)}] [expr {int($maxY - $minY)}]] - Claim $p has camera image $subimage - } - } +When /someone/ wishes /p/ has camera image & the camera frame is /f/ & /p/ has region /r/ { + lassign [regionToBbox $r] minX minY maxX maxY + set subimage [image subimage $f \ + $minX $minY \ + [expr {int($maxX - $minX)}] [expr {int($maxY - $minY)}]] + Claim $p has camera image $subimage } diff --git a/virtual-programs/intersect.folk b/virtual-programs/intersect.folk index ef0f4c79..351a293c 100644 --- a/virtual-programs/intersect.folk +++ b/virtual-programs/intersect.folk @@ -8,27 +8,23 @@ When $this has neighbor /n/ { #Wish $this has neighbors -When /someone/ wishes /p/ has neighbors { - When $p has region /r/ { - When /p2/ has region /r2/ { - if {$p eq $p2} { return } - lassign [regionToBbox $r] bMinX bMinY bMaxX bMaxY - lassign [regionToBbox $r2] b2MinX b2MinY b2MaxX b2MaxY - - set hasIntersections [rectanglesOverlap [list $bMinX $bMinY] \ - [list $bMaxX $bMaxY]\ - [list $b2MinX $b2MinY]\ - [list $b2MaxX $b2MaxY]\ - false ] - #Display::stroke [list [list $bMinX $bMinY] {500 500}] 3 blue - #Display::stroke [list [list $bMaxX $bMaxY] {500 500}] 3 red +When /someone/ wishes /p/ has neighbors & /p/ has region /r/ & /p2/ has region /r2/ { + if {$p eq $p2} { return } + lassign [regionToBbox $r] bMinX bMinY bMaxX bMaxY + lassign [regionToBbox $r2] b2MinX b2MinY b2MaxX b2MaxY + + set hasIntersections [rectanglesOverlap [list $bMinX $bMinY] \ + [list $bMaxX $bMaxY]\ + [list $b2MinX $b2MinY]\ + [list $b2MaxX $b2MaxY]\ + false ] + #Display::stroke [list [list $bMinX $bMinY] {500 500}] 3 blue + #Display::stroke [list [list $bMaxX $bMaxY] {500 500}] 3 red - if {$hasIntersections} { - Claim $p has neighbor $p2 - #Display::stroke [list [list $b2MinX $b2MinY] {500 500}] 3 red - #Display::stroke [list [list $b2MaxX $b2MaxY] {500 500}] 3 white - #Display::stroke [list [list $b2MinX $b2MinY] [list $b2MaxX $b2MaxY]] 10 blue - } - } + if {$hasIntersections} { + Claim $p has neighbor $p2 + #Display::stroke [list [list $b2MinX $b2MinY] {500 500}] 3 red + #Display::stroke [list [list $b2MaxX $b2MaxY] {500 500}] 3 white + #Display::stroke [list [list $b2MinX $b2MinY] [list $b2MaxX $b2MaxY]] 10 blue } } \ No newline at end of file diff --git a/virtual-programs/label.folk b/virtual-programs/label.folk index 036e8752..442bbdc0 100644 --- a/virtual-programs/label.folk +++ b/virtual-programs/label.folk @@ -28,12 +28,10 @@ proc text {coords text angle} { Display::text Display::fb [lindex $coords 0] [lindex $coords 1] _ $text $angle } -When /anyone/ wishes /p/ has halo message /message/ { - When $p has region /r/ { - lassign [lindex $r 0] a b c d - lassign $a x y - text $a $message 0 - text $d $message 180 - Wish $this is labelled TEST - } +When /anyone/ wishes /p/ has halo message /message/ & /p/ has region /r/ { + lassign [lindex $r 0] a b c d + lassign $a x y + text $a $message 0 + text $d $message 180 + Wish $this is labelled TEST } \ No newline at end of file diff --git a/virtual-programs/outline.folk b/virtual-programs/outline.folk index f996e6e0..4b9d31ac 100644 --- a/virtual-programs/outline.folk +++ b/virtual-programs/outline.folk @@ -25,10 +25,8 @@ When the collected matches for [list /someone/ wishes /thing/ is outlined /color } } -When /someone/ wishes /thing/ is outlined thick /color/ { - When $thing has region /region/ { +When /someone/ wishes /thing/ is outlined thick /color/ & /thing/ has region /region/ { # FIXME: assumes path lassign $region vertices edges loopRegion $edges $vertices 6 $color - } } \ No newline at end of file diff --git a/virtual-programs/process.folk b/virtual-programs/process.folk index a0956a13..520d104b 100644 --- a/virtual-programs/process.folk +++ b/virtual-programs/process.folk @@ -1,6 +1,4 @@ Wish $this has filename "process.folk" -When /p/ is running process /process/ { - When process $process has standard output log /log/ { - Wish $p is labelled [join $log "\n"] - } +When /p/ is running process /process/ & process /process/ has standard output log /log/ { + Wish $p is labelled [join $log "\n"] } diff --git a/virtual-programs/regions.folk b/virtual-programs/regions.folk index ab35ca28..e21bd96b 100644 --- a/virtual-programs/regions.folk +++ b/virtual-programs/regions.folk @@ -58,8 +58,6 @@ namespace eval ::region { namespace ensemble create } -When when the distance between /p1/ and /p2/ is /distanceVar/ /body/ with environment /e/ { - When $p1 has region /r1/ & $p2 has region /r2/ { - Claim the distance between $p1 and $p2 is [region distance $r1 $r2] - } +When the distance between /p1/ and /p2/ is /distanceVar/ /body/ with environment /e/ & /p1/ has region /r1/ & /p2/ has region /r2/ { + Claim the distance between $p1 and $p2 is [region distance $r1 $r2] } -- cgit v1.2.3