Skip to content

johnsonjh/pc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

296 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pc: programmers calculator

pc is a substantially enhanced fork of the SGI/Be/Haiku programmers calculator.

Syntax and operators

  • Automatic input and output base conversions::
    • Binary (base 2): 0b111101001101001
    • Octal (base 8): 0o75151 (or 0075151)
    • Decimal (base 10): 31337
    • Hex (base 16): 0x7a69
    • Roman: 0rMCCCXXXVII
    • ASCII: 'zi'
  • Optional support for:
    • Ternary (base 3): 0t1120222122
    • Base 36: 0zO6H

  • Operators:
    • ++, --, ~, !, *, /, %, +, -, <<, >>, <, >, <=, >=, ==, !=, &, ^, |, &&, ||

  • Precedence (highest to lowest):
    • ++, --, ~, !
    • *, /, %
    • +, -
    • <<, >>
    • <, >, <=, >=
    • ==, !=
    • &
    • ^
    • |
    • &&
    • ||

  • Assignment operators:
    • =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=

  • Negation:
    • ! is logical, not bitwise (use ~ for bitwise negation)

  • Parentheses: Full support for grouping and nesting.

  • Explicit modes:
    • Three calculation modes are available, via named commands:
      • auto, signed, and unsigned. The mode command can be used to query the current calculation mode.
      • All operations except comparisons are performed using unsigned integer calculations in the default auto mode.
    • Braces (e.g., { … }) force signed calculation (overriding global mode).
    • Brackets (e.g., [ … ]) force unsigned calculation (overriding global mode).

User variables, builtins, and registers

  • Variables:
    • All C-style names (starting with alpha or _, followed by any alphanumeric or _) can be used.
    • User variables are created on first use (and automatically initialized to zero), and can be listed with vars.
    • Examples:
      • x = 5
      • x = y = 10
      • x = (y++ + 5) * 2
      • (y * 2) + (x & 0xffeef)
      • . << 2
      • z = .

  • Builtins:
    • Builtins provide access to about 50 system constants and runtime values, which can be listed with the help command.

  • Registers:
    • Perform time formatting or casting by storing to special purpose registers (i.e., unsigned C types char, short, int, long, long long), list via the regs command).
      • Stores to registers clamp and mask to the register width.

Portability

  • Easily portable to any POSIX-ish system with a C99 compiler (or Ch 7.0+), less easily to others.
  • Currently developed on IBM AIX, and regularly tested on PASE for IBM i (OS/400), macOS, FreeBSD, NetBSD, OpenBSD, Solaris, illumos, Haiku, Windows (MSVC, MinGW-w64), Atari ST (TOS and MINT, built using CrossMINT), Amiga 68K (built using Amiga-GCC), Linux, ELKS (built using IA16-GCC), Mac OS 68K (built using Retro68), and DOS (built using DJGPP, IA16-GCC, and Open Watcom V2).

Build

  • You can try out pc by executing ./pc.c directly, and it should just run (using shell magic), or,
  • Build it with make. Standard environment variables (e.g., CC, CFLAGS, LDFLAGS) are respected.
  • Build with Microsoft Visual C/C++ using: cl pc.c /O2 /W4
  • Common line editing packages (libedit, editline, readline, and linenoise) are supported and usually automatically configured (via pkg-config magic).
  • Review the Makefile to see available build options.

Usage

  • Supports interactive, command-line (pc 1+1), file (take command), and piped/redirected usage.

  • Interactive examples:
    • 0xFF & 0x0F
    • 1 << 12
    • a = 0x1234; a & 0xFF
    • . + 4 (add to previous result)
    • take examples/sqrt.pc (reads from examples/sqrt.pc)

  • easter.pc is a program that calculates the date of Easter (and checks if it's today).
    • You can modify the program or use something like faketime (i.e., faketime -m '2025-04-20 12:00:00') if you want to test arbitrary dates. You can see the full output of this program here (with all optional base conversions enabled).

Testing

License

  • pc is distributed under the terms of the MIT License.
  • The repository is compliant with version 3.3 of the REUSE specification.

About

pc: programmers calculator

Topics

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •