Just for fun

Greetings!

Well, I had my fun for the week, and I thought someone else out there might find it just as entertaining as I did ;). It turns out that while perhaps not turing-complete, the dc calculator that comes with most Linux distributions today actually has enough commands to do something besides addition! Totally useless for anything serious (lacking any form of file i/o is a big drawback, as is the lack of real loops -- recursion eventually kills the stack), and I don't see DC becoming the next big-shot scripting language. But certainly a fun toy!

So far (about a half hour) I've worked out a loop construct (similar to a BASIC style FOR loop) and a guess-the-number game (yeah, it actually plays, But it's not exactly amazing).

Any thoughts/comments/ideas?

[code]
#loop construct
#Pop the top two values off the stack, using the top value
#as the number of iterations, and the lower value as the
#macro to execute.
#NOTE: Stacks a macro onto the . register!

[3;Lx 1;Lx]0:L #runs macro
[2;L0!=. q]1:L #completeness test

[ 1-2:L #iterations
3:L #macro
[2;L1-2:L 0;Lx]S. #self-executing code
0;Lx #start it up
L.s` #clean up
]sL

#EXAMPLES

#Print "Hello, World!" 10 times, don't modify the stack
#[[Hello, World!]n[]pn] 10 lLx

#find 10 factorial
#top of stack will be 0, as it's used as a counter
#10! = 55, the second item on the stack
#0 10 [S.l.+L.1-] 10 lLx
[/code]

[code]
#Guess-the-Number Game -- Save into a file, and run with dc -f
#The computer will guess your number in 20
#guesses or less.

#register set: #l low value #h high value
#g guess #i input #0,1,2 dispatch
#` garbage disposal #d depth counter


#dispatches {called from main loop}
#0, less-than
[ lgsl [testing]n
]s0

#1, equal-to
[ [You're Number Is: ]nlgps`
ld1+Q
]s1

#2, greater-than
[ lgsh
]s2




#main loop
[ ld1+sd #increment depth
lllhll-2/+[Guess:]npsg #make guess
[<(0), =(1), or >(2)?]ps`?si #get answer
li0=0 li1=1 li2=2 #dispatch
lwx #loop
ld1-sd #decrement depth
]sw



#initialize. Use integer math only.
[0sd 0k 1sl 2 20^sh]x

#execute the macro and quit
[Pick a Number Between ]nlln[ and ]nlhn[.]ps`
lwx q
[/code]
Sincerely,
Jakykong (Jack Mudge)
jack_mudge@hotmail.com
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories