Jump to content

Quoting constructs: Difference between revisions

Added Quackery.
(added Perl programming solution)
(Added Quackery.)
Line 653:
{{"John", "Smith"}, 52389, 97.25}
{} -- the 0-element sequence</syntaxhighlight>
 
=={{header|Quackery}}==
 
In Quackery, everything is code except when it is data. The objects it supports are operators ("primitives" or virtual op-codes), numbers (bigints) and nests (dynamic arrays that can contain operators, numbers, and nests, delimits by <code>[</code> and <code>]</code>). The behaviour of a number is to put its value on the data stack - in essence, numbers are self-quoting. However, in common with operators and nests, they can be explicitly quoted by preceding them with <code>'</code> (pronounced "quote"). The behaviour of <code>'</code> is to put the quackery object that follows it on the data stack rather than evaluate it, which is the default behaviour for all objects.
 
Any object can be given a name during compilation, using the word <code>is</code>, which is a "builder"; a word that is executed during compilation. The behaviour of <code>is</code> is to add the named object to the compiler's dictionary of named objects. So, for example, the number <code>12</code> could be declared as a constant called <code>dozen</code> with the Quackscript <code>12 is dozen</code>. However is it preferable to restrict the use of <code>is</code> to naming nests, (i.e. <code>[ 12 ] is dozen</code>) so that the Quackery decompiler <code>unbuild</code> can differentiate between the constant <code>dozen</code> and the literal <code>12</code>.
 
(Aside: The word <code>builds</code> is analogous to <code>is</code>, but creates new builders. The Quackery compiler is extensible, so new quoting constructs can be created as required.)
 
Other relevant builders include <code>hex</code>, which parses the whitespace delimited string following it as a hexadecimal number, <code>char</code>, which will embed a non-whitespace character (represented by a number), and <code>$</code>, which will embed a string (represented by a nest of characters).
 
The behaviour of <code>$</code> is to consider the first non-whitespace character following it as the string's delimiter, and treat everything following it as the string to be embedded until it encounters a second instance of the delimiter. Typically the delimiter for a string is <code>"</code>, but any non-whitespace character is acceptable, so the following are equivalent: <code>$ "Hello World!"</code>, <code>$ 'Hello World!'</code>, <code>$ ZHello World!Z</code>.
 
The builder <code>constant</code> evaluates the object that precedes it at runtime and embeds it as a literal during compilation, so <code>[ 10 18 ** ] constant</code> compiles as <code>1000000000000000000</code>.
 
Another relevant word is <code>table</code>, which is used to create look-up tables. As an example, the behaviour of the nest <code>[ table 2 3 5 7 11 13 17 19 23 29 ]</code> is to take a number, n, in the range <code>0</code> to <code>9</code> (inclusive) from the data stack and put the corresponding small prime number on the data stack. <code>table</code> is not limited to returning numbers, it will handle any Quackery object, so can be considered as a quoting construct.
 
=={{header|Raku}}==
1,462

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.