The program is supposed to echo stdin to stdout if the program name
starts with 'e'. This was only possible if one do something like:
PATH=$PATH:.
echo '5*5+17' | eprog
because otherwise the first char would be '.' not 'e'.
This was done by using basename(3) and -include libgen.h (in the
Makefile so as to not modify the code more than is necessary).
The Makefile now copies 'prog' to 'eprog' and 'prog' to 'calc'. It also
copies 'prog.alt' to 'eprog.alt' and 'ecalc.alt' as this is what the
program is documented as.
Format fix the calc.1 man page and remove calc.man. This is so that the
formatting is correct and also so as to not have to maintain more than
one copy of the same text (which was almost certainly already out of
sync as I did not update calc.man when I added calc.1 exactly for the
reason that I deleted calc.man).
The try.sh script has been updated and so has the try.this.txt file that
try.sh uses.
This should complete the review of 2015/burton if I'm not very much
mistaken.
136 lines
2.5 KiB
Groff
136 lines
2.5 KiB
Groff
.TH calc 1 "25 March 2023" "calc" "IOCCC 2015"
|
|
.SH NAME
|
|
calc \- an integer expression calculator that outputs in both hex and decimal
|
|
.SH SYNOPSIS
|
|
\fBcalc\fP \fI[expr]\fP
|
|
.SH DESCRIPTION
|
|
.BR calc
|
|
presents a simple signed integer expression calculator.
|
|
The result is printed on
|
|
.BR stdout
|
|
as both a decimal value and its hex equivalent.
|
|
Results are presented as 32-bit values, displaying as 64-bits only if necessary.
|
|
If the program name begins with
|
|
.B e
|
|
the input is also copied to
|
|
.BR stdout .
|
|
.BR stdin
|
|
is read if there are no arguments.
|
|
.PP
|
|
Constants may be decimal, octal or hex.
|
|
There are 26 registers, initialized to zero, named
|
|
.B A-Z\c
|
|
; the most recent result is always remembered in the register
|
|
.BR .
|
|
(dot).
|
|
Assignments are silent; the character
|
|
.I $
|
|
will print a table of the non-zero registers.
|
|
.PP
|
|
All input is case-insensitive.
|
|
Semicolons and/or newlines separate statements.
|
|
Whitespace is ignored, except newlines cannot occur within expressions.
|
|
Comments are introduced with
|
|
.I #
|
|
and extend through the end of the line.
|
|
.PP
|
|
Operator precedence is as found in Go:
|
|
OR and XOR are treated as additive, SHIFTing and AND are multiplicative.
|
|
This allows a more natural expression without parentheses,
|
|
as in
|
|
.BR a&0xff\ |\ b<<8 \c
|
|
\&.
|
|
.PP
|
|
Supported operations in decreasing precedence:
|
|
.TP
|
|
.B ()
|
|
forced precedence
|
|
.TQ
|
|
.B \- + ! ~ ?
|
|
unary minus/plus, not, compl, byte-swap
|
|
.TQ
|
|
.B * / % & << >> >>>
|
|
mul, div, mod, bitand, left shift, right shift, unsigned right shift
|
|
.TQ
|
|
.B + \- | ^
|
|
add, sub, bitor, xor
|
|
.TQ
|
|
.B =
|
|
assignment (\c
|
|
.I [a-z] = expr\c
|
|
)
|
|
.SH EXAMPLES
|
|
.PP
|
|
Display the decimal value of
|
|
.BR 0x42 :
|
|
.sp
|
|
.RS
|
|
.ft B
|
|
calc 0x42
|
|
.br
|
|
.ft R
|
|
66 0x00000042
|
|
.RE
|
|
.PP
|
|
What is the mask value for 22 bits?
|
|
.sp
|
|
.RS
|
|
.ft B
|
|
calc '1<<22-1'
|
|
.br
|
|
.ft R
|
|
4194303 0x003fffff
|
|
.RE
|
|
.PP
|
|
What is the integer value of byte-swapped 0x11223344?
|
|
.RS
|
|
.ft B
|
|
calc ?0x11223344
|
|
.br
|
|
.ft R
|
|
1144201745 0x44332211
|
|
.RE
|
|
.PP
|
|
Some canned expressions:
|
|
.RS
|
|
.ft B
|
|
cat <<! | ecalc
|
|
.br
|
|
# multiple assignments
|
|
.br
|
|
a=32;n=10;c=50;m=100;$
|
|
.br
|
|
7+7/7+7*7-7 # pedmas
|
|
.br
|
|
m|a-a*3 # convenient precedence
|
|
.br
|
|
?(1<<14|1<<9)-1<<4-1<<3
|
|
.br
|
|
57005<<16+48879
|
|
.br
|
|
-1 >> 32 # signed shift
|
|
.br
|
|
-1 >>> 32 # unsigned shift
|
|
.br
|
|
!
|
|
.ft R
|
|
.RE
|
|
.SH BUGS
|
|
An assignment with certain types of syntax error will replace the registers with a partial evaluation:
|
|
.sp
|
|
.RS
|
|
.ft B
|
|
calc 'a = 1; a; a = 3 4; a'
|
|
.br
|
|
1 0x00000001
|
|
.br
|
|
syntax error
|
|
.br
|
|
3 0x00000003
|
|
.ft R
|
|
.RE
|
|
.PP
|
|
Command line expressions longer than 64 characters will give a syntax error.
|
|
.SH SEE ALSO
|
|
\<https://golang.org/\>
|