📄 awkc++.ps
字号:
10 R f
(implies that)1 486 1 2573 1320 t
10 CW f
(x)3100 1320 w
10 R f
(and)3201 1320 w
10 CW f
(y)3386 1320 w
10 R f
( may later be)3 563( This)1 243(are used arithmetically.)2 964 3 3486 1320 t
(changed to)1 443 1 936 1440 t
10 CW f
(int)1412 1440 w
10 R f
(because the variables are only used in)6 1555 1 1625 1440 t
10 CW f
(int)3213 1440 w
10 R f
( used in an)3 457( a numeric variable is)4 891(contexts. If)1 482 3 3426 1440 t
(explicitly)936 1560 w
10 CW f
(double)1365 1560 w
10 R f
(context, such as)2 687 1 1776 1560 t
10 CW f
(sqrt\(x\))2514 1560 w
10 R f
(, or if no further information appears, it will become)9 2322 1 2934 1560 t
10 CW f
(double)936 1680 w
10 R f
( elements are always assumed to be of type)8 1772(. Array)1 313 2 1296 1680 t
10 CW f
(Awk)3412 1680 w
10 R f
( is too uncertain to)4 772(, as are fields, since it)5 892 2 3592 1680 t
(propagate an assumed type.)3 1103 1 936 1800 t
( but instead several passes)4 1072(Ideally, one should do a data flow analysis to propagate type information,)11 2998 2 1186 1930 t
( backwards'' can be handled, so long as the)8 1761(are made over the parse tree; this way, information that ``flows)10 2559 2 936 2050 t
( example, in a sequence like)5 1125( For)1 189(backward path is not too long.)5 1210 3 936 2170 t
9 CW f
({ i = j; j = 2 * k; k = 1; x = y; y = z + 1; z /= 2 })23 2862 1 1224 2340 t
10 R f
( the tree it will be concluded that)7 1319(after two passes over)3 839 2 936 2520 t
10 CW f
(i)3120 2520 w
10 R f
(,)3180 2520 w
10 CW f
(j)3231 2520 w
10 R f
(, and)1 195 1 3291 2520 t
10 CW f
(k)3512 2520 w
10 R f
(are all of type)3 554 1 3598 2520 t
10 CW f
(int)4178 2520 w
10 R f
(, while)1 273 1 4358 2520 t
10 CW f
(x)4657 2520 w
10 R f
(,)4717 2520 w
10 CW f
(y)4768 2520 w
10 R f
(, and)1 195 1 4828 2520 t
10 CW f
(z)5049 2520 w
10 R f
(are)5135 2520 w
(all)936 2640 w
10 CW f
(double)1061 2640 w
10 R f
(.)1421 2640 w
10 B f
(3. The)1 292 1 936 2880 t
10 CB f
(Awk)1253 2880 w
10 B f
(Class)1458 2880 w
10 R f
( clear C++ code, with all the operators in)8 1746(It is easy enough and quite satisfying to generate clean,)9 2324 2 1186 3010 t
( captures the)2 516( turns out to be harder to define a class that)10 1789( It)1 118(place, and no redundant parentheses or braces.)6 1897 4 936 3130 t
(behavior of)1 467 1 936 3250 t
8 R f
(AWK)1438 3250 w
10 R f
( this section we)3 652( In)1 144( results.)1 322(variables so that the clean expressions produce the expected)8 2474 4 1664 3250 t
(will describe the)2 660 1 936 3370 t
10 CW f
(Awk)1621 3370 w
10 R f
(class, which is defined in the header file)7 1602 1 1826 3370 t
10 CW f
(Awk.h)3453 3370 w
10 R f
(.)3753 3370 w
(The)1186 3500 w
10 CW f
(Awk)1366 3500 w
10 R f
(data type keeps track of the value and state of a variable:)11 2263 1 1571 3500 t
9 CW f
(class Awk {)2 594 1 1224 3670 t
(private:)1332 3780 w
( floating-point value if currently valid)5 2160( //)1 270(double fval;)1 702 3 1656 3890 t
( string value if currently valid)5 1728( //)1 270(String sval;)1 702 3 1656 4000 t
( which values are currently valid)5 1782( //)1 216(int state;)1 756 3 1656 4110 t
(...)1656 4220 w
10 R f
(\(The fragments of)2 724 1 936 4400 t
10 CW f
(Awk.h)1687 4400 w
10 R f
( bog-)1 211(presented here have been somewhat simplified to show the essence without)10 3031 2 2014 4400 t
( The)1 210(ging down in details.\))3 887 2 936 4520 t
10 CW f
(state)2062 4520 w
10 R f
(variable holds only two bits, which are set if the numeric or string val-)13 2865 1 2391 4520 t
(ues or both are currently valid.)5 1240 1 936 4640 t
10 CW f
(String)2229 4640 w
10 R f
( tiny subset of the stan-)5 950( is a)2 167( it)1 109(provides reference-counted strings;)2 1413 4 2617 4640 t
(dard C++ library string package.)4 1301 1 936 4760 t
(The next step in the class definition is constructors to create)10 2391 1 1186 4890 t
10 CW f
(Awk)3602 4890 w
10 R f
(s:)3782 4890 w
9 CW f
(public:)1332 5060 w
( fval = 0.0; state = STR|NUM; })7 1674( {)1 486(Awk\(\) : sval\(""\))2 864 3 1656 5170 t
( fval = i; state = NUM; })7 1350( {)1 810(Awk\(int i\))1 540 3 1656 5280 t
( fval = f; state = NUM; })7 1350( {)1 648(Awk\(double f\))1 702 3 1656 5390 t
(Awk\(cchar *s\) : sval\(s\) { state = STR; })8 2160 1 1656 5500 t
( if \(a.state & STR\) sval = a.sval;)7 1836( {)1 756(Awk\(Awk &a\))1 594 3 1656 5610 t
(fval = a.fval; state = a.state; })6 1782 1 3060 5720 t
10 R f
(The type)1 352 1 936 5900 t
10 CW f
(cchar)1313 5900 w
10 R f
(is an abbreviation for)3 851 1 1638 5900 t
10 CW f
(const char)1 565 1 2514 5900 t
10 R f
(here and in the sequel.)4 895 1 3104 5900 t
(Similar functions are necessary for assignment of values to)8 2357 1 1186 6030 t
10 CW f
(Awk)3568 6030 w
10 R f
(s:)3748 6030 w
9 CW f
( fval = i; state = NUM; return *this; })9 2106( {)1 486(Awk &operator =\(int i\))3 1188 3 1656 6200 t
( fval = f; state = NUM; return *this; })9 2106( {)1 324(Awk &operator =\(double f\))3 1350 3 1656 6310 t
( sval = s; state = STR; return *this; })9 2106( {)1 324(Awk &operator =\(cchar *s\))3 1350 3 1656 6420 t
( if \(a.state & STR\) sval = a.sval;)7 1836( {)1 432(Awk &operator =\(Awk &a\))3 1242 3 1656 6530 t
(fval = a.fval; state = a.state;)5 1674 1 3384 6640 t
(return *this; })2 810 1 3384 6750 t
10 R f
(and for increment operators like)4 1280 1 936 6930 t
10 CW f
(+=)2241 6930 w
10 R f
(and for)1 285 1 2386 6930 t
10 CW f
(++)2696 6930 w
10 R f
(and)2841 6930 w
10 CW f
(--)3010 6930 w
10 R f
(.)3130 6930 w
(There are also ``conversion functions'' for fetching the numeric and string values of)12 3358 1 1186 7060 t
10 CW f
(Awk)4569 7060 w
10 R f
(variables:)4774 7060 w
cleartomark
showpage
saveobj restore
end
%%PageBoundingBox: 83 70 554 728
%%EndPage: 4 4
%%Page: 5 5
%%PageBoundingBox: (atend)
DpostDict begin
/saveobj save def
mark
5 pagesetup
9 CW f
(operator double\(\))1 918 1 1224 830 t
({ return state&NUM ? fval : \(state |= NUM, fval = atof\(sval\)\); })12 3456 1 1656 940 t
(operator cchar *\(\))2 972 1 1224 1050 t
({ return state&STR ? sval : \(state |= STR, sval = ftoa\(fval\)\); })12 3456 1 1656 1160 t
10 R f
( implicitly when calling a normal C function that expects one of these types as an argument.)16 3724(These are used)2 596 2 936 1340 t
(For example, since)2 757 1 936 1460 t
10 CW f
(sqrt)1718 1460 w
10 R f
(expects an argument of type)4 1125 1 1983 1460 t
10 CW f
(double)3133 1460 w
10 R f
(,)3493 1460 w
9 CW f
(sqrt\(awkvar\))1224 1630 w
10 R f
(is really)1 319 1 936 1810 t
9 CW f
(sqrt\( \(double\) awkvar \))3 1242 1 1224 1980 t
10 R f
(The alternative of requiring explicit casts for ``downward'' conversions from)9 3102 1 936 2160 t
10 CW f
(Awk)4065 2160 w
10 R f
(to built-in types is unac-)4 984 1 4272 2160 t
(ceptable because it severely affects readability; even a few casts are undesirable.)11 3211 1 936 2280 t
( an)1 138( In)1 152(The real complications begin with the arithmetic and relational operators.)9 3096 3 1186 2410 t
8 R f
(AWK)4616 2410 w
10 R f
(arithmetic)4851 2410 w
(expression involving the operator)3 1340 1 936 2530 t
10 CW f
(+)2301 2530 w
10 R f
(, there are five possible combinations:)5 1520 1 2361 2530 t
9 CW f
(Awk + Awk)2 486 1 1224 2700 t
(Awk + int)2 486 1 1224 2810 t
(int + Awk)2 486 1 1224 2920 t
(Awk + double)2 648 1 1224 3030 t
(double + Awk)2 648 1 1224 3140 t
10 R f
(Each of these produces a)4 991 1 936 3320 t
10 CW f
(double)1952 3320 w
10 R f
(value.)2337 3320 w
(The obvious way to handle this is to overload the)9 1968 1 1186 3450 t
10 CW f
(+)3179 3450 w
10 R f
(operator as a)2 509 1 3264 3450 t
10 CW f
(friend)3798 3450 w
10 R f
(function:)4183 3450 w
9 CW f
(friend double operator +\(Awk &, Awk &\);)6 2106 1 1224 3620 t
10 R f
( cause the side-effect of)4 998(The arguments must be passed by reference since the function has to be able to)14 3322 2 936 3800 t
( A)1 127(updating the numeric state of each argument if necessary.)8 2337 2 936 3920 t
10 CW f
(friend)3430 3920 w
10 R f
( that the left-)3 525(function is required so)3 911 2 3820 3920 t
( an)1 130(hand operand can be)3 852 2 936 4040 t
10 CW f
(int)1954 4040 w
10 R f
(or)2170 4040 w
10 CW f
(double)2289 4040 w
10 R f
(; if a member function were used, the left-hand operand would)10 2607 1 2649 4040 t
(have to be an)3 529 1 936 4160 t
10 CW f
(Awk)1490 4160 w
10 R f
(and this would preclude expressions like)5 1623 1 1695 4160 t
10 CW f
(1+Awk)3343 4160 w
10 R f
(.)3643 4160 w
(Unfortunately, the simple solution doesn't work, because an expression like)9 3032 1 1186 4290 t
9 CW f
(Awk + int)2 486 1 1224 4460 t
10 R f
( could be parsed as either of)6 1119( it)1 106(is ambiguous;)1 559 3 936 4640 t
9 CW f
(Awk + \(Awk\) int)3 810 1 1224 4810 t
(\(int\) Awk + int)3 810 1 1224 4920 t
10 R f
( promoted ``up'' into)3 852(The problem is that basic types can be)7 1535 2 936 5100 t
10 CW f
(Awk)3350 5100 w
10 R f
(s and)1 210 1 3530 5100 t
10 CW f
(Awk)3767 5100 w
10 R f
(s can be converted ``down'' into)5 1309 1 3947 5100 t
( provides an elaborate sequence of)5 1396( C++)1 233( way to state which choice is preferred.)7 1590(basic types, and there is no)5 1101 4 936 5220 t
( is finished, if there are two)6 1197(rules that determines how type-matching of functions is done, but when it)11 3123 2 936 5340 t
(matched functions, the construction is ambiguous.)5 2007 1 936 5460 t
( is that in)3 412(The problem)1 526 2 1186 5590 t
8 R f
(AWK)2163 5590 w
10 R f
(all possible conversions are legal; given both upward and downward)9 2863 1 2393 5590 t
(implicit conversions, the only way to capture this at compile time is to spell out all possible combinations:)17 4250 1 936 5710 t
9 CW f
(friend double operator +\(Awk &, Awk &\);)6 2106 1 1224 5880 t
(friend double operator +\(Awk &, int\);)5 1998 1 1224 5990 t
(friend double operator +\(Awk &, double\);)5 2160 1 1224 6100 t
(friend double operator +\(int, Awk &\);)5 1998 1 1224 6210 t
(friend double operator +\(double, Awk &\);)5 2160 1 1224 6320 t
(// and so on for - * / %)8 1296 1 1224 6430 t
10 R f
(All told there are 5)4 754 1 936 6610 t
10 S f
(\264)1690 6610 w
10 R f
(5 functions for arithmetic operations.)4 1484 1 1745 6610 t
( there are more combinations and)5 1368(As it is for arithmetic operators, so it is for relationals, except that)12 2702 2 1186 6740 t
(the semantics imposed by)3 1039 1 936 6860 t
8 R f
(AWK)2003 6860 w
10 R f
( at the state of each variable in)7 1230(are more complicated: it is necessary to look)7 1804 2 2222 6860 t
( makes another 42 functions)4 1150( This)1 234(a comparison to determine whether the comparison is numeric or string.)10 2936 3 936 6980 t
( most of these are trivial and can be expanded in-line.)10 2137( Fortunately,)1 536(\(6 operators, 7 type combinations\).)4 1398 3 936 7100 t
(By the way, it is necessary to distinguish)7 1696 1 1186 7230 t
10 CW f
(int)2916 7230 w
10 R f
(from)3130 7230 w
10 CW f
(double)3358 7230 w
10 R f
(, rather than relying on the automatic)6 1538 1 3718 7230 t
cleartomark
showpage
saveobj restore
end
%%PageBoundingBox: 83 53 536 728
%%EndPage: 5 5
%%Page: 6 6
%%PageBoundingBox: (atend)
DpostDict begin
/saveobj save def
mark
6 pagesetup
10 R f
( the expression)2 599( Consider)1 411(coercion that would otherwise take place.)5 1657 3 936 840 t
9 CW f
(Awkvar == 0)2 594 1 1224 1010 t
10 R f
(In the absence of explicit functions for)6 1565 1 936 1190 t
10 CW f
(int)2530 1190 w
10 R f
(s,)2710 1190 w
10 CW f
(0)2803 1190 w
10 R f
(can be a)2 334 1 2892 1190 t
10 CW f
(double)3256 1190 w
10 R f
(or a)1 157 1 3646 1190 t
10 CW f
(char*)3833 1190 w
10 R f
(, so this construction would)4 1123 1 4133 1190 t
(be ambiguous.)1 583 1 936 1310 t
10 B f
(4. Fields)1 381 1 936 1550 t
10 R f
( in)1 115( Fields)1 307(The next complication is the treatment of fields.)7 2004 3 1186 1680 t
8 R f
(AWK)3649 1680 w
10 R f
( as)1 121(are for the most part the same)6 1258 2 3877 1680 t
( that they have potential side effects, and there are significant efficiency considera-)12 3310(ordinary variables except)2 1010 2 936 1800 t
(tions since field-splitting is expensive.)4 1536 1 936 1920 t
(Each time a new line is read, the input record)9 1825 1 1186 2050 t
10 CW f
($0)3038 2050 w
10 R f
(is set, but it is undesirable to set)7 1292 1 3185 2050 t
10 CW f
($1)4505 2050 w
10 R f
(, etc., until they)3 631 1 4625 2050 t
( field is assigned to, that invalidates the value of)9 1962( addition, if any)3 648( In)1 138(are actually needed.)2 804 4 936 2170 t
10 CW f
($0)4517 2170 w
10 R f
(, but it is unde-)4 619 1 4637 2170 t
(sirable to recreate)2 718 1 936 2290 t
10 CW f
($0)1684 2290 w
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -