Merge pull request #1674 from xexyl/burton15

Fix 'e' feature of 2015/burton + formatting
This commit is contained in:
Landon Curt Noll authored and GitHub committed 2023-12-10 14:19:26 -08:00
commit 5482b4829e
10 files changed
+107 -110

No files matched your search

+6
View File
@@ -1,6 +1,12 @@
# #
# sort with: sort -d -u # sort with: sort -d -u
*.dSYM *.dSYM
calc
calc.alt
ecalc
ecalc.alt
eprog
eprog.alt
indent indent
indent.c indent.c
indent.o indent.o
+14 -3
View File
@@ -63,7 +63,7 @@ CDEFINE=
# Include files that are needed to compile # Include files that are needed to compile
# #
CINCLUDE= CINCLUDE= -include libgen.h
# Optimization # Optimization
# #
@@ -114,10 +114,10 @@ PROG= prog
# #
OBJ= ${PROG}.o OBJ= ${PROG}.o
DATA= DATA=
TARGET= ${PROG} TARGET= ${PROG} eprog
# #
ALT_OBJ= ${PROG}.alt.o ALT_OBJ= ${PROG}.alt.o
ALT_TARGET= ${PROG}.alt ALT_TARGET= ${PROG}.alt eprog.alt
################# #################
@@ -134,6 +134,11 @@ all: data ${TARGET}
${PROG}: ${PROG}.c ${PROG}: ${PROG}.c
${CC} ${CFLAGS} $< -o $@ ${LDFLAGS} ${CC} ${CFLAGS} $< -o $@ ${LDFLAGS}
${CP} -f $@ calc
eprog: ${PROG}
${CP} -f ${PROG} $@
${CP} -f ${PROG} ecalc
# alternative executable # alternative executable
# #
@@ -143,6 +148,12 @@ alt: data ${ALT_TARGET}
${PROG}.alt: ${PROG}.alt.c ${PROG}.alt: ${PROG}.alt.c
${CC} ${CFLAGS} $< -o $@ ${LDFLAGS} ${CC} ${CFLAGS} $< -o $@ ${LDFLAGS}
eprog.alt: ${PROG}.alt
${CP} -f ${PROG}.alt $@
${CP} -f ${PROG}.alt ecalc.alt
# data files # data files
# #
data: ${DATA} data: ${DATA}
+1 -1
View File
@@ -223,7 +223,7 @@ If the program name begins with an `e`, it echoes `stdin` to `stdout`.
This allows for a convenient test suite: This allows for a convenient test suite:
```sh ```sh
ln -s prog eprog ln -sf prog eprog
PATH=. eprog < test.in | diff - test.out PATH=. eprog < test.in | diff - test.out
``` ```
+20 -15
View File
@@ -11,18 +11,20 @@ The result is printed on
as both a decimal value and its hex equivalent. as both a decimal value and its hex equivalent.
Results are presented as 32-bit values, displaying as 64-bits only if necessary. Results are presented as 32-bit values, displaying as 64-bits only if necessary.
If the program name begins with If the program name begins with
.I e .B e
the input is also copied to the input is also copied to
.BR stdout . .BR stdout .
.BR stdin .BR stdin
is read if there are no arguments. is read if there are no arguments.
.PP .PP
Constants may be decimal, octal or hex. Constants may be decimal, octal or hex.
There are 26 registers, initialized to zero, named A-Z; the most recent result is always remembered in the register There are 26 registers, initialized to zero, named
.IR . .B A-Z\c
; the most recent result is always remembered in the register
.BR .
(dot). (dot).
Assignments are silent; the character Assignments are silent; the character
.IR $ .I $
will print a table of the non-zero registers. will print a table of the non-zero registers.
.PP .PP
All input is case-insensitive. All input is case-insensitive.
@@ -30,7 +32,7 @@ Semicolons and/or newlines separate statements.
Whitespace is ignored, except newlines cannot occur within expressions. Whitespace is ignored, except newlines cannot occur within expressions.
Comments are introduced with Comments are introduced with
.I # .I #
and extend through the following newline. and extend through the end of the line.
.PP .PP
Operator precedence is as found in Go: Operator precedence is as found in Go:
OR and XOR are treated as additive, SHIFTing and AND are multiplicative. OR and XOR are treated as additive, SHIFTing and AND are multiplicative.
@@ -45,45 +47,48 @@ Supported operations in decreasing precedence:
forced precedence forced precedence
.TQ .TQ
.B \- + ! ~ ? .B \- + ! ~ ?
unary plus/minus, not, compl, byteswap unary minus/plus, not, compl, byte-swap
.TQ .TQ
.B * / % & << >> >>> .B * / % & << >> >>>
mul, div, mod, bitand, shift: left, right, unsigned right mul, div, mod, bitand, left shift, right shift, unsigned right shift
.TQ .TQ
.B + \- | ^ .B + \- | ^
add, sub, bitor, xor add, sub, bitor, xor
.TQ .TQ
.B = .B =
assignment ([a-z] = expr) assignment (\c
.I [a-z] = expr\c
)
.SH EXAMPLES .SH EXAMPLES
.PP .PP
Display the decimal value of 0x42: Display the decimal value of
.BR 0x42 :
.sp .sp
.RS .RS
.ft B .ft B
$ calc 0x42 calc 0x42
.br .br
66 0x00000042
.ft R .ft R
66 0x00000042
.RE .RE
.PP .PP
What is the mask value for 22 bits? What is the mask value for 22 bits?
.sp .sp
.RS .RS
.ft B .ft B
$ calc '1<<22-1' calc '1<<22-1'
.br .br
4194303 0x003fffff
.ft R .ft R
4194303 0x003fffff
.RE .RE
.PP .PP
What is the integer value of byte-swapped 0x11223344? What is the integer value of byte-swapped 0x11223344?
.RS .RS
.ft B .ft B
$ calc ?0x11223344 calc ?0x11223344
.br .br
1144201745 0x44332211
.ft R .ft R
1144201745 0x44332211
.RE .RE
.PP .PP
Some canned expressions: Some canned expressions:
-78
View File
@@ -1,78 +0,0 @@
NAME
calc - an integer expression calculator that outputs in both hex and decimal
SYNOPSIS
calc [expr]
DESCRIPTION
calc presents a simple signed integer expression calculator.
The result is printed on 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 'e', the input is also copied to stdout.
stdin is read if there are no arguments.
Constants may be decimal, octal or hex.
There are 26 registers, initialized to zero, named A-Z;
the most recent result is always remembered in the register '.' (dot).
Assignments are silent; the character '$' will print a table of the non-zero registers.
All input is case-insensitive.
Semicolons and/or newlines separate statements.
Whitespace is ignored, except newlines cannot occur within expressions.
Comments are introduced with '#' and extend through the following newline.
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 `a&0xff | b<<8'.
Supported operations in decreasing precedence:
() forced precedence
- + ! ~ ? unary plus/minus, not, compl, byteswap
* / % & << >> >>> mul, div, mod, bitand, shift: left, right, unsigned right
+ - | ^ add, sub, bitor, xor
= assignment ([a-z] = expr)
EXAMPLES
Display the decimal value of 0x42
$ calc 0x42
66 0x00000042
What is the mask value for 22 bits?
$ calc '1<<22-1'
4194303 0x003fffff
What is the integer value of byte-swapped 0x11223344?
$ calc ?0x11223344
1144201745 0x44332211
Some canned expressions:
cat <<! | ecalc
# multiple assignments
a=32;n=10;c=50;m=100;$
7+7/7+7*7-7 # pedmas
m|a-a*3 # convenient precedence
?(1<<14|1<<9)-1<<4-1<<3
57005<<16+48879
-1 >> 32 # signed shift
-1 >>> 32 # unsigned shift
!
BUGS
An assignment with certain types of syntax error will replace the registers with a
partial evaluation:
calc 'a = 1; a; a = 3 4; a'
1 0x00000001
syntax error
3 0x00000003
Command line expressions longer than 64 characters will give a syntax error.
SEE ALSO
https://golang.org/
+2 -1
View File
@@ -87,7 +87,8 @@ I H long long Z;
n>>a n>>a
?h(n)<<a|h(n>>a ?h(n)<<a|h(n>>a
):h(n):g(n); } Q main(Q x ):h(n):g(n); } Q main(Q x
,L**K){ V*A; q(K); W(w())** ,L**K){ V*A; *K=basename(
*K); q(K); W(w())**
K-c*c-b||p(B),_i(!setjmp(l)?T=B K-c*c-b||p(B),_i(!setjmp(l)?T=B
,M(((A=(*o=m()==c-~s&&(j=a|T[-b])&& ,M(((A=(*o=m()==c-~s&&(j=a|T[-b])&&
s-m()==~s)?&o[j-a*u]:(T=B,&_)),*A=F()) s-m()==~s)?&o[j-a*u]:(T=B,&_)),*A=F())
+2 -1
View File
@@ -87,7 +87,8 @@ I H long long Z;
n>>a n>>a
?h(n)<<a|h(n>>a ?h(n)<<a|h(n>>a
):h(n):g(n); } Q main(Q x ):h(n):g(n); } Q main(Q x
,L**K){ V*A; q(K); W(w())** ,L**K){V*A; *K=basename(
*K);q(K); W(w())**
K-c*c-b||p(B),_i(!setjmp(l)?T=B K-c*c-b||p(B),_i(!setjmp(l)?T=B
,M(((A=(*o=m()==c-~s&&(j=a|T[-b])&& ,M(((A=(*o=m()==c-~s&&(j=a|T[-b])&&
c-m()==-c)?&o[j-a*u]:(T=B,&_)),*A=F()) c-m()==-c)?&o[j-a*u]:(T=B,&_)),*A=F())
+18 -10
View File
@@ -15,11 +15,11 @@ make CC="$CC" all >/dev/null || exit 1
# clear screen after compilation so that only the entry is shown # clear screen after compilation so that only the entry is shown
clear clear
echo "$ ./prog '?0x3343<<2'" 1>&2 echo "$ ./calc '?0x3343<<2'" 1>&2
./prog '?0x3343<<2' ./calc '?0x3343<<2'
echo "$ echo '?0x3343<<2' | ./prog" 1>&2 echo "$ echo '?0x3343<<2' | ./ecalc" 1>&2
echo '?0x3343<<2' | ./prog echo '?0x3343<<2' | ./ecalc
echo "$ grep '[0-9]' prog.c" 1>&2 echo "$ grep '[0-9]' prog.c" 1>&2
@@ -27,9 +27,9 @@ read -r -n 1 -p "Press any key to continue (notice how there are no digits!): "
echo 1>&2 echo 1>&2
grep -c '[0-9]' prog.c grep -c '[0-9]' prog.c
read -r -n 1 -p "Press any key to run: ./prog < test.in: " read -r -n 1 -p "Press any key to run: ./calc < test.in: "
echo 1>&2 echo 1>&2
./prog < test.in ./calc < test.in
echo 1>&2 echo 1>&2
read -r -n 1 -p "Press any key to show try.this.txt: " read -r -n 1 -p "Press any key to show try.this.txt: "
@@ -37,11 +37,19 @@ echo 1>&2
cat try.this.txt cat try.this.txt
echo 1>&2 echo 1>&2
read -r -n 1 -p "Press any key to run: ./prog < try.this.txt: " read -r -n 1 -p "Press any key to run: ./calc < try.this.txt: "
echo 1>&2 echo 1>&2
./prog < try.this.txt ./calc < try.this.txt
read -r -n 1 -p "Press any key to show the hex value of 42: " read -r -n 1 -p "Press any key to show the hex value of 42: "
echo 1>&2 echo 1>&2
echo "$ ./prog 42" 1>&2 echo "$ ./calc 42" 1>&2
./prog 42 ./calc 42
read -r -n 1 -p "Press any key to run: ./calc '1<<22-1' (show mask value for 22 bits): "
echo 1>&2
./calc '1<<22-1'
read -r -n 1 -p "Press any key to run: ./calc ?0x11223344 (byte-swapped value of 0x11223344): "
echo 1>&2
./calc ?0x11223344
+10
View File
@@ -4,3 +4,13 @@ a << 16 + b
a=0xdeadbeef a=0xdeadbeef
b=0xcafe b=0xcafe
a << 16 + b a << 16 + b
# multiple assignments
a=32;n=10;c=50;m=100;$
7+7/7+7*7-7 # pedmas
m|a-a*3 # convenient precedence
?(1<<14|1<<9)-1<<4-1<<3
57005<<16+48879
-1 >> 32 # signed shift
-1 >>> 32 # unsigned shift
!
+34 -1
View File
@@ -3798,10 +3798,43 @@ output.
## <a name="2015_burton"></a>[2015/endoh3](/2015/burton/prog.c) ([README.md](/2015/burton/README.md])) ## <a name="2015_burton"></a>[2015/endoh3](/2015/burton/prog.c) ([README.md](/2015/burton/README.md]))
[Cody](#cody) added the [try.sh](/2015/burton/try.sh) script. This involved
[Cody](#cody) fixed the code so one can make use of the echo feature, where the
first character of the filename starts with `e`. The only way it would work
before that is if one did something like:
```sh
ln -sf prog eprog
PATH=$PATH:. ; eprog '...'
```
since otherwise the first character would not be `e` but rather a dot
(`./eprog`). This was done by adding to the Makefile `-include libgen.h` and
adding to `main()` after the variable declaration (`V*A;`) the code:
```c
*K=basename(*K);
```
This was done for both the [prog.c](/2015/burton/prog.c) and
[prog.alt.c](/2015/burton/prog.alt.c). The Makefile was also updated to build
`eprog` and `eprog.alt` to make use of the feature more easily. Each is a new
target (first in `TARGET` and second in `ALT_TARGET`) which have a prerequisite
of `${PROG}` and `${PROG}.alt` respectively and which copies `prog` and
`prog.alt` to `eprog` and `eprog.alt` respectively. With these changes now one
can easily use the echo feature the author implemented.
Cody also added the [try.sh](/2015/burton/try.sh) script. This involved
adding a text file, [try.this.txt](/2015/burton/try.this.txt). adding a text file, [try.this.txt](/2015/burton/try.this.txt).
Cody also added [calc.1](/2015/burton/calc.1) in place of `calc.man` to have
proper formatting (in the style of `man(1)`). The `calc.man` was removed so as
to not have to maintain two copies of the same text.
Cody updated the Makefile to copy the `prog` to `calc` and `prog.alt` to
`calc.alt`. `ecalc` and `ecalc.alt` also work. This is because the program is
called `calc` and is in documentation including the man page. Thus one only need
add a `./` to the commands in the man page/README.md.
## <a name="2015_dogon"></a>[2015/endoh3](/2015/dogon/prog.c) ([README.md](/2015/dogon/README.md])) ## <a name="2015_dogon"></a>[2015/endoh3](/2015/dogon/prog.c) ([README.md](/2015/dogon/README.md]))