Metaprogramming: Difference between revisions

Added Quackery.
(Added Quackery.)
Line 1,074:
print func(5)
</lang>
 
=={{header|Quackery}}==
 
The various forms of metaprogramming available in Quackery are discussed in [https://github.com/GordonCharlton/Quackery The Book of Quackery]. Here, two types are illustrated.
 
1: Extending the Quackery compiler by adding a compiler directive to skip over inline comments indicated by a semicolon. (Quackery has block comments delimited by "(" and ")" but not "comment to end of line".)
 
2: Adding a new control-flow structure (a switch statement) by using meta-control-flow words. (The ones with ]reverse-nested[ names, indicating they convey properties to the nest that invoked them.)
 
<lang Quackery>
( +---------------------------------------------------+ )
( | add inline comments ";" to Quackery with "builds" | )
( +---------------------------------------------------+ )
 
[ dup $ "" = not while
behead carriage =
until ] builds ; ( [ $ --> [ $ )
 
 
; +---------------------------------------------------+
; | add switch to Quackery with ]else[ ]'[ & ]done[ |
; +---------------------------------------------------+
 
[ stack ] is switch.arg ( --> s )
 
[ switch.arg put ] is switch ( x --> )
 
[ switch.arg release ] is otherwise
 
[ switch.arg share
!= iff ]else[ done
otherwise
]'[ do ]done[ ] is case ( x --> )
 
 
[ switch
1 case [ say "The number 1." cr ]
$ "two" case [ say 'The string "two".' cr ]
otherwise [ say "Something else." cr ] ] is test
( x --> )
 
 
' tally test ; output should be: Something else.
$ "two" test ; output should be: The string "two".
1 test ; output should be: The number 1.
 
</lang>
 
{{Out}}
 
<pre>Something else.
The string "two".
The number 1.
</pre>
 
=={{header|Racket}}==
1,462

edits