szg The Calculator

March 13th, 2013


Get szg now!

Get szg now!


szg is the Lazy Man’s command line calculator, crunching the maximum number of numbers with the minimum number of keystrokes.

szg stands for SZámolóGép, which is Hungarian for calculator.
It also happens to be the initials of the author, a particularly Lazy Man.

If you you’re lazy too;

If you like binary, octal and hex formats;

If you hate typing too much;

If you hate mouse-clicking even more;

If you hate calculators going floating-point after the first division;

If you still need floating-point occasionally;

If you typo a lot and prefer to undo them all;

If you like to comment your calculations (well, you don’t, but it’s possible):

Accept no substitute! Use szg.

Features

  • 32-bit integer or floating-point arithmetic
  • decimal, binary, octal or hexadecimal format (commands D, B, O, X)
  • input of expressions, variable assignments and commands on stdin
  • input of expressions directly from the command line
  • output of calculations on stdout
  • error messages (syntax error, division by zero) or stderr
  • unlimited undo
  • user defined variables like $foo
  • operators ()~ ^ */%& +-| =
  • symbol _ means last result, like Perl/Python
  • missing identifier means _ (+5 means last result + 5) like Perl (sorry Python)
  • math functions @s @c @a @l @e @r = sin cos atan log exp sqrt
  • optional unsigned arithmetic (commands N, S)
  • floating-point not upon division, only by user request, like Python (but not Python3)
  • combination if expressions/commands on same line
  • comments starting with #
  • interpreter mode when input file specified
  • manual page

Installation

Debian Packages

The easiest way to install szg is using apt-get on Debian-based systems. Add my Debian repo to /etc/apt/sources.list:

deb http://linux.subogero.com/deb/ /
deb-src http://linux.subogero.com/deb/ /

And then

# apt-get update
# apt-get install szg

Source Tarball

From http://linux.subogero.com/deb/ get szg_X.Y.orig.tar.gz.
szg can be compiled and installed on Linux or Windows/Cygwin. On Windows, it runs both in Cygwin and Windows command line windows. Download and extract the newest one, then

$ cd szg-X.Y
$ ./configure
$ make
# make install # as root

Windows

From http://linux.subogero.com/deb/ get szg_X.Y.zip.
Download and extract the newest one, then copy szg.exe to C:\WINDOWS or another folder which is on PATH.

Git for the Brave

$ git clone http://subogero.dyndns.org/git/szg.git || \
  git clone https://github.com/subogero/szg.git
$ cd szg
$ ./configure
$ make
$ make install # as root

Examples

Desktop calculator workflow with a twist

d 5000    # one steak
5000
d +3*2000 # three whiskies neat
11000
d +1000   # tip
12000
d 20000-  # Let's see how much money I've got left from my 20000 note
8000
d

Command/statement combination for hex/dec conversion, signed/unsigned modes. Watch the prompt!

d XffffD       # change to hex, enter FFFF, change back to dec
65535
d X ffffffff D # change to hex, enter FFFFFFFF, change back to dec
-1
d N            # unsigned (natural number) mode
4294967295
D 240X         # stay in dec, enter 240, change to hex
f0
X D239X        # change to dec, enter 239, change to hex
ef
X

Undo, difference between subtraction and unary minus.

84
d ~42        # Let's subtract 42 ...
-42
d U-42       # Oops, I hit tilde instead of minus
42

szg works as an interpreter as well, an input file can be specified on the command line. This also allows to create executable szg interpreter scripts. For instance let’s create an executable file called LifeUniverseAndEverything:

#!/usr/bin/szg
33*2 X

szg supports floats and binary formats. You can even peek into the insides of floats.

D 1+2
3
d /2    # no implicit float-conversion upon division
1
d /2.0  # Pythonic float-conversion if float-format entered
0.5
f B     # let's peek inside
00111111 00000000 00000000 00000000
f

You can specify an expression on the command line, to maximize laziness:

$ szg -e XffD
255

Background

This is the first remake of my first shot at a unix-style program. In the first shot you had to enter too many capital letters for hex numbers.
It’s a filter, as in unix nearly everything should be. I tried to follow the Sacred Rules of the Art from esr’s book:

Command line option conventions

-h –help for help
-V –version for version info

Data driven development

The source of the usage and version screens are text files which look exactly like those screens. During make, they are sedded into header files with lists of string initializers. The headers are then included in the middle of const table definitions, to make the text available as a list of strings, line by line.

Undo is possible thanks to a stack data-structure and the user-defined variables’ heart and soul is a hash-table, with buckets and all that.

Write programs to write programs

I used Lex and Yacc to parse the input and to write the syntax. I can still hardly believe what these nearly 40 year old tools can do.

  1. Scott
    August 4th, 2012 at 04:28 | #1

    On Win 7 64-bit:

    Hangs: szg -e 3600*24
    AppCrash: szg -e XffD

    Running Cygwin on the same machine:
    $ ./szg -e XffD
    Segmentation fault

  2. August 4th, 2012 at 10:23 | #2

    I’ve never tried szg on a 64-bit machine, as I don’t have any.
    But I’ll try to get hold of one and fix it.

    Is it just the -e option that crashes, or the interactive mode as well?

  3. August 5th, 2012 at 10:21 | #3

    Now I’ve compiled szg for both 64bit Linux and 64bit Windows 7.
    Spotted 2 bugs:
    - the Win32 executable from ‘make win’ does not run correctly in Win64
    - the internal string buffer for ‘-e XXXX’ was not initialized

    The latter strangely did not cause any problems on 32bit platforms.

    There will be a new target ‘make w64′ to produce a proper Win64 executable.
    Update hopefully until tonight.
    And I owe you a beer. :-)

  4. August 7th, 2012 at 00:42 | #4

    Done.

    make w64
    

    After that .w64/szg.exe is the native Windows x64 executable.
    You need to install Cygwin’s x64 Mingw cross-compiler beforehand.
    The Cygwin executable is also fixed: .bin/szg

  5. Scott
    August 10th, 2012 at 21:07 | #5

    OK, I’m not a real programmer, so it took a bit to figure out what Mingw stuff to install. NOTE: You also need flex and bison.

    Anyway, got everything installed, ran make w64, got a error:

    patterns.l: In function ‘yylex’:
    patterns.l:25:25: warning: assignment makes pointer from integer without a cast

    No biggee, .w64 directory contained szg.exe.

    This hangs until ^C
    c:\pktemp\szg-0.5\.w64>szg -e 3600*24
    ^C

    But AHA, this works:
    c:\pktemp\szg-0.5\.w64>szg -e 3600 * 24
    86400

    Could that have been the original problem all along?
    Gotta add some code to require (or put) spaces around the operator?

    “szg -e XffD” – also works.

    But, in a Cygwin Bash shell…
    ./szg -e 3600 * 24 – Hangs until ^C
    ./szg -e 3600*24 – Same

    But this works:
    $ ./szg -e XffD
    255

    A little weird…

    Anyway, interactively it work in both Windows and Bash.

    BTW, “make install” does not install.

    c:\pktemp\szg-0.5>make install
    cp: cannot stat `.bin/szg.exe’: No such file or directory
    makefile:114: recipe for target `install’ failed
    make: *** [install] Error 1

  6. Scott
    August 10th, 2012 at 21:42 | #6

    Correct about the Cygwin Bash attempts. I was using the Win64 binary. So I went into Bash, ran make (no options), cd to .bin, ran szg.exe

    $ ./szg.exe -e 4 * 3
    syntax error
    unable to parse number
    unable to parse number
    unable to parse number
    3

    ./szg -e XffD – Works
    Interactive is also fine.

    I made one of my common mistakes and hit Ctrl-Z to exit szg. Which put szg into the background. So I ran fg to get it back into the foreground so I could quit properly, and got this:

    $ fg
    ./szg
    input in flex scanner failed

    OK, that was a stupid user error, so feel free to ignore it if you wish.

  7. subogero
    August 11th, 2012 at 01:08 | #7

    A short excerpt from the szg manual page:

    When using the szg -e expr form, be careful with shell expansion
    taking place before  passing  expr  to  szg. Quote when necessary.
    
    Examples with Bourne compatible shells:
    
    szg -e 6 * 7 will fail spectacularly, as your current dir listing
    will be substituted for *.
    
    szg -e 6*7 may work, except if you have a file called 617 in your
    current dir, resulting in 617 instead of 42.
    

    In other words, always quote when you multiply:

    szg -e "12 * 43"
    

    As to installation, did you ‘make’ before ‘make install’?
    The normal make compiles the Cygwin target (.bin/szg.exe), which is copied to /usr/bin/ when you install.

  8. Scott
    August 11th, 2012 at 14:02 | #8

    Ah, that makes sense about quoting.

    I only did the “make install” after “make w64″ for the 64-bit version. There was no “.bin/szg.exe” at this time, only “.w64/szg.exe”

Comments are closed.