-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Greatly expand the smalltalk conditional example
- Loading branch information
Showing
1 changed file
with
54 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |