dc

Created: 11 Jan 2025
Updated: 17 Jan 2025
manual https://wolfram.schneider.org/bsd/7thEdManVol2/dc/dc.html
manual https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html
source /usr/src/cmd/dc/dc.c
wiki https://en.wikipedia.org/wiki/Dc_(computer_program)
wiki https://es.wikipedia.org/wiki/DC_(Unix)

cli

dc [OPTIONS] [FILES]
-f FILE executes script file
–file=FILE "
-e SCRIPT runs commands in script on input
–expression=SCRIPT "
DC_LINE_LENGTH=0   # disable line break
DC_LINE_LENGTH=999 # < 2^16 -1

language

$ dc script.dc
$ dc -e '[Enter time: ]P?st' -e '[Distance: ]Plt2^0.5*9.8*p'
$ echo '2 3 +' | dc
$ echo '2 3'   | dc -f - -e 'f'
$ echo '2 3'   | dc -e '? ? +'
  • types
    • numbers
      • hex numbers must be upcased (A-F)
      • use _ for negative numbers
    • strings
      • can only be printed or execute them as macros
      • delimited by []
    • arrays
  • registers (256)
    • valid names: a A 0 . " …
      • one char length
      • you CAN use a register name that overlaps with a command
      • and it will stil work
    • each register has his own stack

commands

Table 1: T=take P=put
T   P  
  f   print full stack
  p   print top of stack, new line
1 n   print top of stack, no \n
1 P 1 print top of stack, no \n, as char if number
2 + 1  
2 - 1 fst - snd
2 * 1  
2 / 1 fst / snd
2 % 1 remainder of /
2 ~ 2 quotient and remainder of /
2 ^ 1 fst ^ snd
3 ¦ 1 mod(fst, snd ^ trd)
1 v 1 square root
* c   clear stack
1 d   dup top of stack
  r   swap top 2 values
  R   rotates the top Nth items
1 sR   stores in register R
1 SR   stores in register R, nuke prev content on R
  lR 1 load from register R, 0 if empty
  LR 1 " and pops it from register R
1 i   set input radix
1 o   set output radix
1 k   set decimal precision
  I 1 push input radix
  O 1 push output radix
  K 1 push decimal precision
1 x   executes as a macro
1 a 1 pop top, and push back 1st char/byte
2 >R   x R if >
2 !>R   x R if <=
2 <R   x R if <
2 !<R   x R if >=
2 =R   x R if =
2 !=R   x R if !=
  ? 1 ask user input, and execute it as a macro
  q   exits 2 level macro
1 Q   exits Nth level macro
1 Z   number of decimal digits
1 X   number of fraction digits
  z 1 number of element in stack
  !   system command
  #   comment
2 :R   store fst in R array, at idx snd
1 ;r   get given array element in register

snippets

sergiosgc/AdventOfCode2021

  • day1 part 1

    [1Lc+sc]sC
    [rdSar>CLaz1<L]sL
    0sc0d=LLcp
    
  • day1 part 2

    [lCsDlBsClAsBsA]sr
    [1lR+sR]si
    [lBlClD++lAlBlC++<i0d=rz0!=l]sl
    0sA0sB0sC0sD0sR
    0d=r0d=r0d=r0d=r
    0d=l0dd=llRp
    

tckmn/polyaoc-2019

articles

videos

implementations