Comma quibbling: Difference between revisions

Content added Content deleted
(→‎{{header|dc}}: Add implementation.)
m (→‎{{header|dc}}: Better register names and comments.)
Line 1,459: Line 1,459:


=={{header|dc}}==
=={{header|dc}}==
<syntaxhighlight lang="dc">[main entry point: print brackets, call o if stack not empty]sx
<syntaxhighlight lang="dc">[(q)uibble: main entry point. print brackets, calling n in between if stack not
empty.]sx
[ [{]n z 0 !=o [}]pR ]sq
[ [{]n z 0 !=n [}]pR ]sq


[nonempty stack: if more than 1 item, call t. then print top of stack.]sx
[(n)onempty: if more than 1 item, call m. then print top of stack.]sx
[ z 1 !=t n ]so
[ z 1 !=m n ]sn


[more than 1 item. reverse (flip) stack into r register, call p to print
[(m)ore: call f to flip stack into r register, call p to print most of it,
most of it, then pop the last item back from r so it's there to be printed
then pop the last item back onto the main stack so it's there to be printed
after we return]sx
after we return]sx
[ lfx lpx Lr ]st
[ lfx lpx Lr ]sm


[utility routine: reverse (flip) the stack into the r register]sx
[(f)lip: utility routine to reverse the stack into the r register]sx
[ Sr z 0 !=f ]sf
[ Sr z 0 !=f ]sf


[main print routine. get next item from stack in r register,
[(p)rint: get next item from stack in r register and print it. If there are
print it. If there are more than 2 items left, print a comma (c). Otherwise,
more than 2 items left, print a comma (c) and recurse. If there are exactly
if there are exactly two items left, print " and " (a). Finally, if there
two items left, print " and " (a) and return.]sx
are more than 2 items left, recurse.]sx
[ Lr n 2 yr >c 2 yr =a 2 yr >p]sp
[ Lr n 2 yr >c 2 yr =a 2 yr >p]sp


[utility routine: print a comma followed by a space]sx
[(c)omma: utility routine to print a comma followed by a space]sx
[ [, ]n ]sc
[ [, ]n ]sc


[utility routine: print a " and "]sx
[(a)and: utility routine to print " and "]sx
[ [ and ]n ]sa
[ [ and ]n ]sa


[run tests]sx
[run tests]sx
lqx
lqx
[ABC] lqx
[ABC] lqx
[ABC] [DEF] lqx
[ABC] [DEF] lqx
[ABC] [DEF] [G] [H] lqx
[ABC] [DEF] [G] [H] lqx</syntaxhighlight>
</syntaxhighlight>


{{out}}
{{out}}