Skip to content

Commit

Permalink
Greatly expand the smalltalk conditional example
Browse files Browse the repository at this point in the history
  • Loading branch information
acook committed Mar 10, 2024
1 parent ad3a5a7 commit 1c35675
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions examples/advanced/smalltalk_style_conditionals.bl
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
;; Smalltalk-style Conditionals

o-new ;; o1 will house our truthy and falsey objects

o-new ;; o2 (truthy object)
[ swap call ] ;; evaluate the wv
if-true: ;; store function in slot
[ swap drop ] ;; do nothing except get rid of the wv
if-false: ;; store function in slot
true: ;; store o2 in slot in o1

o-new ;; o3 (falsy object)
[ swap drop ] ;; do nothing except get rid of the wv
if-true: ;; store function in slot
[ swap call ] ;; evaluate the wv
if-false: ;; store function in slot
false: ;; store o3 in slot in o1

:false ;; summon reference to falsey o3
[ 'I am so false.' say ] ;; a Block to demonstrate the condition
swap :if-false ;; call if-false on o3, will display "I am so false."
;;;;;;;;;;;;;
;; setup ;;
;;;;;;;;;;;;;

o-new ;; o1 will house our truthy and falsey objects

o-new ;; o2 (truthy object)
[ swap call ] ;; evaluate the wv
if: ;; store function in slot
[ swap drop ] ;; do nothing except get rid of the wv
unless: ;; store function in slot
[ swap drop swap call ]
either: ;; if/else which runs only the 'then' block
[ :false ]
not: ;; negation
[ swap drop self :false ]
and: ;; logical and
[ self ]
or: ;; logical or
true: ;; store o2 in slot in o1

o-new ;; o3 (falsy object)
[ swap drop ] ;; do nothing except get rid of the wv
if: ;; store function in slot
[ swap call ] ;; evaluate the wv
unless: ;; store function in slot
[ rot drop swap call ]
either: ;; if/else which runs only the 'else' block
[ :true ]
not: ;; negation
[ swap drop self ]
and:
[ [ self ] [ :true ] rot :either ]
or: ;; logical or
false: ;; store o3 in slot in o1

;; store references to false in true and true in false so they can get each other easily
:true swap :false rot swap false:
:false swap true: drop

;;;;;;;;;;;;;;;;;;;;;
;; usage example ;;
;;;;;;;;;;;;;;;;;;;;;

:false ;; summon reference to falsey o3
:not ;; negate it
swap :true ;; summon reference to truth 02
swap :and ;; logical and the two values on the stack
rot :false ;; summon reference to o3
swap :or ;; logical or the two

[ 'So true bestie.' say ] ;; 'then' block
[ 'I am so false.' say ] ;; 'else' block
rot :either ;; call either

0 comments on commit 1c35675

Please sign in to comment.