improved the standard console handler to handle when ARGV has been overridden in a script
This commit is contained in:
14
qse/regress/awk/adm.d1
Normal file
14
qse/regress/awk/adm.d1
Normal file
@ -0,0 +1,14 @@
|
||||
ld zero # initialize sum to zero
|
||||
st sum
|
||||
loop get # read a number
|
||||
jz done # no more input if number is zero
|
||||
add sum # add in accumulated sum
|
||||
st sum # store new value back in sum
|
||||
j loop # go back and read another number
|
||||
|
||||
done ld sum # print sum
|
||||
put
|
||||
halt
|
||||
|
||||
zero const 0
|
||||
sum const
|
51
qse/regress/awk/asm.awk
Normal file
51
qse/regress/awk/asm.awk
Normal file
@ -0,0 +1,51 @@
|
||||
#
|
||||
# $Id: asm.awk,v 1.4 2007/09/27 11:33:45 bacon Exp $
|
||||
#
|
||||
# Taken from the book "The AWK Programming Language"
|
||||
#
|
||||
|
||||
BEGIN {
|
||||
srcfile = ARGV[1];
|
||||
ARGV[1] = "";
|
||||
tempfile = "asm.temp";
|
||||
n = split("const get put ld st add sub jpos jz j halt", x);
|
||||
for (i = 1; i <= n; i++) op[x[i]] = i - 1;
|
||||
|
||||
# PASS 1
|
||||
FS = "[ \t]+";
|
||||
while (getline <srcfile > 0) {
|
||||
sub (/#.*/, "");
|
||||
symtab[$1] = nextmem;
|
||||
|
||||
if ($2 != "") {
|
||||
print $2 "\t" $3 >tempfile;
|
||||
nextmem++;
|
||||
}
|
||||
}
|
||||
close (tempfile);
|
||||
|
||||
# PASS 2
|
||||
nextmem = 0;
|
||||
while (getline <tempfile > 0) {
|
||||
if ($2 !~ /^[0-9]*$/) $2 = symtab[$2];
|
||||
mem[nextmem++] = 1000 * op[$1] + $2;
|
||||
}
|
||||
|
||||
# INTERPRETER
|
||||
for (pc = 0; pc >= 0; ) {
|
||||
addr = mem[pc] % 1000;
|
||||
code = int(mem[pc++] / 1000);
|
||||
if (code == op["get"]) { if (getline acc <= 0) acc = 0; }
|
||||
else if (code == op["put"]) { print acc; }
|
||||
else if (code == op["st"]) { mem[addr] = acc; }
|
||||
else if (code == op["ld"]) { acc = mem[addr]; }
|
||||
else if (code == op["add"]) { acc += mem[addr]; }
|
||||
else if (code == op["sub"]) { acc -= mem[addr]; }
|
||||
else if (code == op["jpos"]) { if (acc > 0) pc = addr; }
|
||||
else if (code == op["jz"]) { if (acc == 0) pc = addr; }
|
||||
else if (code == op["j"]) { pc = addr; }
|
||||
else if (code == op["halt"]) { pc = -1; }
|
||||
else { pc = -1; }
|
||||
}
|
||||
}
|
||||
|
14
qse/regress/awk/asm.d1
Normal file
14
qse/regress/awk/asm.d1
Normal file
@ -0,0 +1,14 @@
|
||||
ld zero # initialize sum to zero
|
||||
st sum
|
||||
loop get # read a number
|
||||
jz done # no more input if number is zero
|
||||
add sum # add in accumulated sum
|
||||
st sum # store new value back in sum
|
||||
j loop # go back and read another number
|
||||
|
||||
done ld sum # print sum
|
||||
put
|
||||
halt
|
||||
|
||||
zero const 0
|
||||
sum const
|
11
qse/regress/awk/asm.d2
Normal file
11
qse/regress/awk/asm.d2
Normal file
@ -0,0 +1,11 @@
|
||||
10
|
||||
20
|
||||
30
|
||||
40
|
||||
50
|
||||
60
|
||||
70
|
||||
80
|
||||
90
|
||||
100
|
||||
-1
|
1
qse/regress/awk/emp-001.awk
Normal file
1
qse/regress/awk/emp-001.awk
Normal file
@ -0,0 +1 @@
|
||||
$3 > 0 { print $1, $2 * $3; }
|
1
qse/regress/awk/emp-002.awk
Normal file
1
qse/regress/awk/emp-002.awk
Normal file
@ -0,0 +1 @@
|
||||
$3 == 0 { print $1; }
|
1
qse/regress/awk/emp-003.awk
Normal file
1
qse/regress/awk/emp-003.awk
Normal file
@ -0,0 +1 @@
|
||||
{ print NF, $1, $NF; }
|
1
qse/regress/awk/emp-004.awk
Normal file
1
qse/regress/awk/emp-004.awk
Normal file
@ -0,0 +1 @@
|
||||
{ print NR, $0; }
|
1
qse/regress/awk/emp-005.awk
Normal file
1
qse/regress/awk/emp-005.awk
Normal file
@ -0,0 +1 @@
|
||||
{ print "total pay for", $1, "is", $2 * $3; }
|
1
qse/regress/awk/emp-006.awk
Normal file
1
qse/regress/awk/emp-006.awk
Normal file
@ -0,0 +1 @@
|
||||
{ printf ("total pay for %s is $%.2f\n", $1, $2 * $3); }
|
1
qse/regress/awk/emp-007.awk
Normal file
1
qse/regress/awk/emp-007.awk
Normal file
@ -0,0 +1 @@
|
||||
{ printf ("%-8s $%6.2f\n", $1, $2 * $3); }
|
1
qse/regress/awk/emp-008.awk
Normal file
1
qse/regress/awk/emp-008.awk
Normal file
@ -0,0 +1 @@
|
||||
$2 >= 5
|
1
qse/regress/awk/emp-009.awk
Normal file
1
qse/regress/awk/emp-009.awk
Normal file
@ -0,0 +1 @@
|
||||
$2 * $3 > 50 { printf ("$%.2f for %s\n", $2 * $3, $1); }
|
1
qse/regress/awk/emp-010.awk
Normal file
1
qse/regress/awk/emp-010.awk
Normal file
@ -0,0 +1 @@
|
||||
$1 == "Susie"
|
1
qse/regress/awk/emp-011.awk
Normal file
1
qse/regress/awk/emp-011.awk
Normal file
@ -0,0 +1 @@
|
||||
/Susie/
|
1
qse/regress/awk/emp-012.awk
Normal file
1
qse/regress/awk/emp-012.awk
Normal file
@ -0,0 +1 @@
|
||||
$2 >= 4 || $3 >= 20
|
2
qse/regress/awk/emp-013.awk
Normal file
2
qse/regress/awk/emp-013.awk
Normal file
@ -0,0 +1,2 @@
|
||||
$2 >= 4
|
||||
$3 >= 20
|
1
qse/regress/awk/emp-014.awk
Normal file
1
qse/regress/awk/emp-014.awk
Normal file
@ -0,0 +1 @@
|
||||
!($2 < 4 && $3 < 20)
|
5
qse/regress/awk/emp-015.awk
Normal file
5
qse/regress/awk/emp-015.awk
Normal file
@ -0,0 +1,5 @@
|
||||
NF != 3 { print $0, "number of fields is not equal to 3"; }
|
||||
$2 < 3.35 { print $0, "rate is below minimum wage"; }
|
||||
$2 > 10 { print $0, "rate exceeds $10 per hour"; }
|
||||
$3 < 0 { print $0, "negative hours worked"; }
|
||||
$3 > 60 { print $0, "too many hours worked"; }
|
2
qse/regress/awk/emp-016.awk
Normal file
2
qse/regress/awk/emp-016.awk
Normal file
@ -0,0 +1,2 @@
|
||||
BEGIN { print "NAME RATE HOURS"; print ""; }
|
||||
{ print; }
|
2
qse/regress/awk/emp-017.awk
Normal file
2
qse/regress/awk/emp-017.awk
Normal file
@ -0,0 +1,2 @@
|
||||
$3 > 15 { emp = emp + 1; }
|
||||
END { print emp, "employees worked more than 15 hours"; }
|
1
qse/regress/awk/emp-018.awk
Normal file
1
qse/regress/awk/emp-018.awk
Normal file
@ -0,0 +1 @@
|
||||
END { print NR, "employees"; }
|
5
qse/regress/awk/emp-019.awk
Normal file
5
qse/regress/awk/emp-019.awk
Normal file
@ -0,0 +1,5 @@
|
||||
{ pay = pay + $2 * $3; }
|
||||
END { print NR, "employees";
|
||||
print "total pay is", pay;
|
||||
print "average pay is", pay/NR;
|
||||
}
|
2
qse/regress/awk/emp-020.awk
Normal file
2
qse/regress/awk/emp-020.awk
Normal file
@ -0,0 +1,2 @@
|
||||
$2 > maxrate { maxrate = $2; maxemp = $1; }
|
||||
END { print "highest hourly rage:", maxrate, "for", maxemp; }
|
2
qse/regress/awk/emp-021.awk
Normal file
2
qse/regress/awk/emp-021.awk
Normal file
@ -0,0 +1,2 @@
|
||||
{ names = names $1 " "; }
|
||||
END { print names; }
|
2
qse/regress/awk/emp-022.awk
Normal file
2
qse/regress/awk/emp-022.awk
Normal file
@ -0,0 +1,2 @@
|
||||
{ last = $0; }
|
||||
END { print last; }
|
1
qse/regress/awk/emp-023.awk
Normal file
1
qse/regress/awk/emp-023.awk
Normal file
@ -0,0 +1 @@
|
||||
{ print $1, length($1); }
|
4
qse/regress/awk/emp-024.awk
Normal file
4
qse/regress/awk/emp-024.awk
Normal file
@ -0,0 +1,4 @@
|
||||
{ nc = nc + length($0) + 1;
|
||||
nw = nw + NF;
|
||||
}
|
||||
END { print NR, "lines,", nw, "words,", nc, "characters"; }
|
7
qse/regress/awk/emp-025.awk
Normal file
7
qse/regress/awk/emp-025.awk
Normal file
@ -0,0 +1,7 @@
|
||||
$2 > 6 { n = n + 1; pay = pay + $2 * $3; }
|
||||
END { if (n > 0)
|
||||
print n, "employees, total pay is", pay,
|
||||
"average pay is", pay/n;
|
||||
else
|
||||
print "no employees are paid more than $6/hour";
|
||||
}
|
12
qse/regress/awk/emp-026.awk
Normal file
12
qse/regress/awk/emp-026.awk
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
line[NR] = $0;
|
||||
}
|
||||
|
||||
END {
|
||||
i = NR;
|
||||
while (i > 0)
|
||||
{
|
||||
print line[i];
|
||||
i = i - 1;
|
||||
}
|
||||
}
|
8
qse/regress/awk/emp-027.awk
Normal file
8
qse/regress/awk/emp-027.awk
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
line[NR] = $0;
|
||||
}
|
||||
|
||||
END {
|
||||
i = NR;
|
||||
for (i = NR; i > 0; i = i - 1) print line[i];
|
||||
}
|
6
qse/regress/awk/emp.d
Normal file
6
qse/regress/awk/emp.d
Normal file
@ -0,0 +1,6 @@
|
||||
Beth 4.00 0
|
||||
Dan 3.74 0
|
||||
Kathy 4.00 10
|
||||
Mark 5.00 20
|
||||
Mary 5.50 22
|
||||
Susie 4.25 18
|
40
qse/regress/awk/hanoi.awk
Normal file
40
qse/regress/awk/hanoi.awk
Normal file
@ -0,0 +1,40 @@
|
||||
BEGIN {
|
||||
n = arg("-n",5)
|
||||
for (j=0; j<n; j++) push(0,n-j)
|
||||
showstacks()
|
||||
hanoi(n,0,1,2)
|
||||
}
|
||||
|
||||
function hanoi(n,a,b,c) {
|
||||
if (n==1) {
|
||||
move(a,b)
|
||||
} else {
|
||||
hanoi(n-1,a,c,b)
|
||||
move(a,b)
|
||||
hanoi(n-1,c,b,a)
|
||||
}
|
||||
}
|
||||
function move(i,j) {
|
||||
push(j,pop(i))
|
||||
showstacks()
|
||||
}
|
||||
|
||||
|
||||
function showstacks( i,j) {
|
||||
for (i=0; i<=2; i++) {
|
||||
printf "%s ", i
|
||||
for (j=0; j<sp[i]; j++) printf "%s", stack[i,j]
|
||||
print "" }
|
||||
print ""
|
||||
}
|
||||
|
||||
|
||||
function arg(tag,default) {
|
||||
for(i in ARGV)
|
||||
if (ARGV[i] ~ tag)
|
||||
return ARGV[i+1]
|
||||
return default
|
||||
}
|
||||
function push(i,v) { stack[i,sp[i]++]=v }
|
||||
function pop(i) { return stack[i,--sp[i]] }
|
||||
|
56
qse/regress/awk/quicksort.awk
Normal file
56
qse/regress/awk/quicksort.awk
Normal file
@ -0,0 +1,56 @@
|
||||
#.H1 Quicksort.awk
|
||||
#.H2 Synopsis
|
||||
#.P cat numbers | gawk -f quicksort.awk
|
||||
#.H2 Download
|
||||
#.P
|
||||
#Download from
|
||||
#.URL http://lawker.googlecode.com/svn/fridge/lib/awk/quicksort1.awk LAWKER.
|
||||
#.H2 Description
|
||||
#.P
|
||||
#Some Awk implementations come with built in sort routines (e.g. Gawk's asort and asorti functions). But it
|
||||
#can be useful to code these yourself, especially in you are doing data structure tricks.
|
||||
#.P
|
||||
#Quicksort selects a pivot and divides the data into values above and below the pivot. Sorting then
|
||||
#recurses on these sub-lists.
|
||||
#.H2 Code
|
||||
#.H3 Loading the data
|
||||
#.PRE
|
||||
#BEGIN { RS = ""; FS = "\n" }
|
||||
{ A[NR] = $1 }
|
||||
END {
|
||||
qsort(A, 1, NR)
|
||||
for (i = 1; i <= NR; i++) {
|
||||
print A[i] #+ 0;
|
||||
#if (i == NR) break
|
||||
#print ""
|
||||
}
|
||||
}
|
||||
#./PRE
|
||||
#.H3 Sorting the data
|
||||
#.PRE
|
||||
function qsort(A, left, right, i, last) {
|
||||
if (left >= right)
|
||||
return
|
||||
swap(A, left, left+int((right-left+1)*rand()))
|
||||
last = left
|
||||
for (i = left+1; i <= right; i++)
|
||||
{
|
||||
# print A[i] "/" A[left] "....." (A[i] < A[left])
|
||||
if (A[i] < A[left])
|
||||
{
|
||||
swap(A, ++last, i)
|
||||
}
|
||||
}
|
||||
swap(A, left, last)
|
||||
qsort(A, left, last-1)
|
||||
qsort(A, last+1, right)
|
||||
}
|
||||
function swap(A, i, j, t) {
|
||||
t = A[i]; A[i] = A[j]; A[j] = t
|
||||
}
|
||||
#./PRE
|
||||
#.H2 See also
|
||||
#.P
|
||||
#.URL http://awk.info/?quicksort2 quicksort2.awk
|
||||
#.H2 Authors
|
||||
#.P Alfred Aho, Peter Weinberger, Brian Kernighan, 1988.
|
16
qse/regress/awk/quicksort.d
Normal file
16
qse/regress/awk/quicksort.d
Normal file
@ -0,0 +1,16 @@
|
||||
34
|
||||
0.11111111111111111111111111111
|
||||
0.0000000000
|
||||
0b11111
|
||||
0xA
|
||||
1
|
||||
35
|
||||
92
|
||||
19123
|
||||
29
|
||||
1.E12
|
||||
11.2839091
|
||||
12
|
||||
99X
|
||||
301
|
||||
493
|
17
qse/regress/awk/quicksort2.awk
Normal file
17
qse/regress/awk/quicksort2.awk
Normal file
@ -0,0 +1,17 @@
|
||||
BEGIN {
|
||||
recurse1 = "../../cmd/awk/qseawk -f quicksort2.awk #" rand()
|
||||
recurse2 = "../../cmd/awk/qseawk -f quicksort2.awk #" rand()
|
||||
}
|
||||
NR == 1 {
|
||||
pivot=$0;
|
||||
next
|
||||
}
|
||||
NR > 1 { if($0 < pivot) { print | recurse1 }
|
||||
if($0 > pivot) { print | recurse2 }
|
||||
}
|
||||
END {
|
||||
close(recurse1)
|
||||
if(NR > 0) print pivot
|
||||
close(recurse2)
|
||||
}
|
||||
|
16
qse/regress/awk/quicksort2.d
Normal file
16
qse/regress/awk/quicksort2.d
Normal file
@ -0,0 +1,16 @@
|
||||
34
|
||||
0.11111111111111111111111111111
|
||||
0.0000000000
|
||||
0b11111
|
||||
0xA
|
||||
1
|
||||
35
|
||||
92
|
||||
19123
|
||||
29
|
||||
1.E12
|
||||
11.2839091
|
||||
12
|
||||
99X
|
||||
301
|
||||
493
|
85
qse/regress/awk/regress.sh
Executable file
85
qse/regress/awk/regress.sh
Executable file
@ -0,0 +1,85 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo_so()
|
||||
{
|
||||
tput smso
|
||||
while [ $# -gt 0 ]
|
||||
do
|
||||
echo -n "$1 "
|
||||
shift
|
||||
done
|
||||
echo
|
||||
tput rmso
|
||||
}
|
||||
|
||||
print_usage()
|
||||
{
|
||||
echo "Usage: $0 init"
|
||||
echo " $0 test"
|
||||
}
|
||||
|
||||
###################
|
||||
# MAIN #
|
||||
###################
|
||||
|
||||
QSEAWK="../../cmd/awk/qseawk"
|
||||
PROGS="
|
||||
emp-001.awk/emp.d//
|
||||
emp-002.awk/emp.d//
|
||||
emp-003.awk/emp.d//
|
||||
emp-004.awk/emp.d//
|
||||
emp-005.awk/emp.d//
|
||||
emp-006.awk/emp.d//
|
||||
emp-007.awk/emp.d//
|
||||
emp-008.awk/emp.d//
|
||||
emp-009.awk/emp.d//
|
||||
emp-010.awk/emp.d//
|
||||
emp-011.awk/emp.d//
|
||||
emp-012.awk/emp.d//
|
||||
emp-013.awk/emp.d//
|
||||
emp-014.awk/emp.d//
|
||||
emp-015.awk/emp.d//
|
||||
emp-016.awk/emp.d//
|
||||
emp-017.awk/emp.d//
|
||||
emp-018.awk/emp.d//
|
||||
emp-019.awk/emp.d//
|
||||
emp-020.awk/emp.d//
|
||||
emp-021.awk/emp.d//
|
||||
emp-022.awk/emp.d//
|
||||
emp-023.awk/emp.d//
|
||||
emp-024.awk/emp.d//
|
||||
emp-025.awk/emp.d//
|
||||
emp-026.awk/emp.d//
|
||||
emp-027.awk/emp.d//
|
||||
|
||||
quicksort.awk/quicksort.d//
|
||||
quicksort2.awk/quicksort2.d//
|
||||
asm.awk/asm.d1/asm.d2/
|
||||
stripcomment.awk/stripcomment.d//
|
||||
wordfreq.awk/wordfreq.awk//
|
||||
hanoi.awk//
|
||||
"
|
||||
|
||||
[ -x "${QSEAWK}" ] || {
|
||||
echo "ERROR: ${QSEAWK} not found"
|
||||
exit 1;
|
||||
}
|
||||
|
||||
for prog in ${PROGS}
|
||||
do
|
||||
script="`echo ${prog} | cut -d/ -f1`"
|
||||
datafile="`echo ${prog} | cut -d/ -f2`"
|
||||
redinfile="`echo ${prog} | cut -d/ -f3`"
|
||||
awkopts="`echo ${prog} | cut -d/ -f4`"
|
||||
|
||||
if [ -n "${redinfile}" ]
|
||||
then
|
||||
echo_so "${QSEAWK} ${awkopts} -f ${script} ${datafile} < ${redinfile}"
|
||||
${QSEAWK} ${awkopts} -f ${script} ${datafile} < ${redinfile}
|
||||
else
|
||||
echo_so "${QSEAWK} ${awkopts} -f ${script} ${datafile}"
|
||||
${QSEAWK} ${awkopts} -f ${script} ${datafile}
|
||||
fi
|
||||
done
|
||||
|
||||
exit 0
|
11
qse/regress/awk/stripcomment.awk
Normal file
11
qse/regress/awk/stripcomment.awk
Normal file
@ -0,0 +1,11 @@
|
||||
BEGIN {
|
||||
RS = "/\\*([^*]|\\*+[^/*])*\\*+/"
|
||||
# comment is record separator
|
||||
ORS = " "
|
||||
getline hold
|
||||
}
|
||||
|
||||
{ print hold ; hold = $0 }
|
||||
|
||||
END { printf "%s" , hold }
|
||||
|
10
qse/regress/awk/stripcomment.d
Normal file
10
qse/regress/awk/stripcomment.d
Normal file
@ -0,0 +1,10 @@
|
||||
/* sample C program */
|
||||
|
||||
#include <stdio.h>
|
||||
int main ()
|
||||
{
|
||||
/* print hello world
|
||||
* with printf */
|
||||
printf ("hello, world\n");
|
||||
return 0;
|
||||
}
|
20
qse/regress/awk/wordfreq.awk
Normal file
20
qse/regress/awk/wordfreq.awk
Normal file
@ -0,0 +1,20 @@
|
||||
# wordfreq.awk --- print list of word frequencies
|
||||
|
||||
{
|
||||
$0 = tolower($0); # remove case distinctions
|
||||
|
||||
# remove punctuation
|
||||
a=0;
|
||||
gsub(/[^[:alnum:]_[:blank:]]/, " ", $a);
|
||||
#gsub(/[^[:alnum:]_[:blank:]]/, " ");
|
||||
|
||||
for (i = 1; i <= NF; i++) freq[$i]++;
|
||||
}
|
||||
#/[^kkka-bcx-dd-y]|abc|def/
|
||||
|
||||
END {
|
||||
for (word in freq)
|
||||
print word, freq[word];
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user