Comma quibbling: Difference between revisions
Content deleted Content added
→{{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 |
<syntaxhighlight lang="dc">[(q)uibble: main entry point. print brackets, calling n in between if stack not |
||
empty.]sx |
|||
[ [{]n z 0 != |
[ [{]n z 0 !=n [}]pR ]sq |
||
[ |
[(n)onempty: if more than 1 item, call m. then print top of stack.]sx |
||
[ z 1 != |
[ z 1 !=m n ]sn |
||
[ |
[(m)ore: call f to flip stack into r register, call p to print most of it, |
||
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 ] |
[ lfx lpx Lr ]sm |
||
[utility routine |
[(f)lip: utility routine to reverse the stack into the r register]sx |
||
[ Sr z 0 !=f ]sf |
[ Sr z 0 !=f ]sf |
||
[ |
[(p)rint: get next item from stack in r register and print it. If there are |
||
more than 2 items left, print a comma (c) and recurse. If there are exactly |
|||
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 |
[(c)omma: utility routine to print a comma followed by a space]sx |
||
[ [, ]n ]sc |
[ [, ]n ]sc |
||
[utility routine |
[(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}} |