It's a stack calculator for the unwise. Public Domain.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

7 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. -- DC --
  2. desktop calculator implementation
  3. Copyright 2022, 2023 Emil Williams, see LICENSE
  4. GMP based DC. Uses readline. No limitations, either.
  5. -- Notes --
  6. Note that much of the dc(1) man page is applicable, and is the formal root of
  7. this project. It is partly abidded in the featureset, of course this program is
  8. not GNU dc or a POSIX implementation* but it closely aligns to the manual page
  9. as a primary source of how DC should generally operate.
  10. Diviations exist, and this should be either treated as a bug, or a feature
  11. depending on the case regarding it. If some feature incorrectly matches dc(1)
  12. and deliberate incompatibility is not stated then it is a bug. See section
  13. Incompats
  14. Warning : Numbers are purely base 10 and hexadecimal is not implemented in any
  15. way but I've Ensured there are no hinderences with A-F with my
  16. conflicting character set.
  17. * There is no POSIX definition of DC, currently. There is a POSIX defition of
  18. BC, however. A formal proposal of DC should likely be a reduced subset of
  19. GNU/DC, with all normal printing and stack operations, along with registers,
  20. strings, and params, with the miscellaneous features and requirements for
  21. handling the REPL, files and STDIN all in the same or limited way (such that
  22. this program would be complicit) for the CLI. Of course this would come with
  23. the specification of multiple precision.
  24. This section is just a brief airing and goalset of what the codebase can do.
  25. -- Printing --
  26. p - print top
  27. n - print pop no newline
  28. f - print all
  29. -- Arithmetic --
  30. n1 is the first popped value, and so on.
  31. +/-* such that n1 OP n2, pushes 1 result
  32. ^ n2 to the power of n1, and is not limited
  33. v n1's square root
  34. Note that |, % and ~ have been omitted due to being 'pointless'.
  35. They may be added later with mpf_t to mpz_t conversion.
  36. - Stack --
  37. c clear
  38. d duplicate
  39. r reverse n1 and n2
  40. z pushes top
  41. -- Regs --
  42. any character from \0 to \377 [256] is a valid register and check config for
  43. stack max r being any character as stated.
  44. sr sets a popped value from the primary stack onto r
  45. lr gets a value from r and push that onto the stack
  46. Sr pushes a popped value unto the registers stack
  47. Lr pops a value unto the main stack
  48. ;r pops two values from the the primary stack, such and pushes n2 at index n1
  49. :r pops a value and pushes that index unto the primary stack
  50. Note that a sr value is not destroyed by Sr. This is technically an
  51. incompatibility but I don't see a reason to maintain such abnormality.
  52. -- Params --
  53. Push / Pop
  54. I / i set input radix
  55. K / k set the precision (of everything, including registers)
  56. -- Misc. --
  57. q quit
  58. # a comment.
  59. -- Incompats --
  60. - FIXME on i and input radix
  61. A-F notation will refer to the numbers 10-15,
  62. however the Input Radix must be already set higher
  63. than these symbols as mpf_set_str does not respect these symbols normally.
  64. - FIXME on k and precision settings
  65. the expression: 128k2 3/ ran under this program and GNU DC preduce different outputs,
  66. GNU: .66666666666666666666666666666666666666666666666666666666666666666666\
  67. 666666666666666666666666666666666666666666666666666666666666
  68. CACT: 0.6666666666666666666666666666666666666667
  69. 128 refers to apparently a 128 digits of precision, rather than the traditional number of bits.
  70. And it rounds up...
  71. - DISCREPANCY we print a prefixing zero on fractions 1 < & > -1
  72. - FIXME not very meaningful o
  73. o has no effect on output.
  74. - FIXME readline does not take kindly to input
  75. demo:
  76. 20S
  77. L
  78. p
  79. # loads 20 into register 012's stack
  80. # loads that back into the main stack
  81. # prints top of stack
  82. does not have the same effect as when ran as ours, infact, the S call would
  83. preduce a clamping error at the CLI REGCHAR level.
  84. I believe this is an issue with how input is read, thus an issue with how readline filters it's returned string.
  85. - WARN registers without DC_COMPLY
  86. Are reduced to a smaller subset and only normal ascii characters are respected (' ' - '~')
  87. Only 95 registers out of a naturally presumed 256 registers as stated by DC's man page.
  88. - INCOMPAT readline respects DEL sometimes
  89. % echo '^?20' | ./dc
  90. % echo '^?20' | dc
  91. This is the equivalent of UB in your terminal emulator.
  92. MINOR INCOMPAT FIXME dc prints characters greater than 126 (~) in octal under the format PROGN ": %04o unimplemented\n"
  93. -- TODO --
  94. Key:
  95. - Partial work, nothing tangable
  96. ~ Partial completion
  97. ? Experimental stage completion
  98. * Completed with confidence
  99. No work or consideration has been done
  100. X Incompatible completed
  101. [*] dynamic stack allocation
  102. [*] A-F hexadecimal w/ compat
  103. [X] Params
  104. [*] :;
  105. [*] registers
  106. [-] digit counts
  107. [-] cyclical nrotate R
  108. [ ] strings/macros
  109. -- Findings --
  110. GNU dc has leaks but it has two orders of magnitude less total space allocated.